JayBeams  0.1
Another project to have fun coding.
Public Member Functions | Static Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
jb::cpu_set Class Reference

A wrapper for the Linux CPU_SET data structure. More...

#include <cpu_set.hpp>

Public Member Functions

 cpu_set ()
 
std::size_t capacity () const
 Return the number of CPUs that can be stored in the cpu set. More...
 
bool status (int cpu) const
 Returns true if cpu is included in the cpu set. More...
 
int count () const
 Return the number of cpus included in the cpu set. More...
 
cpu_setreset ()
 Remove all cpus from the cpu set. More...
 
cpu_setset (int cpu)
 Add cpu to the cpu set. More...
 
cpu_setclear (int cpu)
 Remove cpu from the cpu set. More...
 
cpu_setset (int cpulo, int cpuhi)
 Add all the cpus in the [cpulo,cpuhi] range to the set. More...
 
cpu_setclear (int cpulo, int cpuhi)
 Remove all the cpus in the [cpulo,cpuhi] range from the set. More...
 
std::string as_list_format () const
 Return the set in the list format representation. More...
 
Implement bitwise operations for cpu sets.
void operator &= (cpu_set const &rhs)
 
void operator|= (cpu_set const &rhs)
 
void operator^= (cpu_set const &rhs)
 
Comparison operators for cpu sets.
bool operator== (cpu_set const &rhs) const
 
bool operator!= (cpu_set const &rhs) const
 
Access the native implementation.
cpu_set_t * native_handle ()
 
cpu_set_t const * native_handle () const
 

Static Public Member Functions

static cpu_set parse (std::string const &value)
 Interpret value as a cpu set in list format. More...
 

Private Member Functions

void check_range (int cpu, char const *op) const
 Check that cpu is in range. More...
 
void check_range (int cpulo, int cpuhi, char const *op) const
 Check that [cpulo,cpuhi] is in range. More...
 

Static Private Member Functions

static void parse_error (std::string const &value)
 Raise an exception because the input to parse() is invalid. More...
 

Private Attributes

cpu_set_t set_
 

Detailed Description

A wrapper for the Linux CPU_SET data structure.

Setting CPU affinity can help improve the predictability of our system. Typically different threads are assigned to different processors and in extreme cases no threads share the same core. The cpu sets are also typically configured using the "List Format" as described in cpuset(7). Briefly this format is:

cpuset ::= range[,cpuset] range ::= (number|number-number) number ::= a positive integer

So one can specify a cpu_set as "1,2,3" (cpus 1, 2 and 3), or "1-15" (cpus from 1 to 15), or "0-4,10,12-14" (cpus 0 to 4, 10 and 12 to 14).

The iostream operators (used for tests, and conversions everywhere) use this format.

