| var CPU_PATH = "/sys/fs/cgroup/cpuset.cpus.effective" |
| cpuAllocator *CpuAllocatorT |
| func (c *CpuContext) Release() { |
| c.cpuAllocator.cpus = append(c.cpuAllocator.cpus, c.cpus...) |
| c.cpus = c.cpus[:0] // empty the list |
| type CpuAllocatorT struct { |
| var cpuAllocator *CpuAllocatorT = nil |
| func (c *CpuAllocatorT) Allocate(nCpus int) (*CpuContext, error) { |
| return nil, fmt.Errorf("could not allocate %d CPUs; available: %d", nCpus, len(c.cpus)) |
| cpuCtx.cpus = c.cpus[0:nCpus] |
| func (c *CpuAllocatorT) readCpus(fname string) error { |
| file, err := os.Open(CPU_PATH) |
| sc := bufio.NewScanner(file) |
| _, err = fmt.Sscanf(line, "%d-%d", &first, &last) |
| for i := first; i <= last; i++ { |
| c.cpus = append(c.cpus, i) |
| func CpuAllocator() (*CpuAllocatorT, error) { |
| cpuAllocator = new(CpuAllocatorT) |
| err := cpuAllocator.readCpus(CPU_PATH) |