Skip to content

Commit e9d32c1

Browse files
John Garryacmel
authored andcommitted
perf vendor events: Add support for arch standard events
For some architectures (like arm), there are architecture- defined events. Sometimes these events may be "recommended" according to the architecture standard, in that the implementer is free ignore the "recommendation" and create its custom event. This patch adds support for parsing standard events from arch-defined JSONs, and fixing up vendor events when they have implemented these events as standard. Support is also ensured that the vendor may implement their own custom events. A new step is added to the pmu events parsing to fix up the vendor events with the arch-standard events. The arch-defined JSONs must be placed in the arch root folder for preprocessing prior to tree JSON processing. In the vendor JSON, to specify that the arch event is supported, the keyword "ArchStdEvent" should be used, like this: [ { "ArchStdEvent": "L1D_CACHE_WR", }, ] Matching is based on the "EventName" field in the architecture JSON. No other JSON objects are strictly required. However, for other objects added, these take precedence over architecture defined standard events, thus supporting separate events which have the same event code. Signed-off-by: John Garry <john.garry@huawei.com> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Andi Kleen <ak@linux.intel.com> Cc: Ganapatrao Kulkarni <ganapatrao.kulkarni@cavium.com> Cc: Jiri Olsa <jolsa@redhat.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Shaokun Zhang <zhangshaokun@hisilicon.com> Cc: Will Deacon <will.deacon@arm.com> Cc: William Cohen <wcohen@redhat.com> Cc: linux-arm-kernel@lists.infradead.org Cc: linuxarm@huawei.com Link: http://lkml.kernel.org/r/1520506716-197429-8-git-send-email-john.garry@huawei.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 82e6fdd commit e9d32c1

3 files changed

Lines changed: 172 additions & 3 deletions

File tree

‎tools/perf/pmu-events/Build‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
hostprogs := jevents
22

33
jevents-y += json.o jsmn.o jevents.o
4+
CHOSTFLAGS_jevents.o = -I$(srctree)/tools/include
45
pmu-events-y += pmu-events.o
56
JDIR = pmu-events/arch/$(SRCARCH)
67
JSON = $(shell [ -d $(JDIR) ] && \
78
find $(JDIR) -name '*.json' -o -name 'mapfile.csv')
9+
810
#
911
# Locate/process JSON files in pmu-events/arch/
1012
# directory and create tables in pmu-events.c.

‎tools/perf/pmu-events/README‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ tree tools/perf/pmu-events/arch/foo.
1616

1717
- Directories are traversed, but all other files are ignored.
1818

19+
- To reduce JSON event duplication per architecture, platform JSONs may
20+
use "ArchStdEvent" keyword to dereference an "Architecture standard
21+
events", defined in architecture standard JSONs.
22+
Architecture standard JSONs must be located in the architecture root
23+
folder. Matching is based on the "EventName" field.
24+
1925
The PMU events supported by a CPU model are expected to grouped into topics
2026
such as Pipelining, Cache, Memory, Floating-point etc. All events for a topic
2127
should be placed in a separate JSON file - where the file name identifies

‎tools/perf/pmu-events/jevents.c‎

Lines changed: 164 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include <sys/resource.h> /* getrlimit */
4646
#include <ftw.h>
4747
#include <sys/stat.h>
48+
#include <linux/list.h>
4849
#include "jsmn.h"
4950
#include "json.h"
5051
#include "jevents.h"
@@ -351,6 +352,81 @@ static int print_events_table_entry(void *data, char *name, char *event,
351352
return 0;
352353
}
353354