TODO(#151) this is a portability problem. Many platforms can specify cpu affinity, but they all use different constructs for it. TODO(#152) we only support up to CPU_SETSIZE cpus, it is possible to control more CPUs in Linux, but the application that has access to more than 1024 cpus is rare indeed.

Definition at line 37 of file cpu_set.hpp.

Constructor & Destructor Documentation

◆ cpu_set()

jb::cpu_set::cpu_set ( )
inline

Definition at line 39 of file cpu_set.hpp.

References set_.

Member Function Documentation

◆ as_list_format()

std::string jb::cpu_set::as_list_format ( ) const

Return the set in the list format representation.

Definition at line 55 of file cpu_set.cpp.

References capacity(), and status().

Referenced by YAML::convert< jb::cpu_set >::encode(), operator!=(), and jb::operator<<().

◆ capacity()

std::size_t jb::cpu_set::capacity ( ) const
inline

Return the number of CPUs that can be stored in the cpu set.

Definition at line 45 of file cpu_set.hpp.

Referenced by as_list_format(), BOOST_AUTO_TEST_CASE(), and check_range().

◆ check_range() [1/2]

void jb::cpu_set::check_range ( int  cpu,
char const *  op 
) const
private

Check that cpu is in range.

Exceptions
std::out_of_rangeif the argument is out of range.

Definition at line 75 of file cpu_set.cpp.

References capacity().

Referenced by clear(), native_handle(), and set().

◆ check_range() [2/2]

void jb::cpu_set::check_range ( int  cpulo,
int  cpuhi,
char const *  op 
) const
private

Check that [cpulo,cpuhi] is in range.

Exceptions
std::out_of_rangeif the arguments are out of range.

Definition at line 85 of file cpu_set.cpp.

References capacity().

◆ clear() [1/2]

cpu_set& jb::cpu_set::clear ( int  cpu)
inline

Remove cpu from the cpu set.

Definition at line 73 of file cpu_set.hpp.

References check_range(), and set_.

Referenced by BOOST_AUTO_TEST_CASE().

◆ clear() [2/2]

jb::cpu_set & jb::cpu_set::clear ( int  cpulo,
int  cpuhi 
)

Remove all the cpus in the [cpulo,cpuhi] range from the set.

Definition at line 16 of file cpu_set.cpp.

References check_range(), and set_.

◆ count()

int jb::cpu_set::count ( ) const
inline

Return the number of cpus included in the cpu set.

Definition at line 55 of file cpu_set.hpp.

References set_.

Referenced by BOOST_AUTO_TEST_CASE().

◆ native_handle() [1/2]

cpu_set_t* jb::cpu_set::native_handle ( )
inline

Definition at line 127 of file cpu_set.hpp.

References set_.

◆ native_handle() [2/2]

cpu_set_t const* jb::cpu_set::native_handle ( ) const
inline

Definition at line 130 of file cpu_set.hpp.

References check_range(), parse_error(), and set_.

◆ operator &=()

void jb::cpu_set::operator&= ( cpu_set const &  rhs)
inline

Definition at line 89 of file cpu_set.hpp.

References set_.

◆ operator!=()

bool jb::cpu_set::operator!= ( cpu_set const &  rhs) const
inline

Definition at line 108 of file cpu_set.hpp.

References as_list_format(), and parse().

◆ operator==()

bool jb::cpu_set::operator== ( cpu_set const &  rhs) const
inline

Definition at line 104 of file cpu_set.hpp.

References set_.

◆ operator^=()

void jb::cpu_set::operator^= ( cpu_set const &  rhs)
inline

Definition at line 95 of file cpu_set.hpp.

References set_.

◆ operator|=()

void jb::cpu_set::operator|= ( cpu_set const &  rhs)
inline

Definition at line 92 of file cpu_set.hpp.

References set_.

◆ parse()

jb::cpu_set jb::cpu_set::parse ( std::string const &  value)
static

Interpret value as a cpu set in list format.

Definition at line 24 of file cpu_set.cpp.

References parse_error(), set(), and jb::strtonum().

Referenced by BOOST_AUTO_TEST_CASE(), YAML::convert< jb::cpu_set >::decode(), operator!=(), and jb::operator>>().

◆ parse_error()

void jb::cpu_set::parse_error ( std::string const &  value)
staticprivate

Raise an exception because the input to parse() is invalid.

Definition at line 94 of file cpu_set.cpp.

Referenced by native_handle(), and parse().

◆ reset()

cpu_set& jb::cpu_set::reset ( )
inline

Remove all cpus from the cpu set.

Definition at line 60 of file cpu_set.hpp.

References set_.

◆ set() [1/2]

cpu_set& jb::cpu_set::set ( int  cpu)
inline

Add cpu to the cpu set.

Definition at line 66 of file cpu_set.hpp.

References check_range(), and set_.

Referenced by BOOST_AUTO_TEST_CASE(), and parse().

◆ set() [2/2]

jb::cpu_set & jb::cpu_set::set ( int  cpulo,
int  cpuhi 
)

Add all the cpus in the [cpulo,cpuhi] range to the set.

Definition at line 8 of file cpu_set.cpp.

References check_range(), and set_.

◆ status()

bool jb::cpu_set::status ( int  cpu) const
inline

Returns true if cpu is included in the cpu set.

Definition at line 50 of file cpu_set.hpp.

References set_.

Referenced by as_list_format(), and BOOST_AUTO_TEST_CASE().

Member Data Documentation

◆ set_

cpu_set_t jb::cpu_set::set_
private

The documentation for this class was generated from the following files: