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 "cpumap.h" 9 #include "cputopo.h" 10 #include "debug.h" 11 #include "expr.h" 12 #include "expr-bison.h" 13 #include "expr-flex.h" 14 #include "smt.h" 15 #include "tsc.h" 16 #include <linux/err.h> 17 #include <linux/kernel.h> 18 #include <linux/zalloc.h> 19 #include <ctype.h> 20 #include <math.h> 21 22 #ifdef PARSER_DEBUG 23 extern int expr_debug; 24 #endif 25 26 struct expr_id_data { 27 union { 28 struct { 29 double val; 30 int source_count; 31 } val; 32 struct { 33 double val; 34 const char *metric_name; 35 const char *metric_expr; 36 } ref; 37 }; 38 39 enum { 40 /* Holding a double value. */ 41 EXPR_ID_DATA__VALUE, 42 /* Reference to another metric. */ 43 EXPR_ID_DATA__REF, 44 /* A reference but the value has been computed. */ 45 EXPR_ID_DATA__REF_VALUE, 46 } kind; 47 }; 48 49 static size_t key_hash(long key, void *ctx __maybe_unused) 50 { 51 const char *str = (const char *)key; 52 size_t hash = 0; 53 54 while (*str != '\0') { 55 hash *= 31; 56 hash += *str; 57 str++; 58 } 59 return hash; 60 } 61 62 static bool key_equal(long key1, long key2, void *ctx __maybe_unused) 63 { 64 return !strcmp((const char *)key1, (const char *)key2); 65 } 66 67 struct hashmap *ids__new(void) 68 { 69 struct hashmap *hash; 70 71 hash = hashmap__new(key_hash, key_equal, NULL); 72 if (IS_ERR(hash)) 73 return NULL; 74 return hash; 75 } 76 77 void ids__free(struct hashmap *ids) 78 { 79 struct hashmap_entry *cur; 80 size_t bkt; 81 82 if (ids == NULL) 83 return; 84 85 hashmap__for_each_entry(ids, cur, bkt) { 86 free((void *)cur->pkey); 87 free((void *)cur->pvalue); 88 } 89 90 hashmap__free(ids); 91 } 92 93 int ids__insert(struct hashmap *ids, const char *id) 94 { 95 struct expr_id_data *data_ptr = NULL, *old_data = NULL; 96 char *old_key = NULL; 97 int ret; 98 99 ret = hashmap__set(ids, id, data_ptr, &old_key, &old_data); 100 if (ret) 101 free(data_ptr); 102 free(old_key); 103 free(old_data); 104 return ret; 105 } 106 107 struct hashmap *ids__union(struct hashmap *ids1, struct hashmap *ids2) 108 { 109 size_t bkt; 110 struct hashmap_entry *cur; 111 int ret; 112 struct expr_id_data *old_data = NULL; 113 char *old_key = NULL; 114 115 if (!ids1) 116 return ids2; 117 118 if (!ids2) 119 return ids1; 120 121 if (hashmap__size(ids1) < hashmap__size(ids2)) { 122 struct hashmap *tmp = ids1; 123 124 ids1 = ids2; 125 ids2 = tmp; 126 } 127 hashmap__for_each_entry(ids2, cur, bkt) { 128 ret = hashmap__set(ids1, cur->key, cur->value, &old_key, &old_data); 129 free(old_key); 130 free(old_data); 131 132 if (ret) { 133 hashmap__free(ids1); 134 hashmap__free(ids2); 135 return NULL; 136 } 137 } 138 hashmap__free(ids2); 139 return ids1; 140 } 141 142 /* Caller must make sure id is allocated */ 143 int expr__add_id(struct expr_parse_ctx *ctx, const char *id) 144 { 145 return ids__insert(ctx->ids, id); 146 } 147 148 /* Caller must make sure id is allocated */ 149 int expr__add_id_val(struct expr_parse_ctx *ctx, const char *id, double val) 150 { 151 return expr__add_id_val_source_count(ctx, id, val, /*source_count=*/1); 152 } 153 154 /* Caller must make sure id is allocated */ 155 int expr__add_id_val_source_count(struct expr_parse_ctx *ctx, const char *id, 156 double val, int source_count) 157 { 158 struct expr_id_data *data_ptr = NULL, *old_data = NULL; 159 char *old_key = NULL; 160 int ret; 161 162 data_ptr = malloc(sizeof(*data_ptr)); 163 if (!data_ptr) 164 return -ENOMEM; 165 data_ptr->val.val = val; 166 data_ptr->val.source_count = source_count; 167 data_ptr->kind = EXPR_ID_DATA__VALUE; 168 169 ret = hashmap__set(ctx->ids, id, data_ptr, &old_key, &old_data); 170 if (ret) 171 free(data_ptr); 172 free(old_key); 173 free(old_data); 174 return ret; 175 } 176 177 int expr__add_ref(struct expr_parse_ctx *ctx, struct metric_ref *ref) 178 { 179 struct expr_id_data *data_ptr = NULL, *old_data = NULL; 180 char *old_key = NULL; 181 char *name; 182 int ret; 183 184 data_ptr = zalloc(sizeof(*data_ptr)); 185 if (!data_ptr) 186 return -ENOMEM; 187 188 name = strdup(ref->metric_name); 189 if (!name) { 190 free(data_ptr); 191 return -ENOMEM; 192 } 193 194 /* 195 * Intentionally passing just const char pointers, 196 * originally from 'struct pmu_event' object. 197 * We don't need to change them, so there's no 198 * need to create our own copy. 199 */ 200 data_ptr->ref.metric_name = ref->metric_name; 201 data_ptr->ref.metric_expr = ref->metric_expr; 202 data_ptr->kind = EXPR_ID_DATA__REF; 203 204 ret = hashmap__set(ctx->ids, name, data_ptr, &old_key, &old_data); 205 if (ret) 206 free(data_ptr); 207 208 pr_debug2("adding ref metric %s: %s\n", 209 ref->metric_name, ref->metric_expr); 210 211 free(old_key); 212 free(old_data); 213 return ret; 214 } 215 216 int expr__get_id(struct expr_parse_ctx *ctx, const char *id, 217 struct expr_id_data **data) 218 { 219 return hashmap__find(ctx->ids, id, data) ? 0 : -1; 220 } 221 222 bool expr__subset_of_ids(struct expr_parse_ctx *haystack, 223 struct expr_parse_ctx *needles) 224 { 225 struct hashmap_entry *cur; 226 size_t bkt; 227 struct expr_id_data *data; 228 229 hashmap__for_each_entry(needles->ids, cur, bkt) { 230 if (expr__get_id(haystack, cur->pkey, &data)) 231 return false; 232 } 233 return true; 234 } 235 236 237 int expr__resolve_id(struct expr_parse_ctx *ctx, const char *id, 238 struct expr_id_data **datap) 239 { 240 struct expr_id_data *data; 241 242 if (expr__get_id(ctx, id, datap) || !*datap) { 243 pr_debug("%s not found\n", id); 244 return -1; 245 } 246 247 data = *datap; 248 249 switch (data->kind) { 250 case EXPR_ID_DATA__VALUE: 251 pr_debug2("lookup(%s): val %f\n", id, data->val.val); 252 break; 253 case EXPR_ID_DATA__REF: 254 pr_debug2("lookup(%s): ref metric name %s\n", id, 255 data->ref.metric_name); 256 pr_debug("processing metric: %s ENTRY\n", id); 257 data->kind = EXPR_ID_DATA__REF_VALUE; 258 if (expr__parse(&data->ref.val, ctx, data->ref.metric_expr)) { 259 pr_debug("%s failed to count\n", id); 260 return -1; 261 } 262 pr_debug("processing metric: %s EXIT: %f\n", id, data->ref.val); 263 break; 264 case EXPR_ID_DATA__REF_VALUE: 265 pr_debug2("lookup(%s): ref val %f metric name %s\n", id, 266 data->ref.val, data->ref.metric_name); 267 break; 268 default: 269 assert(0); /* Unreachable. */ 270 } 271 272 return 0; 273 } 274 275 void expr__del_id(struct expr_parse_ctx *ctx, const char *id) 276 { 277 struct expr_id_data *old_val = NULL; 278 char *old_key = NULL; 279 280 hashmap__delete(ctx->ids, id, &old_key, &old_val); 281 free(old_key); 282 free(old_val); 283 } 284 285 struct expr_parse_ctx *expr__ctx_new(void) 286 { 287 struct expr_parse_ctx *ctx; 288 289 ctx = malloc(sizeof(struct expr_parse_ctx)); 290 if (!ctx) 291 return NULL; 292 293 ctx->ids = hashmap__new(key_hash, key_equal, NULL); 294 if (IS_ERR(ctx->ids)) { 295 free(ctx); 296 return NULL; 297 } 298 ctx->sctx.user_requested_cpu_list = NULL; 299 ctx->sctx.runtime = 0; 300 ctx->sctx.system_wide = false; 301 302 return ctx; 303 } 304 305 void expr__ctx_clear(struct expr_parse_ctx *ctx) 306 { 307 struct hashmap_entry *cur; 308 size_t bkt; 309 310 hashmap__for_each_entry(ctx->ids, cur, bkt) { 311 free((void *)cur->pkey); 312 free(cur->pvalue); 313 } 314 hashmap__clear(ctx->ids); 315 } 316 317 void expr__ctx_free(struct expr_parse_ctx *ctx) 318 { 319 struct hashmap_entry *cur; 320 size_t bkt; 321 322 if (!ctx) 323 return; 324 325 free(ctx->sctx.user_requested_cpu_list); 326 hashmap__for_each_entry(ctx->ids, cur, bkt) { 327 free((void *)cur->pkey); 328 free(cur->pvalue); 329 } 330 hashmap__free(ctx->ids); 331 free(ctx); 332 } 333 334 static int 335 __expr__parse(double *val, struct expr_parse_ctx *ctx, const char *expr, 336 bool compute_ids) 337 { 338 YY_BUFFER_STATE buffer; 339 void *scanner; 340 int ret; 341 342 pr_debug2("parsing metric: %s\n", expr); 343 344 ret = expr_lex_init_extra(&ctx->sctx, &scanner); 345 if (ret) 346 return ret; 347 348 buffer = expr__scan_string(expr, scanner); 349 350 #ifdef PARSER_DEBUG 351 expr_debug = 1; 352 expr_set_debug(1, scanner); 353 #endif 354 355 ret = expr_parse(val, ctx, compute_ids, scanner); 356 357 expr__flush_buffer(buffer, scanner); 358 expr__delete_buffer(buffer, scanner); 359 expr_lex_destroy(scanner); 360 return ret; 361 } 362 363 int expr__parse(double *final_val, struct expr_parse_ctx *ctx, 364 const char *expr) 365 { 366 return __expr__parse(final_val, ctx, expr, /*compute_ids=*/false) ? -1 : 0; 367 } 368 369 int expr__find_ids(const char *expr, const char *one, 370 struct expr_parse_ctx *ctx) 371 { 372 int ret = __expr__parse(NULL, ctx, expr, /*compute_ids=*/true); 373 374 if (one) 375 expr__del_id(ctx, one); 376 377 return ret; 378 } 379 380 double expr_id_data__value(const struct expr_id_data *data) 381 { 382 if (data->kind == EXPR_ID_DATA__VALUE) 383 return data->val.val; 384 assert(data->kind == EXPR_ID_DATA__REF_VALUE); 385 return data->ref.val; 386 } 387 388 double expr_id_data__source_count(const struct expr_id_data *data) 389 { 390 assert(data->kind == EXPR_ID_DATA__VALUE); 391 return data->val.source_count; 392 } 393 394 #if !defined(__i386__) && !defined(__x86_64__) 395 double arch_get_tsc_freq(void) 396 { 397 return 0.0; 398 } 399 #endif 400 401 double expr__get_literal(const char *literal, const struct expr_scanner_ctx *ctx) 402 { 403 static struct cpu_topology *topology; 404 double result = NAN; 405 406 if (!strcmp("#num_cpus", literal)) { 407 result = cpu__max_present_cpu().cpu; 408 goto out; 409 } 410 411 if (!strcasecmp("#system_tsc_freq", literal)) { 412 result = arch_get_tsc_freq(); 413 goto out; 414 } 415 416 /* 417 * Assume that topology strings are consistent, such as CPUs "0-1" 418 * wouldn't be listed as "0,1", and so after deduplication the number of 419 * these strings gives an indication of the number of packages, dies, 420 * etc. 421 */ 422 if (!topology) { 423 topology = cpu_topology__new(); 424 if (!topology) { 425 pr_err("Error creating CPU topology"); 426 goto out; 427 } 428 } 429 if (!strcasecmp("#smt_on", literal)) { 430 result = smt_on(topology) ? 1.0 : 0.0; 431 goto out; 432 } 433 if (!strcmp("#core_wide", literal)) { 434 result = core_wide(ctx->system_wide, ctx->user_requested_cpu_list, topology) 435 ? 1.0 : 0.0; 436 goto out; 437 } 438 if (!strcmp("#num_packages", literal)) { 439 result = topology->package_cpus_lists; 440 goto out; 441 } 442 if (!strcmp("#num_dies", literal)) { 443 result = topology->die_cpus_lists; 444 goto out; 445 } 446 if (!strcmp("#num_cores", literal)) { 447 result = topology->core_cpus_lists; 448 goto out; 449 } 450 451 pr_err("Unrecognized literal '%s'", literal); 452 out: 453 pr_debug2("literal: %s = %f\n", literal, result); 454 return result; 455 } 456