Skip to content

Prefer getaffinity() to detect number of CPUs.#1676

Merged
interwq merged 1 commit intojemalloc:devfrom
interwq:ncpu
Nov 16, 2019
Merged

Prefer getaffinity() to detect number of CPUs.#1676
interwq merged 1 commit intojemalloc:devfrom
interwq:ncpu

Conversation

@interwq
Copy link
Copy Markdown
Contributor

@interwq interwq commented Nov 8, 2019

The getaffinity approach should be preferred than sysconf(_SC_NPROCESSORS_ONLN), because with affinity binding the number of usable cores could be fewer.

Also prefer sched_getaffinity when available.

@interwq interwq merged commit a787d2f into jemalloc:dev Nov 16, 2019
@interwq interwq deleted the ncpu branch November 16, 2019 00:24
azat added a commit to azat/ClickHouse that referenced this pull request Sep 17, 2020
Comment on lines +757 to 767
# if defined(__FreeBSD__)
cpuset_t set;
# else
cpu_set_t set;

# endif
# if defined(JEMALLOC_HAVE_SCHED_SETAFFINITY)
sched_getaffinity(0, sizeof(set), &set);
# else
pthread_getaffinity_np(pthread_self(), sizeof(set), &set);
# endif
result = CPU_COUNT(&set);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But if sched_getaffinity will be preferred over _SC_NPROCESSORS_ONLN then percpu_arena_choose() can return index beyond cached ncpus, seems that all available CPUs should be preferred not only enabled via affinity, since the affinity can be changed on fly and then you are in trouble. Am I missing something?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is an RFC - #1939

azat added a commit to azat-archive/jemalloc that referenced this pull request Sep 20, 2020
Prefer _SC_NPROCESSORS_ONLN over sched_getaffinity() for number of
arenas for percpu_arena enabled.
Since sched_getaffinity() can be changed in runtime, or it can be
allowed to run only on, say, 8 CPU, but in this case:
- ncpus==8
- malloc_getcpu() will return 0/1

Reproducer [1].

  [1]: https://gist.github.com/azat/9770f751c3e7d2da5422cc1ca26339c4

And frankly speaking eligible number of CPUs cannot be used even for
mutex detection, since the affinity mask can be changed later.

Refs: jemalloc#1676
Refs: jemalloc#808
azat added a commit to azat-archive/jemalloc that referenced this pull request Sep 20, 2020
Prefer _SC_NPROCESSORS_ONLN over sched_getaffinity() for number of
arenas for percpu_arena enabled.
Since sched_getaffinity() can be changed in runtime, or it can be
allowed to run only on, say, 8 CPU, but in this case:
- ncpus==8
- malloc_getcpu() will return 0/1

Reproducer [1].

  [1]: https://gist.github.com/azat/9770f751c3e7d2da5422cc1ca26339c4

And frankly speaking eligible number of CPUs cannot be used even for
mutex detection, since the affinity mask can be changed later.

Refs: jemalloc#1676
Refs: jemalloc#808
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants