expr.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 "expr.h"
  10. #include "expr-bison.h"
  11. #include "expr-flex.h"
  12. #include <linux/kernel.h>
  13. #include <linux/zalloc.h>
  14. #include <ctype.h>
  15. #ifdef PARSER_DEBUG
  16. extern int expr_debug;
  17. #endif
  18. static size_t key_hash(const void *key, void *ctx __maybe_unused)
  19. {
  20. const char *str = (const char *)key;
  21. size_t hash = 0;
  22. while (*str != '\0') {
  23. hash *= 31;
  24. hash += *str;
  25. str++;
  26. }
  27. return hash;
  28. }
  29. static bool key_equal(const void *key1, const void *key2,
  30. void *ctx __maybe_unused)
  31. {
  32. return !strcmp((const char *)key1, (const char *)key2);
  33. }
  34. /* Caller must make sure id is allocated */
  35. int expr__add_id(struct expr_parse_ctx *ctx, const char *id)
  36. {
  37. struct expr_id_data *data_ptr = NULL, *old_data = NULL;
  38. char *old_key = NULL;
  39. int ret;
  40. data_ptr = malloc(sizeof(*data_ptr));
  41. if (!data_ptr)
  42. return -ENOMEM;
  43. data_ptr->parent = ctx->parent;
  44. ret = hashmap__set(&ctx->ids, id, data_ptr,
  45. (const void **)&old_key, (void **)&old_data);
  46. if (ret)
  47. free(data_ptr);
  48. free(old_key);
  49. free(old_data);
  50. return ret;
  51. }
  52. /* Caller must make sure id is allocated */
  53. int expr__add_id_val(struct expr_parse_ctx *ctx, const char *id, double val)
  54. {
  55. struct expr_id_data *data_ptr = NULL, *old_data = NULL;
  56. char *old_key = NULL;
  57. int ret;
  58. data_ptr = malloc(sizeof(*data_ptr));
  59. if (!data_ptr)
  60. return -ENOMEM;
  61. data_ptr->val = val;
  62. data_ptr->is_ref = false;
  63. ret = hashmap__set(&ctx->ids, id, data_ptr,
  64. (const void **)&old_key, (void **)&old_data);
  65. if (ret)
  66. free(data_ptr);
  67. free(old_key);
  68. free(old_data);
  69. return ret;
  70. }
  71. int expr__add_ref(struct expr_parse_ctx *ctx, struct metric_ref *ref)
  72. {
  73. struct expr_id_data *data_ptr = NULL, *old_data = NULL;
  74. char *old_key = NULL;
  75. char *name, *p;
  76. int ret;
  77. data_ptr = zalloc(sizeof(*data_ptr));
  78. if (!data_ptr)
  79. return -ENOMEM;
  80. name = strdup(ref->metric_name);
  81. if (!name) {
  82. free(data_ptr);
  83. return -ENOMEM;
  84. }
  85. /*
  86. * The jevents tool converts all metric expressions
  87. * to lowercase, including metric references, hence
  88. * we need to add lowercase name for metric, so it's
  89. * properly found.
  90. */
  91. for (p = name; *p; p++)
  92. *p = tolower(*p);
  93. /*
  94. * Intentionally passing just const char pointers,
  95. * originally from 'struct pmu_event' object.
  96. * We don't need to change them, so there's no
  97. * need to create our own copy.
  98. */
  99. data_ptr->ref.metric_name = ref->metric_name;
  100. data_ptr->ref.metric_expr = ref->metric_expr;
  101. data_ptr->ref.counted = false;
  102. data_ptr->is_ref = true;
  103. ret = hashmap__set(&ctx->ids, name, data_ptr,
  104. (const void **)&old_key, (void **)&old_data);
  105. if (ret)
  106. free(data_ptr);
  107. pr_debug2("adding ref metric %s: %s\n",
  108. ref->metric_name, ref->metric_expr);
  109. free(old_key);
  110. free(old_data);
  111. return ret;
  112. }
  113. int expr__get_id(struct expr_parse_ctx *ctx, const char *id,
  114. struct expr_id_data **data)
  115. {
  116. return hashmap__find(&ctx->ids, id, (void **)data) ? 0 : -1;
  117. }
  118. int expr__resolve_id(struct expr_parse_ctx *ctx, const char *id,
  119. struct expr_id_data **datap)
  120. {
  121. struct expr_id_data *data;
  122. if (expr__get_id(ctx, id, datap) || !*datap) {
  123. pr_debug("%s not found\n", id);
  124. return -1;
  125. }
  126. data = *datap;
  127. pr_debug2("lookup: is_ref %d, counted %d, val %f: %s\n",
  128. data->is_ref, data->ref.counted, data->val, id);
  129. if (data->is_ref && !data->ref.counted) {
  130. data->ref.counted = true;
  131. pr_debug("processing metric: %s ENTRY\n", id);
  132. if (expr__parse(&data->val, ctx, data->ref.metric_expr, 1)) {
  133. pr_debug("%s failed to count\n", id);
  134. return -1;
  135. }
  136. pr_debug("processing metric: %s EXIT: %f\n", id, data->val);
  137. }
  138. return 0;
  139. }
  140. void expr__del_id(struct expr_parse_ctx *ctx, const char *id)
  141. {
  142. struct expr_id_data *old_val = NULL;
  143. char *old_key = NULL;
  144. hashmap__delete(&ctx->ids, id,
  145. (const void **)&old_key, (void **)&old_val);
  146. free(old_key);
  147. free(old_val);
  148. }
  149. void expr__ctx_init(struct expr_parse_ctx *ctx)
  150. {
  151. hashmap__init(&ctx->ids, key_hash, key_equal, NULL);
  152. }
  153. void expr__ctx_clear(struct expr_parse_ctx *ctx)
  154. {
  155. struct hashmap_entry *cur;
  156. size_t bkt;
  157. hashmap__for_each_entry((&ctx->ids), cur, bkt) {
  158. free((char *)cur->key);
  159. free(cur->value);
  160. }
  161. hashmap__clear(&ctx->ids);
  162. }
  163. static int
  164. __expr__parse(double *val, struct expr_parse_ctx *ctx, const char *expr,
  165. int start, int runtime)
  166. {
  167. struct expr_scanner_ctx scanner_ctx = {
  168. .start_token = start,
  169. .runtime = runtime,
  170. };
  171. YY_BUFFER_STATE buffer;
  172. void *scanner;
  173. int ret;
  174. pr_debug2("parsing metric: %s\n", expr);
  175. ret = expr_lex_init_extra(&scanner_ctx, &scanner);
  176. if (ret)
  177. return ret;
  178. buffer = expr__scan_string(expr, scanner);
  179. #ifdef PARSER_DEBUG
  180. expr_debug = 1;
  181. expr_set_debug(1, scanner);
  182. #endif
  183. ret = expr_parse(val, ctx, scanner);
  184. expr__flush_buffer(buffer, scanner);
  185. expr__delete_buffer(buffer, scanner);
  186. expr_lex_destroy(scanner);
  187. return ret;
  188. }
  189. int expr__parse(double *final_val, struct expr_parse_ctx *ctx,
  190. const char *expr, int runtime)
  191. {
  192. return __expr__parse(final_val, ctx, expr, EXPR_PARSE, runtime) ? -1 : 0;
  193. }
  194. int expr__find_other(const char *expr, const char *one,
  195. struct expr_parse_ctx *ctx, int runtime)
  196. {
  197. int ret = __expr__parse(NULL, ctx, expr, EXPR_OTHER, runtime);
  198. if (one)
  199. expr__del_id(ctx, one);
  200. return ret;
  201. }