perf_counter : add new parameter to set cpu affinity
PATCH 1/1 perf_counter : add new parameter to set cpu affinity
new parameter to set cpu affinity
when tracing the cache related performance counters, task migration might cause unpredicted cache events. We can use the cpu affinity to limit which cpus can be used to migrate the current task. I am suggesting -c parameter as in
./perf stat -c 1 -- ls -al
./perf stat -c 1,2 -- ls -al
./perf stat -c 1-3 -- ls -al
tools/perf/builtin-stat.c +2 -0
@@ -37,6 +37,7 @@* Released under the GPL v2. (and only v2, not any later version) */ +#define _GNU_SOURCE#include "perf.h" #include "builtin.h" #include "util/util.h"
@@ -45,6 +46,7 @@#include <sys/prctl.h> #include <math.h> +#include <sched.h> static struct perf_counter_attr default_attrs[] = {
@@ -242,6 +244,10 @@ static int run_perf_stat(int argc __used, const
char **argv)
if (read(go_pipe[0], &buf, 1) == -1)
perror("unable to read pipe");
+ if (enableaffinity)
+ sched_setaffinity(pid, sizeof(affinity_mask),
+ &affinity_mask);
+
execvp(argv[0], (char **)argv);
perror(argv[0]);
@@ -504,6 +510,9 @@ static const struct option options[] = {
"repeat command and print average + stddev (max: 100)"),
OPT_BOOLEAN('n', "null", &null_run,
"null run - dont start any counters"),
+ OPT_CALLBACK('c', "cpus", NULL, "n",
+ "Set cpu affinity i.e: -c 1,3,5-8",
+ set_cpu_affinity),
OPT_END()
};
@@ -511,6 +520,7 @@ int cmd_stat(int argc, const char **argv, const char *prefix __used)
{
int status;
+ enableaffinity = 0;
argc = parse_options(argc, argv, options, stat_usage, 0); if (!argc)
usage_with_options(stat_usage, options);
tools/perf/util/parse-events.c +49 -0
62 lines not on this page · raw message
tools/perf/util/parse-events.h +6 -0
@@ -3,6 +3,12 @@* Parse symbolic events/counts passed in as options: */ +#include <sched.h> + +extern cpu_set_t affinity_mask; + +extern int enableaffinity; +extern int nr_counters;extern struct perf_counter_attr attrs[MAX_COUNTERS ];
@@ -15,3 +21,5 @@ extern int parse_events(const struct option *opt,
const char *str, int unset);
extern void print_events(void);
+extern int set_cpu_affinity(const struct option *opt,
+ const char *str, int unset);
tools/perf/util/util.h +1 -0
@@ -44,6 +44,7 @@#define _XOPEN_SOURCE_EXTENDED 1/* AIX 5.3L needs this */ #endif #define _ALL_SOURCE 1 +#undef _GNU_SOURCE#define _GNU_SOURCE 1#define _BSD_SOURCE 1
Quoted 10 lines · Erdem Aktas
new parameter to set cpu affinity
when tracing the cache related performance counters, task migration might cause unpredicted cache events. We can use the cpu affinity to limit which cpus can be used to migrate the current task. I am suggesting -c parameter as in
./perf stat -c 1 -- ls -al
./perf stat -c 1,2 -- ls -al
./perf stat -c 1-3 -- ls -alSigned-off-by eaktas <[email protected]>
thanks Erdem, this patch looks really useful.
Note, the first patch is line-wrapped: please take a look at Documentation/email-clients.txt to set up your mailer properly for plain-text patches.
The code looks fine, with a few minor things that need to be fixed before i can apply it:
Review int cmd_stat(int argc, const char **argv, const · line 524
Quoted 6 lines
(quoted diff) +1
-0@@ -511,6 +520,7 @@ int cmd_stat(int argc, const char **argv, const char *prefix __used) { int status; + enableaffinity = 0;
please use a tab, not two spaces.
Also, please rename enableaffinity to affinity_enabled - that is more readable.
Note, this initialization is probably not needed at all, as the variable is global and gets initialized to zero already.
tools/perf/util/parse-events.c line 14
Quoted 6 lines
tools/perf/util/parse-events.c +2
-0@@ -9,6 +10,8 @@extern char *strcasestr (const char *haystack,const char *needle);int nr_counters; +cpu_set_t affinity_mask; +int enableaffinity;
Please fix the style here, to match the style of existing variables:
int nr_counters;cpu_set_t affinity_mask;
int affinity_enabled;
Quoted 11 lines
tools/perf/util/parse-events.c +4
-0struct perf_counter_attr attrs[MAX_COUNTERS ];@@ -474,3 +477,49 @@ void print_events(void)exit (129 ); } + +/* +* Set cpu affinity +* Sets which cpus this task can be assigned.
tools/perf/util/parse-events.c void print_events(void) · line 493
Quoted 10 lines
tools/perf/util/parse-events.c +10
-0+* usage can be 1 1,2 1,2,4-8 +*/ +int set_cpu_affinity (const struct option *opt,const char *str,int unset) +{ +char *str_dup, *token, *paramlist[30 ]; +int paramcount =0 ; + +/*I need this dumy if statement, otherwise the compiler gives error */ +if (opt ==NULL && unset ==0 ) { + }
please add __used tags to 'opt' and 'unset' - that is how we annotate unused parameters. (then you can remove this hack)
tools/perf/util/parse-events.c void print_events(void) · line 500
Quoted 7 lines
tools/perf/util/parse-events.c +7
-0+ str_dup =strdup (str); + token =strtok (str_dup,"," ); +CPU_ZERO (&affinity_mask); +while (token) { + paramlist[paramcount++] = token; + token =strtok (NULL ,"," ); + }
this will overflow the paramlist[] array if we have more than 30 elements in the parameter string.
Please add a PARAM_LIMIT define and bail out with an error if that ever gets hit.
tools/perf/util/parse-events.c void print_events(void) · line 509
Quoted 9 lines
tools/perf/util/parse-events.c +9
-0+while (paramcount >0 ) { +int number; + + token =strtok (paramlist[--paramcount],"-" ); + number =-1 ; +while (token !=NULL ) { +if (number !=-1 ) { +int number2; + number2 =atoi (token);
you can initialize number2 in the line of definition.
Also, please put an extra newline after local variable definitions.
Quoted 21 lines
tools/perf/util/parse-events.c +16
-0+while (number < number2) { +CPU_SET (++number, &affinity_mask); + } + }else { + number =atoi (token); +CPU_SET (number, &affinity_mask); + } + token =strtok (NULL ,"-" ); + } + } +free (str_dup); + enableaffinity =1 ; +return 0 ; +} + +tools/perf/util/parse-events.h +0
-0
tools/perf/util/parse-events.h line 12
Quoted 10 lines
tools/perf/util/parse-events.h +6
-0@@ -3,6 +3,12 @@* Parse symbolic events/counts passed in as options: */ +#include <sched.h> + +extern cpu_set_t affinity_mask; + +extern int enableaffinity; +extern int nr_counters;
please align this consistently too.
Quoted 14 lines
tools/perf/util/parse-events.h +2
-0extern struct perf_counter_attr attrs[MAX_COUNTERS ];@@ -15,3 +21,5 @@ extern int parse_events(const struct option *opt, const char *str, int unset);extern void print_events (void ); +extern int set_cpu_affinity (const struct option *opt, +const char *str,int unset);tools/perf/util/util.h +0
-0
tools/perf/util/util.h line 49
Quoted 6 lines
tools/perf/util/util.h +1
-0@@ -44,6 +44,7 @@#define _XOPEN_SOURCE_EXTENDED 1/* AIX 5.3L needs this */ #endif #define _ALL_SOURCE 1 +#undef _GNU_SOURCE#define _GNU_SOURCE 1#define _BSD_SOURCE 1
Why do we undefine _GNU_SOURCE just to define it again in (some) .c files?
Ingo