parse-events-test.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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. #define PERF_TP_SAMPLE_TYPE (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME | \
  14. PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD)
  15. static int test__checkevent_tracepoint(struct perf_evlist *evlist)
  16. {
  17. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  18. struct perf_evsel, node);
  19. TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
  20. TEST_ASSERT_VAL("wrong type", PERF_TYPE_TRACEPOINT == evsel->attr.type);
  21. TEST_ASSERT_VAL("wrong sample_type",
  22. PERF_TP_SAMPLE_TYPE == evsel->attr.sample_type);
  23. TEST_ASSERT_VAL("wrong sample_period", 1 == evsel->attr.sample_period);
  24. return 0;
  25. }
  26. static int test__checkevent_tracepoint_multi(struct perf_evlist *evlist)
  27. {
  28. struct perf_evsel *evsel;
  29. TEST_ASSERT_VAL("wrong number of entries", evlist->nr_entries > 1);
  30. list_for_each_entry(evsel, &evlist->entries, node) {
  31. TEST_ASSERT_VAL("wrong type",
  32. PERF_TYPE_TRACEPOINT == evsel->attr.type);
  33. TEST_ASSERT_VAL("wrong sample_type",
  34. PERF_TP_SAMPLE_TYPE == 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_TP_SAMPLE_TYPE == evsel->attr.sample_type);
  352. TEST_ASSERT_VAL("wrong sample_period", 1 == evsel->attr.sample_period);
  353. TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
  354. TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
  355. TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
  356. TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
  357. /* 1:1:hp */
  358. evsel = list_entry(evsel->node.next, struct perf_evsel, node);
  359. TEST_ASSERT_VAL("wrong type", 1 == evsel->attr.type);
  360. TEST_ASSERT_VAL("wrong config", 1 == evsel->attr.config);
  361. TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
  362. TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
  363. TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
  364. TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
  365. return 0;
  366. }
  367. static int test__checkevent_pmu_name(struct perf_evlist *evlist)
  368. {
  369. struct perf_evsel *evsel;
  370. /* cpu/config=1,name=krava/u */
  371. evsel = list_entry(evlist->entries.next, struct perf_evsel, node);
  372. TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->nr_entries);
  373. TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->attr.type);
  374. TEST_ASSERT_VAL("wrong config", 1 == evsel->attr.config);
  375. TEST_ASSERT_VAL("wrong name", !strcmp(perf_evsel__name(evsel), "krava"));
  376. /* cpu/config=2/u" */
  377. evsel = list_entry(evsel->node.next, struct perf_evsel, node);
  378. TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->nr_entries);
  379. TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->attr.type);
  380. TEST_ASSERT_VAL("wrong config", 2 == evsel->attr.config);
  381. TEST_ASSERT_VAL("wrong name",
  382. !strcmp(perf_evsel__name(evsel), "raw 0x2:u"));
  383. return 0;
  384. }
  385. static int test__checkterms_simple(struct list_head *terms)
  386. {
  387. struct parse_events__term *term;
  388. /* config=10 */
  389. term = list_entry(terms->next, struct parse_events__term, list);
  390. TEST_ASSERT_VAL("wrong type term",
  391. term->type_term == PARSE_EVENTS__TERM_TYPE_CONFIG);
  392. TEST_ASSERT_VAL("wrong type val",
  393. term->type_val == PARSE_EVENTS__TERM_TYPE_NUM);
  394. TEST_ASSERT_VAL("wrong val", term->val.num == 10);
  395. TEST_ASSERT_VAL("wrong config", !term->config);
  396. /* config1 */
  397. term = list_entry(term->list.next, struct parse_events__term, list);
  398. TEST_ASSERT_VAL("wrong type term",
  399. term->type_term == PARSE_EVENTS__TERM_TYPE_CONFIG1);
  400. TEST_ASSERT_VAL("wrong type val",
  401. term->type_val == PARSE_EVENTS__TERM_TYPE_NUM);
  402. TEST_ASSERT_VAL("wrong val", term->val.num == 1);
  403. TEST_ASSERT_VAL("wrong config", !term->config);
  404. /* config2=3 */
  405. term = list_entry(term->list.next, struct parse_events__term, list);
  406. TEST_ASSERT_VAL("wrong type term",
  407. term->type_term == PARSE_EVENTS__TERM_TYPE_CONFIG2);
  408. TEST_ASSERT_VAL("wrong type val",
  409. term->type_val == PARSE_EVENTS__TERM_TYPE_NUM);
  410. TEST_ASSERT_VAL("wrong val", term->val.num == 3);
  411. TEST_ASSERT_VAL("wrong config", !term->config);
  412. /* umask=1*/
  413. term = list_entry(term->list.next, struct parse_events__term, list);
  414. TEST_ASSERT_VAL("wrong type term",
  415. term->type_term == PARSE_EVENTS__TERM_TYPE_USER);
  416. TEST_ASSERT_VAL("wrong type val",
  417. term->type_val == PARSE_EVENTS__TERM_TYPE_NUM);
  418. TEST_ASSERT_VAL("wrong val", term->val.num == 1);
  419. TEST_ASSERT_VAL("wrong config", !strcmp(term->config, "umask"));
  420. return 0;
  421. }
  422. struct test__event_st {
  423. const char *name;
  424. __u32 type;
  425. int (*check)(struct perf_evlist *evlist);
  426. };
  427. static struct test__event_st test__events[] = {
  428. [0] = {
  429. .name = "syscalls:sys_enter_open",
  430. .check = test__checkevent_tracepoint,
  431. },
  432. [1] = {
  433. .name = "syscalls:*",
  434. .check = test__checkevent_tracepoint_multi,
  435. },
  436. [2] = {
  437. .name = "r1a",
  438. .check = test__checkevent_raw,
  439. },
  440. [3] = {
  441. .name = "1:1",
  442. .check = test__checkevent_numeric,
  443. },
  444. [4] = {
  445. .name = "instructions",
  446. .check = test__checkevent_symbolic_name,
  447. },
  448. [5] = {
  449. .name = "cycles/period=100000,config2/",
  450. .check = test__checkevent_symbolic_name_config,
  451. },
  452. [6] = {
  453. .name = "faults",
  454. .check = test__checkevent_symbolic_alias,
  455. },
  456. [7] = {
  457. .name = "L1-dcache-load-miss",
  458. .check = test__checkevent_genhw,
  459. },
  460. [8] = {
  461. .name = "mem:0",
  462. .check = test__checkevent_breakpoint,
  463. },
  464. [9] = {
  465. .name = "mem:0:x",
  466. .check = test__checkevent_breakpoint_x,
  467. },
  468. [10] = {
  469. .name = "mem:0:r",
  470. .check = test__checkevent_breakpoint_r,
  471. },
  472. [11] = {
  473. .name = "mem:0:w",
  474. .check = test__checkevent_breakpoint_w,
  475. },
  476. [12] = {
  477. .name = "syscalls:sys_enter_open:k",
  478. .check = test__checkevent_tracepoint_modifier,
  479. },
  480. [13] = {
  481. .name = "syscalls:*:u",
  482. .check = test__checkevent_tracepoint_multi_modifier,
  483. },
  484. [14] = {
  485. .name = "r1a:kp",
  486. .check = test__checkevent_raw_modifier,
  487. },
  488. [15] = {
  489. .name = "1:1:hp",
  490. .check = test__checkevent_numeric_modifier,
  491. },
  492. [16] = {
  493. .name = "instructions:h",
  494. .check = test__checkevent_symbolic_name_modifier,
  495. },
  496. [17] = {
  497. .name = "faults:u",
  498. .check = test__checkevent_symbolic_alias_modifier,
  499. },
  500. [18] = {
  501. .name = "L1-dcache-load-miss:kp",
  502. .check = test__checkevent_genhw_modifier,
  503. },
  504. [19] = {
  505. .name = "mem:0:u",
  506. .check = test__checkevent_breakpoint_modifier,
  507. },
  508. [20] = {
  509. .name = "mem:0:x:k",
  510. .check = test__checkevent_breakpoint_x_modifier,
  511. },
  512. [21] = {
  513. .name = "mem:0:r:hp",
  514. .check = test__checkevent_breakpoint_r_modifier,
  515. },
  516. [22] = {
  517. .name = "mem:0:w:up",
  518. .check = test__checkevent_breakpoint_w_modifier,
  519. },
  520. [23] = {
  521. .name = "r1,syscalls:sys_enter_open:k,1:1:hp",
  522. .check = test__checkevent_list,
  523. },
  524. [24] = {
  525. .name = "instructions:G",
  526. .check = test__checkevent_exclude_host_modifier,
  527. },
  528. [25] = {
  529. .name = "instructions:H",
  530. .check = test__checkevent_exclude_guest_modifier,
  531. },
  532. [26] = {
  533. .name = "mem:0:rw",
  534. .check = test__checkevent_breakpoint_rw,
  535. },
  536. [27] = {
  537. .name = "mem:0:rw:kp",
  538. .check = test__checkevent_breakpoint_rw_modifier,
  539. },
  540. };
  541. static struct test__event_st test__events_pmu[] = {
  542. [0] = {
  543. .name = "cpu/config=10,config1,config2=3,period=1000/u",
  544. .check = test__checkevent_pmu,
  545. },
  546. [1] = {
  547. .name = "cpu/config=1,name=krava/u,cpu/config=2/u",
  548. .check = test__checkevent_pmu_name,
  549. },
  550. };
  551. struct test__term {
  552. const char *str;
  553. __u32 type;
  554. int (*check)(struct list_head *terms);
  555. };
  556. static struct test__term test__terms[] = {
  557. [0] = {
  558. .str = "config=10,config1,config2=3,umask=1",
  559. .check = test__checkterms_simple,
  560. },
  561. };
  562. #define TEST__TERMS_CNT (sizeof(test__terms) / \
  563. sizeof(struct test__term))
  564. static int test_event(struct test__event_st *e)
  565. {
  566. struct perf_evlist *evlist;
  567. int ret;
  568. evlist = perf_evlist__new(NULL, NULL);
  569. if (evlist == NULL)
  570. return -ENOMEM;
  571. ret = parse_events(evlist, e->name, 0);
  572. if (ret) {
  573. pr_debug("failed to parse event '%s', err %d\n",
  574. e->name, ret);
  575. return ret;
  576. }
  577. ret = e->check(evlist);
  578. perf_evlist__delete(evlist);
  579. return ret;
  580. }
  581. static int test_events(struct test__event_st *events, unsigned cnt)
  582. {
  583. int ret = 0;
  584. unsigned i;
  585. for (i = 0; i < cnt; i++) {
  586. struct test__event_st *e = &events[i];
  587. pr_debug("running test %d '%s'\n", i, e->name);
  588. ret = test_event(e);
  589. if (ret)
  590. break;
  591. }
  592. return ret;
  593. }
  594. static int test_term(struct test__term *t)
  595. {
  596. struct list_head *terms;
  597. int ret;
  598. terms = malloc(sizeof(*terms));
  599. if (!terms)
  600. return -ENOMEM;
  601. INIT_LIST_HEAD(terms);
  602. ret = parse_events_terms(terms, t->str);
  603. if (ret) {
  604. pr_debug("failed to parse terms '%s', err %d\n",
  605. t->str , ret);
  606. return ret;
  607. }
  608. ret = t->check(terms);
  609. parse_events__free_terms(terms);
  610. return ret;
  611. }
  612. static int test_terms(struct test__term *terms, unsigned cnt)
  613. {
  614. int ret = 0;
  615. unsigned i;
  616. for (i = 0; i < cnt; i++) {
  617. struct test__term *t = &terms[i];
  618. pr_debug("running test %d '%s'\n", i, t->str);
  619. ret = test_term(t);
  620. if (ret)
  621. break;
  622. }
  623. return ret;
  624. }
  625. static int test_pmu(void)
  626. {
  627. struct stat st;
  628. char path[PATH_MAX];
  629. int ret;
  630. snprintf(path, PATH_MAX, "%s/bus/event_source/devices/cpu/format/",
  631. sysfs_find_mountpoint());
  632. ret = stat(path, &st);
  633. if (ret)
  634. pr_debug("ommiting PMU cpu tests\n");
  635. return !ret;
  636. }
  637. int parse_events__test(void)
  638. {
  639. int ret;
  640. #define TEST_EVENTS(tests) \
  641. do { \
  642. ret = test_events(tests, ARRAY_SIZE(tests)); \
  643. if (ret) \
  644. return ret; \
  645. } while (0)
  646. TEST_EVENTS(test__events);
  647. if (test_pmu())
  648. TEST_EVENTS(test__events_pmu);
  649. return test_terms(test__terms, ARRAY_SIZE(test__terms));
  650. }