perf maps: Add for_each_entry()/_safe() iterators

To reduce boilerplate, provide a more compact form using an idiom
present in other trees of data structures.

Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Link: https://lkml.kernel.org/n/tip-59gmq4kg1r68ou1wknyjl78x@git.kernel.orgSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent 20419d3a
...@@ -29,7 +29,7 @@ int perf_event__synthesize_extra_kmaps(struct perf_tool *tool, ...@@ -29,7 +29,7 @@ int perf_event__synthesize_extra_kmaps(struct perf_tool *tool,
return -1; return -1;
} }
for (pos = maps__first(maps); pos; pos = map__next(pos)) { maps__for_each_entry(maps, pos) {
struct kmap *kmap; struct kmap *kmap;
size_t size; size_t size;
......
...@@ -727,11 +727,9 @@ static struct task *tasks_list(struct task *task, struct machine *machine) ...@@ -727,11 +727,9 @@ static struct task *tasks_list(struct task *task, struct machine *machine)
static size_t maps__fprintf_task(struct maps *maps, int indent, FILE *fp) static size_t maps__fprintf_task(struct maps *maps, int indent, FILE *fp)
{ {
size_t printed = 0; size_t printed = 0;
struct rb_node *nd; struct map *map;
for (nd = rb_first(&maps->entries); nd; nd = rb_next(nd)) {
struct map *map = rb_entry(nd, struct map, rb_node);
maps__for_each_entry(maps, map) {
printed += fprintf(fp, "%*s %" PRIx64 "-%" PRIx64 " %c%c%c%c %08" PRIx64 " %" PRIu64 " %s\n", printed += fprintf(fp, "%*s %" PRIx64 "-%" PRIx64 " %c%c%c%c %08" PRIx64 " %" PRIu64 " %s\n",
indent, "", map->start, map->end, indent, "", map->start, map->end,
map->prot & PROT_READ ? 'r' : '-', map->prot & PROT_READ ? 'r' : '-',
......
...@@ -182,7 +182,7 @@ int test__vmlinux_matches_kallsyms(struct test *test __maybe_unused, int subtest ...@@ -182,7 +182,7 @@ int test__vmlinux_matches_kallsyms(struct test *test __maybe_unused, int subtest
header_printed = false; header_printed = false;
for (map = maps__first(maps); map; map = map__next(map)) { maps__for_each_entry(maps, map) {
struct map * struct map *
/* /*
* If it is the kernel, kallsyms is always "[kernel.kallsyms]", while * If it is the kernel, kallsyms is always "[kernel.kallsyms]", while
...@@ -207,7 +207,7 @@ int test__vmlinux_matches_kallsyms(struct test *test __maybe_unused, int subtest ...@@ -207,7 +207,7 @@ int test__vmlinux_matches_kallsyms(struct test *test __maybe_unused, int subtest
header_printed = false; header_printed = false;
for (map = maps__first(maps); map; map = map__next(map)) { maps__for_each_entry(maps, map) {
struct map *pair; struct map *pair;
mem_start = vmlinux_map->unmap_ip(vmlinux_map, map->start); mem_start = vmlinux_map->unmap_ip(vmlinux_map, map->start);
...@@ -237,7 +237,7 @@ int test__vmlinux_matches_kallsyms(struct test *test __maybe_unused, int subtest ...@@ -237,7 +237,7 @@ int test__vmlinux_matches_kallsyms(struct test *test __maybe_unused, int subtest
maps = machine__kernel_maps(&kallsyms); maps = machine__kernel_maps(&kallsyms);
for (map = maps__first(maps); map; map = map__next(map)) { maps__for_each_entry(maps, map) {
if (!map->priv) { if (!map->priv) {
if (!header_printed) { if (!header_printed) {
pr_info("WARN: Maps only in kallsyms:\n"); pr_info("WARN: Maps only in kallsyms:\n");
......
...@@ -1057,7 +1057,7 @@ int machine__map_x86_64_entry_trampolines(struct machine *machine, ...@@ -1057,7 +1057,7 @@ int machine__map_x86_64_entry_trampolines(struct machine *machine,
* In the vmlinux case, pgoff is a virtual address which must now be * In the vmlinux case, pgoff is a virtual address which must now be
* mapped to a vmlinux offset. * mapped to a vmlinux offset.
*/ */
for (map = maps__first(maps); map; map = map__next(map)) { maps__for_each_entry(maps, map) {
struct kmap *kmap = __map__kmap(map); struct kmap *kmap = __map__kmap(map);
struct map *dest_map; struct map *dest_map;
......
...@@ -594,28 +594,20 @@ void map_groups__insert(struct map_groups *mg, struct map *map) ...@@ -594,28 +594,20 @@ void map_groups__insert(struct map_groups *mg, struct map *map)
static void __maps__purge(struct maps *maps) static void __maps__purge(struct maps *maps)
{ {
struct rb_root *root = &maps->entries; struct map *pos, *next;
struct rb_node *next = rb_first(root);
while (next) { maps__for_each_entry_safe(maps, pos, next) {
struct map *pos = rb_entry(next, struct map, rb_node); rb_erase_init(&pos->rb_node, &maps->entries);
next = rb_next(&pos->rb_node);
rb_erase_init(&pos->rb_node, root);
map__put(pos); map__put(pos);
} }
} }
static void __maps__purge_names(struct maps *maps) static void __maps__purge_names(struct maps *maps)
{ {
struct rb_root *root = &maps->names; struct map *pos, *next;
struct rb_node *next = rb_first(root);
while (next) {
struct map *pos = rb_entry(next, struct map, rb_node_name);
next = rb_next(&pos->rb_node_name); maps__for_each_entry_by_name_safe(maps, pos, next) {
rb_erase_init(&pos->rb_node_name, root); rb_erase_init(&pos->rb_node_name, &maps->names);
map__put(pos); map__put(pos);
} }
} }
...@@ -687,13 +679,11 @@ struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *name, ...@@ -687,13 +679,11 @@ struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *name,
struct map **mapp) struct map **mapp)
{ {
struct symbol *sym; struct symbol *sym;
struct rb_node *nd; struct map *pos;
down_read(&maps->lock); down_read(&maps->lock);
for (nd = rb_first(&maps->entries); nd; nd = rb_next(nd)) { maps__for_each_entry(maps, pos) {
struct map *pos = rb_entry(nd, struct map, rb_node);
sym = map__find_symbol_by_name(pos, name); sym = map__find_symbol_by_name(pos, name);
if (sym == NULL) if (sym == NULL)
...@@ -739,12 +729,11 @@ int map_groups__find_ams(struct addr_map_symbol *ams) ...@@ -739,12 +729,11 @@ int map_groups__find_ams(struct addr_map_symbol *ams)
static size_t maps__fprintf(struct maps *maps, FILE *fp) static size_t maps__fprintf(struct maps *maps, FILE *fp)
{ {
size_t printed = 0; size_t printed = 0;
struct rb_node *nd; struct map *pos;
down_read(&maps->lock); down_read(&maps->lock);
for (nd = rb_first(&maps->entries); nd; nd = rb_next(nd)) { maps__for_each_entry(maps, pos) {
struct map *pos = rb_entry(nd, struct map, rb_node);
printed += fprintf(fp, "Map:"); printed += fprintf(fp, "Map:");
printed += map__fprintf(pos, fp); printed += map__fprintf(pos, fp);
if (verbose > 2) { if (verbose > 2) {
...@@ -889,7 +878,7 @@ int map_groups__clone(struct thread *thread, struct map_groups *parent) ...@@ -889,7 +878,7 @@ int map_groups__clone(struct thread *thread, struct map_groups *parent)
down_read(&maps->lock); down_read(&maps->lock);
for (map = maps__first(maps); map; map = map__next(map)) { maps__for_each_entry(maps, map) {
struct map *new = map__clone(map); struct map *new = map__clone(map);
if (new == NULL) if (new == NULL)
goto out_unlock; goto out_unlock;
...@@ -1021,6 +1010,29 @@ struct map *map__next(struct map *map) ...@@ -1021,6 +1010,29 @@ struct map *map__next(struct map *map)
return map ? __map__next(map) : NULL; return map ? __map__next(map) : NULL;
} }
struct map *maps__first_by_name(struct maps *maps)
{
struct rb_node *first = rb_first(&maps->names);
if (first)
return rb_entry(first, struct map, rb_node_name);
return NULL;
}
static struct map *__map__next_by_name(struct map *map)
{
struct rb_node *next = rb_next(&map->rb_node_name);
if (next)
return rb_entry(next, struct map, rb_node_name);
return NULL;
}
struct map *map__next_by_name(struct map *map)
{
return map ? __map__next_by_name(map) : NULL;
}
struct kmap *__map__kmap(struct map *map) struct kmap *__map__kmap(struct map *map)
{ {
if (!map->dso || !map->dso->kernel) if (!map->dso || !map->dso->kernel)
......
...@@ -25,7 +25,22 @@ void maps__remove(struct maps *maps, struct map *map); ...@@ -25,7 +25,22 @@ void maps__remove(struct maps *maps, struct map *map);
struct map *maps__find(struct maps *maps, u64 addr); struct map *maps__find(struct maps *maps, u64 addr);
struct map *maps__first(struct maps *maps); struct map *maps__first(struct maps *maps);
struct map *map__next(struct map *map); struct map *map__next(struct map *map);
#define maps__for_each_entry(maps, map) \
for (map = maps__first(maps); map; map = map__next(map))
#define maps__for_each_entry_safe(maps, map, next) \
for (map = maps__first(maps), next = map__next(map); map; map = next, next = map__next(map))
struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *name, struct map **mapp); struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *name, struct map **mapp);
struct map *maps__first_by_name(struct maps *maps);
struct map *map__next_by_name(struct map *map);
#define maps__for_each_entry_by_name(maps, map) \
for (map = maps__first_by_name(maps); map; map = map__next_by_name(map))
#define maps__for_each_entry_by_name_safe(maps, map, next) \
for (map = maps__first_by_name(maps), next = map__next_by_name(map); map; map = next, next = map__next_by_name(map))
struct map_groups { struct map_groups {
struct maps maps; struct maps maps;
......
...@@ -153,7 +153,7 @@ static struct map *kernel_get_module_map(const char *module) ...@@ -153,7 +153,7 @@ static struct map *kernel_get_module_map(const char *module)
return map__get(pos); return map__get(pos);
} }
for (pos = maps__first(maps); pos; pos = map__next(pos)) { maps__for_each_entry(maps, pos) {
/* short_name is "[module]" */ /* short_name is "[module]" */
if (strncmp(pos->dso->short_name + 1, module, if (strncmp(pos->dso->short_name + 1, module,
pos->dso->short_name_len - 2) == 0 && pos->dso->short_name_len - 2) == 0 &&
......
...@@ -242,28 +242,24 @@ void symbols__fixup_end(struct rb_root_cached *symbols) ...@@ -242,28 +242,24 @@ void symbols__fixup_end(struct rb_root_cached *symbols)
void map_groups__fixup_end(struct map_groups *mg) void map_groups__fixup_end(struct map_groups *mg)
{ {
struct maps *maps = &mg->maps; struct maps *maps = &mg->maps;
struct map *next, *curr; struct map *prev = NULL, *curr;
down_write(&maps->lock); down_write(&maps->lock);
curr = maps__first(maps); maps__for_each_entry(maps, curr) {
if (curr == NULL) if (prev != NULL && !prev->end)
goto out_unlock; prev->end = curr->start;
for (next = map__next(curr); next; next = map__next(curr)) { prev = curr;
if (!curr->end)
curr->end = next->start;
curr = next;
} }
/* /*
* We still haven't the actual symbols, so guess the * We still haven't the actual symbols, so guess the
* last map final address. * last map final address.
*/ */
if (!curr->end) if (curr && !curr->end)
curr->end = ~0ULL; curr->end = ~0ULL;
out_unlock:
up_write(&maps->lock); up_write(&maps->lock);
} }
......
...@@ -438,7 +438,7 @@ int perf_event__synthesize_modules(struct perf_tool *tool, perf_event__handler_t ...@@ -438,7 +438,7 @@ int perf_event__synthesize_modules(struct perf_tool *tool, perf_event__handler_t
else else
event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL; event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
for (pos = maps__first(maps); pos; pos = map__next(pos)) { maps__for_each_entry(maps, pos) {
size_t size; size_t size;
if (!__map__is_kmodule(pos)) if (!__map__is_kmodule(pos))
......
...@@ -350,7 +350,7 @@ static int __thread__prepare_access(struct thread *thread) ...@@ -350,7 +350,7 @@ static int __thread__prepare_access(struct thread *thread)
down_read(&maps->lock); down_read(&maps->lock);
for (map = maps__first(maps); map; map = map__next(map)) { maps__for_each_entry(maps, map) {
err = unwind__prepare_access(thread->mg, map, &initialized); err = unwind__prepare_access(thread->mg, map, &initialized);
if (err || initialized) if (err || initialized)
break; break;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment