expr.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "util/cputopo.h"
  3. #include "util/debug.h"
  4. #include "util/expr.h"
  5. #include "util/hashmap.h"
  6. #include "util/header.h"
  7. #include "util/smt.h"
  8. #include "tests.h"
  9. #include <math.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <string2.h>
  13. #include <linux/zalloc.h>
  14. static int test_ids_union(void)
  15. {
  16. struct hashmap *ids1, *ids2;
  17. /* Empty union. */
  18. ids1 = ids__new();
  19. TEST_ASSERT_VAL("ids__new", ids1);
  20. ids2 = ids__new();
  21. TEST_ASSERT_VAL("ids__new", ids2);
  22. ids1 = ids__union(ids1, ids2);
  23. TEST_ASSERT_EQUAL("union", (int)hashmap__size(ids1), 0);
  24. /* Union {foo, bar} against {}. */
  25. ids2 = ids__new();
  26. TEST_ASSERT_VAL("ids__new", ids2);
  27. TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids1, strdup("foo")), 0);
  28. TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids1, strdup("bar")), 0);
  29. ids1 = ids__union(ids1, ids2);
  30. TEST_ASSERT_EQUAL("union", (int)hashmap__size(ids1), 2);
  31. /* Union {foo, bar} against {foo}. */
  32. ids2 = ids__new();
  33. TEST_ASSERT_VAL("ids__new", ids2);
  34. TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids2, strdup("foo")), 0);
  35. ids1 = ids__union(ids1, ids2);
  36. TEST_ASSERT_EQUAL("union", (int)hashmap__size(ids1), 2);
  37. /* Union {foo, bar} against {bar,baz}. */
  38. ids2 = ids__new();
  39. TEST_ASSERT_VAL("ids__new", ids2);
  40. TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids2, strdup("bar")), 0);
  41. TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids2, strdup("baz")), 0);
  42. ids1 = ids__union(ids1, ids2);
  43. TEST_ASSERT_EQUAL("union", (int)hashmap__size(ids1), 3);
  44. ids__free(ids1);
  45. return 0;
  46. }
  47. static int test(struct expr_parse_ctx *ctx, const char *e, double val2)
  48. {
  49. double val;
  50. if (expr__parse(&val, ctx, e))
  51. TEST_ASSERT_VAL("parse test failed", 0);
  52. TEST_ASSERT_VAL("unexpected value", val == val2);
  53. return 0;
  54. }
  55. static int test__expr(struct test_suite *t __maybe_unused, int subtest __maybe_unused)
  56. {
  57. struct expr_id_data *val_ptr;
  58. const char *p;
  59. double val, num_cpus_online, num_cpus, num_cores, num_dies, num_packages;
  60. int ret;
  61. struct expr_parse_ctx *ctx;
  62. bool is_intel = false;
  63. char strcmp_cpuid_buf[256];
  64. struct perf_pmu *pmu = perf_pmus__find_core_pmu();
  65. char *cpuid = perf_pmu__getcpuid(pmu);
  66. char *escaped_cpuid1, *escaped_cpuid2;
  67. TEST_ASSERT_VAL("get_cpuid", cpuid);
  68. is_intel = strstr(cpuid, "Intel") != NULL;
  69. TEST_ASSERT_EQUAL("ids_union", test_ids_union(), 0);
  70. ctx = expr__ctx_new();
  71. TEST_ASSERT_VAL("expr__ctx_new", ctx);
  72. expr__add_id_val(ctx, strdup("FOO"), 1);
  73. expr__add_id_val(ctx, strdup("BAR"), 2);
  74. ret = test(ctx, "1+1", 2);
  75. ret |= test(ctx, "FOO+BAR", 3);
  76. ret |= test(ctx, "(BAR/2)%2", 1);
  77. ret |= test(ctx, "1 - -4", 5);
  78. ret |= test(ctx, "(FOO-1)*2 + (BAR/2)%2 - -4", 5);
  79. ret |= test(ctx, "1-1 | 1", 1);
  80. ret |= test(ctx, "1-1 & 1", 0);
  81. ret |= test(ctx, "min(1,2) + 1", 2);
  82. ret |= test(ctx, "max(1,2) + 1", 3);
  83. ret |= test(ctx, "1+1 if 3*4 else 0", 2);
  84. ret |= test(ctx, "100 if 1 else 200 if 1 else 300", 100);
  85. ret |= test(ctx, "100 if 0 else 200 if 1 else 300", 200);
  86. ret |= test(ctx, "100 if 1 else 200 if 0 else 300", 100);
  87. ret |= test(ctx, "100 if 0 else 200 if 0 else 300", 300);
  88. ret |= test(ctx, "1.1 + 2.1", 3.2);
  89. ret |= test(ctx, ".1 + 2.", 2.1);
  90. ret |= test(ctx, "d_ratio(1, 2)", 0.5);
  91. ret |= test(ctx, "d_ratio(2.5, 0)", 0);
  92. ret |= test(ctx, "1.1 < 2.2", 1);
  93. ret |= test(ctx, "2.2 > 1.1", 1);
  94. ret |= test(ctx, "1.1 < 1.1", 0);
  95. ret |= test(ctx, "2.2 > 2.2", 0);
  96. ret |= test(ctx, "2.2 < 1.1", 0);
  97. ret |= test(ctx, "1.1 > 2.2", 0);
  98. ret |= test(ctx, "1.1e10 < 1.1e100", 1);
  99. ret |= test(ctx, "1.1e2 > 1.1e-2", 1);
  100. if (ret) {
  101. expr__ctx_free(ctx);
  102. return ret;
  103. }
  104. p = "FOO/0";
  105. ret = expr__parse(&val, ctx, p);
  106. TEST_ASSERT_VAL("division by zero", ret == 0);
  107. TEST_ASSERT_VAL("division by zero", isnan(val));
  108. p = "BAR/";
  109. ret = expr__parse(&val, ctx, p);
  110. TEST_ASSERT_VAL("missing operand", ret == -1);
  111. expr__ctx_clear(ctx);
  112. TEST_ASSERT_VAL("find ids",
  113. expr__find_ids("FOO + BAR + BAZ + BOZO", "FOO",
  114. ctx) == 0);
  115. TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 3);
  116. TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "BAR", &val_ptr));
  117. TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "BAZ", &val_ptr));
  118. TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "BOZO", &val_ptr));
  119. expr__ctx_clear(ctx);
  120. ctx->sctx.runtime = 3;
  121. TEST_ASSERT_VAL("find ids",
  122. expr__find_ids("EVENT1\\,param\\=?@ + EVENT2\\,param\\=?@",
  123. NULL, ctx) == 0);
  124. TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 2);
  125. TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1,param=3@", &val_ptr));
  126. TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT2,param=3@", &val_ptr));
  127. expr__ctx_clear(ctx);
  128. TEST_ASSERT_VAL("find ids",
  129. expr__find_ids("dash\\-event1 - dash\\-event2",
  130. NULL, ctx) == 0);
  131. TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 2);
  132. TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "dash-event1", &val_ptr));
  133. TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "dash-event2", &val_ptr));
  134. /* Only EVENT1 or EVENT2 need be measured depending on the value of smt_on. */
  135. {
  136. bool smton = smt_on();
  137. bool corewide = core_wide(/*system_wide=*/false,
  138. /*user_requested_cpus=*/false);
  139. expr__ctx_clear(ctx);
  140. TEST_ASSERT_VAL("find ids",
  141. expr__find_ids("EVENT1 if #smt_on else EVENT2",
  142. NULL, ctx) == 0);
  143. TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
  144. TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids,
  145. smton ? "EVENT1" : "EVENT2",
  146. &val_ptr));
  147. expr__ctx_clear(ctx);
  148. TEST_ASSERT_VAL("find ids",
  149. expr__find_ids("EVENT1 if #core_wide else EVENT2",
  150. NULL, ctx) == 0);
  151. TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
  152. TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids,
  153. corewide ? "EVENT1" : "EVENT2",
  154. &val_ptr));
  155. }
  156. /* The expression is a constant 1.0 without needing to evaluate EVENT1. */
  157. expr__ctx_clear(ctx);
  158. TEST_ASSERT_VAL("find ids",
  159. expr__find_ids("1.0 if EVENT1 > 100.0 else 1.0",
  160. NULL, ctx) == 0);
  161. TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0);
  162. /* The expression is a constant 0.0 without needing to evaluate EVENT1. */
  163. expr__ctx_clear(ctx);
  164. TEST_ASSERT_VAL("find ids",
  165. expr__find_ids("0 & EVENT1 > 0", NULL, ctx) == 0);
  166. TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0);
  167. expr__ctx_clear(ctx);
  168. TEST_ASSERT_VAL("find ids",
  169. expr__find_ids("EVENT1 > 0 & 0", NULL, ctx) == 0);
  170. TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0);
  171. expr__ctx_clear(ctx);
  172. TEST_ASSERT_VAL("find ids",
  173. expr__find_ids("1 & EVENT1 > 0", NULL, ctx) == 0);
  174. TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
  175. TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1", &val_ptr));
  176. expr__ctx_clear(ctx);
  177. TEST_ASSERT_VAL("find ids",
  178. expr__find_ids("EVENT1 > 0 & 1", NULL, ctx) == 0);
  179. TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
  180. TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1", &val_ptr));
  181. /* The expression is a constant 1.0 without needing to evaluate EVENT1. */
  182. expr__ctx_clear(ctx);
  183. TEST_ASSERT_VAL("find ids",
  184. expr__find_ids("1 | EVENT1 > 0", NULL, ctx) == 0);
  185. TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0);
  186. expr__ctx_clear(ctx);
  187. TEST_ASSERT_VAL("find ids",
  188. expr__find_ids("EVENT1 > 0 | 1", NULL, ctx) == 0);
  189. TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0);
  190. expr__ctx_clear(ctx);
  191. TEST_ASSERT_VAL("find ids",
  192. expr__find_ids("0 | EVENT1 > 0", NULL, ctx) == 0);
  193. TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
  194. TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1", &val_ptr));
  195. expr__ctx_clear(ctx);
  196. TEST_ASSERT_VAL("find ids",
  197. expr__find_ids("EVENT1 > 0 | 0", NULL, ctx) == 0);
  198. TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
  199. TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1", &val_ptr));
  200. /* Test toplogy constants appear well ordered. */
  201. expr__ctx_clear(ctx);
  202. TEST_ASSERT_VAL("#num_cpus_online",
  203. expr__parse(&num_cpus_online, ctx, "#num_cpus_online") == 0);
  204. TEST_ASSERT_VAL("#num_cpus", expr__parse(&num_cpus, ctx, "#num_cpus") == 0);
  205. TEST_ASSERT_VAL("#num_cpus >= #num_cpus_online", num_cpus >= num_cpus_online);
  206. TEST_ASSERT_VAL("#num_cores", expr__parse(&num_cores, ctx, "#num_cores") == 0);
  207. TEST_ASSERT_VAL("#num_cpus >= #num_cores", num_cpus >= num_cores);
  208. TEST_ASSERT_VAL("#num_dies", expr__parse(&num_dies, ctx, "#num_dies") == 0);
  209. TEST_ASSERT_VAL("#num_cores >= #num_dies", num_cores >= num_dies);
  210. TEST_ASSERT_VAL("#num_packages", expr__parse(&num_packages, ctx, "#num_packages") == 0);
  211. if (num_dies) // Some platforms do not have CPU die support, for example s390
  212. TEST_ASSERT_VAL("#num_dies >= #num_packages", num_dies >= num_packages);
  213. TEST_ASSERT_VAL("#system_tsc_freq", expr__parse(&val, ctx, "#system_tsc_freq") == 0);
  214. if (is_intel)
  215. TEST_ASSERT_VAL("#system_tsc_freq > 0", val > 0);
  216. else
  217. TEST_ASSERT_VAL("#system_tsc_freq == 0", fpclassify(val) == FP_ZERO);
  218. /*
  219. * Source count returns the number of events aggregating in a leader
  220. * event including the leader. Check parsing yields an id.
  221. */
  222. expr__ctx_clear(ctx);
  223. TEST_ASSERT_VAL("source count",
  224. expr__find_ids("source_count(EVENT1)",
  225. NULL, ctx) == 0);
  226. TEST_ASSERT_VAL("source count", hashmap__size(ctx->ids) == 1);
  227. TEST_ASSERT_VAL("source count", hashmap__find(ctx->ids, "EVENT1", &val_ptr));
  228. /* Test no cpuid match */
  229. ret = test(ctx, "strcmp_cpuid_str(0x0)", 0);
  230. /*
  231. * Test cpuid match with current cpuid. Special chars have to be
  232. * escaped.
  233. */
  234. escaped_cpuid1 = strreplace_chars('-', cpuid, "\\-");
  235. free(cpuid);
  236. escaped_cpuid2 = strreplace_chars(',', escaped_cpuid1, "\\,");
  237. free(escaped_cpuid1);
  238. escaped_cpuid1 = strreplace_chars('=', escaped_cpuid2, "\\=");
  239. free(escaped_cpuid2);
  240. scnprintf(strcmp_cpuid_buf, sizeof(strcmp_cpuid_buf),
  241. "strcmp_cpuid_str(%s)", escaped_cpuid1);
  242. free(escaped_cpuid1);
  243. ret |= test(ctx, strcmp_cpuid_buf, 1);
  244. /* has_event returns 1 when an event exists. */
  245. expr__add_id_val(ctx, strdup("cycles"), 2);
  246. ret |= test(ctx, "has_event(cycles)", 1);
  247. expr__ctx_free(ctx);
  248. return ret;
  249. }
  250. DEFINE_SUITE("Simple expression parser", expr);