Re: [RFC] Adding support for setting the affinity of the recording process

From: James Clark
Date: Mon Jun 12 2023 - 11:27:44 EST




On 12/06/2023 11:26, Yang Jihong wrote:
> Hello everyone,
>
> Currently, perf-record supports profiling an existing process, thread,
> or a specified command.
>
> Sometimes we may need to set CPU affinity of the target process before
> recording:
>
>   # taskset -pc <cpus> <pid>
>   # perf record -p <pid> -- sleep 10
>
> or:
>
>   # perf record -- `taskset -c <cpus> COMMAND`
>
> I'm thinking about getting perf to support setting the affinity of the
> recording process, for example:
>
> 1. set the CPU affinity of the <pid1> process to <cpus1>, <pid2> process
> to <cpus2>,  and record:
>
>   # perf record -p <pid1>/<cpus1>:<pid2>/<cpus2> -- sleep 10
>

I'm not sure if this is necessary. You can already do this with taskset
when you launch the processes or for existing ones.

> and
>
> 2. set CPU affinity of the COMMAND and record:
>
>   # perf record --taskset-command <cpus> COMMAND
>
> In doing so, perf, as an observer, actually changes some of the
> properties of the target process, which may be contrary to the purpose
> of perf tool.
>
>
> Will we consider accepting this approach?
>

For #2 I do this sometimes, but I prefix the perf command with taskset
because otherwise there is a small time between when taskset does its
thing and launching the child process that it runs in the wrong place.

Then one issue with the above method is that perf itself gets pinned to
those CPUs as well. I suppose that could influence your application but
I've never had an issue with it.

If you really can't live with perf also being pinned to those CPUs I
would say it makes sense to add options for #2. Otherwise I would just
run everything under taskset and no changes are needed.

I think you would still need to have perf itself pinned to the CPUs just
before it does the fork and exec, and then after that it can undo its
pinning. Otherwise you'd still get that small time running on the wrong
cores.

James

> Thanks,
> Yang.