1 #define _XOPEN_SOURCE 500 45 #include <sys/resource.h> 48 #include <linux/list.h> 67 ret = vfprintf(stderr, fmt, args);
82 unsigned int len = strlen(a) + 1 + strlen(sep);
83 int olen = *dst ? strlen(*dst) : 0;
87 out = realloc(*dst, len + olen + blen);
100 strncat(*dst, map + bt->
start, blen);
111 char *e = s + strlen(s);
130 for (p = s; *p; p++) {
140 fixed = (
char *)
malloc(len + 1);
146 for (p = s; *p; p++) {
162 {
"0x3F6",
"ldlat=" },
163 {
"0x1A6",
"offcore_rsp=" },
164 {
"0x1A7",
"offcore_rsp=" },
165 {
"0x3F7",
"frontend=" },
173 {
"UMask",
"umask=" },
174 {
"CounterMask",
"cmask=" },
175 {
"Invert",
"inv=" },
176 {
"AnyThread",
"any=" },
177 {
"EdgeDetect",
"edge=" },
178 {
"SampleAfterValue",
"period=" },
179 {
"FCMask",
"fc_mask=" },
180 {
"PortMask",
"ch_mask=" },
189 for (i = newval->
start; i < newval->end; i++) {
222 pr_err(
"%s: Unknown MSR in event file %.*s\n",
prog,
232 {
"CBO",
"uncore_cbox" },
233 {
"QPI LL",
"uncore_qpi" },
234 {
"SBO",
"uncore_sbox" },
235 {
"iMPH-U",
"uncore_arb" },
243 for (i = 0; table[i].
json; i++) {
245 return table[i].
perf;
250 #define EXPECT(e, t, m) do { if (!(e)) { \ 251 jsmntok_t *loc = (t); \ 252 if (!(t)->start && (t) > tokens) \ 254 pr_err("%s:%d: " m ", got %s\n", fn, \ 255 json_line(map, loc), \ 269 i = asprintf(&tp,
"%s", topic);
275 for (i = 0; i < (int) strlen(tp); i++) {
292 topic = strdup(bname);
294 pr_info(
"%s: strdup() error %s for file %s\n",
prog,
310 fprintf(fp,
"struct pmu_event %s[] = {\n", tblname);
315 char *
desc,
char *long_desc,
316 char *pmu,
char *unit,
char *perpkg,
318 char *metric_name,
char *metric_group)
322 char *topic = pd->
topic;
328 fprintf(outfp,
"{\n");
331 fprintf(outfp,
"\t.name = \"%s\",\n", name);
333 fprintf(outfp,
"\t.event = \"%s\",\n", event);
334 fprintf(outfp,
"\t.desc = \"%s\",\n", desc);
335 fprintf(outfp,
"\t.topic = \"%s\",\n", topic);
336 if (long_desc && long_desc[0])
337 fprintf(outfp,
"\t.long_desc = \"%s\",\n", long_desc);
339 fprintf(outfp,
"\t.pmu = \"%s\",\n", pmu);
341 fprintf(outfp,
"\t.unit = \"%s\",\n", unit);
343 fprintf(outfp,
"\t.perpkg = \"%s\",\n", perpkg);
345 fprintf(outfp,
"\t.metric_expr = \"%s\",\n", metric_expr);
347 fprintf(outfp,
"\t.metric_name = \"%s\",\n", metric_name);
349 fprintf(outfp,
"\t.metric_group = \"%s\",\n", metric_group);
350 fprintf(outfp,
"},\n");
356 struct list_head list;
369 #define ADD_EVENT_FIELD(field) do { if (field) { \ 370 es->field = strdup(field); \ 375 #define FREE_EVENT_FIELD(field) free(es->field) 377 #define TRY_FIXUP_FIELD(field) do { if (es->field && !*field) {\ 378 *field = strdup(es->field); \ 383 #define FOR_ALL_EVENT_STRUCT_FIELDS(op) do { \ 402 list_for_each_entry_safe(es, next, &arch_std_events,
list) {
415 struct stat *sb =
data;
420 memset(es, 0,
sizeof(*es));
422 list_add_tail(&es->
list, &arch_std_events);
432 fprintf(outfp,
"{\n");
434 fprintf(outfp,
"\t.name = 0,\n");
435 fprintf(outfp,
"\t.event = 0,\n");
436 fprintf(outfp,
"\t.desc = 0,\n");
438 fprintf(outfp,
"},\n");
439 fprintf(outfp,
"};\n");
447 {
"inst_retired.any",
"event=0xc0" },
448 {
"inst_retired.any_p",
"event=0xc0" },
449 {
"cpu_clk_unhalted.ref",
"event=0x0,umask=0x03" },
450 {
"cpu_clk_unhalted.thread",
"event=0x3c" },
451 {
"cpu_clk_unhalted.thread_any",
"event=0x3c,any=1" },
466 if (!strcasecmp(name,
fixed[i].name))
473 char **
name,
char **long_desc,
char **pmu,
char **
filter,
474 char **perpkg,
char **unit,
char **
metric_expr,
char **metric_name,
475 char **metric_group,
unsigned long long eventcode)
480 list_for_each_entry(es, &arch_std_events,
list) {
481 if (!strcmp(arch_std, es->
name)) {
482 if (!eventcode && es->
event) {
492 pr_err(
"%s: could not find matching %s for %s\n",
521 for (i = 0; i < tokens->
size; i++) {
522 char *
event = NULL, *desc = NULL, *name = NULL;
523 char *long_desc = NULL;
524 char *extra_desc = NULL;
529 char *metric_expr = NULL;
530 char *metric_name = NULL;
531 char *metric_group = NULL;
532 char *arch_std = NULL;
533 unsigned long long eventcode = 0;
534 struct msrmap *msr = NULL;
540 for (j = 0; j < obj->
size; j += 2) {
547 "Expected field name");
550 "Expected string value");
555 }
else if (
json_streq(map, field,
"EventCode")) {
558 eventcode |= strtoul(code, NULL, 0);
560 }
else if (
json_streq(map, field,
"ExtSel")) {
563 eventcode |= strtoul(code, NULL, 0) << 21;
565 }
else if (
json_streq(map, field,
"EventName")) {
567 }
else if (
json_streq(map, field,
"BriefDescription")) {
571 "PublicDescription")) {
572 addfield(map, &long_desc,
"",
"", val);
574 }
else if (
json_streq(map, field,
"PEBS") && nz) {
576 }
else if (
json_streq(map, field,
"MSRIndex") && nz) {
578 }
else if (
json_streq(map, field,
"MSRValue")) {
580 }
else if (
json_streq(map, field,
"Errata") &&
583 " Spec update: ", val);
584 }
else if (
json_streq(map, field,
"Data_LA") && nz) {
586 " Supports address when precise",
596 pmu = strdup(
"uncore_");
598 for (s = pmu; *s; s++)
601 addfield(map, &desc,
". ",
"Unit: ", NULL);
602 addfield(map, &desc,
"", pmu, NULL);
603 addfield(map, &desc,
"",
" ", NULL);
604 }
else if (
json_streq(map, field,
"Filter")) {
605 addfield(map, &filter,
"",
"", val);
606 }
else if (
json_streq(map, field,
"ScaleUnit")) {
608 }
else if (
json_streq(map, field,
"PerPkg")) {
609 addfield(map, &perpkg,
"",
"", val);
610 }
else if (
json_streq(map, field,
"MetricName")) {
611 addfield(map, &metric_name,
"",
"", val);
612 }
else if (
json_streq(map, field,
"MetricGroup")) {
613 addfield(map, &metric_group,
"",
"", val);
614 }
else if (
json_streq(map, field,
"MetricExpr")) {
615 addfield(map, &metric_expr,
"",
"", val);
616 for (s = metric_expr; *s; s++)
618 }
else if (
json_streq(map, field,
"ArchStdEvent")) {
619 addfield(map, &arch_std,
"",
"", val);
620 for (s = arch_std; *s; s++)
625 if (precise && desc && !strstr(desc,
"(Precise Event)")) {
628 "(Must be precise)", NULL);
631 "(Precise event)", NULL);
633 snprintf(buf,
sizeof buf,
"event=%#llx", eventcode);
634 addfield(map, &event,
",", buf, NULL);
635 if (desc && extra_desc)
636 addfield(map, &desc,
" ", extra_desc, NULL);
637 if (long_desc && extra_desc)
638 addfield(map, &long_desc,
" ", extra_desc, NULL);
640 addfield(map, &event,
",", filter, NULL);
651 err =
try_fixup(fn, arch_std, &event, &desc, &name,
652 &long_desc, &pmu, &filter, &perpkg,
653 &unit, &metric_expr, &metric_name,
654 &metric_group, eventcode);
659 pmu, unit, perpkg, metric_expr, metric_name, metric_group);
679 EXPECT(tok - tokens == len, tok,
"unexpected objects at end");
698 n = asprintf(&tblname,
"pme_%s", fname);
700 pr_info(
"%s: asprintf() error %s for file %s\n",
prog,
705 for (i = 0; i < strlen(tblname); i++) {
708 if (c ==
'-' || c ==
'/')
713 }
else if (!
isalnum(c) && c !=
'_') {
714 pr_err(
"%s: Invalid character '%c' in file name %s\n",
715 prog, c, basename(fname));
727 fprintf(outfp,
"struct pmu_events_map pmu_events_map[] = {\n");
735 fprintf(outfp,
"{\n");
736 fprintf(outfp,
"\t.cpuid = 0,\n");
737 fprintf(outfp,
"\t.version = 0,\n");
738 fprintf(outfp,
"\t.type = 0,\n");
739 fprintf(outfp,
"\t.table = 0,\n");
740 fprintf(outfp,
"},\n");
743 fprintf(outfp,
"};\n");
755 pr_info(
"%s: Processing mapfile %s\n",
prog, fpath);
761 mapfp = fopen(fpath,
"r");
771 p = fgets(line, n, mapfp);
780 p = fgets(line, n, mapfp);
784 if (line[0] ==
'#' || line[0] ==
'\n')
787 if (line[strlen(line)-1] !=
'\n') {
789 pr_info(
"%s: Mapfile %s: line %d too long, aborting\n",
790 prog, fpath, line_num);
793 line[strlen(line)-1] =
'\0';
795 cpuid =
fixregex(strtok_r(p,
",", &save));
796 version = strtok_r(NULL,
",", &save);
797 fname = strtok_r(NULL,
",", &save);
798 type = strtok_r(NULL,
",", &save);
801 fprintf(outfp,
"{\n");
802 fprintf(outfp,
"\t.cpuid = \"%s\",\n", cpuid);
803 fprintf(outfp,
"\t.version = \"%s\",\n", version);
804 fprintf(outfp,
"\t.type = \"%s\",\n", type);
815 fprintf(outfp,
"\t.table = %s\n", tblname);
816 fprintf(outfp,
"},\n");
833 pr_info(
"%s: Creating empty pmu_events_map[] table\n",
prog);
836 outfp = fopen(output_file,
"w");
842 fprintf(outfp,
"#include \"../../pmu-events/pmu-events.h\"\n");
852 if (getrlimit(RLIMIT_NOFILE, &rlim) == 0)
853 return min((
int)rlim.rlim_max / 2, 512);
875 while ((dir = readdir(d)) != NULL) {
876 if (!strcmp(dir->d_name,
".") || !strcmp(dir->d_name,
".."))
879 if (dir->d_type == DT_DIR) {
882 }
else if (dir->d_type == DT_UNKNOWN) {
886 sprintf(path,
"%s/%s", fpath, dir->d_name);
890 if (S_ISDIR(st.st_mode)) {
906 if (strlen(name) < 5)
909 suffix = name + strlen(name) - 5;
911 if (strncmp(suffix,
".json", 5) == 0)
917 int typeflag,
struct FTW *ftwbuf)
919 int level = ftwbuf->level;
920 int is_file = typeflag == FTW_F;
929 int typeflag,
struct FTW *ftwbuf)
931 char *tblname, *bname;
932 int is_dir = typeflag == FTW_D;
933 int is_file = typeflag == FTW_F;
934 int level = ftwbuf->level;
937 if (level == 2 && is_dir) {
943 bname = (
char *) fpath + ftwbuf->base - 2;
951 bname = (
char *) fpath + ftwbuf->base;
954 is_file ?
"f" : is_dir ?
"d" :
"x",
955 level, sb->st_size, bname, fpath);
958 if (level == 0 || level > 3)
963 if ((level == 1 && is_dir &&
is_leaf_dir(fpath)) ||
964 (level == 2 && is_dir)) {
975 pr_info(
"%s: Error determining table name for %s\n",
prog,
990 if (level == 1 && is_file) {
991 if (!strcmp(bname,
"mapfile.csv")) {
992 mapfile = strdup(fpath);
1006 pr_info(
"%s: Ignoring file without .json suffix %s\n",
prog,
1042 #define PATH_MAX 4096 1064 const char *output_file;
1065 const char *start_dirname;
1068 prog = basename(argv[0]);
1070 pr_err(
"Usage: %s <arch> <starting_dir> <output_file>\n", prog);
1075 start_dirname = argv[2];
1076 output_file = argv[3];
1081 eventsfp = fopen(output_file,
"w");
1083 pr_err(
"%s Unable to create required file %s (%s)\n",
1084 prog, output_file,
strerror(errno));
1088 sprintf(ldirname,
"%s/%s", start_dirname, arch);
1091 if (stat(ldirname, &stbuf) < 0) {
1092 pr_info(
"%s: Arch %s has no PMU event lists\n", prog, arch);
1097 fprintf(eventsfp,
"#include \"../../pmu-events/pmu-events.h\"\n");
1113 pr_info(
"%s: Error preprocessing arch standard files %s\n",
1116 }
else if (rc < 0) {
1126 pr_info(
"%s: Error walking file tree %s\n", prog, ldirname);
1128 }
else if (rc < 0) {
1140 pr_info(
"%s: No CPU->JSON mapping?\n", prog);
1145 pr_info(
"%s: Error processing mapfile %s\n", prog, mapfile);
#define FOR_ALL_EVENT_STRUCT_FIELDS(op)
static int save_arch_std_events(void *data, char *name, char *event, char *desc, char *long_desc, char *pmu, char *unit, char *perpkg, char *metric_expr, char *metric_name, char *metric_group)
static void print_mapping_table_suffix(FILE *outfp)
static int print_events_table_entry(void *data, char *name, char *event, char *desc, char *long_desc, char *pmu, char *unit, char *perpkg, char *metric_expr, char *metric_name, char *metric_group)
static int process_one_file(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf)
static struct version version
#define FREE_EVENT_FIELD(field)
static int try_fixup(const char *fn, char *arch_std, char **event, char **desc, char **name, char **long_desc, char **pmu, char **filter, char **perpkg, char **unit, char **metric_expr, char **metric_name, char **metric_group, unsigned long long eventcode)
static void free_arch_std_events(void)
static const char * field_to_perf(struct map *table, char *map, jsmntok_t *val)
static char * real_event(const char *name, char *event)
static char * get_topic(void)
x86 movsq based memset() in arch/x86/lib/memset_64.S") MEMSET_FN(memset_erms
static struct field fields[]
static LIST_HEAD(arch_std_events)
static void addfield(char *map, char **dst, const char *sep, const char *a, jsmntok_t *bt)
int main(int argc, char *argv[])
#define TRY_FIXUP_FIELD(field)
#define pr_debug(fmt,...)
int json_streq(char *map, jsmntok_t *t, const char *s)
static int match_field(char *map, jsmntok_t *field, int nz, char **event, jsmntok_t *val)
#define ADD_EVENT_FIELD(field)
static void print_events_table_suffix(FILE *outfp)
static void print_mapping_table_prefix(FILE *outfp)
int json_len(jsmntok_t *t)
static int preprocess_arch_std_files(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf)
int eprintf(int level, int var, const char *fmt,...)
static int process_mapfile(FILE *outfp, char *fpath)
static struct fixed fixed[]
static char * file_name_to_table_name(char *fname)
static struct map unit_to_pmu[]
static void print_events_table_prefix(FILE *fp, const char *tblname)
static struct msrmap * lookup_msr(char *map, jsmntok_t *val)
struct strfilter * filter
static void fixdesc(char *s)
static void create_empty_mapping(const char *output_file)
static int add_topic(char *bname)
void free_json(char *map, size_t size, jsmntok_t *tokens)
jsmntok_t * parse_json(const char *fn, char **map, size_t *size, int *len)
static void fixname(char *s)
static char * fixregex(char *s)
static int get_maxfds(void)
static void cut_comma(char *map, jsmntok_t *newval)
int json_events(const char *fn, int(*func)(void *data, char *name, char *event, char *desc, char *long_desc, char *pmu, char *unit, char *perpkg, char *metric_expr, char *metric_name, char *metric_group), void *data)
static int is_json_file(const char *name)
static int is_leaf_dir(const char *fpath)