1 2 #include <linux/list.h> 3 #include <sys/types.h> 4 #include <sys/stat.h> 5 #include <unistd.h> 6 #include <stdio.h> 7 #include <dirent.h> 8 #include "sysfs.h" 9 #include "util.h" 10 #include "pmu.h" 11 #include "parse-events.h" 12 13 int perf_pmu_parse(struct list_head *list, char *name); 14 extern FILE *perf_pmu_in; 15 16 static LIST_HEAD(pmus); 17 18 /* 19 * Parse & process all the sysfs attributes located under 20 * the directory specified in 'dir' parameter. 21 */ 22 static int pmu_format_parse(char *dir, struct list_head *head) 23 { 24 struct dirent *evt_ent; 25 DIR *format_dir; 26 int ret = 0; 27 28 format_dir = opendir(dir); 29 if (!format_dir) 30 return -EINVAL; 31 32 while (!ret && (evt_ent = readdir(format_dir))) { 33 char path[PATH_MAX]; 34 char *name = evt_ent->d_name; 35 FILE *file; 36 37 if (!strcmp(name, ".") || !strcmp(name, "..")) 38 continue; 39 40 snprintf(path, PATH_MAX, "%s/%s", dir, name); 41 42 ret = -EINVAL; 43 file = fopen(path, "r"); 44 if (!file) 45 break; 46 47 perf_pmu_in = file; 48 ret = perf_pmu_parse(head, name); 49 fclose(file); 50 } 51 52 closedir(format_dir); 53 return ret; 54 } 55 56 /* 57 * Reading/parsing the default pmu format definition, which should be 58 * located at: 59 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes. 60 */ 61 static int pmu_format(char *name, struct list_head *format) 62 { 63 struct stat st; 64 char path[PATH_MAX]; 65 const char *sysfs; 66 67 sysfs = sysfs_find_mountpoint(); 68 if (!sysfs) 69 return -1; 70 71 snprintf(path, PATH_MAX, 72 "%s/bus/event_source/devices/%s/format", sysfs, name); 73 74 if (stat(path, &st) < 0) 75 return -1; 76 77 if (pmu_format_parse(path, format)) 78 return -1; 79 80 return 0; 81 } 82 83 /* 84 * Reading/parsing the default pmu type value, which should be 85 * located at: 86 * /sys/bus/event_source/devices/<dev>/type as sysfs attribute. 87 */ 88 static int pmu_type(char *name, __u32 *type) 89 { 90 struct stat st; 91 char path[PATH_MAX]; 92 const char *sysfs; 93 FILE *file; 94 int ret = 0; 95 96 sysfs = sysfs_find_mountpoint(); 97 if (!sysfs) 98 return -1; 99 100 snprintf(path, PATH_MAX, 101 "%s/bus/event_source/devices/%s/type", sysfs, name); 102 103 if (stat(path, &st) < 0) 104 return -1; 105 106 file = fopen(path, "r"); 107 if (!file) 108 return -EINVAL; 109 110 if (1 != fscanf(file, "%u", type)) 111 ret = -1; 112 113 fclose(file); 114 return ret; 115 } 116 117 static struct perf_pmu *pmu_lookup(char *name) 118 { 119 struct perf_pmu *pmu; 120 LIST_HEAD(format); 121 __u32 type; 122 123 /* 124 * The pmu data we store & need consists of the pmu 125 * type value and format definitions. Load both right 126 * now. 127 */ 128 if (pmu_format(name, &format)) 129 return NULL; 130 131 if (pmu_type(name, &type)) 132 return NULL; 133 134 pmu = zalloc(sizeof(*pmu)); 135 if (!pmu) 136 return NULL; 137 138 INIT_LIST_HEAD(&pmu->format); 139 list_splice(&format, &pmu->format); 140 pmu->name = strdup(name); 141 pmu->type = type; 142 return pmu; 143 } 144 145 static struct perf_pmu *pmu_find(char *name) 146 { 147 struct perf_pmu *pmu; 148 149 list_for_each_entry(pmu, &pmus, list) 150 if (!strcmp(pmu->name, name)) 151 return pmu; 152 153 return NULL; 154 } 155 156 struct perf_pmu *perf_pmu__find(char *name) 157 { 158 struct perf_pmu *pmu; 159 160 /* 161 * Once PMU is loaded it stays in the list, 162 * so we keep us from multiple reading/parsing 163 * the pmu format definitions. 164 */ 165 pmu = pmu_find(name); 166 if (pmu) 167 return pmu; 168 169 return pmu_lookup(name); 170 } 171 172 static struct perf_pmu__format* 173 pmu_find_format(struct list_head *formats, char *name) 174 { 175 struct perf_pmu__format *format; 176 177 list_for_each_entry(format, formats, list) 178 if (!strcmp(format->name, name)) 179 return format; 180 181 return NULL; 182 } 183 184 /* 185 * Returns value based on the format definition (format parameter) 186 * and unformated value (value parameter). 187 * 188 * TODO maybe optimize a little ;) 189 */ 190 static __u64 pmu_format_value(unsigned long *format, __u64 value) 191 { 192 unsigned long fbit, vbit; 193 __u64 v = 0; 194 195 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) { 196 197 if (!test_bit(fbit, format)) 198 continue; 199 200 if (!(value & (1llu << vbit++))) 201 continue; 202 203 v |= (1llu << fbit); 204 } 205 206 return v; 207 } 208 209 /* 210 * Setup one of config[12] attr members based on the 211 * user input data - temr parameter. 212 */ 213 static int pmu_config_term(struct list_head *formats, 214 struct perf_event_attr *attr, 215 struct parse_events__term *term) 216 { 217 struct perf_pmu__format *format; 218 __u64 *vp; 219 220 /* 221 * Support only for hardcoded and numnerial terms. 222 * Hardcoded terms should be already in, so nothing 223 * to be done for them. 224 */ 225 if (parse_events__is_hardcoded_term(term)) 226 return 0; 227 228 if (term->type_val != PARSE_EVENTS__TERM_TYPE_NUM) 229 return -EINVAL; 230 231 format = pmu_find_format(formats, term->config); 232 if (!format) 233 return -EINVAL; 234 235 switch (format->value) { 236 case PERF_PMU_FORMAT_VALUE_CONFIG: 237 vp = &attr->config; 238 break; 239 case PERF_PMU_FORMAT_VALUE_CONFIG1: 240 vp = &attr->config1; 241 break; 242 case PERF_PMU_FORMAT_VALUE_CONFIG2: 243 vp = &attr->config2; 244 break; 245 default: 246 return -EINVAL; 247 } 248 249 /* 250 * XXX If we ever decide to go with string values for 251 * non-hardcoded terms, here's the place to translate 252 * them into value. 253 */ 254 *vp |= pmu_format_value(format->bits, term->val.num); 255 return 0; 256 } 257 258 static int pmu_config(struct list_head *formats, struct perf_event_attr *attr, 259 struct list_head *head_terms) 260 { 261 struct parse_events__term *term, *h; 262 263 list_for_each_entry_safe(term, h, head_terms, list) 264 if (pmu_config_term(formats, attr, term)) 265 return -EINVAL; 266 267 return 0; 268 } 269 270 /* 271 * Configures event's 'attr' parameter based on the: 272 * 1) users input - specified in terms parameter 273 * 2) pmu format definitions - specified by pmu parameter 274 */ 275 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr, 276 struct list_head *head_terms) 277 { 278 attr->type = pmu->type; 279 return pmu_config(&pmu->format, attr, head_terms); 280 } 281 282 int perf_pmu__new_format(struct list_head *list, char *name, 283 int config, unsigned long *bits) 284 { 285 struct perf_pmu__format *format; 286 287 format = zalloc(sizeof(*format)); 288 if (!format) 289 return -ENOMEM; 290 291 format->name = strdup(name); 292 format->value = config; 293 memcpy(format->bits, bits, sizeof(format->bits)); 294 295 list_add_tail(&format->list, list); 296 return 0; 297 } 298 299 void perf_pmu__set_format(unsigned long *bits, long from, long to) 300 { 301 long b; 302 303 if (!to) 304 to = from; 305 306 memset(bits, 0, BITS_TO_LONGS(PERF_PMU_FORMAT_BITS)); 307 for (b = from; b <= to; b++) 308 set_bit(b, bits); 309 } 310 311 /* Simulated format definitions. */ 312 static struct test_format { 313 const char *name; 314 const char *value; 315 } test_formats[] = { 316 { "krava01", "config:0-1,62-63\n", }, 317 { "krava02", "config:10-17\n", }, 318 { "krava03", "config:5\n", }, 319 { "krava11", "config1:0,2,4,6,8,20-28\n", }, 320 { "krava12", "config1:63\n", }, 321 { "krava13", "config1:45-47\n", }, 322 { "krava21", "config2:0-3,10-13,20-23,30-33,40-43,50-53,60-63\n", }, 323 { "krava22", "config2:8,18,48,58\n", }, 324 { "krava23", "config2:28-29,38\n", }, 325 }; 326 327 #define TEST_FORMATS_CNT (sizeof(test_formats) / sizeof(struct test_format)) 328 329 /* Simulated users input. */ 330 static struct parse_events__term test_terms[] = { 331 { 332 .config = (char *) "krava01", 333 .val.num = 15, 334 .type_val = PARSE_EVENTS__TERM_TYPE_NUM, 335 .type_term = PARSE_EVENTS__TERM_TYPE_USER, 336 }, 337 { 338 .config = (char *) "krava02", 339 .val.num = 170, 340 .type_val = PARSE_EVENTS__TERM_TYPE_NUM, 341 .type_term = PARSE_EVENTS__TERM_TYPE_USER, 342 }, 343 { 344 .config = (char *) "krava03", 345 .val.num = 1, 346 .type_val = PARSE_EVENTS__TERM_TYPE_NUM, 347 .type_term = PARSE_EVENTS__TERM_TYPE_USER, 348 }, 349 { 350 .config = (char *) "krava11", 351 .val.num = 27, 352 .type_val = PARSE_EVENTS__TERM_TYPE_NUM, 353 .type_term = PARSE_EVENTS__TERM_TYPE_USER, 354 }, 355 { 356 .config = (char *) "krava12", 357 .val.num = 1, 358 .type_val = PARSE_EVENTS__TERM_TYPE_NUM, 359 .type_term = PARSE_EVENTS__TERM_TYPE_USER, 360 }, 361 { 362 .config = (char *) "krava13", 363 .val.num = 2, 364 .type_val = PARSE_EVENTS__TERM_TYPE_NUM, 365 .type_term = PARSE_EVENTS__TERM_TYPE_USER, 366 }, 367 { 368 .config = (char *) "krava21", 369 .val.num = 119, 370 .type_val = PARSE_EVENTS__TERM_TYPE_NUM, 371 .type_term = PARSE_EVENTS__TERM_TYPE_USER, 372 }, 373 { 374 .config = (char *) "krava22", 375 .val.num = 11, 376 .type_val = PARSE_EVENTS__TERM_TYPE_NUM, 377 .type_term = PARSE_EVENTS__TERM_TYPE_USER, 378 }, 379 { 380 .config = (char *) "krava23", 381 .val.num = 2, 382 .type_val = PARSE_EVENTS__TERM_TYPE_NUM, 383 .type_term = PARSE_EVENTS__TERM_TYPE_USER, 384 }, 385 }; 386 #define TERMS_CNT (sizeof(test_terms) / sizeof(struct parse_events__term)) 387 388 /* 389 * Prepare format directory data, exported by kernel 390 * at /sys/bus/event_source/devices/<dev>/format. 391 */ 392 static char *test_format_dir_get(void) 393 { 394 static char dir[PATH_MAX]; 395 unsigned int i; 396 397 snprintf(dir, PATH_MAX, "/tmp/perf-pmu-test-format-XXXXXX"); 398 if (!mkdtemp(dir)) 399 return NULL; 400 401 for (i = 0; i < TEST_FORMATS_CNT; i++) { 402 static char name[PATH_MAX]; 403 struct test_format *format = &test_formats[i]; 404 FILE *file; 405 406 snprintf(name, PATH_MAX, "%s/%s", dir, format->name); 407 408 file = fopen(name, "w"); 409 if (!file) 410 return NULL; 411 412 if (1 != fwrite(format->value, strlen(format->value), 1, file)) 413 break; 414 415 fclose(file); 416 } 417 418 return dir; 419 } 420 421 /* Cleanup format directory. */ 422 static int test_format_dir_put(char *dir) 423 { 424 char buf[PATH_MAX]; 425 snprintf(buf, PATH_MAX, "rm -f %s/*\n", dir); 426 if (system(buf)) 427 return -1; 428 429 snprintf(buf, PATH_MAX, "rmdir %s\n", dir); 430 return system(buf); 431 } 432 433 static struct list_head *test_terms_list(void) 434 { 435 static LIST_HEAD(terms); 436 unsigned int i; 437 438 for (i = 0; i < TERMS_CNT; i++) 439 list_add_tail(&test_terms[i].list, &terms); 440 441 return &terms; 442 } 443 444 #undef TERMS_CNT 445 446 int perf_pmu__test(void) 447 { 448 char *format = test_format_dir_get(); 449 LIST_HEAD(formats); 450 struct list_head *terms = test_terms_list(); 451 int ret; 452 453 if (!format) 454 return -EINVAL; 455 456 do { 457 struct perf_event_attr attr; 458 459 memset(&attr, 0, sizeof(attr)); 460 461 ret = pmu_format_parse(format, &formats); 462 if (ret) 463 break; 464 465 ret = pmu_config(&formats, &attr, terms); 466 if (ret) 467 break; 468 469 ret = -EINVAL; 470 471 if (attr.config != 0xc00000000002a823) 472 break; 473 if (attr.config1 != 0x8000400000000145) 474 break; 475 if (attr.config2 != 0x0400000020041d07) 476 break; 477 478 ret = 0; 479 } while (0); 480 481 test_format_dir_put(format); 482 return ret; 483 } 484