Skip to content

Instantly share code, notes, and snippets.

@dln
Created October 19, 2016 13:06
Show Gist options
  • Select an option

  • Save dln/23ceda77fbed89b8d4df6541553aa196 to your computer and use it in GitHub Desktop.

Select an option

Save dln/23ceda77fbed89b8d4df6541553aa196 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
libhack=$(mktemp --tmpdir libhack_XXXXX.so)
trap "rm -f ${libhack}" EXIT
gcc -O3 -fPIC -shared -Wl,-soname,libnumcpus.so -o ${libhack} -x c - <<EOF
#include <stdlib.h>
#include <unistd.h>
int JVM_ActiveProcessorCount(void) {
char* val = getenv("_NUM_CPUS");
return val != NULL ? atoi(val) : sysconf(_SC_NPROCESSORS_ONLN);
}
EOF
LD_PRELOAD=${libhack} _NUM_CPUS=2 "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment