Prefer getaffinity() to detect number of CPUs.#1676
Merged
interwq merged 1 commit intojemalloc:devfrom Nov 16, 2019
Merged
Conversation
yinan1048576
approved these changes
Nov 16, 2019
azat
reviewed
Sep 19, 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); |
Contributor
There was a problem hiding this comment.
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?
Contributor
There was a problem hiding this comment.
Here is a reproducer - https://gist.github.com/azat/9770f751c3e7d2da5422cc1ca26339c4
Tested on:
- 5.2.1 + this PR
- dev branch (259c5e3)
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
getaffinityapproach should be preferred thansysconf(_SC_NPROCESSORS_ONLN), because with affinity binding the number of usable cores could be fewer.Also prefer
sched_getaffinitywhen available.