Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 3aef0c7

Browse files
authored
Merge pull request #2431 from kubo39/cpu_count
Add CPU_COUNT merged-on-behalf-of: Nicholas Wilson <thewilsonator@users.noreply.github.com>
2 parents 29e9ac2 + 9bc3b6c commit 3aef0c7

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

changelog/cpu_count.dd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Added `core.sys.linux.sched.CPU_COUNT`.
2+
3+
Added `core.sys.linux.sched.CPU_COUNT`, which returns the number of CPUs in set.

src/core/sys/linux/sched.d

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
module core.sys.linux.sched;
1616

17+
import core.bitop : popcnt;
1718
import core.sys.posix.sched;
1819
import core.sys.posix.config;
1920
import core.sys.posix.sys.types;
@@ -63,6 +64,14 @@ private // helpers
6364
return (cpusetp.__bits[__CPUELT(cpu)] & __CPUMASK(cpu)) != 0;
6465
return false;
6566
}
67+
68+
int __CPU_COUNT_S(size_t setsize, cpu_set_t* cpusetp) pure
69+
{
70+
int s = 0;
71+
foreach (i; cpusetp.__bits[0 .. (setsize / cpu_mask.sizeof)])
72+
s += popcnt(i);
73+
return s;
74+
}
6675
}
6776

6877
/// Type for array elements in 'cpu_set_t'.
@@ -86,6 +95,11 @@ bool CPU_ISSET(size_t cpu, cpu_set_t* cpusetp) pure
8695
return __CPU_ISSET_S(cpu, cpu_set_t.sizeof, cpusetp);
8796
}
8897

98+
int CPU_COUNT(cpu_set_t* cpusetp) pure
99+
{
100+
return __CPU_COUNT_S(cpu_set_t.sizeof, cpusetp);
101+
}
102+
89103
/* Functions */
90104
int sched_setaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask);
91105
int sched_getaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask);

0 commit comments

Comments
 (0)