linux/tools/perf/ui/browsers/res_sample.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/* Display a menu with individual samples to browse with perf script */
   3#include "hist.h"
   4#include "evsel.h"
   5#include "hists.h"
   6#include "sort.h"
   7#include "config.h"
   8#include "time-utils.h"
   9#include <linux/time64.h>
  10#include <linux/zalloc.h>
  11
  12static u64 context_len = 10 * NSEC_PER_MSEC;
  13
  14static int res_sample_config(const char *var, const char *value, void *data __maybe_unused)
  15{
  16        if (!strcmp(var, "samples.context"))
  17                return perf_config_u64(&context_len, var, value);
  18        return 0;
  19}
  20
  21void res_sample_init(void)
  22{
  23        perf_config(res_sample_config, NULL);
  24}
  25
  26int res_sample_browse(struct res_sample *res_samples, int num_res,
  27                      struct perf_evsel *evsel, enum rstype rstype)
  28{
  29        char **names;
  30        int i, n;
  31        int choice;
  32        char *cmd;
  33        char pbuf[256], tidbuf[32], cpubuf[32];
  34        const char *perf = perf_exe(pbuf, sizeof pbuf);
  35        char trange[128], tsample[64];
  36        struct res_sample *r;
  37        char extra_format[256];
  38
  39        names = calloc(num_res, sizeof(char *));
  40        if (!names)
  41                return -1;
  42        for (i = 0; i < num_res; i++) {
  43                char tbuf[64];
  44
  45                timestamp__scnprintf_nsec(res_samples[i].time, tbuf, sizeof tbuf);
  46                if (asprintf(&names[i], "%s: CPU %d tid %d", tbuf,
  47                             res_samples[i].cpu, res_samples[i].tid) < 0) {
  48                        while (--i >= 0)
  49                                zfree(&names[i]);
  50                        free(names);
  51                        return -1;
  52                }
  53        }
  54        choice = ui__popup_menu(num_res, names);
  55        for (i = 0; i < num_res; i++)
  56                zfree(&names[i]);
  57        free(names);
  58
  59        if (choice < 0 || choice >= num_res)
  60                return -1;
  61        r = &res_samples[choice];
  62
  63        n = timestamp__scnprintf_nsec(r->time - context_len, trange, sizeof trange);
  64        trange[n++] = ',';
  65        timestamp__scnprintf_nsec(r->time + context_len, trange + n, sizeof trange - n);
  66
  67        timestamp__scnprintf_nsec(r->time, tsample, sizeof tsample);
  68
  69        attr_to_script(extra_format, &evsel->attr);
  70
  71        if (asprintf(&cmd, "%s script %s%s --time %s %s%s %s%s --ns %s %s %s %s %s | less +/%s",
  72                     perf,
  73                     input_name ? "-i " : "",
  74                     input_name ? input_name : "",
  75                     trange,
  76                     r->cpu >= 0 ? "--cpu " : "",
  77                     r->cpu >= 0 ? (sprintf(cpubuf, "%d", r->cpu), cpubuf) : "",
  78                     r->tid ? "--tid " : "",
  79                     r->tid ? (sprintf(tidbuf, "%d", r->tid), tidbuf) : "",
  80                     extra_format,
  81                     rstype == A_ASM ? "-F +insn --xed" :
  82                     rstype == A_SOURCE ? "-F +srcline,+srccode" : "",
  83                     symbol_conf.inline_name ? "--inline" : "",
  84                     "--show-lost-events ",
  85                     r->tid ? "--show-switch-events --show-task-events " : "",
  86                     tsample) < 0)
  87                return -1;
  88        run_script(cmd);
  89        free(cmd);
  90        return 0;
  91}
  92