|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2017, APT Group, School of Computer Science, |
| 2 | + * Copyright (c) 2017, 2019, APT Group, School of Computer Science, |
3 | 3 | * The University of Manchester. All rights reserved.
|
4 |
| - * Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved. |
| 4 | + * Copyright (c) 2007, 2019,Oracle and/or its affiliates. All rights reserved. |
5 | 5 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
6 | 6 | *
|
7 | 7 | * This code is free software; you can redistribute it and/or modify it
|
|
35 | 35 | #include <sys/ioctl.h>
|
36 | 36 | #include <sys/stat.h>
|
37 | 37 | #include <sys/time.h>
|
| 38 | +#include <sched.h> |
38 | 39 | #if os_DARWIN
|
39 | 40 | #include <sys/poll.h>
|
40 | 41 | #else
|
@@ -252,7 +253,26 @@ JVM_ActiveProcessorCount(void) {
|
252 | 253 | }
|
253 | 254 | // Otherwise return number of online cpus
|
254 | 255 | return online_cpus;
|
255 |
| -#elif os_LINUX || os_DARWIN |
| 256 | +#elif os_LINUX |
| 257 | + cpu_set_t cpus; // can represent at most 1024 (CPU_SETSIZE) processors |
| 258 | + int cpus_size = sizeof(cpu_set_t); |
| 259 | + int processor_count = sysconf(_SC_NPROCESSORS_CONF); |
| 260 | + int cpu_count = 0; |
| 261 | + |
| 262 | + // pid 0 means the current thread - which we have to assume represents the process |
| 263 | + if (sched_getaffinity(0, cpus_size, &cpus) == 0) { |
| 264 | + // only look up to the number of configured processors |
| 265 | + for (int i = 0; i < processor_count; i++) { |
| 266 | + if (CPU_ISSET(i, &cpus)) { |
| 267 | + cpu_count++; |
| 268 | + } |
| 269 | + } |
| 270 | + } else { |
| 271 | + cpu_count = sysconf(_SC_NPROCESSORS_ONLN); |
| 272 | + } |
| 273 | + |
| 274 | + return cpu_count; |
| 275 | +#elif os_DARWIN |
256 | 276 | // Linux doesn't yet have a (official) notion of processor sets,
|
257 | 277 | // so just return the number of online processors.
|
258 | 278 | int online_cpus = sysconf(_SC_NPROCESSORS_ONLN);
|
|
0 commit comments