The lua-affinity module for Lua is a thin wrapper around Linux
schedutils calls sched_setaffinity(2)
and sched_getaffinity(2)
.
It also contains a Lua interface for creating and manipulating
the cpu_set_t
CPU masks used by the interface.
Creating cpu_set_t
objects:
local cpu_set = require 'affinity.cpuset'
c = cpu_set.new("0-1,4") -- create cpu_set from cstr list
x = cpu_set.new("0xf0") -- create a cpu_set from hex strin
v = cpu_set.new() -- Empty cpu_set
m = cpu_set.new("00000000,0000000f") -- Linux bit string format
Operators:
- Equals:
s1 == s2
: test fors1
equalss2
(same CPUs in set) - Addition:
s1 + s1
: Return the union ofs1
ands2
(returns copy) - Deletion: (
s1 - s2
): Unset CPUs ins2
froms1
- Length: (
#s1
): Return number of CPUs set in cpuset - Tostring: (
tostring (s1)
): Convert s1 to cstr type CPU list (0-1,4) - Concatenation (
..
): string concatenation as above (implied tostring)
Named methods:
s:set (i,...)
: Explicitly set CPU or CPUss:clr (i,...)
: Explicitly clear CPU or CPUss:isset (i)
: Return true if CPUi
set ins
s:zero ()
: Clear all cpus from set ss:first ()
: Return first set CPU ins
s:last ()
: Return last set CPU ins
s:count ()
: Return number of CPUs in the sets:weight ()
: Return number of CPUs in the sets:tohex ()
: Return hex string representation ofs
(linux bitstring format)s:union (s2)
: Set all bits ins1
that are set ins1
ors2
s:intersect (s2)
: unset bits in s1 that are not set ins2
s:is_in (s2)
: Is sets
contained withins2
s:contains (s2)
: Is sets2
contained within sets
s:expand ([fn])
: Expands
into a table, optionally using functionfn
as a filter.
Iterator:
-
s:setbits ()
: Iterate over all set bits ins
:s = cpuset.new ("0xf001") for i in s:setbits () do print (i) end
s[i]
: Return true if CPUi
is set in cpu_set otherwise falses[i] = 0
: Unset CPUi
ins
s[i] = 1
: Set CPUi
ins
local affinity = require 'affinity'
local s1 = affinity.getaffinity ()
print (s1)
local s2 = affinity.cpuset.new ("0-1")
local rc, err = affinity.setaffinity (s2)