123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625 |
- #include "perf.h"
- #include "util/debug.h"
- #include "util/symbol.h"
- #include "util/sort.h"
- #include "util/evsel.h"
- #include "util/evlist.h"
- #include "util/machine.h"
- #include "util/thread.h"
- #include "util/parse-events.h"
- #include "tests/tests.h"
- #include "tests/hists_common.h"
- struct sample {
- u32 cpu;
- u32 pid;
- u64 ip;
- struct thread *thread;
- struct map *map;
- struct symbol *sym;
- };
- /* For the numbers, see hists_common.c */
- static struct sample fake_samples[] = {
- /* perf [kernel] schedule() */
- { .cpu = 0, .pid = FAKE_PID_PERF1, .ip = FAKE_IP_KERNEL_SCHEDULE, },
- /* perf [perf] main() */
- { .cpu = 1, .pid = FAKE_PID_PERF1, .ip = FAKE_IP_PERF_MAIN, },
- /* perf [perf] cmd_record() */
- { .cpu = 1, .pid = FAKE_PID_PERF1, .ip = FAKE_IP_PERF_CMD_RECORD, },
- /* perf [libc] malloc() */
- { .cpu = 1, .pid = FAKE_PID_PERF1, .ip = FAKE_IP_LIBC_MALLOC, },
- /* perf [libc] free() */
- { .cpu = 2, .pid = FAKE_PID_PERF1, .ip = FAKE_IP_LIBC_FREE, },
- /* perf [perf] main() */
- { .cpu = 2, .pid = FAKE_PID_PERF2, .ip = FAKE_IP_PERF_MAIN, },
- /* perf [kernel] page_fault() */
- { .cpu = 2, .pid = FAKE_PID_PERF2, .ip = FAKE_IP_KERNEL_PAGE_FAULT, },
- /* bash [bash] main() */
- { .cpu = 3, .pid = FAKE_PID_BASH, .ip = FAKE_IP_BASH_MAIN, },
- /* bash [bash] xmalloc() */
- { .cpu = 0, .pid = FAKE_PID_BASH, .ip = FAKE_IP_BASH_XMALLOC, },
- /* bash [kernel] page_fault() */
- { .cpu = 1, .pid = FAKE_PID_BASH, .ip = FAKE_IP_KERNEL_PAGE_FAULT, },
- };
- static int add_hist_entries(struct hists *hists, struct machine *machine)
- {
- struct addr_location al;
- struct perf_evsel *evsel = hists_to_evsel(hists);
- struct perf_sample sample = { .period = 100, };
- size_t i;
- for (i = 0; i < ARRAY_SIZE(fake_samples); i++) {
- const union perf_event event = {
- .header = {
- .misc = PERF_RECORD_MISC_USER,
- },
- };
- struct hist_entry_iter iter = {
- .evsel = evsel,
- .sample = &sample,
- .ops = &hist_iter_normal,
- .hide_unresolved = false,
- };
- sample.cpu = fake_samples[i].cpu;
- sample.pid = fake_samples[i].pid;
- sample.tid = fake_samples[i].pid;
- sample.ip = fake_samples[i].ip;
- if (perf_event__preprocess_sample(&event, machine, &al,
- &sample) < 0)
- goto out;
- if (hist_entry_iter__add(&iter, &al, PERF_MAX_STACK_DEPTH,
- NULL) < 0) {
- addr_location__put(&al);
- goto out;
- }
- fake_samples[i].thread = al.thread;
- fake_samples[i].map = al.map;
- fake_samples[i].sym = al.sym;
- }
- return TEST_OK;
- out:
- pr_debug("Not enough memory for adding a hist entry\n");
- return TEST_FAIL;
- }
- static void del_hist_entries(struct hists *hists)
- {
- struct hist_entry *he;
- struct rb_root *root_in;
- struct rb_root *root_out;
- struct rb_node *node;
- if (sort__need_collapse)
- root_in = &hists->entries_collapsed;
- else
- root_in = hists->entries_in;
- root_out = &hists->entries;
- while (!RB_EMPTY_ROOT(root_out)) {
- node = rb_first(root_out);
- he = rb_entry(node, struct hist_entry, rb_node);
- rb_erase(node, root_out);
- rb_erase(&he->rb_node_in, root_in);
- hist_entry__delete(he);
- }
- }
- typedef int (*test_fn_t)(struct perf_evsel *, struct machine *);
- #define COMM(he) (thread__comm_str(he->thread))
- #define DSO(he) (he->ms.map->dso->short_name)
- #define SYM(he) (he->ms.sym->name)
- #define CPU(he) (he->cpu)
- #define PID(he) (he->thread->tid)
- /* default sort keys (no field) */
- static int test1(struct perf_evsel *evsel, struct machine *machine)
- {
- int err;
- struct hists *hists = evsel__hists(evsel);
- struct hist_entry *he;
- struct rb_root *root;
- struct rb_node *node;
- field_order = NULL;
- sort_order = NULL; /* equivalent to sort_order = "comm,dso,sym" */
- setup_sorting();
- /*
- * expected output:
- *
- * Overhead Command Shared Object Symbol
- * ======== ======= ============= ==============
- * 20.00% perf perf [.] main
- * 10.00% bash [kernel] [k] page_fault
- * 10.00% bash bash [.] main
- * 10.00% bash bash [.] xmalloc
- * 10.00% perf [kernel] [k] page_fault
- * 10.00% perf [kernel] [k] schedule
- * 10.00% perf libc [.] free
- * 10.00% perf libc [.] malloc
- * 10.00% perf perf [.] cmd_record
- */
- err = add_hist_entries(hists, machine);
- if (err < 0)
- goto out;
- hists__collapse_resort(hists, NULL);
- hists__output_resort(hists, NULL);
- if (verbose > 2) {
- pr_info("[fields = %s, sort = %s]\n", field_order, sort_order);
- print_hists_out(hists);
- }
- root = &hists->entries;
- node = rb_first(root);
- he = rb_entry(node, struct hist_entry, rb_node);
- TEST_ASSERT_VAL("Invalid hist entry",
- !strcmp(COMM(he), "perf") && !strcmp(DSO(he), "perf") &&
- !strcmp(SYM(he), "main") && he->stat.period == 200);
- node = rb_next(node);
- he = rb_entry(node, struct hist_entry, rb_node);
- TEST_ASSERT_VAL("Invalid hist entry",
- !strcmp(COMM(he), "bash") && !strcmp(DSO(he), "[kernel]") &&
- !strcmp(SYM(he), "page_fault") && he->stat.period == 100);
- node = rb_next(node);
- he = rb_entry(node, struct hist_entry, rb_node);
- TEST_ASSERT_VAL("Invalid hist entry",
- !strcmp(COMM(he), "bash") && !strcmp(DSO(he), "bash") &&
- !strcmp(SYM(he), "main") && he->stat.period == 100);
- node = rb_next(node);
- he = rb_entry(node, struct hist_entry, rb_node);
- TEST_ASSERT_VAL("Invalid hist entry",
- !strcmp(COMM(he), "bash") && !strcmp(DSO(he), "bash") &&
- !strcmp(SYM(he), "xmalloc") && he->stat.period == 100);
- node = rb_next(node);
- he = rb_entry(node, struct hist_entry, rb_node);
- TEST_ASSERT_VAL("Invalid hist entry",
- !strcmp(COMM(he), "perf") && !strcmp(DSO(he), "[kernel]") &&
- !strcmp(SYM(he), "page_fault") && he->stat.period == 100);
- node = rb_next(node);
- he = rb_entry(node, struct hist_entry, rb_node);
- TEST_ASSERT_VAL("Invalid hist entry",
- !strcmp(COMM(he), "perf") && !strcmp(DSO(he), "[kernel]") &&
- !strcmp(SYM(he), "schedule") && he->stat.period == 100);
- node = rb_next(node);
- he = rb_entry(node, struct hist_entry, rb_node);
- TEST_ASSERT_VAL("Invalid hist entry",
- !strcmp(COMM(he), "perf") && !strcmp(DSO(he), "libc") &&
- !strcmp(SYM(he), "free") && he->stat.period == 100);
- node = rb_next(node);
- he = rb_entry(node, struct hist_entry, rb_node);
- TEST_ASSERT_VAL("Invalid hist entry",
- !strcmp(COMM(he), "perf") && !strcmp(DSO(he), "libc") &&
- !strcmp(SYM(he), "malloc") && he->stat.period == 100);
- node = rb_next(node);
- he = rb_entry(node, struct hist_entry, rb_node);
- TEST_ASSERT_VAL("Invalid hist entry",
- !strcmp(COMM(he), "perf") && !strcmp(DSO(he), "perf") &&
- !strcmp(SYM(he), "cmd_record") && he->stat.period == 100);
- out:
- del_hist_entries(hists);
- reset_output_field();
- return err;
- }
- /* mixed fields and sort keys */
- static int test2(struct perf_evsel *evsel, struct machine *machine)
- {
- int err;
- struct hists *hists = evsel__hists(evsel);
- struct hist_entry *he;
- struct rb_root *root;
- struct rb_node *node;
- field_order = "overhead,cpu";
- sort_order = "pid";
- setup_sorting();
- /*
- * expected output:
- *
- * Overhead CPU Command: Pid
- * ======== === =============
- * 30.00% 1 perf : 100
- * 10.00% 0 perf : 100
- * 10.00% 2 perf : 100
- * 20.00% 2 perf : 200
- * 10.00% 0 bash : 300
- * 10.00% 1 bash : 300
- * 10.00% 3 bash : 300
- */
- err = add_hist_entries(hists, machine);
- if (err < 0)
- goto out;
- hists__collapse_resort(hists, NULL);
- hists__output_resort(hists, NULL);
- if (verbose > 2) {
- pr_info("[fields = %s, sort = %s]\n", field_order, sort_order);
- print_hists_out(hists);
- }
- root = &hists->entries;
- node = rb_first(root);
- he = rb_entry(node, struct hist_entry, rb_node);
- TEST_ASSERT_VAL("Invalid hist entry",
- CPU(he) == 1 && PID(he) == 100 && he->stat.period == 300);
- node = rb_next(node);
- he = rb_entry(node, struct hist_entry, rb_node);
- TEST_ASSERT_VAL("Invalid hist entry",
- CPU(he) == 0 && PID(he) == 100 && he->stat.period == 100);
- out:
- del_hist_entries(hists);
- reset_output_field();
- return err;
- }
- /* fields only (no sort key) */
- static int test3(struct perf_evsel *evsel, struct machine *machine)
- {
- int err;
- struct hists *hists = evsel__hists(evsel);
- struct hist_entry *he;
- struct rb_root *root;
- struct rb_node *node;
- field_order = "comm,overhead,dso";
- sort_order = NULL;
- setup_sorting();
- /*
- * expected output:
- *
- * Command Overhead Shared Object
- * ======= ======== =============
- * bash 20.00% bash
- * bash 10.00% [kernel]
- * perf 30.00% perf
- * perf 20.00% [kernel]
- * perf 20.00% libc
- */
- err = add_hist_entries(hists, machine);
- if (err < 0)
- goto out;
- hists__collapse_resort(hists, NULL);
- hists__output_resort(hists, NULL);
- if (verbose > 2) {
- pr_info("[fields = %s, sort = %s]\n", field_order, sort_order);
- print_hists_out(hists);
- }
- root = &hists->entries;
- node = rb_first(root);
- he = rb_entry(node, struct hist_entry, rb_node);
- TEST_ASSERT_VAL("Invalid hist entry",
- !strcmp(COMM(he), "bash") && !strcmp(DSO(he), "bash") &&
- he->stat.period == 200);
- node = rb_next(node);
- he = rb_entry(node, struct hist_entry, rb_node);
- TEST_ASSERT_VAL("Invalid hist entry",
- !strcmp(COMM(he), "bash") && !strcmp(DSO(he), "[kernel]") &&
- he->stat.period == 100);
- node = rb_next(node);
- he = rb_entry(node, struct hist_entry, rb_node);
- TEST_ASSERT_VAL("Invalid hist entry",
- !strcmp(COMM(he), "perf") && !strcmp(DSO(he), "perf") &&
- he->stat.period == 300);
- node = rb_next(node);
- he = rb_entry(node, struct hist_entry, rb_node);
- TEST_ASSERT_VAL("Invalid hist entry",
- !strcmp(COMM(he), "perf") && !strcmp(DSO(he), "[kernel]") &&
- he->stat.period == 200);
- node = rb_next(node);
- he = rb_entry(node, struct hist_entry, rb_node);
- TEST_ASSERT_VAL("Invalid hist entry",
- !strcmp(COMM(he), "perf") && !strcmp(DSO(he), "libc") &&
- he->stat.period == 200);
- out:
- del_hist_entries(hists);
- reset_output_field();
- return err;
- }
- /* handle duplicate 'dso' field */
- static int test4(struct perf_evsel *evsel, struct machine *machine)
- {
- int err;
- struct hists *hists = evsel__hists(evsel);
- struct hist_entry *he;
- struct rb_root *root;
- struct rb_node *node;
- field_order = "dso,sym,comm,overhead,dso";
- sort_order = "sym";
- setup_sorting();
- /*
- * expected output:
- *
- * Shared Object Symbol Command Overhead
- * ============= ============== ======= ========
- * perf [.] cmd_record perf 10.00%
- * libc [.] free perf 10.00%
- * bash [.] main bash 10.00%
- * perf [.] main perf 20.00%
- * libc [.] malloc perf 10.00%
- * [kernel] [k] page_fault bash 10.00%
- * [kernel] [k] page_fault perf 10.00%
- * [kernel] [k] schedule perf 10.00%
- * bash [.] xmalloc bash 10.00%
- */
- err = add_hist_entries(hists, machine);
- if (err < 0)
- goto out;
- hists__collapse_resort(hists, NULL);
- hists__output_resort(hists, NULL);
- if (verbose > 2) {
- pr_info("[fields = %s, sort = %s]\n", field_order, sort_order);
- print_hists_out(hists);
- }
- root = &hists->entries;
- node = rb_first(root);
- he = rb_entry(node, struct hist_entry, rb_node);
- TEST_ASSERT_VAL("Invalid hist entry",
- !strcmp(DSO(he), "perf") && !strcmp(SYM(he), "cmd_record") &&
- !strcmp(COMM(he), "perf") && he->stat.period == 100);
- node = rb_next(node);
- he = rb_entry(node, struct hist_entry, rb_node);
- TEST_ASSERT_VAL("Invalid hist entry",
- !strcmp(DSO(he), "libc") && !strcmp(SYM(he), "free") &&
- !strcmp(COMM(he), "perf") && he->stat.period == 100);
- node = rb_next(node);
- he = rb_entry(node, struct hist_entry, rb_node);
- TEST_ASSERT_VAL("Invalid hist entry",
- !strcmp(DSO(he), "bash") && !strcmp(SYM(he), "main") &&
- !strcmp(COMM(he), "bash") && he->stat.period == 100);
- node = rb_next(node);
- he = rb_entry(node, struct hist_entry, rb_node);
- TEST_ASSERT_VAL("Invalid hist entry",
- !strcmp(DSO(he), "perf") && !strcmp(SYM(he), "main") &&
- !strcmp(COMM(he), "perf") && he->stat.period == 200);
- node = rb_next(node);
- he = rb_entry(node, struct hist_entry, rb_node);
- TEST_ASSERT_VAL("Invalid hist entry",
- !strcmp(DSO(he), "libc") && !strcmp(SYM(he), "malloc") &&
- !strcmp(COMM(he), "perf") && he->stat.period == 100);
- node = rb_next(node);
- he = rb_entry(node, struct hist_entry, rb_node);
- TEST_ASSERT_VAL("Invalid hist entry",
- !strcmp(DSO(he), "[kernel]") && !strcmp(SYM(he), "page_fault") &&
- !strcmp(COMM(he), "bash") && he->stat.period == 100);
- node = rb_next(node);
- he = rb_entry(node, struct hist_entry, rb_node);
- TEST_ASSERT_VAL("Invalid hist entry",
- !strcmp(DSO(he), "[kernel]") && !strcmp(SYM(he), "page_fault") &&
- !strcmp(COMM(he), "perf") && he->stat.period == 100);
- node = rb_next(node);
- he = rb_entry(node, struct hist_entry, rb_node);
- TEST_ASSERT_VAL("Invalid hist entry",
- !strcmp(DSO(he), "[kernel]") && !strcmp(SYM(he), "schedule") &&
- !strcmp(COMM(he), "perf") && he->stat.period == 100);
- node = rb_next(node);
- he = rb_entry(node, struct hist_entry, rb_node);
- TEST_ASSERT_VAL("Invalid hist entry",
- !strcmp(DSO(he), "bash") && !strcmp(SYM(he), "xmalloc") &&
- !strcmp(COMM(he), "bash") && he->stat.period == 100);
- out:
- del_hist_entries(hists);
- reset_output_field();
- return err;
- }
- /* full sort keys w/o overhead field */
- static int test5(struct perf_evsel *evsel, struct machine *machine)
- {
- int err;
- struct hists *hists = evsel__hists(evsel);
- struct hist_entry *he;
- struct rb_root *root;
- struct rb_node *node;
- field_order = "cpu,pid,comm,dso,sym";
- sort_order = "dso,pid";
- setup_sorting();
- /*
- * expected output:
- *
- * CPU Command: Pid Command Shared Object Symbol
- * === ============= ======= ============= ==============
- * 0 perf: 100 perf [kernel] [k] schedule
- * 2 perf: 200 perf [kernel] [k] page_fault
- * 1 bash: 300 bash [kernel] [k] page_fault
- * 0 bash: 300 bash bash [.] xmalloc
- * 3 bash: 300 bash bash [.] main
- * 1 perf: 100 perf libc [.] malloc
- * 2 perf: 100 perf libc [.] free
- * 1 perf: 100 perf perf [.] cmd_record
- * 1 perf: 100 perf perf [.] main
- * 2 perf: 200 perf perf [.] main
- */
- err = add_hist_entries(hists, machine);
- if (err < 0)
- goto out;
- hists__collapse_resort(hists, NULL);
- hists__output_resort(hists, NULL);
- if (verbose > 2) {
- pr_info("[fields = %s, sort = %s]\n", field_order, sort_order);
- print_hists_out(hists);
- }
- root = &hists->entries;
- node = rb_first(root);
- he = rb_entry(node, struct hist_entry, rb_node);
- TEST_ASSERT_VAL("Invalid hist entry",
- CPU(he) == 0 && PID(he) == 100 &&
- !strcmp(COMM(he), "perf") && !strcmp(DSO(he), "[kernel]") &&
- !strcmp(SYM(he), "schedule") && he->stat.period == 100);
- node = rb_next(node);
- he = rb_entry(node, struct hist_entry, rb_node);
- TEST_ASSERT_VAL("Invalid hist entry",
- CPU(he) == 2 && PID(he) == 200 &&
- !strcmp(COMM(he), "perf") && !strcmp(DSO(he), "[kernel]") &&
- !strcmp(SYM(he), "page_fault") && he->stat.period == 100);
- node = rb_next(node);
- he = rb_entry(node, struct hist_entry, rb_node);
- TEST_ASSERT_VAL("Invalid hist entry",
- CPU(he) == 1 && PID(he) == 300 &&
- !strcmp(COMM(he), "bash") && !strcmp(DSO(he), "[kernel]") &&
- !strcmp(SYM(he), "page_fault") && he->stat.period == 100);
- node = rb_next(node);
- he = rb_entry(node, struct hist_entry, rb_node);
- TEST_ASSERT_VAL("Invalid hist entry",
- CPU(he) == 0 && PID(he) == 300 &&
- !strcmp(COMM(he), "bash") && !strcmp(DSO(he), "bash") &&
- !strcmp(SYM(he), "xmalloc") && he->stat.period == 100);
- node = rb_next(node);
- he = rb_entry(node, struct hist_entry, rb_node);
- TEST_ASSERT_VAL("Invalid hist entry",
- CPU(he) == 3 && PID(he) == 300 &&
- !strcmp(COMM(he), "bash") && !strcmp(DSO(he), "bash") &&
- !strcmp(SYM(he), "main") && he->stat.period == 100);
- node = rb_next(node);
- he = rb_entry(node, struct hist_entry, rb_node);
- TEST_ASSERT_VAL("Invalid hist entry",
- CPU(he) == 1 && PID(he) == 100 &&
- !strcmp(COMM(he), "perf") && !strcmp(DSO(he), "libc") &&
- !strcmp(SYM(he), "malloc") && he->stat.period == 100);
- node = rb_next(node);
- he = rb_entry(node, struct hist_entry, rb_node);
- TEST_ASSERT_VAL("Invalid hist entry",
- CPU(he) == 2 && PID(he) == 100 &&
- !strcmp(COMM(he), "perf") && !strcmp(DSO(he), "libc") &&
- !strcmp(SYM(he), "free") && he->stat.period == 100);
- node = rb_next(node);
- he = rb_entry(node, struct hist_entry, rb_node);
- TEST_ASSERT_VAL("Invalid hist entry",
- CPU(he) == 1 && PID(he) == 100 &&
- !strcmp(COMM(he), "perf") && !strcmp(DSO(he), "perf") &&
- !strcmp(SYM(he), "cmd_record") && he->stat.period == 100);
- node = rb_next(node);
- he = rb_entry(node, struct hist_entry, rb_node);
- TEST_ASSERT_VAL("Invalid hist entry",
- CPU(he) == 1 && PID(he) == 100 &&
- !strcmp(COMM(he), "perf") && !strcmp(DSO(he), "perf") &&
- !strcmp(SYM(he), "main") && he->stat.period == 100);
- node = rb_next(node);
- he = rb_entry(node, struct hist_entry, rb_node);
- TEST_ASSERT_VAL("Invalid hist entry",
- CPU(he) == 2 && PID(he) == 200 &&
- !strcmp(COMM(he), "perf") && !strcmp(DSO(he), "perf") &&
- !strcmp(SYM(he), "main") && he->stat.period == 100);
- out:
- del_hist_entries(hists);
- reset_output_field();
- return err;
- }
- int test__hists_output(void)
- {
- int err = TEST_FAIL;
- struct machines machines;
- struct machine *machine;
- struct perf_evsel *evsel;
- struct perf_evlist *evlist = perf_evlist__new();
- size_t i;
- test_fn_t testcases[] = {
- test1,
- test2,
- test3,
- test4,
- test5,
- };
- TEST_ASSERT_VAL("No memory", evlist);
- err = parse_events(evlist, "cpu-clock", NULL);
- if (err)
- goto out;
- machines__init(&machines);
- /* setup threads/dso/map/symbols also */
- machine = setup_fake_machine(&machines);
- if (!machine)
- goto out;
- if (verbose > 1)
- machine__fprintf(machine, stderr);
- evsel = perf_evlist__first(evlist);
- for (i = 0; i < ARRAY_SIZE(testcases); i++) {
- err = testcases[i](evsel, machine);
- if (err < 0)
- break;
- }
- out:
- /* tear down everything */
- perf_evlist__delete(evlist);
- machines__exit(&machines);
- return err;
- }
|