Skip to content

Commit 45ac496

Browse files
captain5050namhyung
authored andcommitted
perf machine: Move fprintf to for_each loop and a callback
Avoid exposing the threads data structure by switching to the callback machine__for_each_thread approach. machine__fprintf is only used in tests and verbose >3 output so don't turn to list and sort. Add machine__threads_nr to be refactored later. Note, all existing *_fprintf routines ignore fprintf errors. Signed-off-by: Ian Rogers <[email protected]> Acked-by: Namhyung Kim <[email protected]> Cc: Yang Jihong <[email protected]> Cc: Oliver Upton <[email protected]> Signed-off-by: Namhyung Kim <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent f178ffd commit 45ac496

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

tools/perf/util/machine.c

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,29 +1113,40 @@ size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
11131113
return printed;
11141114
}
11151115

1116-
size_t machine__fprintf(struct machine *machine, FILE *fp)
1116+
struct machine_fprintf_cb_args {
1117+
FILE *fp;
1118+
size_t printed;
1119+
};
1120+
1121+
static int machine_fprintf_cb(struct thread *thread, void *data)
11171122
{
1118-
struct rb_node *nd;
1119-
size_t ret;
1120-
int i;
1123+
struct machine_fprintf_cb_args *args = data;
11211124

1122-
for (i = 0; i < THREADS__TABLE_SIZE; i++) {
1123-
struct threads *threads = &machine->threads[i];
1125+
/* TODO: handle fprintf errors. */
1126+
args->printed += thread__fprintf(thread, args->fp);
1127+
return 0;
1128+
}
11241129

1125-
down_read(&threads->lock);
1130+
static size_t machine__threads_nr(const struct machine *machine)
1131+
{
1132+
size_t nr = 0;
11261133

1127-
ret = fprintf(fp, "Threads: %u\n", threads->nr);
1134+
for (int i = 0; i < THREADS__TABLE_SIZE; i++)
1135+
nr += machine->threads[i].nr;
11281136

1129-
for (nd = rb_first_cached(&threads->entries); nd;
1130-
nd = rb_next(nd)) {
1131-
struct thread *pos = rb_entry(nd, struct thread_rb_node, rb_node)->thread;
1137+
return nr;
1138+
}
11321139

1133-
ret += thread__fprintf(pos, fp);
1134-
}
1140+
size_t machine__fprintf(struct machine *machine, FILE *fp)
1141+
{
1142+
struct machine_fprintf_cb_args args = {
1143+
.fp = fp,
1144+
.printed = 0,
1145+
};
1146+
size_t ret = fprintf(fp, "Threads: %zu\n", machine__threads_nr(machine));
11351147

1136-
up_read(&threads->lock);
1137-
}
1138-
return ret;
1148+
machine__for_each_thread(machine, machine_fprintf_cb, &args);
1149+
return ret + args.printed;
11391150
}
11401151

11411152
static struct dso *machine__get_kernel(struct machine *machine)

0 commit comments

Comments
 (0)