Skip to content

Instantly share code, notes, and snippets.

@azat
Last active September 20, 2020 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azat/9770f751c3e7d2da5422cc1ca26339c4 to your computer and use it in GitHub Desktop.
Save azat/9770f751c3e7d2da5422cc1ca26339c4 to your computer and use it in GitHub Desktop.
/**
* Reproducer for percpu_arena jemalloc issue [1].
* Requires debug jemalloc (w/o NDEBUG to include assert()s)
*
* [1]: https://github.com/jemalloc/jemalloc/pull/1676
*
* $ gcc -g3 -o /tmp/sched /tmp/sched.c
* $ MALLOC_CONF=percpu_arena:percpu LD_PRELOAD=jemalloc/build/lib/libjemalloc.so taskset --cpu-list 3 /tmp/sched
* <jemalloc>: ../src/jemalloc.c:321: Failed assertion: "ind <= narenas_total_get()"
* Aborted (core dumped)
*/
#define _GNU_SOURCE
#include <sched.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
cpu_set_t set;
int err;
err = sched_getaffinity(0, sizeof(set), &set);
assert(!err);
printf("sched_getcpu() = %i\n", sched_getcpu());
printf("sched_getaffinity() = %i\n", CPU_COUNT(&set));
printf("_SC_NPROCESSORS_ONLN = %i\n", sysconf(_SC_NPROCESSORS_ONLN));
return EXIT_SUCCESS;
}
@azat
Copy link
Author

azat commented Sep 20, 2020

No sched_setaffinity(), since you can have different number of CPUs, if it will not hit the assert() then change the cpu in taskset (or you have non-debug jemalloc, i.e. w/ NDEBUG)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment