Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <dirent.h>
0003 #include <errno.h>
0004 #include <stdio.h>
0005 #include <stdlib.h>
0006 #include <string.h>
0007 #include <sys/param.h>
0008 
0009 #include <api/fs/tracing_path.h>
0010 #include <linux/stddef.h>
0011 #include <linux/perf_event.h>
0012 #include <linux/zalloc.h>
0013 #include <subcmd/pager.h>
0014 
0015 #include "build-id.h"
0016 #include "debug.h"
0017 #include "evsel.h"
0018 #include "metricgroup.h"
0019 #include "parse-events.h"
0020 #include "pmu.h"
0021 #include "print-events.h"
0022 #include "probe-file.h"
0023 #include "string2.h"
0024 #include "strlist.h"
0025 #include "tracepoint.h"
0026 #include "pfm.h"
0027 #include "pmu-hybrid.h"
0028 
0029 #define MAX_NAME_LEN 100
0030 
0031 static const char * const event_type_descriptors[] = {
0032     "Hardware event",
0033     "Software event",
0034     "Tracepoint event",
0035     "Hardware cache event",
0036     "Raw hardware event descriptor",
0037     "Hardware breakpoint",
0038 };
0039 
0040 static const struct event_symbol event_symbols_tool[PERF_TOOL_MAX] = {
0041     [PERF_TOOL_DURATION_TIME] = {
0042         .symbol = "duration_time",
0043         .alias  = "",
0044     },
0045     [PERF_TOOL_USER_TIME] = {
0046         .symbol = "user_time",
0047         .alias  = "",
0048     },
0049     [PERF_TOOL_SYSTEM_TIME] = {
0050         .symbol = "system_time",
0051         .alias  = "",
0052     },
0053 };
0054 
0055 static int cmp_string(const void *a, const void *b)
0056 {
0057     const char * const *as = a;
0058     const char * const *bs = b;
0059 
0060     return strcmp(*as, *bs);
0061 }
0062 
0063 /*
0064  * Print the events from <debugfs_mount_point>/tracing/events
0065  */
0066 void print_tracepoint_events(const char *subsys_glob,
0067                  const char *event_glob, bool name_only)
0068 {
0069     DIR *sys_dir, *evt_dir;
0070     struct dirent *sys_dirent, *evt_dirent;
0071     char evt_path[MAXPATHLEN];
0072     char *dir_path;
0073     char **evt_list = NULL;
0074     unsigned int evt_i = 0, evt_num = 0;
0075     bool evt_num_known = false;
0076 
0077 restart:
0078     sys_dir = tracing_events__opendir();
0079     if (!sys_dir)
0080         return;
0081 
0082     if (evt_num_known) {
0083         evt_list = zalloc(sizeof(char *) * evt_num);
0084         if (!evt_list)
0085             goto out_close_sys_dir;
0086     }
0087 
0088     for_each_subsystem(sys_dir, sys_dirent) {
0089         if (subsys_glob != NULL &&
0090             !strglobmatch(sys_dirent->d_name, subsys_glob))
0091             continue;
0092 
0093         dir_path = get_events_file(sys_dirent->d_name);
0094         if (!dir_path)
0095             continue;
0096         evt_dir = opendir(dir_path);
0097         if (!evt_dir)
0098             goto next;
0099 
0100         for_each_event(dir_path, evt_dir, evt_dirent) {
0101             if (event_glob != NULL &&
0102                 !strglobmatch(evt_dirent->d_name, event_glob))
0103                 continue;
0104 
0105             if (!evt_num_known) {
0106                 evt_num++;
0107                 continue;
0108             }
0109 
0110             snprintf(evt_path, MAXPATHLEN, "%s:%s",
0111                  sys_dirent->d_name, evt_dirent->d_name);
0112 
0113             evt_list[evt_i] = strdup(evt_path);
0114             if (evt_list[evt_i] == NULL) {
0115                 put_events_file(dir_path);
0116                 goto out_close_evt_dir;
0117             }
0118             evt_i++;
0119         }
0120         closedir(evt_dir);
0121 next:
0122         put_events_file(dir_path);
0123     }
0124     closedir(sys_dir);
0125 
0126     if (!evt_num_known) {
0127         evt_num_known = true;
0128         goto restart;
0129     }
0130     qsort(evt_list, evt_num, sizeof(char *), cmp_string);
0131     evt_i = 0;
0132     while (evt_i < evt_num) {
0133         if (name_only) {
0134             printf("%s ", evt_list[evt_i++]);
0135             continue;
0136         }
0137         printf("  %-50s [%s]\n", evt_list[evt_i++],
0138                 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
0139     }
0140     if (evt_num && pager_in_use())
0141         printf("\n");
0142 
0143 out_free:
0144     evt_num = evt_i;
0145     for (evt_i = 0; evt_i < evt_num; evt_i++)
0146         zfree(&evt_list[evt_i]);
0147     zfree(&evt_list);
0148     return;
0149 
0150 out_close_evt_dir:
0151     closedir(evt_dir);
0152 out_close_sys_dir:
0153     closedir(sys_dir);
0154 
0155     printf("FATAL: not enough memory to print %s\n",
0156             event_type_descriptors[PERF_TYPE_TRACEPOINT]);
0157     if (evt_list)
0158         goto out_free;
0159 }
0160 
0161 void print_sdt_events(const char *subsys_glob, const char *event_glob,
0162               bool name_only)
0163 {
0164     struct probe_cache *pcache;
0165     struct probe_cache_entry *ent;
0166     struct strlist *bidlist, *sdtlist;
0167     struct strlist_config cfg = {.dont_dupstr = true};
0168     struct str_node *nd, *nd2;
0169     char *buf, *path, *ptr = NULL;
0170     bool show_detail = false;
0171     int ret;
0172 
0173     sdtlist = strlist__new(NULL, &cfg);
0174     if (!sdtlist) {
0175         pr_debug("Failed to allocate new strlist for SDT\n");
0176         return;
0177     }
0178     bidlist = build_id_cache__list_all(true);
0179     if (!bidlist) {
0180         pr_debug("Failed to get buildids: %d\n", errno);
0181         return;
0182     }
0183     strlist__for_each_entry(nd, bidlist) {
0184         pcache = probe_cache__new(nd->s, NULL);
0185         if (!pcache)
0186             continue;
0187         list_for_each_entry(ent, &pcache->entries, node) {
0188             if (!ent->sdt)
0189                 continue;
0190             if (subsys_glob &&
0191                 !strglobmatch(ent->pev.group, subsys_glob))
0192                 continue;
0193             if (event_glob &&
0194                 !strglobmatch(ent->pev.event, event_glob))
0195                 continue;
0196             ret = asprintf(&buf, "%s:%s@%s", ent->pev.group,
0197                     ent->pev.event, nd->s);
0198             if (ret > 0)
0199                 strlist__add(sdtlist, buf);
0200         }
0201         probe_cache__delete(pcache);
0202     }
0203     strlist__delete(bidlist);
0204 
0205     strlist__for_each_entry(nd, sdtlist) {
0206         buf = strchr(nd->s, '@');
0207         if (buf)
0208             *(buf++) = '\0';
0209         if (name_only) {
0210             printf("%s ", nd->s);
0211             continue;
0212         }
0213         nd2 = strlist__next(nd);
0214         if (nd2) {
0215             ptr = strchr(nd2->s, '@');
0216             if (ptr)
0217                 *ptr = '\0';
0218             if (strcmp(nd->s, nd2->s) == 0)
0219                 show_detail = true;
0220         }
0221         if (show_detail) {
0222             path = build_id_cache__origname(buf);
0223             ret = asprintf(&buf, "%s@%s(%.12s)", nd->s, path, buf);
0224             if (ret > 0) {
0225                 printf("  %-50s [%s]\n", buf, "SDT event");
0226                 free(buf);
0227             }
0228             free(path);
0229         } else
0230             printf("  %-50s [%s]\n", nd->s, "SDT event");
0231         if (nd2) {
0232             if (strcmp(nd->s, nd2->s) != 0)
0233                 show_detail = false;
0234             if (ptr)
0235                 *ptr = '@';
0236         }
0237     }
0238     strlist__delete(sdtlist);
0239 }
0240 
0241 int print_hwcache_events(const char *event_glob, bool name_only)
0242 {
0243     unsigned int type, op, i, evt_i = 0, evt_num = 0, npmus = 0;
0244     char name[64], new_name[128];
0245     char **evt_list = NULL, **evt_pmus = NULL;
0246     bool evt_num_known = false;
0247     struct perf_pmu *pmu = NULL;
0248 
0249     if (perf_pmu__has_hybrid()) {
0250         npmus = perf_pmu__hybrid_pmu_num();
0251         evt_pmus = zalloc(sizeof(char *) * npmus);
0252         if (!evt_pmus)
0253             goto out_enomem;
0254     }
0255 
0256 restart:
0257     if (evt_num_known) {
0258         evt_list = zalloc(sizeof(char *) * evt_num);
0259         if (!evt_list)
0260             goto out_enomem;
0261     }
0262 
0263     for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
0264         for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
0265             /* skip invalid cache type */
0266             if (!evsel__is_cache_op_valid(type, op))
0267                 continue;
0268 
0269             for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
0270                 unsigned int hybrid_supported = 0, j;
0271                 bool supported;
0272 
0273                 __evsel__hw_cache_type_op_res_name(type, op, i, name, sizeof(name));
0274                 if (event_glob != NULL && !strglobmatch(name, event_glob))
0275                     continue;
0276 
0277                 if (!perf_pmu__has_hybrid()) {
0278                     if (!is_event_supported(PERF_TYPE_HW_CACHE,
0279                                 type | (op << 8) | (i << 16))) {
0280                         continue;
0281                     }
0282                 } else {
0283                     perf_pmu__for_each_hybrid_pmu(pmu) {
0284                         if (!evt_num_known) {
0285                             evt_num++;
0286                             continue;
0287                         }
0288 
0289                         supported = is_event_supported(
0290                             PERF_TYPE_HW_CACHE,
0291                             type | (op << 8) | (i << 16) |
0292                             ((__u64)pmu->type << PERF_PMU_TYPE_SHIFT));
0293                         if (supported) {
0294                             snprintf(new_name, sizeof(new_name),
0295                                  "%s/%s/", pmu->name, name);
0296                             evt_pmus[hybrid_supported] =
0297                                 strdup(new_name);
0298                             hybrid_supported++;
0299                         }
0300                     }
0301 
0302                     if (hybrid_supported == 0)
0303                         continue;
0304                 }
0305 
0306                 if (!evt_num_known) {
0307                     evt_num++;
0308                     continue;
0309                 }
0310 
0311                 if ((hybrid_supported == 0) ||
0312                     (hybrid_supported == npmus)) {
0313                     evt_list[evt_i] = strdup(name);
0314                     if (npmus > 0) {
0315                         for (j = 0; j < npmus; j++)
0316                             zfree(&evt_pmus[j]);
0317                     }
0318                 } else {
0319                     for (j = 0; j < hybrid_supported; j++) {
0320                         evt_list[evt_i++] = evt_pmus[j];
0321                         evt_pmus[j] = NULL;
0322                     }
0323                     continue;
0324                 }
0325 
0326                 if (evt_list[evt_i] == NULL)
0327                     goto out_enomem;
0328                 evt_i++;
0329             }
0330         }
0331     }
0332 
0333     if (!evt_num_known) {
0334         evt_num_known = true;
0335         goto restart;
0336     }
0337 
0338     for (evt_i = 0; evt_i < evt_num; evt_i++) {
0339         if (!evt_list[evt_i])
0340             break;
0341     }
0342 
0343     evt_num = evt_i;
0344     qsort(evt_list, evt_num, sizeof(char *), cmp_string);
0345     evt_i = 0;
0346     while (evt_i < evt_num) {
0347         if (name_only) {
0348             printf("%s ", evt_list[evt_i++]);
0349             continue;
0350         }
0351         printf("  %-50s [%s]\n", evt_list[evt_i++],
0352                 event_type_descriptors[PERF_TYPE_HW_CACHE]);
0353     }
0354     if (evt_num && pager_in_use())
0355         printf("\n");
0356 
0357 out_free:
0358     evt_num = evt_i;
0359     for (evt_i = 0; evt_i < evt_num; evt_i++)
0360         zfree(&evt_list[evt_i]);
0361     zfree(&evt_list);
0362 
0363     for (evt_i = 0; evt_i < npmus; evt_i++)
0364         zfree(&evt_pmus[evt_i]);
0365     zfree(&evt_pmus);
0366     return evt_num;
0367 
0368 out_enomem:
0369     printf("FATAL: not enough memory to print %s\n",
0370         event_type_descriptors[PERF_TYPE_HW_CACHE]);
0371     if (evt_list)
0372         goto out_free;
0373     return evt_num;
0374 }
0375 
0376 static void print_tool_event(const struct event_symbol *syms, const char *event_glob,
0377                  bool name_only)
0378 {
0379     if (syms->symbol == NULL)
0380         return;
0381 
0382     if (event_glob && !(strglobmatch(syms->symbol, event_glob) ||
0383           (syms->alias && strglobmatch(syms->alias, event_glob))))
0384         return;
0385 
0386     if (name_only)
0387         printf("%s ", syms->symbol);
0388     else {
0389         char name[MAX_NAME_LEN];
0390 
0391         if (syms->alias && strlen(syms->alias))
0392             snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
0393         else
0394             strlcpy(name, syms->symbol, MAX_NAME_LEN);
0395         printf("  %-50s [%s]\n", name, "Tool event");
0396     }
0397 }
0398 
0399 void print_tool_events(const char *event_glob, bool name_only)
0400 {
0401     // Start at 1 because the first enum entry means no tool event.
0402     for (int i = 1; i < PERF_TOOL_MAX; ++i)
0403         print_tool_event(event_symbols_tool + i, event_glob, name_only);
0404 
0405     if (pager_in_use())
0406         printf("\n");
0407 }
0408 
0409 void print_symbol_events(const char *event_glob, unsigned int type,
0410              struct event_symbol *syms, unsigned int max,
0411              bool name_only)
0412 {
0413     unsigned int i, evt_i = 0, evt_num = 0;
0414     char name[MAX_NAME_LEN];
0415     char **evt_list = NULL;
0416     bool evt_num_known = false;
0417 
0418 restart:
0419     if (evt_num_known) {
0420         evt_list = zalloc(sizeof(char *) * evt_num);
0421         if (!evt_list)
0422             goto out_enomem;
0423         syms -= max;
0424     }
0425 
0426     for (i = 0; i < max; i++, syms++) {
0427         /*
0428          * New attr.config still not supported here, the latest
0429          * example was PERF_COUNT_SW_CGROUP_SWITCHES
0430          */
0431         if (syms->symbol == NULL)
0432             continue;
0433 
0434         if (event_glob != NULL && !(strglobmatch(syms->symbol, event_glob) ||
0435               (syms->alias && strglobmatch(syms->alias, event_glob))))
0436             continue;
0437 
0438         if (!is_event_supported(type, i))
0439             continue;
0440 
0441         if (!evt_num_known) {
0442             evt_num++;
0443             continue;
0444         }
0445 
0446         if (!name_only && strlen(syms->alias))
0447             snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
0448         else
0449             strlcpy(name, syms->symbol, MAX_NAME_LEN);
0450 
0451         evt_list[evt_i] = strdup(name);
0452         if (evt_list[evt_i] == NULL)
0453             goto out_enomem;
0454         evt_i++;
0455     }
0456 
0457     if (!evt_num_known) {
0458         evt_num_known = true;
0459         goto restart;
0460     }
0461     qsort(evt_list, evt_num, sizeof(char *), cmp_string);
0462     evt_i = 0;
0463     while (evt_i < evt_num) {
0464         if (name_only) {
0465             printf("%s ", evt_list[evt_i++]);
0466             continue;
0467         }
0468         printf("  %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
0469     }
0470     if (evt_num && pager_in_use())
0471         printf("\n");
0472 
0473 out_free:
0474     evt_num = evt_i;
0475     for (evt_i = 0; evt_i < evt_num; evt_i++)
0476         zfree(&evt_list[evt_i]);
0477     zfree(&evt_list);
0478     return;
0479 
0480 out_enomem:
0481     printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
0482     if (evt_list)
0483         goto out_free;
0484 }
0485 
0486 /*
0487  * Print the help text for the event symbols:
0488  */
0489 void print_events(const char *event_glob, bool name_only, bool quiet_flag,
0490             bool long_desc, bool details_flag, bool deprecated,
0491             const char *pmu_name)
0492 {
0493     print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
0494                 event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
0495 
0496     print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
0497                 event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
0498     print_tool_events(event_glob, name_only);
0499 
0500     print_hwcache_events(event_glob, name_only);
0501 
0502     print_pmu_events(event_glob, name_only, quiet_flag, long_desc,
0503             details_flag, deprecated, pmu_name);
0504 
0505     if (event_glob != NULL)
0506         return;
0507 
0508     if (!name_only) {
0509         printf("  %-50s [%s]\n",
0510                "rNNN",
0511                event_type_descriptors[PERF_TYPE_RAW]);
0512         printf("  %-50s [%s]\n",
0513                "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
0514                event_type_descriptors[PERF_TYPE_RAW]);
0515         if (pager_in_use())
0516             printf("   (see 'man perf-list' on how to encode it)\n\n");
0517 
0518         printf("  %-50s [%s]\n",
0519                "mem:<addr>[/len][:access]",
0520             event_type_descriptors[PERF_TYPE_BREAKPOINT]);
0521         if (pager_in_use())
0522             printf("\n");
0523     }
0524 
0525     print_tracepoint_events(NULL, NULL, name_only);
0526 
0527     print_sdt_events(NULL, NULL, name_only);
0528 
0529     metricgroup__print(true, true, NULL, name_only, details_flag,
0530                pmu_name);
0531 
0532     print_libpfm_events(name_only, long_desc);
0533 }