man-pages/man3/CPU_SET.3.html
2021-03-31 01:06:50 +01:00

521 lines
12 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of CPU_SET</TITLE>
</HEAD><BODY>
<H1>CPU_SET</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
CPU_SET, CPU_CLR, CPU_ISSET, CPU_ZERO, CPU_COUNT,
CPU_AND, CPU_OR, CPU_XOR, CPU_EQUAL,
CPU_ALLOC, CPU_ALLOC_SIZE, CPU_FREE,
CPU_SET_S, CPU_CLR_S, CPU_ISSET_S, CPU_ZERO_S,
CPU_COUNT_S, CPU_AND_S, CPU_OR_S, CPU_XOR_S, CPU_EQUAL_S -
macros for manipulating CPU sets
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#define _GNU_SOURCE</B> /* See <A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A>(7) */
<B>#include &lt;<A HREF="file:///usr/include/sched.h">sched.h</A>&gt;</B>
<B>void CPU_ZERO(cpu_set_t *</B><I>set</I><B>);</B>
<B>void CPU_SET(int </B><I>cpu</I><B>, cpu_set_t *</B><I>set</I><B>);</B>
<B>void CPU_CLR(int </B><I>cpu</I><B>, cpu_set_t *</B><I>set</I><B>);</B>
<B>int CPU_ISSET(int </B><I>cpu</I><B>, cpu_set_t *</B><I>set</I><B>);</B>
<B>int CPU_COUNT(cpu_set_t *</B><I>set</I><B>);</B>
<B>void CPU_AND(cpu_set_t *</B><I>destset</I><B>,</B>
<B> cpu_set_t *</B><I>srcset1</I><B>, cpu_set_t *</B><I>srcset2</I><B>);</B>
<B>void CPU_OR(cpu_set_t *</B><I>destset</I><B>,</B>
<B> cpu_set_t *</B><I>srcset1</I><B>, cpu_set_t *</B><I>srcset2</I><B>);</B>
<B>void CPU_XOR(cpu_set_t *</B><I>destset</I><B>,</B>
<B> cpu_set_t *</B><I>srcset1</I><B>, cpu_set_t *</B><I>srcset2</I><B>);</B>
<B>int CPU_EQUAL(cpu_set_t *</B><I>set1</I><B>, cpu_set_t *</B><I>set2</I><B>);</B>
<B>cpu_set_t *CPU_ALLOC(int </B><I>num_cpus</I><B>);</B>
<B>void CPU_FREE(cpu_set_t *</B><I>set</I><B>);</B>
<B>size_t CPU_ALLOC_SIZE(int </B><I>num_cpus</I><B>);</B>
<B>void CPU_ZERO_S(size_t </B><I>setsize</I><B>, cpu_set_t *</B><I>set</I><B>);</B>
<B>void CPU_SET_S(int </B><I>cpu</I><B>, size_t </B><I>setsize</I><B>, cpu_set_t *</B><I>set</I><B>);</B>
<B>void CPU_CLR_S(int </B><I>cpu</I><B>, size_t </B><I>setsize</I><B>, cpu_set_t *</B><I>set</I><B>);</B>
<B>int CPU_ISSET_S(int </B><I>cpu</I><B>, size_t </B><I>setsize</I><B>, cpu_set_t *</B><I>set</I><B>);</B>
<B>int CPU_COUNT_S(size_t </B><I>setsize</I><B>, cpu_set_t *</B><I>set</I><B>);</B>
<B>void CPU_AND_S(size_t </B><I>setsize</I><B>, cpu_set_t *</B><I>destset</I><B>,</B>
<B> cpu_set_t *</B><I>srcset1</I><B>, cpu_set_t *</B><I>srcset2</I><B>);</B>
<B>void CPU_OR_S(size_t </B><I>setsize</I><B>, cpu_set_t *</B><I>destset</I><B>,</B>
<B> cpu_set_t *</B><I>srcset1</I><B>, cpu_set_t *</B><I>srcset2</I><B>);</B>
<B>void CPU_XOR_S(size_t </B><I>setsize</I><B>, cpu_set_t *</B><I>destset</I><B>,</B>
<B> cpu_set_t *</B><I>srcset1</I><B>, cpu_set_t *</B><I>srcset2</I><B>);</B>
<B>int CPU_EQUAL_S(size_t </B><I>setsize</I><B>, cpu_set_t *</B><I>set1</I><B>, cpu_set_t *</B><I>set2</I><B>);</B>
</PRE>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
The
<I>cpu_set_t</I>
data structure represents a set of CPUs.
CPU sets are used by
<B><A HREF="/cgi-bin/man/man2html?2+sched_setaffinity">sched_setaffinity</A></B>(2)
and similar interfaces.
<P>
The
<I>cpu_set_t</I>
data type is implemented as a bit mask.
However, the data structure should be treated as opaque:
all manipulation of CPU sets should be done via the macros
described in this page.
<P>
The following macros are provided to operate on the CPU set
<I>set</I>:
<DL COMPACT>
<DT id="1"><B>CPU_ZERO</B>()
<DD>
Clears
<I>set</I>,
so that it contains no CPUs.
<DT id="2"><B>CPU_SET</B>()
<DD>
Add CPU
<I>cpu</I>
to
<I>set</I>.
<DT id="3"><B>CPU_CLR</B>()
<DD>
Remove CPU
<I>cpu</I>
from
<I>set</I>.
<DT id="4"><B>CPU_ISSET</B>()
<DD>
Test to see if CPU
<I>cpu</I>
is a member of
<I>set</I>.
<DT id="5"><B>CPU_COUNT</B>()
<DD>
Return the number of CPUs in
<I>set</I>.
</DL>
<P>
Where a
<I>cpu</I>
argument is specified, it should not produce side effects,
since the above macros may evaluate the argument more than once.
<P>
The first CPU on the system corresponds to a
<I>cpu</I>
value of 0, the next CPU corresponds to a
<I>cpu</I>
value of 1, and so on.
No assumptions should be made about particular CPUs being
available, or the set of CPUs being contiguous, since CPUs can
be taken offline dynamically or be otherwise absent.
The constant
<B>CPU_SETSIZE</B>
(currently 1024) specifies a value one greater than the maximum CPU
number that can be stored in
<I>cpu_set_t</I>.
<P>
The following macros perform logical operations on CPU sets:
<DL COMPACT>
<DT id="6"><B>CPU_AND</B>()
<DD>
Store the intersection of the sets
<I>srcset1</I>
and
<I>srcset2</I>
in
<I>destset</I>
(which may be one of the source sets).
<DT id="7"><B>CPU_OR</B>()
<DD>
Store the union of the sets
<I>srcset1</I>
and
<I>srcset2</I>
in
<I>destset</I>
(which may be one of the source sets).
<DT id="8"><B>CPU_XOR</B>()
<DD>
Store the XOR of the sets
<I>srcset1</I>
and
<I>srcset2</I>
in
<I>destset</I>
(which may be one of the source sets).
The XOR means the set of CPUs that are in either
<I>srcset1</I>
or
<I>srcset2</I>,
but not both.
<DT id="9"><B>CPU_EQUAL</B>()
<DD>
Test whether two CPU set contain exactly the same CPUs.
</DL>
<A NAME="lbAE">&nbsp;</A>
<H3>Dynamically sized CPU sets</H3>
Because some applications may require the ability to dynamically
size CPU sets (e.g., to allocate sets larger than that
defined by the standard
<I>cpu_set_t</I>
data type), glibc nowadays provides a set of macros to support this.
<P>
The following macros are used to allocate and deallocate CPU sets:
<DL COMPACT>
<DT id="10"><B>CPU_ALLOC</B>()
<DD>
Allocate a CPU set large enough to hold CPUs
in the range 0 to
<I>num_cpus-1</I>.
<DT id="11"><B>CPU_ALLOC_SIZE</B>()
<DD>
Return the size in bytes of the CPU set that would be needed to
hold CPUs in the range 0 to
<I>num_cpus-1</I>.
This macro provides the value that can be used for the
<I>setsize</I>
argument in the
<B>CPU_*_S</B>()
macros described below.
<DT id="12"><B>CPU_FREE</B>()
<DD>
Free a CPU set previously allocated by
<B>CPU_ALLOC</B>().
</DL>
<P>
The macros whose names end with &quot;_S&quot; are the analogs of
the similarly named macros without the suffix.
These macros perform the same tasks as their analogs,
but operate on the dynamically allocated CPU set(s) whose size is
<I>setsize</I>
bytes.
<A NAME="lbAF">&nbsp;</A>
<H2>RETURN VALUE</H2>
<B>CPU_ISSET</B>()
and
<B>CPU_ISSET_S</B>()
return nonzero if
<I>cpu</I>
is in
<I>set</I>;
otherwise, it returns 0.
<P>
<B>CPU_COUNT</B>()
and
<B>CPU_COUNT_S</B>()
return the number of CPUs in
<I>set</I>.
<P>
<B>CPU_EQUAL</B>()
and
<B>CPU_EQUAL_S</B>()
return nonzero if the two CPU sets are equal; otherwise they return 0.
<P>
<B>CPU_ALLOC</B>()
returns a pointer on success, or NULL on failure.
(Errors are as for
<B><A HREF="/cgi-bin/man/man2html?3+malloc">malloc</A></B>(3).)
<P>
<B>CPU_ALLOC_SIZE</B>()
returns the number of bytes required to store a
CPU set of the specified cardinality.
<P>
The other functions do not return a value.
<A NAME="lbAG">&nbsp;</A>
<H2>VERSIONS</H2>
The
<B>CPU_ZERO</B>(),
<B>CPU_SET</B>(),
<B>CPU_CLR</B>(),
and
<B>CPU_ISSET</B>()
macros were added in glibc 2.3.3.
<P>
<B>CPU_COUNT</B>()
first appeared in glibc 2.6.
<P>
<B>CPU_AND</B>(),
<B>CPU_OR</B>(),
<B>CPU_XOR</B>(),
<B>CPU_EQUAL</B>(),
<B>CPU_ALLOC</B>(),
<B>CPU_ALLOC_SIZE</B>(),
<B>CPU_FREE</B>(),
<B>CPU_ZERO_S</B>(),
<B>CPU_SET_S</B>(),
<B>CPU_CLR_S</B>(),
<B>CPU_ISSET_S</B>(),
<B>CPU_AND_S</B>(),
<B>CPU_OR_S</B>(),
<B>CPU_XOR_S</B>(),
and
<B>CPU_EQUAL_S</B>()
first appeared in glibc 2.7.
<A NAME="lbAH">&nbsp;</A>
<H2>CONFORMING TO</H2>
These interfaces are Linux-specific.
<A NAME="lbAI">&nbsp;</A>
<H2>NOTES</H2>
To duplicate a CPU set, use
<B><A HREF="/cgi-bin/man/man2html?3+memcpy">memcpy</A></B>(3).
<P>
Since CPU sets are bit masks allocated in units of long words,
the actual number of CPUs in a dynamically
allocated CPU set will be rounded up to the next multiple of
<I>sizeof(unsigned long)</I>.
An application should consider the contents of these extra bits
to be undefined.
<P>
Notwithstanding the similarity in the names,
note that the constant
<B>CPU_SETSIZE</B>
indicates the number of CPUs in the
<I>cpu_set_t</I>
data type (thus, it is effectively a count of the bits in the bit mask),
while the
<I>setsize</I>
argument of the
<B>CPU_*_S</B>()
macros is a size in bytes.
<P>
The data types for arguments and return values shown
in the SYNOPSIS are hints what about is expected in each case.
However, since these interfaces are implemented as macros,
the compiler won't necessarily catch all type errors
if you violate the suggestions.
<A NAME="lbAJ">&nbsp;</A>
<H2>BUGS</H2>
On 32-bit platforms with glibc 2.8 and earlier,
<B>CPU_ALLOC</B>()
allocates twice as much space as is required, and
<B>CPU_ALLOC_SIZE</B>()
returns a value twice as large as it should.
This bug should not affect the semantics of a program,
but does result in wasted memory
and less efficient operation of the macros that
operate on dynamically allocated CPU sets.
These bugs are fixed in glibc 2.9.
<A NAME="lbAK">&nbsp;</A>
<H2>EXAMPLE</H2>
The following program demonstrates the use of some of the macros
used for dynamically allocated CPU sets.
<P>
#define _GNU_SOURCE
#include &lt;<A HREF="file:///usr/include/sched.h">sched.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/assert.h">assert.h</A>&gt;
<P>
int
main(int argc, char *argv[])
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;cpu_set_t&nbsp;*cpusetp;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;size_t&nbsp;size;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;num_cpus,&nbsp;cpu;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(argc&nbsp;&lt;&nbsp;2)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;&quot;Usage:&nbsp;%s&nbsp;&lt;num-cpus&gt;\n&quot;,&nbsp;argv[0]);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;num_cpus&nbsp;=&nbsp;atoi(argv[1]);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;cpusetp&nbsp;=&nbsp;CPU_ALLOC(num_cpus);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(cpusetp&nbsp;==&nbsp;NULL)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(&quot;CPU_ALLOC&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;size&nbsp;=&nbsp;CPU_ALLOC_SIZE(num_cpus);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;CPU_ZERO_S(size,&nbsp;cpusetp);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(cpu&nbsp;=&nbsp;0;&nbsp;cpu&nbsp;&lt;&nbsp;num_cpus;&nbsp;cpu&nbsp;+=&nbsp;2)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CPU_SET_S(cpu,&nbsp;size,&nbsp;cpusetp);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;CPU_COUNT()&nbsp;of&nbsp;set:&nbsp;&nbsp;&nbsp;&nbsp;%d\n&quot;,&nbsp;CPU_COUNT_S(size,&nbsp;cpusetp));
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;CPU_FREE(cpusetp);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_SUCCESS);
}
<A NAME="lbAL">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?2+sched_setaffinity">sched_setaffinity</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?3+pthread_attr_setaffinity_np">pthread_attr_setaffinity_np</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+pthread_setaffinity_np">pthread_setaffinity_np</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?7+cpuset">cpuset</A></B>(7)
<A NAME="lbAM">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="13"><A HREF="#lbAB">NAME</A><DD>
<DT id="14"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="15"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DL>
<DT id="16"><A HREF="#lbAE">Dynamically sized CPU sets</A><DD>
</DL>
<DT id="17"><A HREF="#lbAF">RETURN VALUE</A><DD>
<DT id="18"><A HREF="#lbAG">VERSIONS</A><DD>
<DT id="19"><A HREF="#lbAH">CONFORMING TO</A><DD>
<DT id="20"><A HREF="#lbAI">NOTES</A><DD>
<DT id="21"><A HREF="#lbAJ">BUGS</A><DD>
<DT id="22"><A HREF="#lbAK">EXAMPLE</A><DD>
<DT id="23"><A HREF="#lbAL">SEE ALSO</A><DD>
<DT id="24"><A HREF="#lbAM">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:38 GMT, March 31, 2021
</BODY>
</HTML>