From patchwork Mon Nov 4 16:10:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Marc_Poulhi=C3=A8s?= X-Patchwork-Id: 2006299 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=adacore.com header.i=@adacore.com header.a=rsa-sha256 header.s=google header.b=fl5Itl7l; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4XhxNk5FF8z1xwF for ; Tue, 5 Nov 2024 03:13:58 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2BE6E385AC1B for ; Mon, 4 Nov 2024 16:13:56 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by sourceware.org (Postfix) with ESMTPS id B59063857704 for ; Mon, 4 Nov 2024 16:11:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B59063857704 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org B59063857704 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::32e ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1730736725; cv=none; b=Tdd8mebVGdSUW+JnkU8Z2tKGbGJMHukmmHnyBfQBqEnirA7sKKRP1kTtu0KIMtXzWjZXSo+3S3PoKBQVVYrdHAgFGrF5hBDrBSykzoIJajb/YuSd3YzQ53b7lAWF+crjqaVGNEr+zcTUTyqQtYjaLfl6D/scgdGYY0+XACqQPKY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1730736725; c=relaxed/simple; bh=H58eNcOBZizQ/FAe+EDN5Y6gXSQvdA3g9NIyvLuppHA=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=SfopON0/bCzowqJRkzgM1v27a4FuCVgCAPssOX1EfN5cGlS7tNSzdGi/aZAGQUf1Qnq2gr+Mc6KX5NnZ4JwGn3NU0Bd2kPfdtdwrOzsG2SCoM3hapkPzEd4tb40cF1+wLQQqesuVIcVEnQ07ooUDcomdrM9o0kpZF2c4dDIUyKk= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-wm1-x32e.google.com with SMTP id 5b1f17b1804b1-43169902057so34633265e9.0 for ; Mon, 04 Nov 2024 08:11:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1730736700; x=1731341500; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=01nlqck8nLhs7sX+s9aXOgS7nkTlm88iFRexYZcBe4o=; b=fl5Itl7luo66OMe37SDeEUCCAk3JH8lx1RYrRg3GqcGgftwjrJ7Dt5VLXjzihSeVOe bkSANGZBsmkiM89EJIraJM98PZ6zJfv4ceQHy2vnVPVGDG2xiUd8KpEw3JvkZuQHBNF4 4vBh3XMmNdrH9OetKbgsxii52rb39JS2UlK8JGjdTihnzd2l/vqOIrryB7iHR1PgZCFG Hn2V8oKKfy9Y0dX5DiLP9RL98e3cEGaRmPWmJqPLGfSyIgINtMRo0vMt3ihd67FSmK/B dexdhHrEj4/EUSrwUdYdxp31ATTjJAkffy7vMmBlrgTqTA3olpVfOOFbgOCoubbGESYg VIpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730736700; x=1731341500; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=01nlqck8nLhs7sX+s9aXOgS7nkTlm88iFRexYZcBe4o=; b=PSQR+IceGuB1LJvRCECAfvJN48eacMCvAsK1IKXZ0dALh9RdukWfq2S6aXEkIsWTAG 25HJxal8bZsFlO7msiD2SjXOWxElXg6AbQ+SRcdwD82i1hBajBBsbEPU21xAdN+5C/0j SBdxkACO2nuaUdW3r/VXfUaXcrjEwl0Q9elw+6y2+aBTCNuVZt2wDlz2lGgoUuXfYqPE IgL3KE62S9vfUpErdgMfFMN4A5ov8BjiLODSx88BP+TML+ToHtvandG8tNfVZBsIlgpu NBce0+ybPS+L2Xxf37Xn9/E2I7vhuI0m53ZZ+b8Yo6gzG0jGDZTRGdrSaG0F+RCLP7ro wffQ== X-Gm-Message-State: AOJu0Yxxitip9rUufWmHk802+dODcozAArYqNSiBJnyITux1zNIOVH5q Ixv6ryr7ldcCidIXYpNYtGpC/fFz8OdDKr09RKVGoW+2vugNq+W3qhjFbjRpaf0ZR6QksLNEkkU = X-Google-Smtp-Source: AGHT+IFCyvUJROM414O0O1ZPxl0bnCaCw3pn3sg+doc/CvNF1kPm0EyJkTQ0l1c2p7iHCae6jNx3Rw== X-Received: by 2002:a5d:4489:0:b0:37d:454f:b49a with SMTP id ffacd0b85a97d-381b70f0802mr15336481f8f.43.1730736700263; Mon, 04 Nov 2024 08:11:40 -0800 (PST) Received: from poulhies-Precision-5550.telnowedge.local (lmontsouris-659-1-24-67.w81-250.abo.wanadoo.fr. [81.250.175.67]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-381c10e747csm13574463f8f.64.2024.11.04.08.11.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Nov 2024 08:11:39 -0800 (PST) From: =?utf-8?q?Marc_Poulhi=C3=A8s?= To: gcc-patches@gcc.gnu.org Cc: Ronan Desplanques Subject: [COMMITTED 18/38] ada: Tweak CPU affinity handling Date: Mon, 4 Nov 2024 17:10:53 +0100 Message-ID: <20241104161116.1431659-18-poulhies@adacore.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241104161116.1431659-1-poulhies@adacore.com> References: <20241104161116.1431659-1-poulhies@adacore.com> MIME-Version: 1.0 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org From: Ronan Desplanques The primary motivation for this change is making the taskset command line tool work as expected for tasking programs that don't use features from section D.16 of the Ada reference manual. A couple of components are added to the ATCB record to make it possible to tell values that come from explicit aspects and subprogram calls from values that are inherited from activating tasks. gcc/ada/ChangeLog: * libgnarl/s-mudido__affinity.adb (Unchecked_Set_Affinity): Set new ATCB component. * libgnarl/s-taprop__linux.adb (Create_Task): Only set CPU affinity when required. (Requires_Affinity_Change): New subprogram. (Set_Task_Affinity): Likewise. * libgnarl/s-tarest.adb (Create_Restricted_Task): Adapt to Initialize_ATCB change. * libgnarl/s-taskin.adb (Initialize_ATCB): Update parameter list. Record whether aspects were explicitly specified. * libgnarl/s-taskin.ads (Common_ATCB): Add component. * libgnarl/s-tassta.adb (Create_Task): Update call to Initialize_ATCB. * libgnarl/s-tporft.adb (Register_Foreign_Thread): Likewise. Tested on x86_64-pc-linux-gnu, committed on master. --- gcc/ada/libgnarl/s-mudido__affinity.adb | 1 + gcc/ada/libgnarl/s-taprop__linux.adb | 27 +++++++++++++++++-- gcc/ada/libgnarl/s-tarest.adb | 3 ++- gcc/ada/libgnarl/s-taskin.adb | 12 +++++---- gcc/ada/libgnarl/s-taskin.ads | 36 ++++++++++++++++--------- gcc/ada/libgnarl/s-tassta.adb | 3 ++- gcc/ada/libgnarl/s-tporft.adb | 4 +-- 7 files changed, 63 insertions(+), 23 deletions(-) diff --git a/gcc/ada/libgnarl/s-mudido__affinity.adb b/gcc/ada/libgnarl/s-mudido__affinity.adb index ec8c8f6c19f..e9c17ef68b6 100644 --- a/gcc/ada/libgnarl/s-mudido__affinity.adb +++ b/gcc/ada/libgnarl/s-mudido__affinity.adb @@ -367,6 +367,7 @@ package body System.Multiprocessors.Dispatching_Domains is -- Attach the CPU to the task T.Common.Base_CPU := CPU; + T.Common.CPU_Is_Explicit := True; -- Change the number of tasks attached to a given task in the system -- domain if needed. diff --git a/gcc/ada/libgnarl/s-taprop__linux.adb b/gcc/ada/libgnarl/s-taprop__linux.adb index 0a51b3601c0..d9425e0d2fa 100644 --- a/gcc/ada/libgnarl/s-taprop__linux.adb +++ b/gcc/ada/libgnarl/s-taprop__linux.adb @@ -259,6 +259,11 @@ package body System.Task_Primitives.Operations is -- Initialize the lock L. If Ceiling_Support is True, then set the ceiling -- to Prio. Returns 0 for success, or ENOMEM for out-of-memory. + function Requires_Affinity_Change + (Domain : Dispatching_Domain_Access) return Boolean; + -- Returns whether a call to pthread_setaffinity_np is required to assign a + -- task to Domain. + ------------------- -- Abort_Handler -- ------------------- @@ -521,6 +526,20 @@ package body System.Task_Primitives.Operations is Ceiling_Violation := Result = EINVAL; end Read_Lock; + ------------------------------ + -- Requires_Affinity_Change -- + ------------------------------ + + function Requires_Affinity_Change + (Domain : Dispatching_Domain_Access) return Boolean is + begin + return + Domain /= System_Domain + or else Domain.all + /= [Multiprocessors.CPU'First + .. Multiprocessors.Number_Of_CPUs => True]; + end Requires_Affinity_Change; + ------------ -- Unlock -- ------------ @@ -941,7 +960,9 @@ package body System.Task_Primitives.Operations is -- Support is available - elsif T.Common.Base_CPU /= Multiprocessors.Not_A_Specific_CPU then + elsif T.Common.CPU_Is_Explicit + and then T.Common.Base_CPU /= Multiprocessors.Not_A_Specific_CPU + then declare CPUs : constant size_t := C.size_t (Multiprocessors.Number_Of_CPUs); @@ -971,7 +992,7 @@ package body System.Task_Primitives.Operations is -- Handle dispatching domains - else + elsif Requires_Affinity_Change (T.Common.Domain) then declare CPUs : constant size_t := C.size_t (Multiprocessors.Number_Of_CPUs); @@ -1464,6 +1485,8 @@ package body System.Task_Primitives.Operations is if pthread_setaffinity_np'Address /= Null_Address and then T.Common.LL.Thread /= Null_Thread_Id + and then (T.Common.CPU_Is_Explicit + or else Requires_Affinity_Change (T.Common.Domain)) then declare CPUs : constant size_t := diff --git a/gcc/ada/libgnarl/s-tarest.adb b/gcc/ada/libgnarl/s-tarest.adb index 5c2ee90e84b..df07869757f 100644 --- a/gcc/ada/libgnarl/s-tarest.adb +++ b/gcc/ada/libgnarl/s-tarest.adb @@ -514,7 +514,8 @@ package body System.Tasking.Restricted.Stages is Initialize_ATCB (Self_ID, State, Discriminants, Self_ID, Elaborated, Base_Priority, - Base_CPU, null, Task_Info, Stack_Size, Created_Task, Success); + Base_CPU, CPU /= Unspecified_CPU, null, Task_Info, Stack_Size, + Created_Task, Success); -- If we do our job right then there should never be any failures, which -- was probably said about the Titanic; so just to be safe, let's retain diff --git a/gcc/ada/libgnarl/s-taskin.adb b/gcc/ada/libgnarl/s-taskin.adb index 95c95ed3110..9fade5dea92 100644 --- a/gcc/ada/libgnarl/s-taskin.adb +++ b/gcc/ada/libgnarl/s-taskin.adb @@ -89,12 +89,12 @@ package body System.Tasking is Elaborated : Access_Boolean; Base_Priority : System.Any_Priority; Base_CPU : System.Multiprocessors.CPU_Range; + CPU_Is_Explicit : Boolean; Domain : Dispatching_Domain_Access; Task_Info : System.Task_Info.Task_Info_Type; Stack_Size : System.Parameters.Size_Type; T : Task_Id; - Success : out Boolean) - is + Success : out Boolean) is begin T.Common.State := Unactivated; @@ -110,9 +110,10 @@ package body System.Tasking is -- would be illegal, because Common_ATCB is limited because -- Task_Primitives.Private_Data is limited. - T.Common.Parent := Parent; - T.Common.Base_Priority := Base_Priority; - T.Common.Base_CPU := Base_CPU; + T.Common.Parent := Parent; + T.Common.Base_Priority := Base_Priority; + T.Common.CPU_Is_Explicit := CPU_Is_Explicit; + T.Common.Base_CPU := Base_CPU; -- The Domain defaults to that of the activator. But that can be null in -- the case of foreign threads (see Register_Foreign_Thread), in which @@ -235,6 +236,7 @@ package body System.Tasking is Elaborated => null, Base_Priority => Base_Priority, Base_CPU => Base_CPU, + CPU_Is_Explicit => Main_CPU /= Unspecified_CPU, Domain => System_Domain, Task_Info => Task_Info.Unspecified_Task_Info, Stack_Size => 0, diff --git a/gcc/ada/libgnarl/s-taskin.ads b/gcc/ada/libgnarl/s-taskin.ads index 1bae7e114cf..77851633181 100644 --- a/gcc/ada/libgnarl/s-taskin.ads +++ b/gcc/ada/libgnarl/s-taskin.ads @@ -518,6 +518,17 @@ package System.Tasking is -- -- Protection: Only written by Self, accessed by anyone + CPU_Is_Explicit : Boolean; + -- True if the task is either assigned to a CPU or explicitly not + -- assigned to a CPU through Not_A_Specific_CPU being used with the CPU + -- Aspect a subprogram in System.Multiprocessors.Dispatching_Domains. + -- False otherwise. + -- We keep track of this information to make it possible to accomodate + -- native affinity inheritance on some platforms when no RM D.16 + -- features are used. An example of such a platform is Linux, where we + -- strive to make the taskset command line tool have the expected effect + -- when the program does not use RM D.16 features. + Base_CPU : System.Multiprocessors.CPU_Range; -- Base CPU, only changed via dispatching domains package. -- @@ -1184,18 +1195,19 @@ package System.Tasking is -- System.Tasking.Initialization being present, as was done before. procedure Initialize_ATCB - (Self_ID : Task_Id; - Task_Entry_Point : Task_Procedure_Access; - Task_Arg : System.Address; - Parent : Task_Id; - Elaborated : Access_Boolean; - Base_Priority : System.Any_Priority; - Base_CPU : System.Multiprocessors.CPU_Range; - Domain : Dispatching_Domain_Access; - Task_Info : System.Task_Info.Task_Info_Type; - Stack_Size : System.Parameters.Size_Type; - T : Task_Id; - Success : out Boolean); + (Self_ID : Task_Id; + Task_Entry_Point : Task_Procedure_Access; + Task_Arg : System.Address; + Parent : Task_Id; + Elaborated : Access_Boolean; + Base_Priority : System.Any_Priority; + Base_CPU : System.Multiprocessors.CPU_Range; + CPU_Is_Explicit : Boolean; + Domain : Dispatching_Domain_Access; + Task_Info : System.Task_Info.Task_Info_Type; + Stack_Size : System.Parameters.Size_Type; + T : Task_Id; + Success : out Boolean); -- Initialize fields of the TCB for task T, and link into global TCB -- structures. Call this only with abort deferred and holding RTS_Lock. -- Self_ID is the calling task (normally the activator of T). Success is diff --git a/gcc/ada/libgnarl/s-tassta.adb b/gcc/ada/libgnarl/s-tassta.adb index 594a1672866..65e950af655 100644 --- a/gcc/ada/libgnarl/s-tassta.adb +++ b/gcc/ada/libgnarl/s-tassta.adb @@ -584,7 +584,8 @@ package body System.Tasking.Stages is end if; Initialize_ATCB (Self_ID, State, Discriminants, P, Elaborated, - Base_Priority, Base_CPU, Domain, Task_Info, Stack_Size, T, Success); + Base_Priority, Base_CPU, CPU /= Unspecified_CPU, Domain, Task_Info, + Stack_Size, T, Success); if not Success then Free (T); diff --git a/gcc/ada/libgnarl/s-tporft.adb b/gcc/ada/libgnarl/s-tporft.adb index 66a9f02656e..a1570321f06 100644 --- a/gcc/ada/libgnarl/s-tporft.adb +++ b/gcc/ada/libgnarl/s-tporft.adb @@ -66,8 +66,8 @@ begin System.Tasking.Initialize_ATCB (Self_Id, null, Null_Address, Null_Task, Foreign_Task_Elaborated'Access, - System.Priority'First, System.Multiprocessors.Not_A_Specific_CPU, null, - Task_Info.Unspecified_Task_Info, 0, Self_Id, Succeeded); + System.Priority'First, System.Multiprocessors.Not_A_Specific_CPU, False, + null, Task_Info.Unspecified_Task_Info, 0, Self_Id, Succeeded); Unlock_RTS; pragma Assert (Succeeded);