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 fors1equalss2(same CPUs in set) - Addition:
s1 + s1: Return the union ofs1ands2(returns copy) - Deletion: (
s1 - s2): Unset CPUs ins2froms1 - 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 CPUiset inss:zero (): Clear all cpus from set ss:first (): Return first set CPU inss:last (): Return last set CPU inss: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 ins1that are set ins1ors2s:intersect (s2): unset bits in s1 that are not set ins2s:is_in (s2): Is setscontained withins2s:contains (s2): Is sets2contained within setss:expand ([fn]): Expandsinto a table, optionally using functionfnas 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 CPUiis set in cpu_set otherwise falses[i] = 0: Unset CPUiinss[i] = 1: Set CPUiins
local affinity = require 'affinity'
local s1 = affinity.getaffinity ()
print (s1)
local s2 = affinity.cpuset.new ("0-1")
local rc, err = affinity.setaffinity (s2)