From owner-dev-commits-src-all@freebsd.org Sun Apr 25 14:22:12 2021 Return-Path: Delivered-To: dev-commits-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 5761F62444D; Sun, 25 Apr 2021 14:22:12 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4FSqwh23dSz3R2c; Sun, 25 Apr 2021 14:22:12 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 39EC015A3E; Sun, 25 Apr 2021 14:22:12 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 13PEMCqK084981; Sun, 25 Apr 2021 14:22:12 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 13PEMCNX084980; Sun, 25 Apr 2021 14:22:12 GMT (envelope-from git) Date: Sun, 25 Apr 2021 14:22:12 GMT Message-Id: <202104251422.13PEMCNX084980@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Robert Watson Subject: git: 8e491aaeac6e - main - Add code examples to cpuset(2), and improve cross referencing. MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: rwatson X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 8e491aaeac6eab92b1ba8f077f75cebac39adb1c Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-all@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for all branches of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 25 Apr 2021 14:22:12 -0000 The branch main has been updated by rwatson: URL: https://cgit.FreeBSD.org/src/commit/?id=8e491aaeac6eab92b1ba8f077f75cebac39adb1c commit 8e491aaeac6eab92b1ba8f077f75cebac39adb1c Author: Robert Watson AuthorDate: 2021-04-25 14:22:00 +0000 Commit: Robert Watson CommitDate: 2021-04-25 14:22:00 +0000 Add code examples to cpuset(2), and improve cross referencing. MFC after: 1 week Reviewed by: jeff, jrtc27, kevans, bcr (manpages) Differential revision: https://reviews.freebsd.org/D27803 --- lib/libc/sys/cpuset.2 | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/lib/libc/sys/cpuset.2 b/lib/libc/sys/cpuset.2 index a2e5269ac042..8b17f537e7fa 100644 --- a/lib/libc/sys/cpuset.2 +++ b/lib/libc/sys/cpuset.2 @@ -1,5 +1,6 @@ .\" Copyright (c) 2008 Christian Brueffer .\" Copyright (c) 2008 Jeffrey Roberson +.\" Copyright (c) 2021 Robert N. M. Watson .\" All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without @@ -180,9 +181,72 @@ The actual contents of the sets may be retrieved or manipulated using .Xr cpuset_setaffinity 2 , .Xr cpuset_getdomain 2 , and .Xr cpuset_setdomain 2 . +The +.Xr cpuset 9 +macros may be used to manipulate masks of type +.Ft cpuset_t +get and set using those APIs. See those manual pages for more detail. .Sh RETURN VALUES .Rv -std +.Sh EXAMPLES +In this example, a CPU set mask is configured to limit execution to the first +CPU using +.Xr CPU_ZERO 9 +and +.Xr CPU_SET 9 , +members of the +.Xr cpuset 9 +programming interface. +Then, the mask is applied to a new anonymous CPU set associated with the +current process using +.Xr cpuset_setaffinity 2 . +This mask will be used by the current process, and inherited by any new +child processes. +.Bd -literal -offset indent +#include +#include + +#include + +cpuset_t cpuset_mask; + +/* Initialize a CPU mask and enable CPU 0. */ +CPU_ZERO(&cpuset_mask); +CPU_SET(0, &cpuset_mask); + +/* Set affinity for the CPU set for the current process. */ +if (cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, + sizeof(cpuset_mask), &cpuset_mask) < 0) + err(EX_OSERR, "cpuset_setaffinity"); +.Ed +.Pp +In the next example, a named CPU set is created containing the current +process, and its affinity similarly configured. +The resulting CPU set ID can then be used for further external management of +the affinity of the set. +.Bd -literal -offset indent +#include +#include + +#include + +cpusetid_t cpuset_id; +cpuset_t cpuset_mask; + +/* Create new cpuset for the current process. */ +if (cpuset(&cpuset_id) < 0) + err(EX_OSERR, "cpuset"); + +/* Initialize a CPU mask and enable CPU 0. */ +CPU_ZERO(&cpuset_mask); +CPU_SET(0, &cpuset_mask); + +/* Set affinity for the CPU set for the current process. */ +if (cpuset_setaffinity(CPU_LEVEL_SET, CPU_WHICH_CPUSET, cpuset_id, + sizeof(cpuset_mask), &cpuset_mask) < 0) + err(EX_OSERR, "cpuset_setaffinity"); +.Ed .Sh ERRORS The following error codes may be set in .Va errno : @@ -226,6 +290,8 @@ for allocation. .Xr cpuset_setdomain 2 , .Xr pthread_affinity_np 3 , .Xr pthread_attr_affinity_np 3 , +.Xr CPU_SET 9 , +.Xr CPU_ZERO 9 , .Xr cpuset 9 .Sh HISTORY The