355+
struct event_struct {
356+
struct list_head list;
357+
char *name;
358+
char *event;
359+
char *desc;
360+
char *long_desc;
361+
char *pmu;
362+
char *unit;
363+
char *perpkg;
364+
char *metric_expr;
365+
char *metric_name;
366+
char *metric_group;
367+
};
368+
369+
#define ADD_EVENT_FIELD(field) do { if (field) { \
370+
es->field = strdup(field); \
371+
if (!es->field) \
372+
goto out_free; \
373+
} } while (0)
374+
375+
#define FREE_EVENT_FIELD(field) free(es->field)
376+
377+
#define TRY_FIXUP_FIELD(field) do { if (es->field && !*field) {\
378+
*field = strdup(es->field); \
379+
if (!*field) \
380+
return -ENOMEM; \
381+
} } while (0)
382+
383+
#define FOR_ALL_EVENT_STRUCT_FIELDS(op) do { \
384+
op(name); \
385+
op(event); \
386+
op(desc); \
387+
op(long_desc); \
388+
op(pmu); \
389+
op(unit); \
390+
op(perpkg); \
391+
op(metric_expr); \
392+
op(metric_name); \
393+
op(metric_group); \
394+
} while (0)
395+
396+
static LIST_HEAD(arch_std_events);
397+
398+
static void free_arch_std_events(void)
399+
{
400+
struct event_struct *es, *next;
401+
402+
list_for_each_entry_safe(es, next, &arch_std_events, list) {
403+
FOR_ALL_EVENT_STRUCT_FIELDS(FREE_EVENT_FIELD);
404+
list_del(&es->list);
405+
free(es);
406+
}
407+
}
408+
409+
static int save_arch_std_events(void *data, char *name, char *event,
410+
char *desc, char *long_desc, char *pmu,
411+
char *unit, char *perpkg, char *metric_expr,
412+
char *metric_name, char *metric_group)
413+
{
414+
struct event_struct *es;
415+
struct stat *sb = data;
416+
417+
es = malloc(sizeof(*es));
418+
if (!es)
419+
return -ENOMEM;
420+
memset(es, 0, sizeof(*es));
421+
FOR_ALL_EVENT_STRUCT_FIELDS(ADD_EVENT_FIELD);
422+
list_add_tail(&es->list, &arch_std_events);
423+
return 0;
424+
out_free:
425+
FOR_ALL_EVENT_STRUCT_FIELDS(FREE_EVENT_FIELD);
426+
free(es);
427+
return -ENOMEM;
428+
}
429+
354430
static void print_events_table_suffix(FILE *outfp)
355431
{
356432
fprintf(outfp, "{\n");
@@ -392,6 +468,32 @@ static char *real_event(const char *name, char *event)
392468
return event;
393469
}
394470

