parse-events-test.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. #include "parse-events.h"
  2. #include "evsel.h"
  3. #include "evlist.h"
  4. #include "sysfs.h"
  5. #include "../../../include/linux/hw_breakpoint.h"
  6. #define TEST_ASSERT_VAL(text, cond) \
  7. do { \
  8. if (!(cond)) { \
  9. pr_debug("FAILED %s:%d %s\n", __FILE__, __LINE__, text); \
  10. return -1; \
  11. } \
  12. } while (0)
  13. static int test__checkevent_tracepoint(struct perf_evlist *evlist)
  14. {
  15. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  16. struct perf_evsel, node);
  17. TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
  18. TEST_ASSERT_VAL("wrong type", PERF_TYPE_TRACEPOINT == evsel->attr.type);
  19. TEST_ASSERT_VAL("wrong sample_type",
  20. (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME | PERF_SAMPLE_CPU) ==
  21. evsel->attr.sample_type);
  22. TEST_ASSERT_VAL("wrong sample_period", 1 == evsel->attr.sample_period);
  23. return 0;
  24. }
  25. static int test__checkevent_tracepoint_multi(struct perf_evlist *evlist)
  26. {
  27. struct perf_evsel *evsel;
  28. TEST_ASSERT_VAL("wrong number of entries", evlist->nr_entries > 1);
  29. list_for_each_entry(evsel, &evlist->entries, node) {
  30. TEST_ASSERT_VAL("wrong type",
  31. PERF_TYPE_TRACEPOINT == evsel->attr.type);
  32. TEST_ASSERT_VAL("wrong sample_type",
  33. (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME | PERF_SAMPLE_CPU)
  34. == evsel->attr.sample_type);
  35. TEST_ASSERT_VAL("wrong sample_period",
  36. 1 == evsel->attr.sample_period);
  37. }
  38. return 0;
  39. }
  40. static int test__checkevent_raw(struct perf_evlist *evlist)
  41. {
  42. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  43. struct perf_evsel, node);
  44. TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
  45. TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->attr.type);
  46. TEST_ASSERT_VAL("wrong config", 0x1a == evsel->attr.config);
  47. return 0;
  48. }
  49. static int test__checkevent_numeric(struct perf_evlist *evlist)
  50. {
  51. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  52. struct perf_evsel, node);
  53. TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
  54. TEST_ASSERT_VAL("wrong type", 1 == evsel->attr.type);
  55. TEST_ASSERT_VAL("wrong config", 1 == evsel->attr.config);
  56. return 0;
  57. }
  58. static int test__checkevent_symbolic_name(struct perf_evlist *evlist)
  59. {
  60. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  61. struct perf_evsel, node);
  62. TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
  63. TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->attr.type);
  64. TEST_ASSERT_VAL("wrong config",
  65. PERF_COUNT_HW_INSTRUCTIONS == evsel->attr.config);
  66. return 0;
  67. }
  68. static int test__checkevent_symbolic_name_config(struct perf_evlist *evlist)
  69. {
  70. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  71. struct perf_evsel, node);
  72. TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
  73. TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->attr.type);
  74. TEST_ASSERT_VAL("wrong config",
  75. PERF_COUNT_HW_CPU_CYCLES == evsel->attr.config);
  76. TEST_ASSERT_VAL("wrong period",
  77. 100000 == evsel->attr.sample_period);
  78. TEST_ASSERT_VAL("wrong config1",
  79. 0 == evsel->attr.config1);
  80. TEST_ASSERT_VAL("wrong config2",
  81. 1 == evsel->attr.config2);
  82. return 0;
  83. }
  84. static int test__checkevent_symbolic_alias(struct perf_evlist *evlist)
  85. {
  86. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  87. struct perf_evsel, node);
  88. TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
  89. TEST_ASSERT_VAL("wrong type", PERF_TYPE_SOFTWARE == evsel->attr.type);
  90. TEST_ASSERT_VAL("wrong config",
  91. PERF_COUNT_SW_PAGE_FAULTS == evsel->attr.config);
  92. return 0;
  93. }
  94. static int test__checkevent_genhw(struct perf_evlist *evlist)
  95. {
  96. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  97. struct perf_evsel, node);
  98. TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
  99. TEST_ASSERT_VAL("wrong type", PERF_TYPE_HW_CACHE == evsel->attr.type);
  100. TEST_ASSERT_VAL("wrong config", (1 << 16) == evsel->attr.config);
  101. return 0;
  102. }
  103. static int test__checkevent_breakpoint(struct perf_evlist *evlist)
  104. {
  105. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  106. struct perf_evsel, node);
  107. TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
  108. TEST_ASSERT_VAL("wrong type", PERF_TYPE_BREAKPOINT == evsel->attr.type);
  109. TEST_ASSERT_VAL("wrong config", 0 == evsel->attr.config);
  110. TEST_ASSERT_VAL("wrong bp_type", (HW_BREAKPOINT_R | HW_BREAKPOINT_W) ==
  111. evsel->attr.bp_type);
  112. TEST_ASSERT_VAL("wrong bp_len", HW_BREAKPOINT_LEN_4 ==
  113. evsel->attr.bp_len);
  114. return 0;
  115. }
  116. static int test__checkevent_breakpoint_x(struct perf_evlist *evlist)
  117. {
  118. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  119. struct perf_evsel, node);
  120. TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
  121. TEST_ASSERT_VAL("wrong type", PERF_TYPE_BREAKPOINT == evsel->attr.type);
  122. TEST_ASSERT_VAL("wrong config", 0 == evsel->attr.config);
  123. TEST_ASSERT_VAL("wrong bp_type",
  124. HW_BREAKPOINT_X == evsel->attr.bp_type);
  125. TEST_ASSERT_VAL("wrong bp_len", sizeof(long) == evsel->attr.bp_len);
  126. return 0;
  127. }
  128. static int test__checkevent_breakpoint_r(struct perf_evlist *evlist)
  129. {
  130. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  131. struct perf_evsel, node);
  132. TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
  133. TEST_ASSERT_VAL("wrong type",
  134. PERF_TYPE_BREAKPOINT == evsel->attr.type);
  135. TEST_ASSERT_VAL("wrong config", 0 == evsel->attr.config);
  136. TEST_ASSERT_VAL("wrong bp_type",
  137. HW_BREAKPOINT_R == evsel->attr.bp_type);
  138. TEST_ASSERT_VAL("wrong bp_len",
  139. HW_BREAKPOINT_LEN_4 == evsel->attr.bp_len);
  140. return 0;
  141. }
  142. static int test__checkevent_breakpoint_w(struct perf_evlist *evlist)
  143. {
  144. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  145. struct perf_evsel, node);
  146. TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
  147. TEST_ASSERT_VAL("wrong type",
  148. PERF_TYPE_BREAKPOINT == evsel->attr.type);
  149. TEST_ASSERT_VAL("wrong config", 0 == evsel->attr.config);
  150. TEST_ASSERT_VAL("wrong bp_type",
  151. HW_BREAKPOINT_W == evsel->attr.bp_type);
  152. TEST_ASSERT_VAL("wrong bp_len",
  153. HW_BREAKPOINT_LEN_4 == evsel->attr.bp_len);
  154. return 0;
  155. }
  156. static int test__checkevent_breakpoint_rw(struct perf_evlist *evlist)
  157. {
  158. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  159. struct perf_evsel, node);
  160. TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
  161. TEST_ASSERT_VAL("wrong type",
  162. PERF_TYPE_BREAKPOINT == evsel->attr.type);
  163. TEST_ASSERT_VAL("wrong config", 0 == evsel->attr.config);
  164. TEST_ASSERT_VAL("wrong bp_type",
  165. (HW_BREAKPOINT_R|HW_BREAKPOINT_W) == evsel->attr.bp_type);
  166. TEST_ASSERT_VAL("wrong bp_len",
  167. HW_BREAKPOINT_LEN_4 == evsel->attr.bp_len);
  168. return 0;
  169. }
  170. static int test__checkevent_tracepoint_modifier(struct perf_evlist *evlist)
  171. {
  172. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  173. struct perf_evsel, node);
  174. TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
  175. TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
  176. TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
  177. TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
  178. return test__checkevent_tracepoint(evlist);
  179. }
  180. static int
  181. test__checkevent_tracepoint_multi_modifier(struct perf_evlist *evlist)
  182. {
  183. struct perf_evsel *evsel;
  184. TEST_ASSERT_VAL("wrong number of entries", evlist->nr_entries > 1);
  185. list_for_each_entry(evsel, &evlist->entries, node) {
  186. TEST_ASSERT_VAL("wrong exclude_user",
  187. !evsel->attr.exclude_user);
  188. TEST_ASSERT_VAL("wrong exclude_kernel",
  189. evsel->attr.exclude_kernel);
  190. TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
  191. TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
  192. }
  193. return test__checkevent_tracepoint_multi(evlist);
  194. }
  195. static int test__checkevent_raw_modifier(struct perf_evlist *evlist)
  196. {
  197. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  198. struct perf_evsel, node);
  199. TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
  200. TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
  201. TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
  202. TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
  203. return test__checkevent_raw(evlist);
  204. }
  205. static int test__checkevent_numeric_modifier(struct perf_evlist *evlist)
  206. {
  207. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  208. struct perf_evsel, node);
  209. TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
  210. TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
  211. TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
  212. TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
  213. return test__checkevent_numeric(evlist);
  214. }
  215. static int test__checkevent_symbolic_name_modifier(struct perf_evlist *evlist)
  216. {
  217. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  218. struct perf_evsel, node);
  219. TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
  220. TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
  221. TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
  222. TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
  223. return test__checkevent_symbolic_name(evlist);
  224. }
  225. static int test__checkevent_exclude_host_modifier(struct perf_evlist *evlist)
  226. {
  227. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  228. struct perf_evsel, node);
  229. TEST_ASSERT_VAL("wrong exclude guest", !evsel->attr.exclude_guest);
  230. TEST_ASSERT_VAL("wrong exclude host", evsel->attr.exclude_host);
  231. return test__checkevent_symbolic_name(evlist);
  232. }
  233. static int test__checkevent_exclude_guest_modifier(struct perf_evlist *evlist)
  234. {
  235. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  236. struct perf_evsel, node);
  237. TEST_ASSERT_VAL("wrong exclude guest", evsel->attr.exclude_guest);
  238. TEST_ASSERT_VAL("wrong exclude host", !evsel->attr.exclude_host);
  239. return test__checkevent_symbolic_name(evlist);
  240. }
  241. static int test__checkevent_symbolic_alias_modifier(struct perf_evlist *evlist)
  242. {
  243. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  244. struct perf_evsel, node);
  245. TEST_ASSERT_VAL("wrong exclude_user", !evsel->attr.exclude_user);
  246. TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
  247. TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
  248. TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
  249. return test__checkevent_symbolic_alias(evlist);
  250. }
  251. static int test__checkevent_genhw_modifier(struct perf_evlist *evlist)
  252. {
  253. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  254. struct perf_evsel, node);
  255. TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
  256. TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
  257. TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
  258. TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
  259. return test__checkevent_genhw(evlist);
  260. }
  261. static int test__checkevent_breakpoint_modifier(struct perf_evlist *evlist)
  262. {
  263. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  264. struct perf_evsel, node);
  265. TEST_ASSERT_VAL("wrong exclude_user", !evsel->attr.exclude_user);
  266. TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
  267. TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
  268. TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
  269. TEST_ASSERT_VAL("wrong name",
  270. !strcmp(perf_evsel__name(evsel), "mem:0x0:rw:u"));
  271. return test__checkevent_breakpoint(evlist);
  272. }
  273. static int test__checkevent_breakpoint_x_modifier(struct perf_evlist *evlist)
  274. {
  275. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  276. struct perf_evsel, node);
  277. TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
  278. TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
  279. TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
  280. TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
  281. TEST_ASSERT_VAL("wrong name",
  282. !strcmp(perf_evsel__name(evsel), "mem:0x0:x:k"));
  283. return test__checkevent_breakpoint_x(evlist);
  284. }
  285. static int test__checkevent_breakpoint_r_modifier(struct perf_evlist *evlist)
  286. {
  287. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  288. struct perf_evsel, node);
  289. TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
  290. TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
  291. TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
  292. TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
  293. TEST_ASSERT_VAL("wrong name",
  294. !strcmp(perf_evsel__name(evsel), "mem:0x0:r:hp"));
  295. return test__checkevent_breakpoint_r(evlist);
  296. }
  297. static int test__checkevent_breakpoint_w_modifier(struct perf_evlist *evlist)
  298. {
  299. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  300. struct perf_evsel, node);
  301. TEST_ASSERT_VAL("wrong exclude_user", !evsel->attr.exclude_user);
  302. TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
  303. TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
  304. TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
  305. TEST_ASSERT_VAL("wrong name",
  306. !strcmp(perf_evsel__name(evsel), "mem:0x0:w:up"));
  307. return test__checkevent_breakpoint_w(evlist);
  308. }
  309. static int test__checkevent_breakpoint_rw_modifier(struct perf_evlist *evlist)
  310. {
  311. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  312. struct perf_evsel, node);
  313. TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
  314. TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
  315. TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
  316. TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
  317. TEST_ASSERT_VAL("wrong name",
  318. !strcmp(perf_evsel__name(evsel), "mem:0x0:rw:kp"));
  319. return test__checkevent_breakpoint_rw(evlist);
  320. }
  321. static int test__checkevent_pmu(struct perf_evlist *evlist)
  322. {
  323. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  324. struct perf_evsel, node);
  325. TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
  326. TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->attr.type);
  327. TEST_ASSERT_VAL("wrong config", 10 == evsel->attr.config);
  328. TEST_ASSERT_VAL("wrong config1", 1 == evsel->attr.config1);
  329. TEST_ASSERT_VAL("wrong config2", 3 == evsel->attr.config2);
  330. TEST_ASSERT_VAL("wrong period", 1000 == evsel->attr.sample_period);
  331. return 0;
  332. }
  333. static int test__checkevent_list(struct perf_evlist *evlist)
  334. {
  335. struct perf_evsel *evsel;
  336. TEST_ASSERT_VAL("wrong number of entries", 3 == evlist->nr_entries);
  337. /* r1 */
  338. evsel = list_entry(evlist->entries.next, struct perf_evsel, node);
  339. TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->attr.type);
  340. TEST_ASSERT_VAL("wrong config", 1 == evsel->attr.config);
  341. TEST_ASSERT_VAL("wrong config1", 0 == evsel->attr.config1);
  342. TEST_ASSERT_VAL("wrong config2", 0 == evsel->attr.config2);
  343. TEST_ASSERT_VAL("wrong exclude_user", !evsel->attr.exclude_user);
  344. TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
  345. TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
  346. TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
  347. /* syscalls:sys_enter_open:k */
  348. evsel = list_entry(evsel->node.next, struct perf_evsel, node);
  349. TEST_ASSERT_VAL("wrong type", PERF_TYPE_TRACEPOINT == evsel->attr.type);
  350. TEST_ASSERT_VAL("wrong sample_type",
  351. (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME | PERF_SAMPLE_CPU) ==
  352. evsel->attr.sample_type);
  353. TEST_ASSERT_VAL("wrong sample_period", 1 == evsel->attr.sample_period);
  354. TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
  355. TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
  356. TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
  357. TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
  358. /* 1:1:hp */
  359. evsel = list_entry(evsel->node.next, struct perf_evsel, node);
  360. TEST_ASSERT_VAL("wrong type", 1 == evsel->attr.type);
  361. TEST_ASSERT_VAL("wrong config", 1 == evsel->attr.config);
  362. TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
  363. TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
  364. TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
  365. TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
  366. return 0;
  367. }
  368. static int test__checkevent_pmu_name(struct perf_evlist *evlist)
  369. {
  370. struct perf_evsel *evsel;
  371. /* cpu/config=1,name=krava/u */
  372. evsel = list_entry(evlist->entries.next, struct perf_evsel, node);
  373. TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->nr_entries);
  374. TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->attr.type);
  375. TEST_ASSERT_VAL("wrong config", 1 == evsel->attr.config);
  376. TEST_ASSERT_VAL("wrong name", !strcmp(perf_evsel__name(evsel), "krava"));
  377. /* cpu/config=2/u" */
  378. evsel = list_entry(evsel->node.next, struct perf_evsel, node);
  379. TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->nr_entries);
  380. TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->attr.type);
  381. TEST_ASSERT_VAL("wrong config", 2 == evsel->attr.config);
  382. TEST_ASSERT_VAL("wrong name",
  383. !strcmp(perf_evsel__name(evsel), "raw 0x2:u"));
  384. return 0;
  385. }
  386. static int test__checkterms_simple(struct list_head *terms)
  387. {
  388. struct parse_events__term *term;
  389. /* config=10 */
  390. term = list_entry(terms->next, struct parse_events__term, list);
  391. TEST_ASSERT_VAL("wrong type term",
  392. term->type_term == PARSE_EVENTS__TERM_TYPE_CONFIG);
  393. TEST_ASSERT_VAL("wrong type val",
  394. term->type_val == PARSE_EVENTS__TERM_TYPE_NUM);
  395. TEST_ASSERT_VAL("wrong val", term->val.num == 10);
  396. TEST_ASSERT_VAL("wrong config", !term->config);
  397. /* config1 */
  398. term = list_entry(term->list.next, struct parse_events__term, list);
  399. TEST_ASSERT_VAL("wrong type term",
  400. term->type_term == PARSE_EVENTS__TERM_TYPE_CONFIG1);
  401. TEST_ASSERT_VAL("wrong type val",
  402. term->type_val == PARSE_EVENTS__TERM_TYPE_NUM);
  403. TEST_ASSERT_VAL("wrong val", term->val.num == 1);
  404. TEST_ASSERT_VAL("wrong config", !term->config);
  405. /* config2=3 */
  406. term = list_entry(term->list.next, struct parse_events__term, list);
  407. TEST_ASSERT_VAL("wrong type term",
  408. term->type_term == PARSE_EVENTS__TERM_TYPE_CONFIG2);
  409. TEST_ASSERT_VAL("wrong type val",
  410. term->type_val == PARSE_EVENTS__TERM_TYPE_NUM);
  411. TEST_ASSERT_VAL("wrong val", term->val.num == 3);
  412. TEST_ASSERT_VAL("wrong config", !term->config);
  413. /* umask=1*/
  414. term = list_entry(term->list.next, struct parse_events__term, list);
  415. TEST_ASSERT_VAL("wrong type term",
  416. term->type_term == PARSE_EVENTS__TERM_TYPE_USER);
  417. TEST_ASSERT_VAL("wrong type val",
  418. term->type_val == PARSE_EVENTS__TERM_TYPE_NUM);
  419. TEST_ASSERT_VAL("wrong val", term->val.num == 1);
  420. TEST_ASSERT_VAL("wrong config", !strcmp(term->config, "umask"));
  421. return 0;
  422. }
  423. struct test__event_st {
  424. const char *name;
  425. __u32 type;
  426. int (*check)(struct perf_evlist *evlist);
  427. };
  428. static struct test__event_st test__events[] = {
  429. [0] = {
  430. .name = "syscalls:sys_enter_open",
  431. .check = test__checkevent_tracepoint,
  432. },
  433. [1] = {
  434. .name = "syscalls:*",
  435. .check = test__checkevent_tracepoint_multi,
  436. },
  437. [2] = {
  438. .name = "r1a",
  439. .check = test__checkevent_raw,
  440. },
  441. [3] = {
  442. .name = "1:1",
  443. .check = test__checkevent_numeric,
  444. },
  445. [4] = {
  446. .name = "instructions",
  447. .check = test__checkevent_symbolic_name,
  448. },
  449. [5] = {
  450. .name = "cycles/period=100000,config2/",
  451. .check = test__checkevent_symbolic_name_config,
  452. },
  453. [6] = {
  454. .name = "faults",
  455. .check = test__checkevent_symbolic_alias,
  456. },
  457. [7] = {
  458. .name = "L1-dcache-load-miss",
  459. .check = test__checkevent_genhw,
  460. },
  461. [8] = {
  462. .name = "mem:0",
  463. .check = test__checkevent_breakpoint,
  464. },
  465. [9] = {
  466. .name = "mem:0:x",
  467. .check = test__checkevent_breakpoint_x,
  468. },
  469. [10] = {
  470. .name = "mem:0:r",
  471. .check = test__checkevent_breakpoint_r,
  472. },
  473. [11] = {
  474. .name = "mem:0:w",
  475. .check = test__checkevent_breakpoint_w,
  476. },
  477. [12] = {
  478. .name = "syscalls:sys_enter_open:k",
  479. .check = test__checkevent_tracepoint_modifier,
  480. },
  481. [13] = {
  482. .name = "syscalls:*:u",
  483. .check = test__checkevent_tracepoint_multi_modifier,
  484. },
  485. [14] = {
  486. .name = "r1a:kp",
  487. .check = test__checkevent_raw_modifier,
  488. },
  489. [15] = {
  490. .name = "1:1:hp",
  491. .check = test__checkevent_numeric_modifier,
  492. },
  493. [16] = {
  494. .name = "instructions:h",
  495. .check = test__checkevent_symbolic_name_modifier,
  496. },
  497. [17] = {
  498. .name = "faults:u",
  499. .check = test__checkevent_symbolic_alias_modifier,
  500. },
  501. [18] = {
  502. .name = "L1-dcache-load-miss:kp",
  503. .check = test__checkevent_genhw_modifier,
  504. },
  505. [19] = {
  506. .name = "mem:0:u",
  507. .check = test__checkevent_breakpoint_modifier,
  508. },
  509. [20] = {
  510. .name = "mem:0:x:k",
  511. .check = test__checkevent_breakpoint_x_modifier,
  512. },
  513. [21] = {
  514. .name = "mem:0:r:hp",
  515. .check = test__checkevent_breakpoint_r_modifier,
  516. },
  517. [22] = {
  518. .name = "mem:0:w:up",
  519. .check = test__checkevent_breakpoint_w_modifier,
  520. },
  521. [23] = {
  522. .name = "r1,syscalls:sys_enter_open:k,1:1:hp",
  523. .check = test__checkevent_list,
  524. },
  525. [24] = {
  526. .name = "instructions:G",
  527. .check = test__checkevent_exclude_host_modifier,
  528. },
  529. [25] = {
  530. .name = "instructions:H",
  531. .check = test__checkevent_exclude_guest_modifier,
  532. },
  533. [26] = {
  534. .name = "mem:0:rw",
  535. .check = test__checkevent_breakpoint_rw,
  536. },
  537. [27] = {
  538. .name = "mem:0:rw:kp",
  539. .check = test__checkevent_breakpoint_rw_modifier,
  540. },
  541. };
  542. #define TEST__EVENTS_CNT (sizeof(test__events) / sizeof(struct test__event_st))
  543. static struct test__event_st test__events_pmu[] = {
  544. [0] = {
  545. .name = "cpu/config=10,config1,config2=3,period=1000/u",
  546. .check = test__checkevent_pmu,
  547. },
  548. [1] = {
  549. .name = "cpu/config=1,name=krava/u,cpu/config=2/u",
  550. .check = test__checkevent_pmu_name,
  551. },
  552. };
  553. #define TEST__EVENTS_PMU_CNT (sizeof(test__events_pmu) / \
  554. sizeof(struct test__event_st))
  555. struct test__term {
  556. const char *str;
  557. __u32 type;
  558. int (*check)(struct list_head *terms);
  559. };
  560. static struct test__term test__terms[] = {
  561. [0] = {
  562. .str = "config=10,config1,config2=3,umask=1",
  563. .check = test__checkterms_simple,
  564. },
  565. };
  566. #define TEST__TERMS_CNT (sizeof(test__terms) / \
  567. sizeof(struct test__term))
  568. static int test_event(struct test__event_st *e)
  569. {
  570. struct perf_evlist *evlist;
  571. int ret;
  572. evlist = perf_evlist__new(NULL, NULL);
  573. if (evlist == NULL)
  574. return -ENOMEM;
  575. ret = parse_events(evlist, e->name, 0);
  576. if (ret) {
  577. pr_debug("failed to parse event '%s', err %d\n",
  578. e->name, ret);
  579. return ret;
  580. }
  581. ret = e->check(evlist);
  582. perf_evlist__delete(evlist);
  583. return ret;
  584. }
  585. static int test_events(struct test__event_st *events, unsigned cnt)
  586. {
  587. int ret = 0;
  588. unsigned i;
  589. for (i = 0; i < cnt; i++) {
  590. struct test__event_st *e = &events[i];
  591. pr_debug("running test %d '%s'\n", i, e->name);
  592. ret = test_event(e);
  593. if (ret)
  594. break;
  595. }
  596. return ret;
  597. }
  598. static int test_term(struct test__term *t)
  599. {
  600. struct list_head *terms;
  601. int ret;
  602. terms = malloc(sizeof(*terms));
  603. if (!terms)
  604. return -ENOMEM;
  605. INIT_LIST_HEAD(terms);
  606. ret = parse_events_terms(terms, t->str);
  607. if (ret) {
  608. pr_debug("failed to parse terms '%s', err %d\n",
  609. t->str , ret);
  610. return ret;
  611. }
  612. ret = t->check(terms);
  613. parse_events__free_terms(terms);
  614. return ret;
  615. }
  616. static int test_terms(struct test__term *terms, unsigned cnt)
  617. {
  618. int ret = 0;
  619. unsigned i;
  620. for (i = 0; i < cnt; i++) {
  621. struct test__term *t = &terms[i];
  622. pr_debug("running test %d '%s'\n", i, t->str);
  623. ret = test_term(t);
  624. if (ret)
  625. break;
  626. }
  627. return ret;
  628. }
  629. static int test_pmu(void)
  630. {
  631. struct stat st;
  632. char path[PATH_MAX];
  633. int ret;
  634. snprintf(path, PATH_MAX, "%s/bus/event_source/devices/cpu/format/",
  635. sysfs_find_mountpoint());
  636. ret = stat(path, &st);
  637. if (ret)
  638. pr_debug("ommiting PMU cpu tests\n");
  639. return !ret;
  640. }
  641. int parse_events__test(void)
  642. {
  643. int ret;
  644. do {
  645. ret = test_events(test__events, TEST__EVENTS_CNT);
  646. if (ret)
  647. break;
  648. if (test_pmu()) {
  649. ret = test_events(test__events_pmu,
  650. TEST__EVENTS_PMU_CNT);
  651. if (ret)
  652. break;
  653. }
  654. ret = test_terms(test__terms, TEST__TERMS_CNT);
  655. } while (0);
  656. return ret;
  657. }