Skip to content

Instantly share code, notes, and snippets.

@apangin
Last active March 14, 2023 23:46
Show Gist options
  • Save apangin/0886b7a50560411897cd1794d065989a to your computer and use it in GitHub Desktop.
Save apangin/0886b7a50560411897cd1794d065989a to your computer and use it in GitHub Desktop.
Fool JVM about the number of available processors according to /sys/devices/system/cpu/online
// Compile: gcc -O2 -fPIC -shared -o libproccount.so proccount.c
// Run: LD_PRELOAD=/path/to/libproccount.so java args
#define _GNU_SOURCE
#include <sched.h>
#include <unistd.h>
static int online_cpus;
__attribute__((constructor))
static void init_online_cpus() {
online_cpus = (int)sysconf(_SC_NPROCESSORS_ONLN);
}
int sched_getaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask) {
CPU_ZERO_S(cpusetsize, mask);
int i;
for (i = 0; i < online_cpus; i++) {
CPU_SET_S(i, cpusetsize, mask);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment