expr.c 10 KB

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