Skip to content

Commit 7e2ed09

Browse files
committed
perf evlist: Store pointer to the cpu and thread maps
So that we don't have to pass it around to the several methods that needs it, simplifying usage. There is one case where we don't have the thread/cpu map in advance, which is in the parsing routines used by top, stat, record, that we have to wait till all options are parsed to know if a cpu or thread list was passed to then create those maps. For that case consolidate the cpu and thread map creation via perf_evlist__create_maps() out of the code in top and record, while also providing a perf_evlist__set_maps() for cases where multiple evlists share maps or for when maps that represent CPU sockets, for instance, get crafted out of topology information or subsets of threads in a particular application are to be monitored, providing more granularity in specifying which cpus and threads to monitor. Cc: Frederic Weisbecker <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Mike Galbraith <[email protected]> Cc: Paul Mackerras <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Stephane Eranian <[email protected]> Cc: Tom Zanussi <[email protected]> LKML-Reference: <new-submission> Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent f8a9530 commit 7e2ed09

File tree

8 files changed

+148
-119
lines changed

8 files changed

+148
-119
lines changed

tools/perf/builtin-record.c

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ static u64 user_interval = ULLONG_MAX;
4242
static u64 default_interval = 0;
4343
static u64 sample_type;
4444

45-
static struct cpu_map *cpus;
4645
static unsigned int page_size;
4746
static unsigned int mmap_pages = 128;
4847
static unsigned int user_freq = UINT_MAX;
@@ -58,7 +57,6 @@ static bool sample_id_all_avail = true;
5857
static bool system_wide = false;
5958
static pid_t target_pid = -1;
6059
static pid_t target_tid = -1;
61-
static struct thread_map *threads;
6260
static pid_t child_pid = -1;
6361
static bool no_inherit = false;
6462
static enum write_mode_t write_mode = WRITE_FORCE;
@@ -189,7 +187,7 @@ static void create_counter(struct perf_evsel *evsel, int cpu)
189187
int thread_index;
190188
int ret;
191189

