expr.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <stdbool.h>
  3. #include <assert.h>
  4. #include <errno.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "metricgroup.h"
  8. #include "debug.h"
  9. #include "evlist.h"
  10. #include "expr.h"
  11. #include "smt.h"
  12. #include "tool_pmu.h"
  13. #include <util/expr-bison.h>
  14. #include <util/expr-flex.h>
  15. #include "util/hashmap.h"
  16. #include "util/header.h"
  17. #include "util/pmu.h"
  18. #include <perf/cpumap.h>
  19. #include <linux/err.h>
  20. #include <linux/kernel.h>
  21. #include <linux/zalloc.h>
  22. #include <ctype.h>
  23. #include <math.h>
  24. struct expr_id_data {
  25. union {
  26. struct {
  27. double val;
  28. int source_count;
  29. } val;
  30. struct {
  31. double val;
  32. const char *metric_name;
  33. const char *metric_expr;
  34. } ref;
  35. };
  36. enum {
  37. /* Holding a double value. */
  38. EXPR_ID_DATA__VALUE,
  39. /* Reference to another metric. */
  40. EXPR_ID_DATA__REF,
  41. /* A reference but the value has been computed. */
  42. EXPR_ID_DATA__REF_VALUE,
  43. } kind;
  44. };
  45. static size_t key_hash(long key, void *ctx __maybe_unused)
  46. {
  47. const char *str = (const char *)key;
  48. size_t hash = 0;
  49. while (*str != '\0') {
  50. hash *= 31;
  51. hash += *str;
  52. str++;
  53. }
  54. return hash;
  55. }
  56. static bool key_equal(long key1, long key2, void *ctx __maybe_unused)
  57. {
  58. return !strcmp((const char *)key1, (const char *)key2);
  59. }
  60. struct hashmap *ids__new(void)
  61. {
  62. struct hashmap *hash;
  63. hash = hashmap__new(key_hash, key_equal, NULL);
  64. if (IS_ERR(hash))
  65. return NULL;
  66. return hash;
  67. }
  68. void ids__free(struct hashmap *ids)
  69. {
  70. struct hashmap_entry *cur;
  71. size_t bkt;
  72. if (ids == NULL)
  73. return;
  74. hashmap__for_each_entry(ids, cur, bkt) {
  75. zfree(&cur->pkey);
  76. zfree(&cur->pvalue);
  77. }
  78. hashmap__free(ids);
  79. }
  80. int ids__insert(struct hashmap *ids, const char *id)
  81. {
  82. struct expr_id_data *data_ptr = NULL, *old_data = NULL;
  83. char *old_key = NULL;
  84. int ret;
  85. ret = hashmap__set(ids, id, data_ptr, &old_key, &old_data);
  86. if (ret)
  87. free(data_ptr);
  88. free(old_key);
  89. free(old_data);
  90. return ret;
  91. }
  92. struct hashmap *ids__union(struct hashmap *ids1, struct hashmap *ids2)
  93. {
  94. size_t bkt;
  95. struct hashmap_entry *cur;
  96. int ret;
  97. struct expr_id_data *old_data = NULL;
  98. char *old_key = NULL;
  99. if (!ids1)
  100. return ids2;
  101. if (!ids2)
  102. return ids1;
  103. if (hashmap__size(ids1) < hashmap__size(ids2)) {
  104. struct hashmap *tmp = ids1;
  105. ids1 = ids2;
  106. ids2 = tmp;
  107. }
  108. hashmap__for_each_entry(ids2, cur, bkt) {
  109. ret = hashmap__set(ids1, cur->key, cur->value, &old_key, &old_data);
  110. free(old_key);
  111. free(old_data);
  112. if (ret) {
  113. hashmap__free(ids1);
  114. hashmap__free(ids2);
  115. return NULL;
  116. }
  117. }
  118. hashmap__free(ids2);
  119. return ids1;
  120. }
  121. /* Caller must make sure id is allocated */
  122. int expr__add_id(struct expr_parse_ctx *ctx, const char *id)
  123. {
  124. return ids__insert(ctx->ids, id);
  125. }
  126. /* Caller must make sure id is allocated */
  127. int expr__add_id_val(struct expr_parse_ctx *ctx, const char *id, double val)
  128. {
  129. return expr__add_id_val_source_count(ctx, id, val, /*source_count=*/1);
  130. }
  131. /* Caller must make sure id is allocated */
  132. int expr__add_id_val_source_count(struct expr_parse_ctx *ctx, const char *id,
  133. double val, int source_count)
  134. {
  135. struct expr_id_data *data_ptr = NULL, *old_data = NULL;
  136. char *old_key = NULL;
  137. int ret;
  138. data_ptr = malloc(sizeof(*data_ptr));
  139. if (!data_ptr)
  140. return -ENOMEM;
  141. data_ptr->val.val = val;
  142. data_ptr->val.source_count = source_count;
  143. data_ptr->kind = EXPR_ID_DATA__VALUE;
  144. ret = hashmap__set(ctx->ids, id, data_ptr, &old_key, &old_data);
  145. if (ret) {
  146. free(data_ptr);
  147. } else if (old_data) {
  148. data_ptr->val.val += old_data->val.val;
  149. data_ptr->val.source_count += old_data->val.source_count;
  150. }
  151. free(old_key);
  152. free(old_data);
  153. return ret;
  154. }
  155. int expr__add_ref(struct expr_parse_ctx *ctx, struct metric_ref *ref)
  156. {
  157. struct expr_id_data *data_ptr = NULL, *old_data = NULL;
  158. char *old_key = NULL;
  159. char *name;
  160. int ret;
  161. data_ptr = zalloc(sizeof(*data_ptr));
  162. if (!data_ptr)
  163. return -ENOMEM;
  164. name = strdup(ref->metric_name);
  165. if (!name) {
  166. free(data_ptr);
  167. return -ENOMEM;
  168. }
  169. /*
  170. * Intentionally passing just const char pointers,
  171. * originally from 'struct pmu_event' object.
  172. * We don't need to change them, so there's no
  173. * need to create our own copy.
  174. */
  175. data_ptr->ref.metric_name = ref->metric_name;
  176. data_ptr->ref.metric_expr = ref->metric_expr;
  177. data_ptr->kind = EXPR_ID_DATA__REF;
  178. ret = hashmap__set(ctx->ids, name, data_ptr, &old_key, &old_data);
  179. if (ret)
  180. free(data_ptr);
  181. pr_debug2("adding ref metric %s: %s\n",
  182. ref->metric_name, ref->metric_expr);
  183. free(old_key);
  184. free(old_data);
  185. return ret;
  186. }
  187. int expr__get_id(struct expr_parse_ctx *ctx, const char *id,
  188. struct expr_id_data **data)
  189. {
  190. if (!ctx || !id)
  191. return -1;
  192. return hashmap__find(ctx->ids, id, data) ? 0 : -1;
  193. }
  194. bool expr__subset_of_ids(struct expr_parse_ctx *haystack,
  195. struct expr_parse_ctx *needles)
  196. {
  197. struct hashmap_entry *cur;
  198. size_t bkt;
  199. struct expr_id_data *data;
  200. hashmap__for_each_entry(needles->ids, cur, bkt) {
  201. if (expr__get_id(haystack, cur->pkey, &data))
  202. return false;
  203. }
  204. return true;
  205. }
  206. int expr__resolve_id(struct expr_parse_ctx *ctx, const char *id,
  207. struct expr_id_data **datap)
  208. {
  209. struct expr_id_data *data;
  210. if (expr__get_id(ctx, id, datap) || !*datap) {
  211. pr_debug("%s not found\n", id);
  212. return -1;
  213. }
  214. data = *datap;
  215. switch (data->kind) {
  216. case EXPR_ID_DATA__VALUE:
  217. pr_debug2("lookup(%s): val %f\n", id, data->val.val);
  218. break;
  219. case EXPR_ID_DATA__REF:
  220. pr_debug2("lookup(%s): ref metric name %s\n", id,
  221. data->ref.metric_name);
  222. pr_debug("processing metric: %s ENTRY\n", id);
  223. data->kind = EXPR_ID_DATA__REF_VALUE;
  224. if (expr__parse(&data->ref.val, ctx, data->ref.metric_expr)) {
  225. pr_debug("%s failed to count\n", id);
  226. return -1;
  227. }
  228. pr_debug("processing metric: %s EXIT: %f\n", id, data->ref.val);
  229. break;
  230. case EXPR_ID_DATA__REF_VALUE:
  231. pr_debug2("lookup(%s): ref val %f metric name %s\n", id,
  232. data->ref.val, data->ref.metric_name);
  233. break;
  234. default:
  235. assert(0); /* Unreachable. */
  236. }
  237. return 0;
  238. }
  239. void expr__del_id(struct expr_parse_ctx *ctx, const char *id)
  240. {
  241. struct expr_id_data *old_val = NULL;
  242. char *old_key = NULL;
  243. hashmap__delete(ctx->ids, id, &old_key, &old_val);
  244. free(old_key);
  245. free(old_val);
  246. }
  247. struct expr_parse_ctx *expr__ctx_new(void)
  248. {
  249. struct expr_parse_ctx *ctx;
  250. ctx = calloc(1, sizeof(struct expr_parse_ctx));
  251. if (!ctx)
  252. return NULL;
  253. ctx->ids = hashmap__new(key_hash, key_equal, NULL);
  254. if (IS_ERR(ctx->ids)) {
  255. free(ctx);
  256. return NULL;
  257. }
  258. return ctx;
  259. }
  260. void expr__ctx_clear(struct expr_parse_ctx *ctx)
  261. {
  262. struct hashmap_entry *cur;
  263. size_t bkt;
  264. hashmap__for_each_entry(ctx->ids, cur, bkt) {
  265. zfree(&cur->pkey);
  266. zfree(&cur->pvalue);
  267. }
  268. hashmap__clear(ctx->ids);
  269. }
  270. void expr__ctx_free(struct expr_parse_ctx *ctx)
  271. {
  272. struct hashmap_entry *cur;
  273. size_t bkt;
  274. if (!ctx)
  275. return;
  276. zfree(&ctx->sctx.user_requested_cpu_list);
  277. hashmap__for_each_entry(ctx->ids, cur, bkt) {
  278. zfree(&cur->pkey);
  279. zfree(&cur->pvalue);
  280. }
  281. hashmap__free(ctx->ids);
  282. free(ctx);
  283. }
  284. static int
  285. __expr__parse(double *val, struct expr_parse_ctx *ctx, const char *expr,
  286. bool compute_ids)
  287. {
  288. YY_BUFFER_STATE buffer;
  289. void *scanner;
  290. int ret;
  291. pr_debug2("parsing metric: %s\n", expr);
  292. ret = expr_lex_init_extra(&ctx->sctx, &scanner);
  293. if (ret)
  294. return ret;
  295. buffer = expr__scan_string(expr, scanner);
  296. #ifdef PARSER_DEBUG
  297. expr_debug = 1;
  298. expr_set_debug(1, scanner);
  299. #endif
  300. ret = expr_parse(val, ctx, compute_ids, scanner);
  301. expr__flush_buffer(buffer, scanner);
  302. expr__delete_buffer(buffer, scanner);
  303. expr_lex_destroy(scanner);
  304. return ret;
  305. }
  306. int expr__parse(double *final_val, struct expr_parse_ctx *ctx,
  307. const char *expr)
  308. {
  309. return __expr__parse(final_val, ctx, expr, /*compute_ids=*/false) ? -1 : 0;
  310. }
  311. int expr__find_ids(const char *expr, const char *one,
  312. struct expr_parse_ctx *ctx)
  313. {
  314. int ret = __expr__parse(NULL, ctx, expr, /*compute_ids=*/true);
  315. if (one)
  316. expr__del_id(ctx, one);
  317. return ret;
  318. }
  319. double expr_id_data__value(const struct expr_id_data *data)
  320. {
  321. if (data->kind == EXPR_ID_DATA__VALUE)
  322. return data->val.val;
  323. assert(data->kind == EXPR_ID_DATA__REF_VALUE);
  324. return data->ref.val;
  325. }
  326. double expr_id_data__source_count(const struct expr_id_data *data)
  327. {
  328. assert(data->kind == EXPR_ID_DATA__VALUE);
  329. return data->val.source_count;
  330. }
  331. double expr__get_literal(const char *literal, const struct expr_scanner_ctx *ctx)
  332. {
  333. double result = NAN;
  334. enum tool_pmu_event ev = tool_pmu__str_to_event(literal + 1);
  335. if (ev != TOOL_PMU__EVENT_NONE) {
  336. u64 count;
  337. if (tool_pmu__read_event(ev, /*evsel=*/NULL,
  338. ctx->system_wide, ctx->user_requested_cpu_list,
  339. &count))
  340. result = count;
  341. else
  342. pr_err("Failure to read '%s'", literal);
  343. } else {
  344. pr_err("Unrecognized literal '%s'", literal);
  345. }
  346. pr_debug2("literal: %s = %f\n", literal, result);
  347. return result;
  348. }
  349. /* Does the event 'id' parse? Determine via ctx->ids if possible. */
  350. double expr__has_event(const struct expr_parse_ctx *ctx, bool compute_ids, const char *id)
  351. {
  352. struct evlist *tmp;
  353. double ret;
  354. if (hashmap__find(ctx->ids, id, /*value=*/NULL))
  355. return 1.0;
  356. if (!compute_ids)
  357. return 0.0;
  358. tmp = evlist__new();
  359. if (!tmp)
  360. return NAN;
  361. if (strchr(id, '@')) {
  362. char *tmp_id, *p;
  363. tmp_id = strdup(id);
  364. if (!tmp_id) {
  365. ret = NAN;
  366. goto out;
  367. }
  368. p = strchr(tmp_id, '@');
  369. *p = '/';
  370. p = strrchr(tmp_id, '@');
  371. *p = '/';
  372. ret = parse_event(tmp, tmp_id) ? 0 : 1;
  373. free(tmp_id);
  374. } else {
  375. ret = parse_event(tmp, id) ? 0 : 1;
  376. }
  377. out:
  378. evlist__delete(tmp);
  379. return ret;
  380. }
  381. double expr__strcmp_cpuid_str(const struct expr_parse_ctx *ctx __maybe_unused,
  382. bool compute_ids __maybe_unused, const char *test_id)
  383. {
  384. double ret;
  385. struct perf_cpu cpu = {-1};
  386. char *cpuid = get_cpuid_allow_env_override(cpu);
  387. if (!cpuid)
  388. return NAN;
  389. ret = !strcmp_cpuid_str(test_id, cpuid);
  390. free(cpuid);
  391. return ret;
  392. }