Skip to content

Add support for changing cpu affinity - #988

Closed
koct9i wants to merge 1 commit into
google:masterfrom
koct9i:cpu-affinity
Closed

Add support for changing cpu affinity#988
koct9i wants to merge 1 commit into
google:masterfrom
koct9i:cpu-affinity

Conversation

@koct9i

@koct9i koct9i commented Jun 19, 2020

Copy link
Copy Markdown
Contributor

CPU affinity mask allows to specify set of processors for each thread.
This could be used in seveal ways:

  • limit cpu power to show how benchmark scales with machine size
  • bind threads to processors to eliminate noise from cpu scheduler

Add mehods Processors(), ProcessorRange(), DenseProcessorRange() which
define count of available processors similar to count of threads.

Method BindThreadsToProcessors() pins each thread to exactly one processor.
When count of threads bigger than count of processors then processors are
distributed in round-robin order.

Signed-off-by: Konstantin Khlebnikov khlebnikov@yandex-team.ru

CPU affinity mask allows to specify set of processors for each thread.
This could be used in seveal ways:

* limit cpu power to show how benchmark scales with machine size
* bind threads to processors to eliminate noise from cpu scheduler

Add mehods Processors(), ProcessorRange(), DenseProcessorRange() which
define count of available processors similar to count of threads.

Method BindThreadsToProcessors() pins each thread to exactly one processor.
When count of threads bigger than count of processors then processors are
distributed in round-robin order.

Signed-off-by: Konstantin Khlebnikov <khlebnikov@yandex-team.ru>
Comment thread src/sysinfo.cc

#ifdef BENCHMARK_OS_WINDOWS
DWORD_PTR procmask, sysmask;


Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

These symbols look out of place here and elsewhere

@dmah42

dmah42 commented Jun 19, 2020

Copy link
Copy Markdown
Member

looks like cpu_set_t isn't available on osx. maybe this?

our CI doesn't cover android or ppc or riscv, so we may need some followups to support these platforms. It might be worth being more explicit in your #ifdef blocks about which platforms are expected to be supported and then making it clear to users if they try to call the method on an unsupported platform.

@lkleen

lkleen commented Jul 1, 2020

Copy link
Copy Markdown

looks like cpu_set_t isn't available on osx. maybe this?

yes, this code sets the cpu affinity on mac

// note: this should be called before thread was started!
thread_affinity_policy ap;
ap.affinity_tag = cpu + 1; // tag == 0 -> no affinity
int ret = thread_policy_set (mach_thread_self (), THREAD_AFFINITY_POLICY, (integer_t*) &ap, THREAD_AFFINITY_POLICY_COUNT);

@koct9i

koct9i commented Jul 1, 2020

Copy link
Copy Markdown
Contributor Author

Yes, macos has no API for true cpu-affinity. Only binding each thread to one cpu.
So, fist use case (emulating smaller machine by limiting count of cpus) cannot be implemented here.

Maybe this feature should be dropped in favor of equal functionality for all platforms.
But benchmark should still check cpu-affinity before starting to not disturb externally set limitation.
(i.e. # taskset -c 0-3 ./benchmark)

Or we could keep it for platforms where it works. Macos machines are not so big in comparison to usual linux servers.

@dmah42 dmah42 added the incomplete work needed label May 6, 2021
@dmah42 dmah42 closed this May 30, 2021
@dmah42
dmah42 deleted the branch google:master May 30, 2021 08:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants