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