Skip to content

Commit ff59ffd

Browse files
committed
[fix] Fix Runtime.availableProcessors() returning false cpu count
Successfully tested on Big.Little (Kaleao KMAX)
1 parent 506a590 commit ff59ffd

File tree

4 files changed

+27
-95
lines changed

4 files changed

+27
-95
lines changed

com.oracle.max.vm.native/substrate/jvm.c

+23-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
2-
* Copyright (c) 2017, APT Group, School of Computer Science,
2+
* Copyright (c) 2017, 2019, APT Group, School of Computer Science,
33
* 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.
55
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
66
*
77
* This code is free software; you can redistribute it and/or modify it
@@ -35,6 +35,7 @@
3535
#include <sys/ioctl.h>
3636
#include <sys/stat.h>
3737
#include <sys/time.h>
38+
#include <sched.h>
3839
#if os_DARWIN
3940
#include <sys/poll.h>
4041
#else
@@ -252,7 +253,26 @@ JVM_ActiveProcessorCount(void) {
252253
}
253254
// Otherwise return number of online cpus
254255
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
256276
// Linux doesn't yet have a (official) notion of processor sets,
257277
// so just return the number of online processors.
258278
int online_cpus = sysconf(_SC_NPROCESSORS_ONLN);

com.oracle.max.vm.native/substrate/runtime.c

-88
This file was deleted.

com.oracle.max.vm.native/substrate/substrate.mk

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ PROJECT = ../../..
2828
LIB = jvm
2929

3030
SOURCES = c.c condition.c log.c image.c $(ISA).c jni.c jvm.c maxine.c memory.c mutex.c \
31-
relocation.c dataio.c runtime.c snippet.c threads.c threadLocals.c time.c trap.c \
31+
relocation.c dataio.c snippet.c threads.c threadLocals.c time.c trap.c \
3232
virtualMemory.c jnitests.c sync.c signal.c jmm.c jvmti.c
3333

3434

test/src/test/output/RuntimeTest0.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/*
2+
* Copyright (c) 2019, APT Group, School of Computer Science,
3+
* The University of Manchester. All rights reserved.
24
* Copyright (c) 2012, 2012, Oracle and/or its affiliates. All rights reserved.
35
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
46
*
@@ -31,9 +33,7 @@ public static void main(String[] args) {
3133
System.out.println("Start");
3234
int numCpus = Runtime.getRuntime().availableProcessors();
3335
assert numCpus > 0 : "Cannot have less than 1 processor returned";
34-
if (args.length > 0 && args[0].equals("-p")) {
35-
System.out.println("Runtime.getRuntime().availableProcessors() : " + numCpus);
36-
}
36+
System.out.println("Runtime.getRuntime().availableProcessors() : " + numCpus);
3737
System.out.println("Done");
3838
}
3939

0 commit comments

Comments
 (0)