0001
0002 #include "tests.h"
0003 #include "debug.h"
0004 #include "symbol.h"
0005 #include "sort.h"
0006 #include "evsel.h"
0007 #include "evlist.h"
0008 #include "machine.h"
0009 #include "parse-events.h"
0010 #include "hists_common.h"
0011 #include "util/mmap.h"
0012 #include <errno.h>
0013 #include <linux/kernel.h>
0014
0015 struct sample {
0016 u32 pid;
0017 u64 ip;
0018 struct thread *thread;
0019 struct map *map;
0020 struct symbol *sym;
0021 };
0022
0023
0024 static struct sample fake_common_samples[] = {
0025
0026 { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_KERNEL_SCHEDULE, },
0027
0028 { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_PERF_MAIN, },
0029
0030 { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_PERF_CMD_RECORD, },
0031
0032 { .pid = FAKE_PID_BASH, .ip = FAKE_IP_BASH_XMALLOC, },
0033
0034 { .pid = FAKE_PID_BASH, .ip = FAKE_IP_LIBC_MALLOC, },
0035 };
0036
0037 static struct sample fake_samples[][5] = {
0038 {
0039
0040 { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_PERF_RUN_COMMAND, },
0041
0042 { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_LIBC_MALLOC, },
0043
0044 { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_KERNEL_PAGE_FAULT, },
0045
0046 { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_KERNEL_SYS_PERF_EVENT_OPEN, },
0047
0048 { .pid = FAKE_PID_BASH, .ip = FAKE_IP_LIBC_FREE, },
0049 },
0050 {
0051
0052 { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_LIBC_FREE, },
0053
0054 { .pid = FAKE_PID_BASH, .ip = FAKE_IP_LIBC_MALLOC, },
0055
0056 { .pid = FAKE_PID_BASH, .ip = FAKE_IP_BASH_XFREE, },
0057
0058 { .pid = FAKE_PID_BASH, .ip = FAKE_IP_LIBC_REALLOC, },
0059
0060 { .pid = FAKE_PID_BASH, .ip = FAKE_IP_KERNEL_PAGE_FAULT, },
0061 },
0062 };
0063
0064 static int add_hist_entries(struct evlist *evlist, struct machine *machine)
0065 {
0066 struct evsel *evsel;
0067 struct addr_location al;
0068 struct hist_entry *he;
0069 struct perf_sample sample = { .period = 1, .weight = 1, };
0070 size_t i = 0, k;
0071
0072
0073
0074
0075
0076
0077 evlist__for_each_entry(evlist, evsel) {
0078 struct hists *hists = evsel__hists(evsel);
0079
0080 for (k = 0; k < ARRAY_SIZE(fake_common_samples); k++) {
0081 sample.cpumode = PERF_RECORD_MISC_USER;
0082 sample.pid = fake_common_samples[k].pid;
0083 sample.tid = fake_common_samples[k].pid;
0084 sample.ip = fake_common_samples[k].ip;
0085
0086 if (machine__resolve(machine, &al, &sample) < 0)
0087 goto out;
0088
0089 he = hists__add_entry(hists, &al, NULL,
0090 NULL, NULL, &sample, true);
0091 if (he == NULL) {
0092 addr_location__put(&al);
0093 goto out;
0094 }
0095
0096 fake_common_samples[k].thread = al.thread;
0097 fake_common_samples[k].map = al.map;
0098 fake_common_samples[k].sym = al.sym;
0099 }
0100
0101 for (k = 0; k < ARRAY_SIZE(fake_samples[i]); k++) {
0102 sample.pid = fake_samples[i][k].pid;
0103 sample.tid = fake_samples[i][k].pid;
0104 sample.ip = fake_samples[i][k].ip;
0105 if (machine__resolve(machine, &al, &sample) < 0)
0106 goto out;
0107
0108 he = hists__add_entry(hists, &al, NULL,
0109 NULL, NULL, &sample, true);
0110 if (he == NULL) {
0111 addr_location__put(&al);
0112 goto out;
0113 }
0114
0115 fake_samples[i][k].thread = al.thread;
0116 fake_samples[i][k].map = al.map;
0117 fake_samples[i][k].sym = al.sym;
0118 }
0119 i++;
0120 }
0121
0122 return 0;
0123
0124 out:
0125 pr_debug("Not enough memory for adding a hist entry\n");
0126 return -1;
0127 }
0128
0129 static int find_sample(struct sample *samples, size_t nr_samples,
0130 struct thread *t, struct map *m, struct symbol *s)
0131 {
0132 while (nr_samples--) {
0133 if (samples->thread == t && samples->map == m &&
0134 samples->sym == s)
0135 return 1;
0136 samples++;
0137 }
0138 return 0;
0139 }
0140
0141 static int __validate_match(struct hists *hists)
0142 {
0143 size_t count = 0;
0144 struct rb_root_cached *root;
0145 struct rb_node *node;
0146
0147
0148
0149
0150 if (hists__has(hists, need_collapse))
0151 root = &hists->entries_collapsed;
0152 else
0153 root = hists->entries_in;
0154
0155 node = rb_first_cached(root);
0156 while (node) {
0157 struct hist_entry *he;
0158
0159 he = rb_entry(node, struct hist_entry, rb_node_in);
0160
0161 if (hist_entry__has_pairs(he)) {
0162 if (find_sample(fake_common_samples,
0163 ARRAY_SIZE(fake_common_samples),
0164 he->thread, he->ms.map, he->ms.sym)) {
0165 count++;
0166 } else {
0167 pr_debug("Can't find the matched entry\n");
0168 return -1;
0169 }
0170 }
0171
0172 node = rb_next(node);
0173 }
0174
0175 if (count != ARRAY_SIZE(fake_common_samples)) {
0176 pr_debug("Invalid count for matched entries: %zd of %zd\n",
0177 count, ARRAY_SIZE(fake_common_samples));
0178 return -1;
0179 }
0180
0181 return 0;
0182 }
0183
0184 static int validate_match(struct hists *leader, struct hists *other)
0185 {
0186 return __validate_match(leader) || __validate_match(other);
0187 }
0188
0189 static int __validate_link(struct hists *hists, int idx)
0190 {
0191 size_t count = 0;
0192 size_t count_pair = 0;
0193 size_t count_dummy = 0;
0194 struct rb_root_cached *root;
0195 struct rb_node *node;
0196
0197
0198
0199
0200
0201
0202 if (hists__has(hists, need_collapse))
0203 root = &hists->entries_collapsed;
0204 else
0205 root = hists->entries_in;
0206
0207 node = rb_first_cached(root);
0208 while (node) {
0209 struct hist_entry *he;
0210
0211 he = rb_entry(node, struct hist_entry, rb_node_in);
0212
0213 if (hist_entry__has_pairs(he)) {
0214 if (!find_sample(fake_common_samples,
0215 ARRAY_SIZE(fake_common_samples),
0216 he->thread, he->ms.map, he->ms.sym) &&
0217 !find_sample(fake_samples[idx],
0218 ARRAY_SIZE(fake_samples[idx]),
0219 he->thread, he->ms.map, he->ms.sym)) {
0220 count_dummy++;
0221 }
0222 count_pair++;
0223 } else if (idx) {
0224 pr_debug("A entry from the other hists should have pair\n");
0225 return -1;
0226 }
0227
0228 count++;
0229 node = rb_next(node);
0230 }
0231
0232
0233
0234
0235 if (idx == 0) {
0236 if (count_dummy != ARRAY_SIZE(fake_samples[1]) - 1) {
0237 pr_debug("Invalid count of dummy entries: %zd of %zd\n",
0238 count_dummy, ARRAY_SIZE(fake_samples[1]) - 1);
0239 return -1;
0240 }
0241 if (count != count_pair + ARRAY_SIZE(fake_samples[0])) {
0242 pr_debug("Invalid count of total leader entries: %zd of %zd\n",
0243 count, count_pair + ARRAY_SIZE(fake_samples[0]));
0244 return -1;
0245 }
0246 } else {
0247 if (count != count_pair) {
0248 pr_debug("Invalid count of total other entries: %zd of %zd\n",
0249 count, count_pair);
0250 return -1;
0251 }
0252 if (count_dummy > 0) {
0253 pr_debug("Other hists should not have dummy entries: %zd\n",
0254 count_dummy);
0255 return -1;
0256 }
0257 }
0258
0259 return 0;
0260 }
0261
0262 static int validate_link(struct hists *leader, struct hists *other)
0263 {
0264 return __validate_link(leader, 0) || __validate_link(other, 1);
0265 }
0266
0267 static int test__hists_link(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
0268 {
0269 int err = -1;
0270 struct hists *hists, *first_hists;
0271 struct machines machines;
0272 struct machine *machine = NULL;
0273 struct evsel *evsel, *first;
0274 struct evlist *evlist = evlist__new();
0275
0276 if (evlist == NULL)
0277 return -ENOMEM;
0278
0279 err = parse_event(evlist, "cpu-clock");
0280 if (err)
0281 goto out;
0282 err = parse_event(evlist, "task-clock");
0283 if (err)
0284 goto out;
0285
0286 err = TEST_FAIL;
0287
0288 if (setup_sorting(NULL) < 0)
0289 goto out;
0290
0291 machines__init(&machines);
0292
0293
0294 machine = setup_fake_machine(&machines);
0295 if (!machine)
0296 goto out;
0297
0298 if (verbose > 1)
0299 machine__fprintf(machine, stderr);
0300
0301
0302 err = add_hist_entries(evlist, machine);
0303 if (err < 0)
0304 goto out;
0305
0306 evlist__for_each_entry(evlist, evsel) {
0307 hists = evsel__hists(evsel);
0308 hists__collapse_resort(hists, NULL);
0309
0310 if (verbose > 2)
0311 print_hists_in(hists);
0312 }
0313
0314 first = evlist__first(evlist);
0315 evsel = evlist__last(evlist);
0316
0317 first_hists = evsel__hists(first);
0318 hists = evsel__hists(evsel);
0319
0320
0321 hists__match(first_hists, hists);
0322 err = validate_match(first_hists, hists);
0323 if (err)
0324 goto out;
0325
0326
0327 hists__link(first_hists, hists);
0328 err = validate_link(first_hists, hists);
0329 if (err)
0330 goto out;
0331
0332 err = 0;
0333
0334 out:
0335
0336 evlist__delete(evlist);
0337 reset_output_field();
0338 machines__exit(&machines);
0339
0340 return err;
0341 }
0342
0343 DEFINE_SUITE("Match and link multiple hists", hists_link);