How to get possible CPUS in Ubuntu 14.04 or 16.04

Multi tool use
I have a process which is pinned on core 1. I have written the below function to get the affinity.
count_cpu_cores()
{
long int n_cores;
n_cores = sysconf(_SC_NPROCESSORS_CONF);
if (n_cores > 0) {
cpu_set_t *set = CPU_ALLOC(n_cores);
if (set) {
size_t size = CPU_ALLOC_SIZE(n_cores);
if (!sched_getaffinity(146244, size, set)) {
printf("enter sched affinityn");
n_cores = CPU_COUNT_S(size, set);
}
CPU_FREE(set);
}
printf("n_cores-%d",n_cores);
}
When i print ncores , it give me value of 32 which is incorrect. On further debugging i find that
sched_getaffinity return EINVAL as it is expecting the greater size. Now when i hardcode n_cores
to be 144 which is the number in possible cpus as below. I get the correct value of the number of
CPUS that is 1. How can i fix this problem in my program without hardcoding ?
sdn@dell33:~$ cd /sys/devices/system/cpu
sdn@dell33:/sys/devices/system/cpu$ ls
cpu0 cpu12 cpu16 cpu2 cpu23 cpu27 cpu30 cpu6 cpufreq kernel_max online uevent
cpu1 cpu13 cpu17 cpu20 cpu24 cpu28 cpu31 cpu7 cpuidle microcode possible vulnerabilities
cpu10 cpu14 cpu18 cpu21 cpu25 cpu29 cpu4 cpu8 intel_pstate modalias power
cpu11 cpu15 cpu19 cpu22 cpu26 cpu3 cpu5 cpu9 isolated offline present
sdn@dell33:/sys/devices/system/cpu$ cat possible
0-143
sdn@dell33:/sys/devices/system/cpu$ cat kernel_max
511
14.04 16.04 cpu coreutils cpuinfo
add a comment |
I have a process which is pinned on core 1. I have written the below function to get the affinity.
count_cpu_cores()
{
long int n_cores;
n_cores = sysconf(_SC_NPROCESSORS_CONF);
if (n_cores > 0) {
cpu_set_t *set = CPU_ALLOC(n_cores);
if (set) {
size_t size = CPU_ALLOC_SIZE(n_cores);
if (!sched_getaffinity(146244, size, set)) {
printf("enter sched affinityn");
n_cores = CPU_COUNT_S(size, set);
}
CPU_FREE(set);
}
printf("n_cores-%d",n_cores);
}
When i print ncores , it give me value of 32 which is incorrect. On further debugging i find that
sched_getaffinity return EINVAL as it is expecting the greater size. Now when i hardcode n_cores
to be 144 which is the number in possible cpus as below. I get the correct value of the number of
CPUS that is 1. How can i fix this problem in my program without hardcoding ?
sdn@dell33:~$ cd /sys/devices/system/cpu
sdn@dell33:/sys/devices/system/cpu$ ls
cpu0 cpu12 cpu16 cpu2 cpu23 cpu27 cpu30 cpu6 cpufreq kernel_max online uevent
cpu1 cpu13 cpu17 cpu20 cpu24 cpu28 cpu31 cpu7 cpuidle microcode possible vulnerabilities
cpu10 cpu14 cpu18 cpu21 cpu25 cpu29 cpu4 cpu8 intel_pstate modalias power
cpu11 cpu15 cpu19 cpu22 cpu26 cpu3 cpu5 cpu9 isolated offline present
sdn@dell33:/sys/devices/system/cpu$ cat possible
0-143
sdn@dell33:/sys/devices/system/cpu$ cat kernel_max
511
14.04 16.04 cpu coreutils cpuinfo
add a comment |
I have a process which is pinned on core 1. I have written the below function to get the affinity.
count_cpu_cores()
{
long int n_cores;
n_cores = sysconf(_SC_NPROCESSORS_CONF);
if (n_cores > 0) {
cpu_set_t *set = CPU_ALLOC(n_cores);
if (set) {
size_t size = CPU_ALLOC_SIZE(n_cores);
if (!sched_getaffinity(146244, size, set)) {
printf("enter sched affinityn");
n_cores = CPU_COUNT_S(size, set);
}
CPU_FREE(set);
}
printf("n_cores-%d",n_cores);
}
When i print ncores , it give me value of 32 which is incorrect. On further debugging i find that
sched_getaffinity return EINVAL as it is expecting the greater size. Now when i hardcode n_cores
to be 144 which is the number in possible cpus as below. I get the correct value of the number of
CPUS that is 1. How can i fix this problem in my program without hardcoding ?
sdn@dell33:~$ cd /sys/devices/system/cpu
sdn@dell33:/sys/devices/system/cpu$ ls
cpu0 cpu12 cpu16 cpu2 cpu23 cpu27 cpu30 cpu6 cpufreq kernel_max online uevent
cpu1 cpu13 cpu17 cpu20 cpu24 cpu28 cpu31 cpu7 cpuidle microcode possible vulnerabilities
cpu10 cpu14 cpu18 cpu21 cpu25 cpu29 cpu4 cpu8 intel_pstate modalias power
cpu11 cpu15 cpu19 cpu22 cpu26 cpu3 cpu5 cpu9 isolated offline present
sdn@dell33:/sys/devices/system/cpu$ cat possible
0-143
sdn@dell33:/sys/devices/system/cpu$ cat kernel_max
511
14.04 16.04 cpu coreutils cpuinfo
I have a process which is pinned on core 1. I have written the below function to get the affinity.
count_cpu_cores()
{
long int n_cores;
n_cores = sysconf(_SC_NPROCESSORS_CONF);
if (n_cores > 0) {
cpu_set_t *set = CPU_ALLOC(n_cores);
if (set) {
size_t size = CPU_ALLOC_SIZE(n_cores);
if (!sched_getaffinity(146244, size, set)) {
printf("enter sched affinityn");
n_cores = CPU_COUNT_S(size, set);
}
CPU_FREE(set);
}
printf("n_cores-%d",n_cores);
}
When i print ncores , it give me value of 32 which is incorrect. On further debugging i find that
sched_getaffinity return EINVAL as it is expecting the greater size. Now when i hardcode n_cores
to be 144 which is the number in possible cpus as below. I get the correct value of the number of
CPUS that is 1. How can i fix this problem in my program without hardcoding ?
sdn@dell33:~$ cd /sys/devices/system/cpu
sdn@dell33:/sys/devices/system/cpu$ ls
cpu0 cpu12 cpu16 cpu2 cpu23 cpu27 cpu30 cpu6 cpufreq kernel_max online uevent
cpu1 cpu13 cpu17 cpu20 cpu24 cpu28 cpu31 cpu7 cpuidle microcode possible vulnerabilities
cpu10 cpu14 cpu18 cpu21 cpu25 cpu29 cpu4 cpu8 intel_pstate modalias power
cpu11 cpu15 cpu19 cpu22 cpu26 cpu3 cpu5 cpu9 isolated offline present
sdn@dell33:/sys/devices/system/cpu$ cat possible
0-143
sdn@dell33:/sys/devices/system/cpu$ cat kernel_max
511
14.04 16.04 cpu coreutils cpuinfo
14.04 16.04 cpu coreutils cpuinfo
asked Jan 16 at 4:13


Vincent AVincent A
11
11
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1110144%2fhow-to-get-possible-cpus-in-ubuntu-14-04-or-16-04%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Ask Ubuntu!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1110144%2fhow-to-get-possible-cpus-in-ubuntu-14-04-or-16-04%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
RYFZP,EX2Pv R7opFTq3ObrcnhcaTruUt4ko yodbRimPM3gRgBAGY