471+
static int
472+
try_fixup(const char *fn, char *arch_std, char **event, char **desc,
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)
476+
{
477+
/* try to find matching event from arch standard values */
478+
struct event_struct *es;
479+
480+
list_for_each_entry(es, &arch_std_events, list) {
481+
if (!strcmp(arch_std, es->name)) {
482+
if (!eventcode && es->event) {
483+
/* allow EventCode to be overridden */
484+
free(*event);
485+
*event = NULL;
486+
}
487+
FOR_ALL_EVENT_STRUCT_FIELDS(TRY_FIXUP_FIELD);
488+
return 0;
489+
}
490+
}
491+
492+
pr_err("%s: could not find matching %s for %s\n",
493+
prog, arch_std, fn);
494+
return -1;
495+
}
496+
395497
/* Call func with each event in the json file */
396498
int json_events(const char *fn,
397499
int (*func)(void *data, char *name, char *event, char *desc,
@@ -427,6 +529,7 @@ int json_events(const char *fn,
427529
char *metric_expr = NULL;
428530
char *metric_name = NULL;
429531
char *metric_group = NULL;
532+
char *arch_std = NULL;
430533
unsigned long long eventcode = 0;
431534
struct msrmap *msr = NULL;
432535
jsmntok_t *msrval = NULL;
@@ -512,6 +615,10 @@ int json_events(const char *fn,
512615
addfield(map, &metric_expr, "", "", val);
513616
for (s = metric_expr; *s; s++)
514617
*s = tolower(*s);
618+
} else if (json_streq(map, field, "ArchStdEvent")) {
619+
addfield(map, &arch_std, "", "", val);
620+
for (s = arch_std; *s; s++)
621+
*s = tolower(*s);
515622
}
516623
/* ignore unknown fields */
517624
}
@@ -536,8 +643,21 @@ int json_events(const char *fn,
536643
if (name)
537644
fixname(name);
538645

646+
if (arch_std) {
647+
/*
648+
* An arch standard event is referenced, so try to
649+
* fixup any unassigned values.
650+
*/
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);
655+
if (err)
656+
goto free_strings;
657+
}
539658
err = func(data, name, real_event(name, event), desc, long_desc,
540659
pmu, unit, perpkg, metric_expr, metric_name, metric_group);
660+
free_strings:
541661
free(event);
542662
free(desc);
543663
free(name);
@@ -550,6 +670,8 @@ int json_events(const char *fn,
550670
free(metric_expr);
551671
free(metric_name);
552672
free(metric_group);
673+
free(arch_std);
674+
553675
if (err)
554676
break;
555677
tok += j;
@@ -777,6 +899,32 @@ static int is_leaf_dir(const char *fpath)
777899
return res;
778900
}
779901

902+
static int is_json_file(const char *name)
903+
{
904+
const char *suffix;
905+
906+
if (strlen(name) < 5)
907+
return 0;
908+
909+
suffix = name + strlen(name) - 5;
910+
911+
if (strncmp(suffix, ".json", 5) == 0)
912+
return 1;
913+
return 0;
914+
}
915+
916+
static int preprocess_arch_std_files(const char *fpath, const struct stat *sb,
917+
int typeflag, struct FTW *ftwbuf)
918+
{
919+
int level = ftwbuf->level;
920+
int is_file = typeflag == FTW_F;
921+
922+
if (level == 1 && is_file && is_json_file(fpath))
923+
return json_events(fpath, save_arch_std_events, (void *)sb);
924+
925+
return 0;
926+
}
927+
780928
static int process_one_file(const char *fpath, const struct stat *sb,
781929
int typeflag, struct FTW *ftwbuf)
782930
{
@@ -854,9 +1002,7 @@ static int process_one_file(const char *fpath, const struct stat *sb,
8541002
* ignore it. It could be a readme.txt for instance.
8551003
*/
8561004
if (is_file) {
857-
char *suffix = bname + strlen(bname) - 5;
858-
859-
if (strncmp(suffix, ".json", 5)) {
1005+
if (!is_json_file(bname)) {
8601006
pr_info("%s: Ignoring file without .json suffix %s\n", prog,
8611007
fpath);
8621008
return 0;
@@ -962,12 +1108,26 @@ int main(int argc, char *argv[])
9621108

9631109
maxfds = get_maxfds();
9641110
mapfile = NULL;
1111+
rc = nftw(ldirname, preprocess_arch_std_files, maxfds, 0);
1112+
if (rc && verbose) {
1113+
pr_info("%s: Error preprocessing arch standard files %s\n",
1114+
prog, ldirname);
1115+
goto empty_map;
1116+
} else if (rc < 0) {
1117+
/* Make build fail */
1118+
free_arch_std_events();
1119+
return 1;
1120+
} else if (rc) {
1121+
goto empty_map;
1122+
}
1123+
9651124
rc = nftw(ldirname, process_one_file, maxfds, 0);
9661125
if (rc && verbose) {
9671126
pr_info("%s: Error walking file tree %s\n", prog, ldirname);
9681127
goto empty_map;
9691128
} else if (rc < 0) {
9701129
/* Make build fail */
1130+
free_arch_std_events();
9711131
return 1;
9721132
} else if (rc) {
9731133
goto empty_map;
@@ -992,5 +1152,6 @@ int main(int argc, char *argv[])
9921152
empty_map:
9931153
fclose(eventsfp);
9941154
create_empty_mapping(output_file);
1155+
free_arch_std_events();
9951156
return 0;
9961157
}

0 commit comments

Comments
 (0)