192-
for (thread_index = 0; thread_index < threads->nr; thread_index++) {
190+
for (thread_index = 0; thread_index < evsel_list->threads->nr; thread_index++) {
193191
h_attr = get_header_attr(attr, evsel->idx);
194192
if (h_attr == NULL)
195193
die("nomem\n");
@@ -317,7 +315,8 @@ static void open_counters(struct perf_evlist *evlist)
317315
retry_sample_id:
318316
attr->sample_id_all = sample_id_all_avail ? 1 : 0;
319317
try_again:
320-
if (perf_evsel__open(pos, cpus, threads, group, !no_inherit) < 0) {
318+
if (perf_evsel__open(pos, evlist->cpus, evlist->threads, group,
319+
!no_inherit) < 0) {
321320
int err = errno;
322321

323322
if (err == EPERM || err == EACCES)
@@ -368,10 +367,10 @@ static void open_counters(struct perf_evlist *evlist)
368367
}
369368
}
370369

371-
if (perf_evlist__mmap(evlist, cpus, threads, mmap_pages, false) < 0)
370+
if (perf_evlist__mmap(evlist, mmap_pages, false) < 0)
372371
die("failed to mmap with %d (%s)\n", errno, strerror(errno));
373372

374-
for (cpu = 0; cpu < cpus->nr; ++cpu) {
373+
for (cpu = 0; cpu < evsel_list->cpus->nr; ++cpu) {
375374
list_for_each_entry(pos, &evlist->entries, node)
376375
create_counter(pos, cpu);
377376
}
@@ -450,7 +449,7 @@ static void mmap_read_all(void)
450449
{
451450
int i;
452451

453-
for (i = 0; i < cpus->nr; i++) {
452+
for (i = 0; i < evsel_list->cpus->nr; i++) {
454453
if (evsel_list->mmap[i].base)
455454
mmap_read(&evsel_list->mmap[i]);
456455
}
@@ -584,7 +583,7 @@ static int __cmd_record(int argc, const char **argv)
584583
}
585584

586585
if (!system_wide && target_tid == -1 && target_pid == -1)
587-
threads->map[0] = child_pid;
586+
evsel_list->threads->map[0] = child_pid;
588587

589588
close(child_ready_pipe[1]);
590589
close(go_pipe[0]);
@@ -718,12 +717,12 @@ static int __cmd_record(int argc, const char **argv)
718717
}
719718

720719
if (done) {
721-
for (i = 0; i < cpus->nr; i++) {
720+
for (i = 0; i < evsel_list->cpus->nr; i++) {
722721
struct perf_evsel *pos;
723722

724723
list_for_each_entry(pos, &evsel_list->entries, node) {
725724
for (thread = 0;
726-
thread < threads->nr;
725+
thread < evsel_list->threads->nr;
727726
thread++)
728727
ioctl(FD(pos, i, thread),
729728
PERF_EVENT_IOC_DISABLE);
@@ -816,7 +815,7 @@ int cmd_record(int argc, const char **argv, const char *prefix __used)
816815
int err = -ENOMEM;
817816
struct perf_evsel *pos;
818817

819-
evsel_list = perf_evlist__new();
818+
evsel_list = perf_evlist__new(NULL, NULL);
820819
if (evsel_list == NULL)
821820
return -ENOMEM;
822821

@@ -850,28 +849,19 @@ int cmd_record(int argc, const char **argv, const char *prefix __used)
850849
if (target_pid != -1)
851850
target_tid = target_pid;
852851

853-
threads = thread_map__new(target_pid, target_tid);
854-
if (threads == NULL) {
855-
pr_err("Problems finding threads of monitor\n");
856-
usage_with_options(record_usage, record_options);
857-
}
858-
859-
if (target_tid != -1)
860-
cpus = cpu_map__dummy_new();
861-
else
862-
cpus = cpu_map__new(cpu_list);
863-
864-
if (cpus == NULL)
852+
if (perf_evlist__create_maps(evsel_list, target_pid,
853+
target_tid, cpu_list) < 0)
865854
usage_with_options(record_usage, record_options);
866855

867856
list_for_each_entry(pos, &evsel_list->entries, node) {
868-
if (perf_evsel__alloc_fd(pos, cpus->nr, threads->nr) < 0)
857+
if (perf_evsel__alloc_fd(pos, evsel_list->cpus->nr,
858+
evsel_list->threads->nr) < 0)
869859
goto out_free_fd;
870860
if (perf_header__push_event(pos->attr.config, event_name(pos)))
871861
goto out_free_fd;
872862
}
873863

874-
if (perf_evlist__alloc_pollfd(evsel_list, cpus->nr, threads->nr) < 0)
864+
if (perf_evlist__alloc_pollfd(evsel_list) < 0)
875865
goto out_free_fd;
876866

877867
if (user_interval != ULLONG_MAX)
@@ -893,10 +883,8 @@ int cmd_record(int argc, const char **argv, const char *prefix __used)
893883
}
894884

895885
err = __cmd_record(argc, argv);
896-
897886
out_free_fd:
898-
thread_map__delete(threads);
899-
threads = NULL;
887+
perf_evlist__delete_maps(evsel_list);
900888
out_symbol_exit:
901889
symbol__exit();
902890
return err;

tools/perf/builtin-stat.c

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ static struct perf_event_attr default_attrs[] = {
7676
struct perf_evlist *evsel_list;
7777

7878
static bool system_wide = false;
79-
static struct cpu_map *cpus;
8079
static int run_idx = 0;
8180

8281
static int run_count = 1;
@@ -85,7 +84,6 @@ static bool scale = true;
8584
static bool no_aggr = false;
8685
static pid_t target_pid = -1;
8786
static pid_t target_tid = -1;
88-
static struct thread_map *threads;
8987
static pid_t child_pid = -1;
9088
static bool null_run = false;
9189
static bool big_num = true;
@@ -170,15 +168,15 @@ static int create_perf_stat_counter(struct perf_evsel *evsel)
170168
PERF_FORMAT_TOTAL_TIME_RUNNING;
171169

172170
if (system_wide)
173-
return perf_evsel__open_per_cpu(evsel, cpus, false, false);
171+
return perf_evsel__open_per_cpu(evsel, evsel_list->cpus, false, false);
174172

175173
attr->inherit = !no_inherit;
176174
if (target_pid == -1 && target_tid == -1) {
177175
attr->disabled = 1;
178176
attr->enable_on_exec = 1;
179177
}
180178

181-
return perf_evsel__open_per_thread(evsel, threads, false, false);
179+
return perf_evsel__open_per_thread(evsel, evsel_list->threads, false, false);
182180
}
183181

184182
/*
@@ -203,7 +201,8 @@ static int read_counter_aggr(struct perf_evsel *counter)
203201
u64 *count = counter->counts->aggr.values;
204202
int i;
205203

206-
if (__perf_evsel__read(counter, cpus->nr, threads->nr, scale) < 0)
204+
if (__perf_evsel__read(counter, evsel_list->cpus->nr,
205+
evsel_list->threads->nr, scale) < 0)
207206
return -1;
208207

209208
for (i = 0; i < 3; i++)
@@ -236,7 +235,7 @@ static int read_counter(struct perf_evsel *counter)
236235
u64 *count;
237236
int cpu;
238237

239-
for (cpu = 0; cpu < cpus->nr; cpu++) {
238+
for (cpu = 0; cpu < evsel_list->cpus->nr; cpu++) {
240239
if (__perf_evsel__read_on_cpu(counter, cpu, 0, scale) < 0)
241240
return -1;
242241

@@ -301,7 +300,7 @@ static int run_perf_stat(int argc __used, const char **argv)
301300
}
302301

303302
if (target_tid == -1 && target_pid == -1 && !system_wide)
304-
threads->map[0] = child_pid;
303+
evsel_list->threads->map[0] = child_pid;
305304

306305
/*
307306
* Wait for the child to be ready to exec.
@@ -353,12 +352,13 @@ static int run_perf_stat(int argc __used, const char **argv)
353352
if (no_aggr) {
354353
list_for_each_entry(counter, &evsel_list->entries, node) {
355354
read_counter(counter);
356-
perf_evsel__close_fd(counter, cpus->nr, 1);
355+
perf_evsel__close_fd(counter, evsel_list->cpus->nr, 1);
357356
}
358357
} else {
359358
list_for_each_entry(counter, &evsel_list->entries, node) {
360359
read_counter_aggr(counter);
361-
perf_evsel__close_fd(counter, cpus->nr, threads->nr);
360+
perf_evsel__close_fd(counter, evsel_list->cpus->nr,
361+
evsel_list->threads->nr);
362362
}
363363
}
364364

@@ -386,7 +386,7 @@ static void nsec_printout(int cpu, struct perf_evsel *evsel, double avg)
386386
if (no_aggr)
387387
sprintf(cpustr, "CPU%*d%s",
388388
csv_output ? 0 : -4,
389-
cpus->map[cpu], csv_sep);
389+
evsel_list->cpus->map[cpu], csv_sep);
390390

391391
fprintf(stderr, fmt, cpustr, msecs, csv_sep, event_name(evsel));
392392

@@ -414,7 +414,7 @@ static void abs_printout(int cpu, struct perf_evsel *evsel, double avg)
414414
if (no_aggr)
415415
sprintf(cpustr, "CPU%*d%s",
416416
csv_output ? 0 : -4,
417-
cpus->map[cpu], csv_sep);
417+
evsel_list->cpus->map[cpu], csv_sep);
418418
else
419419
cpu = 0;
420420

@@ -500,14 +500,14 @@ static void print_counter(struct perf_evsel *counter)
500500
u64 ena, run, val;
501501
int cpu;
502502

503-
for (cpu = 0; cpu < cpus->nr; cpu++) {
503+
for (cpu = 0; cpu < evsel_list->cpus->nr; cpu++) {
504504
val = counter->counts->cpu[cpu].val;
505505
ena = counter->counts->cpu[cpu].ena;
506506
run = counter->counts->cpu[cpu].run;
507507
if (run == 0 || ena == 0) {
508508
fprintf(stderr, "CPU%*d%s%*s%s%-24s",
509509
csv_output ? 0 : -4,
510-
cpus->map[cpu], csv_sep,
510+
evsel_list->cpus->map[cpu], csv_sep,
511511
csv_output ? 0 : 18,
512512
"<not counted>", csv_sep,
513513
event_name(counter));
@@ -652,7 +652,7 @@ int cmd_stat(int argc, const char **argv, const char *prefix __used)
652652

653653
setlocale(LC_ALL, "");
654654

655-
evsel_list = perf_evlist__new();
655+
evsel_list = perf_evlist__new(NULL, NULL);
656656
if (evsel_list == NULL)
657657
return -ENOMEM;
658658

@@ -701,27 +701,27 @@ int cmd_stat(int argc, const char **argv, const char *prefix __used)
701701
if (target_pid != -1)
702702
target_tid = target_pid;
703703

704-
threads = thread_map__new(target_pid, target_tid);
705-
if (threads == NULL) {
704+
evsel_list->threads = thread_map__new(target_pid, target_tid);
705+
if (evsel_list->threads == NULL) {
706706
pr_err("Problems finding threads of monitor\n");
707707
usage_with_options(stat_usage, options);
708708
}
709709

710710
if (system_wide)
711-
cpus = cpu_map__new(cpu_list);
711+
evsel_list->cpus = cpu_map__new(cpu_list);
712712
else
713-
cpus = cpu_map__dummy_new();
713+
evsel_list->cpus = cpu_map__dummy_new();
714714

715-
if (cpus == NULL) {
715+
if (evsel_list->cpus == NULL) {
716716
perror("failed to parse CPUs map");
717717
usage_with_options(stat_usage, options);
718718
return -1;
719719
}
720720

721721
list_for_each_entry(pos, &evsel_list->entries, node) {
722722
if (perf_evsel__alloc_stat_priv(pos) < 0 ||
723-
perf_evsel__alloc_counts(pos, cpus->nr) < 0 ||
724-
perf_evsel__alloc_fd(pos, cpus->nr, threads->nr) < 0)
723+
perf_evsel__alloc_counts(pos, evsel_list->cpus->nr) < 0 ||
724+
perf_evsel__alloc_fd(pos, evsel_list->cpus->nr, evsel_list->threads->nr) < 0)
725725
goto out_free_fd;
726726
}
727727

@@ -750,7 +750,6 @@ int cmd_stat(int argc, const char **argv, const char *prefix __used)
750750
perf_evsel__free_stat_priv(pos);
751751
perf_evlist__delete(evsel_list);
752752
out:
753-
thread_map__delete(threads);
754-
threads = NULL;
753+
perf_evlist__delete_maps(evsel_list);
755754
return status;
756755
}

tools/perf/builtin-test.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ static int test__basic_mmap(void)
509509
goto out_free_cpus;
510510
}
511511

512-
evlist = perf_evlist__new();
512+
evlist = perf_evlist__new(cpus, threads);
513513
if (evlist == NULL) {
514514
pr_debug("perf_evlist__new\n");
515515
goto out_free_cpus;
@@ -537,7 +537,7 @@ static int test__basic_mmap(void)
537537
}
538538
}
539539

540-
if (perf_evlist__mmap(evlist, cpus, threads, 128, true) < 0) {
540+
if (perf_evlist__mmap(evlist, 128, true) < 0) {
541541
pr_debug("failed to mmap events: %d (%s)\n", errno,
542542
strerror(errno));
543543
goto out_close_fd;
@@ -579,7 +579,7 @@ static int test__basic_mmap(void)
579579

580580
err = 0;
581581
out_munmap:
582-
perf_evlist__munmap(evlist, 1);
582+
perf_evlist__munmap(evlist);
583583
out_close_fd:
584584
for (i = 0; i < nsyscalls; ++i)
585585
perf_evsel__close_fd(evsels[i], 1, threads->nr);

0 commit comments

Comments
 (0)