Resguardo inicial

This commit is contained in:
lastdragon
2026-09-06 12:53:21 -06:00
parent 662efe3c2a
commit ad2088d178
94853 changed files with 43200404 additions and 0 deletions
+7
View File
@@ -0,0 +1,7 @@
perf-ui-y += annotate.o
perf-ui-y += annotate-data.o
perf-ui-y += hists.o
perf-ui-y += map.o
perf-ui-y += scripts.o
perf-ui-y += header.o
perf-ui-y += res_sample.o
@@ -0,0 +1,614 @@
// SPDX-License-Identifier: GPL-2.0
#include <inttypes.h>
#include <string.h>
#include <linux/zalloc.h>
#include <sys/ttydefaults.h>
#include "ui/browser.h"
#include "ui/helpline.h"
#include "ui/keysyms.h"
#include "ui/ui.h"
#include "util/annotate.h"
#include "util/annotate-data.h"
#include "util/evsel.h"
#include "util/evlist.h"
#include "util/sort.h"
#define FOLDED_SIGN '+'
#define UNFOLD_SIGN '-'
#define NOCHLD_SIGN ' '
struct browser_entry {
struct list_head node;
struct annotated_member *data;
struct type_hist_entry *hists;
struct browser_entry *parent;
struct list_head children;
int indent; /*indentation level, starts from 0 */
int nr_entries; /* # of visible entries: self + descendents */
bool folded; /* only can be false when it has children */
};
struct annotated_data_browser {
struct ui_browser b;
struct list_head entries;
struct browser_entry *curr;
int nr_events;
};
static struct annotated_data_browser *get_browser(struct ui_browser *uib)
{
return container_of(uib, struct annotated_data_browser, b);
}
static void update_hist_entry(struct type_hist_entry *dst,
struct type_hist_entry *src)
{
dst->nr_samples += src->nr_samples;
dst->period += src->period;
}
static int get_member_overhead(struct annotated_data_type *adt,
struct browser_entry *entry,
struct evsel *leader)
{
struct annotated_member *member = entry->data;
int i, k;
for (i = 0; i < member->size; i++) {
struct type_hist *h;
struct evsel *evsel;
int offset = member->offset + i;
k = 0;
for_each_group_evsel(evsel, leader) {
if (symbol_conf.skip_empty &&
evsel__hists(evsel)->stats.nr_samples == 0)
continue;
h = adt->histograms[evsel->core.idx];
update_hist_entry(&entry->hists[k++], &h->addr[offset]);
}
}
return 0;
}
static int add_child_entries(struct annotated_data_browser *browser,
struct browser_entry *parent,
struct annotated_data_type *adt,
struct annotated_member *member,
struct evsel *evsel, int indent)
{
struct annotated_member *pos;
struct browser_entry *entry;
struct list_head *parent_list;
entry = zalloc(sizeof(*entry));
if (entry == NULL)
return -1;
entry->hists = calloc(browser->nr_events, sizeof(*entry->hists));
if (entry->hists == NULL) {
free(entry);
return -1;
}
entry->data = member;
entry->parent = parent;
entry->indent = indent;
if (get_member_overhead(adt, entry, evsel) < 0) {
free(entry);
return -1;
}
INIT_LIST_HEAD(&entry->children);
if (parent)
parent_list = &parent->children;
else
parent_list = &browser->entries;
list_add_tail(&entry->node, parent_list);
list_for_each_entry(pos, &member->children, node) {
int nr = add_child_entries(browser, entry, adt, pos, evsel,
indent + 1);
if (nr < 0)
return nr;
}
/* add an entry for the closing bracket ("}") */
if (!list_empty(&member->children)) {
struct browser_entry *bracket;
bracket = zalloc(sizeof(*bracket));
if (bracket == NULL)
return -1;
bracket->indent = indent;
bracket->parent = entry;
bracket->folded = true;
bracket->nr_entries = 1;
INIT_LIST_HEAD(&bracket->children);
list_add_tail(&bracket->node, &entry->children);
}
/* fold child entries by default */
entry->folded = true;
entry->nr_entries = 1;
return 0;
}
static u32 count_visible_entries(struct annotated_data_browser *browser)
{
int nr = 0;
struct browser_entry *entry;
list_for_each_entry(entry, &browser->entries, node)
nr += entry->nr_entries;
return nr;
}
static int annotated_data_browser__collect_entries(struct annotated_data_browser *browser)
{
struct hist_entry *he = browser->b.priv;
struct annotated_data_type *adt = he->mem_type;
struct evsel *evsel = hists_to_evsel(he->hists);
INIT_LIST_HEAD(&browser->entries);
add_child_entries(browser, /*parent=*/NULL, adt, &adt->self, evsel,
/*indent=*/0);
browser->b.entries = &browser->entries;
browser->b.nr_entries = count_visible_entries(browser);
return 0;
}
static void annotated_data_browser__delete_entries(struct annotated_data_browser *browser)
{
struct browser_entry *pos, *tmp;
list_for_each_entry_safe(pos, tmp, &browser->entries, node) {
list_del_init(&pos->node);
zfree(&pos->hists);
free(pos);
}
}
static struct browser_entry *get_first_child(struct browser_entry *entry)
{
if (list_empty(&entry->children))
return NULL;
return list_first_entry(&entry->children, struct browser_entry, node);
}
static struct browser_entry *get_last_child(struct browser_entry *entry)
{
if (list_empty(&entry->children))
return NULL;
return list_last_entry(&entry->children, struct browser_entry, node);
}
static bool is_first_child(struct browser_entry *entry)
{
/* This will be checked in a different way */
if (entry->parent == NULL)
return false;
return get_first_child(entry->parent) == entry;
}
static bool is_last_child(struct browser_entry *entry)
{
/* This will be checked in a different way */
if (entry->parent == NULL)
return false;
return get_last_child(entry->parent) == entry;
}
static struct browser_entry *browser__prev_entry(struct ui_browser *uib,
struct browser_entry *entry)
{
struct annotated_data_browser *browser = get_browser(uib);
struct browser_entry *first;
first = list_first_entry(&browser->entries, struct browser_entry, node);
while (entry != first) {
if (is_first_child(entry))
entry = entry->parent;
else {
entry = list_prev_entry(entry, node);
while (!entry->folded)
entry = get_last_child(entry);
}
if (!uib->filter || !uib->filter(uib, &entry->node))
return entry;
}
return first;
}
static struct browser_entry *browser__next_entry(struct ui_browser *uib,
struct browser_entry *entry)
{
struct annotated_data_browser *browser = get_browser(uib);
struct browser_entry *last;
last = list_last_entry(&browser->entries, struct browser_entry, node);
while (!last->folded)
last = get_last_child(last);
while (entry != last) {
if (!entry->folded)
entry = get_first_child(entry);
else {
while (is_last_child(entry))
entry = entry->parent;
entry = list_next_entry(entry, node);
}
if (!uib->filter || !uib->filter(uib, &entry->node))
return entry;
}
return last;
}
static void browser__seek(struct ui_browser *uib, off_t offset, int whence)
{
struct annotated_data_browser *browser = get_browser(uib);
struct browser_entry *entry;
if (uib->nr_entries == 0)
return;
switch (whence) {
case SEEK_SET:
entry = list_first_entry(&browser->entries, typeof(*entry), node);
if (uib->filter && uib->filter(uib, &entry->node))
entry = browser__next_entry(uib, entry);
break;
case SEEK_CUR:
entry = list_entry(uib->top, typeof(*entry), node);
break;
case SEEK_END:
entry = list_last_entry(&browser->entries, typeof(*entry), node);
while (!entry->folded)
entry = get_last_child(entry);
if (uib->filter && uib->filter(uib, &entry->node))
entry = browser__prev_entry(uib, entry);
break;
default:
return;
}
assert(entry != NULL);
if (offset > 0) {
while (offset-- != 0)
entry = browser__next_entry(uib, entry);
} else {
while (offset++ != 0)
entry = browser__prev_entry(uib, entry);
}
uib->top = &entry->node;
}
static unsigned int browser__refresh(struct ui_browser *uib)
{
struct annotated_data_browser *browser = get_browser(uib);
struct browser_entry *entry, *next;
int row = 0;
if (uib->top == NULL || uib->top == uib->entries)
browser__seek(uib, SEEK_SET, 0);
entry = list_entry(uib->top, typeof(*entry), node);
while (true) {
if (!uib->filter || !uib->filter(uib, &entry->node)) {
ui_browser__gotorc(uib, row, 0);
uib->write(uib, entry, row);
if (uib->top_idx + row == uib->index)
browser->curr = entry;
if (++row == uib->rows)
break;
}
next = browser__next_entry(uib, entry);
if (next == entry)
break;
entry = next;
}
return row;
}
static int browser__show(struct ui_browser *uib)
{
struct hist_entry *he = uib->priv;
struct annotated_data_type *adt = he->mem_type;
struct annotated_data_browser *browser = get_browser(uib);
const char *help = "Press 'h' for help on key bindings";
char title[256];
snprintf(title, sizeof(title), "Annotate type: '%s' (%d samples)",
adt->self.type_name, he->stat.nr_events);
if (ui_browser__show(uib, title, help) < 0)
return -1;
/* second line header */
ui_browser__gotorc_title(uib, 0, 0);
ui_browser__set_color(uib, HE_COLORSET_ROOT);
if (symbol_conf.show_total_period)
strcpy(title, "Period");
else if (symbol_conf.show_nr_samples)
strcpy(title, "Samples");
else
strcpy(title, "Percent");
ui_browser__printf(uib, "%*s %10s %10s %10s %s",
2 + 11 * (browser->nr_events - 1), "",
title, "Offset", "Size", "Field");
ui_browser__write_nstring(uib, "", uib->width);
return 0;
}
static void browser__write_overhead(struct ui_browser *uib,
struct type_hist *total,
struct type_hist_entry *hist, int row)
{
u64 period = hist->period;
double percent = total->period ? (100.0 * period / total->period) : 0;
bool current = ui_browser__is_current_entry(uib, row);
int nr_samples = 0;
ui_browser__set_percent_color(uib, percent, current);
if (symbol_conf.show_total_period)
ui_browser__printf(uib, " %10" PRIu64, period);
else if (symbol_conf.show_nr_samples)
ui_browser__printf(uib, " %10d", nr_samples);
else
ui_browser__printf(uib, " %10.2f", percent);
ui_browser__set_percent_color(uib, 0, current);
}
static void browser__write(struct ui_browser *uib, void *entry, int row)
{
struct annotated_data_browser *browser = get_browser(uib);
struct browser_entry *be = entry;
struct annotated_member *member = be->data;
struct hist_entry *he = uib->priv;
struct annotated_data_type *adt = he->mem_type;
struct evsel *leader = hists_to_evsel(he->hists);
struct evsel *evsel;
int idx = 0;
bool current = ui_browser__is_current_entry(uib, row);
if (member == NULL) {
/* print the closing bracket */
ui_browser__set_percent_color(uib, 0, current);
ui_browser__printf(uib, "%c ", NOCHLD_SIGN);
ui_browser__write_nstring(uib, "", 11 * browser->nr_events);
ui_browser__printf(uib, " %10s %10s %*s};",
"", "", be->indent * 4, "");
ui_browser__write_nstring(uib, "", uib->width);
return;
}
ui_browser__set_percent_color(uib, 0, current);
if (!list_empty(&be->children))
ui_browser__printf(uib, "%c ", be->folded ? FOLDED_SIGN : UNFOLD_SIGN);
else
ui_browser__printf(uib, "%c ", NOCHLD_SIGN);
/* print the number */
for_each_group_evsel(evsel, leader) {
struct type_hist *h = adt->histograms[evsel->core.idx];
if (symbol_conf.skip_empty &&
evsel__hists(evsel)->stats.nr_samples == 0)
continue;
browser__write_overhead(uib, h, &be->hists[idx++], row);
}
/* print type info */
if (be->indent == 0 && !member->var_name) {
ui_browser__printf(uib, " %#10x %#10x %s%s",
member->offset, member->size,
member->type_name,
list_empty(&member->children) || be->folded? ";" : " {");
} else {
ui_browser__printf(uib, " %#10x %#10x %*s%s\t%s%s",
member->offset, member->size,
be->indent * 4, "", member->type_name,
member->var_name ?: "",
list_empty(&member->children) || be->folded ? ";" : " {");
}
/* fill the rest */
ui_browser__write_nstring(uib, "", uib->width);
}
static void annotated_data_browser__fold(struct annotated_data_browser *browser,
struct browser_entry *entry,
bool recursive)
{
struct browser_entry *child;
if (list_empty(&entry->children))
return;
if (entry->folded && !recursive)
return;
if (recursive) {
list_for_each_entry(child, &entry->children, node)
annotated_data_browser__fold(browser, child, true);
}
entry->nr_entries = 1;
entry->folded = true;
}
static void annotated_data_browser__unfold(struct annotated_data_browser *browser,
struct browser_entry *entry,
bool recursive)
{
struct browser_entry *child;
int nr_entries;
if (list_empty(&entry->children))
return;
if (!entry->folded && !recursive)
return;
nr_entries = 1; /* for self */
list_for_each_entry(child, &entry->children, node) {
if (recursive)
annotated_data_browser__unfold(browser, child, true);
nr_entries += child->nr_entries;
}
entry->nr_entries = nr_entries;
entry->folded = false;
}
static void annotated_data_browser__toggle_fold(struct annotated_data_browser *browser,
bool recursive)
{
struct browser_entry *curr = browser->curr;
struct browser_entry *parent;
parent = curr->parent;
while (parent) {
parent->nr_entries -= curr->nr_entries;
parent = parent->parent;
}
browser->b.nr_entries -= curr->nr_entries;
if (curr->folded)
annotated_data_browser__unfold(browser, curr, recursive);
else
annotated_data_browser__fold(browser, curr, recursive);
parent = curr->parent;
while (parent) {
parent->nr_entries += curr->nr_entries;
parent = parent->parent;
}
browser->b.nr_entries += curr->nr_entries;
assert(browser->b.nr_entries == count_visible_entries(browser));
}
static int annotated_data_browser__run(struct annotated_data_browser *browser,
struct evsel *evsel __maybe_unused,
struct hist_browser_timer *hbt)
{
int delay_secs = hbt ? hbt->refresh : 0;
int key;
if (browser__show(&browser->b) < 0)
return -1;
while (1) {
key = ui_browser__run(&browser->b, delay_secs);
switch (key) {
case K_TIMER:
if (hbt)
hbt->timer(hbt->arg);
continue;
case K_F1:
case 'h':
ui_browser__help_window(&browser->b,
"UP/DOWN/PGUP\n"
"PGDN/SPACE Navigate\n"
"</> Move to prev/next symbol\n"
"e Expand/Collapse current entry\n"
"E Expand/Collapse all children of the current\n"
"q/ESC/CTRL+C Exit\n\n");
continue;
case 'e':
annotated_data_browser__toggle_fold(browser,
/*recursive=*/false);
break;
case 'E':
annotated_data_browser__toggle_fold(browser,
/*recursive=*/true);
break;
case K_LEFT:
case '<':
case '>':
case K_ESC:
case 'q':
case CTRL('c'):
goto out;
default:
ui_browser__warn_unhandled_hotkey(&browser->b, key, delay_secs, ", use 'h'/F1 to see actions");
continue;
}
}
out:
ui_browser__hide(&browser->b);
return key;
}
int hist_entry__annotate_data_tui(struct hist_entry *he, struct evsel *evsel,
struct hist_browser_timer *hbt)
{
struct annotated_data_browser browser = {
.b = {
.refresh = browser__refresh,
.seek = browser__seek,
.write = browser__write,
.priv = he,
.extra_title_lines = 1,
},
.nr_events = 1,
};
int ret;
ui_helpline__push("Press ESC to exit");
if (evsel__is_group_event(evsel)) {
struct evsel *pos;
int nr = 0;
for_each_group_evsel(pos, evsel) {
if (!symbol_conf.skip_empty ||
evsel__hists(pos)->stats.nr_samples)
nr++;
}
browser.nr_events = nr;
}
ret = annotated_data_browser__collect_entries(&browser);
if (ret < 0)
goto out;
/* To get the top and current entry */
browser__refresh(&browser.b);
/* Show the first-level child entries by default */
annotated_data_browser__toggle_fold(&browser, /*recursive=*/false);
ret = annotated_data_browser__run(&browser, evsel, hbt);
out:
annotated_data_browser__delete_entries(&browser);
return ret;
}
File diff suppressed because it is too large Load Diff
+128
View File
@@ -0,0 +1,128 @@
// SPDX-License-Identifier: GPL-2.0
#include "ui/browser.h"
#include "ui/keysyms.h"
#include "ui/ui.h"
#include "ui/util.h"
#include "ui/libslang.h"
#include "util/header.h"
#include "util/session.h"
#include <sys/ttydefaults.h>
static void ui_browser__argv_write(struct ui_browser *browser,
void *entry, int row)
{
char **arg = entry;
char *str = *arg;
char empty[] = " ";
bool current_entry = ui_browser__is_current_entry(browser, row);
unsigned long offset = (unsigned long)browser->priv;
if (offset >= strlen(str))
str = empty;
else
str = str + offset;
ui_browser__set_color(browser, current_entry ? HE_COLORSET_SELECTED :
HE_COLORSET_NORMAL);
ui_browser__write_nstring(browser, str, browser->width);
}
static int list_menu__run(struct ui_browser *menu)
{
int key;
unsigned long offset;
static const char help[] =
"h/?/F1 Show this window\n"
"UP/DOWN/PGUP\n"
"PGDN/SPACE\n"
"LEFT/RIGHT Navigate\n"
"q/ESC/CTRL+C Exit browser";
if (ui_browser__show(menu, "Header information", "Press 'q' to exit") < 0)
return -1;
while (1) {
key = ui_browser__run(menu, 0);
switch (key) {
case K_RIGHT:
offset = (unsigned long)menu->priv;
offset += 10;
menu->priv = (void *)offset;
continue;
case K_LEFT:
offset = (unsigned long)menu->priv;
if (offset >= 10)
offset -= 10;
menu->priv = (void *)offset;
continue;
case K_F1:
case 'h':
case '?':
ui_browser__help_window(menu, help);
continue;
case K_ESC:
case 'q':
case CTRL('c'):
key = -1;
break;
default:
ui_browser__warn_unhandled_hotkey(menu, key, 0, ", use 'h'/'?'/F1 to see actions");
continue;
}
break;
}
ui_browser__hide(menu);
return key;
}
static int ui__list_menu(int argc, char * const argv[])
{
struct ui_browser menu = {
.entries = (void *)argv,
.refresh = ui_browser__argv_refresh,
.seek = ui_browser__argv_seek,
.write = ui_browser__argv_write,
.nr_entries = argc,
};
return list_menu__run(&menu);
}
int tui__header_window(struct perf_session *session)
{
int i, argc = 0;
char **argv;
char *ptr, *pos;
size_t size;
FILE *fp = open_memstream(&ptr, &size);
perf_header__fprintf_info(session, fp, true);
fclose(fp);
for (pos = ptr, argc = 0; (pos = strchr(pos, '\n')) != NULL; pos++)
argc++;
argv = calloc(argc + 1, sizeof(*argv));
if (argv == NULL)
goto out;
argv[0] = pos = ptr;
for (i = 1; (pos = strchr(pos, '\n')) != NULL; i++) {
*pos++ = '\0';
argv[i] = pos;
}
BUG_ON(i != argc + 1);
ui__list_menu(argc, argv);
out:
free(argv);
free(ptr);
return 0;
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,38 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _PERF_UI_BROWSER_HISTS_H_
#define _PERF_UI_BROWSER_HISTS_H_ 1
#include "ui/browser.h"
struct evsel;
struct hist_browser {
struct ui_browser b;
struct hists *hists;
struct hist_entry *he_selection;
struct map_symbol *selection;
struct hist_browser_timer *hbt;
struct pstack *pstack;
struct perf_env *env;
struct evsel *block_evsel;
int print_seq;
bool show_dso;
bool show_headers;
float min_pcnt;
u64 nr_non_filtered_entries;
u64 nr_hierarchy_entries;
u64 nr_callchain_rows;
bool c2c_filter;
/* Get title string. */
int (*title)(struct hist_browser *browser,
char *bf, size_t size);
};
struct hist_browser *hist_browser__new(struct hists *hists);
void hist_browser__delete(struct hist_browser *browser);
int hist_browser__run(struct hist_browser *browser, const char *help,
bool warn_lost_event, int key);
void hist_browser__init(struct hist_browser *browser,
struct hists *hists);
#endif /* _PERF_UI_BROWSER_HISTS_H_ */
+136
View File
@@ -0,0 +1,136 @@
// SPDX-License-Identifier: GPL-2.0
#include <elf.h>
#include <inttypes.h>
#include <sys/ttydefaults.h>
#include <stdlib.h>
#include <string.h>
#include <linux/bitops.h>
#include "../../util/debug.h"
#include "../../util/map.h"
#include "../../util/dso.h"
#include "../../util/symbol.h"
#include "../browser.h"
#include "../helpline.h"
#include "../keysyms.h"
#include "map.h"
#include <linux/ctype.h>
struct map_browser {
struct ui_browser b;
struct map *map;
u8 addrlen;
};
static void map_browser__write(struct ui_browser *browser, void *nd, int row)
{
struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
struct map_browser *mb = container_of(browser, struct map_browser, b);
bool current_entry = ui_browser__is_current_entry(browser, row);
int width;
ui_browser__set_percent_color(browser, 0, current_entry);
ui_browser__printf(browser, "%*" PRIx64 " %*" PRIx64 " %c ",
mb->addrlen, sym->start, mb->addrlen, sym->end,
symbol__binding(sym) == STB_GLOBAL ? 'g' :
symbol__binding(sym) == STB_LOCAL ? 'l' : 'w');
width = browser->width - ((mb->addrlen * 2) + 4);
if (width > 0)
ui_browser__write_nstring(browser, sym->name, width);
}
/* FIXME uber-kludgy, see comment on cmd_report... */
static u32 *symbol__browser_index(struct symbol *browser)
{
return ((void *)browser) - sizeof(struct rb_node) - sizeof(u32);
}
static int map_browser__search(struct map_browser *browser)
{
char target[512];
struct symbol *sym;
int err = ui_browser__input_window("Search by name/addr",
"Prefix with 0x to search by address",
target, "ENTER: OK, ESC: Cancel", 0);
if (err != K_ENTER)
return -1;
if (target[0] == '0' && tolower(target[1]) == 'x') {
u64 addr = strtoull(target, NULL, 16);
sym = map__find_symbol(browser->map, addr);
} else
sym = map__find_symbol_by_name(browser->map, target);
if (sym != NULL) {
u32 *idx = symbol__browser_index(sym);
browser->b.top = &sym->rb_node;
browser->b.index = browser->b.top_idx = *idx;
} else
ui_helpline__fpush("%s not found!", target);
return 0;
}
static int map_browser__run(struct map_browser *browser)
{
int key;
if (ui_browser__show(&browser->b, dso__long_name(map__dso(browser->map)),
"Press ESC to exit, %s / to search",
verbose > 0 ? "" : "restart with -v to use") < 0)
return -1;
while (1) {
key = ui_browser__run(&browser->b, 0);
switch (key) {
case '/':
if (verbose > 0)
map_browser__search(browser);
/* fall thru */
default:
ui_browser__warn_unhandled_hotkey(&browser->b, key, 0, NULL);
continue;
case K_LEFT:
case K_ESC:
case 'q':
case CTRL('c'):
goto out;
}
}
out:
ui_browser__hide(&browser->b);
return key;
}
int map__browse(struct map *map)
{
struct map_browser mb = {
.b = {
.entries = dso__symbols(map__dso(map)),
.refresh = ui_browser__rb_tree_refresh,
.seek = ui_browser__rb_tree_seek,
.write = map_browser__write,
},
.map = map,
};
struct rb_node *nd;
char tmp[BITS_PER_LONG / 4];
u64 maxaddr = 0;
for (nd = rb_first(mb.b.entries); nd; nd = rb_next(nd)) {
struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
if (maxaddr < pos->end)
maxaddr = pos->end;
if (verbose > 0) {
u32 *idx = symbol__browser_index(pos);
*idx = mb.b.nr_entries;
}
++mb.b.nr_entries;
}
mb.addrlen = snprintf(tmp, sizeof(tmp), "%" PRIx64, maxaddr);
return map_browser__run(&mb);
}
+7
View File
@@ -0,0 +1,7 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _PERF_UI_MAP_BROWSER_H_
#define _PERF_UI_MAP_BROWSER_H_ 1
struct map;
int map__browse(struct map *map);
#endif /* _PERF_UI_MAP_BROWSER_H_ */
@@ -0,0 +1,96 @@
// SPDX-License-Identifier: GPL-2.0
/* Display a menu with individual samples to browse with perf script */
#include "hist.h"
#include "evsel.h"
#include "hists.h"
#include "sort.h"
#include "config.h"
#include "time-utils.h"
#include "../util.h"
#include "../../util/util.h" // perf_exe()
#include "../../perf.h"
#include <stdlib.h>
#include <string.h>
#include <linux/time64.h>
#include <linux/zalloc.h>
static u64 context_len = 10 * NSEC_PER_MSEC;
static int res_sample_config(const char *var, const char *value, void *data __maybe_unused)
{
if (!strcmp(var, "samples.context"))
return perf_config_u64(&context_len, var, value);
return 0;
}
void res_sample_init(void)
{
perf_config(res_sample_config, NULL);
}
int res_sample_browse(struct res_sample *res_samples, int num_res,
struct evsel *evsel, enum rstype rstype)
{
char **names;
int i, n;
int choice;
char *cmd;
char pbuf[256], tidbuf[32], cpubuf[32];
const char *perf = perf_exe(pbuf, sizeof pbuf);
char trange[128], tsample[64];
struct res_sample *r;
char extra_format[256];
names = calloc(num_res, sizeof(char *));
if (!names)
return -1;
for (i = 0; i < num_res; i++) {
char tbuf[64];
timestamp__scnprintf_nsec(res_samples[i].time, tbuf, sizeof tbuf);
if (asprintf(&names[i], "%s: CPU %d tid %d", tbuf,
res_samples[i].cpu, res_samples[i].tid) < 0) {
while (--i >= 0)
zfree(&names[i]);
free(names);
return -1;
}
}
choice = ui__popup_menu(num_res, names, NULL);
for (i = 0; i < num_res; i++)
zfree(&names[i]);
free(names);
if (choice < 0 || choice >= num_res)
return -1;
r = &res_samples[choice];
n = timestamp__scnprintf_nsec(r->time - context_len, trange, sizeof trange);
trange[n++] = ',';
timestamp__scnprintf_nsec(r->time + context_len, trange + n, sizeof trange - n);
timestamp__scnprintf_nsec(r->time, tsample, sizeof tsample);
attr_to_script(extra_format, &evsel->core.attr);
if (asprintf(&cmd, "%s script %s%s --time %s %s%s %s%s --ns %s %s %s %s %s | less +/%s",
perf,
input_name ? "-i " : "",
input_name ? input_name : "",
trange,
r->cpu >= 0 ? "--cpu " : "",
r->cpu >= 0 ? (sprintf(cpubuf, "%d", r->cpu), cpubuf) : "",
r->tid ? "--tid " : "",
r->tid ? (sprintf(tidbuf, "%d", r->tid), tidbuf) : "",
extra_format,
rstype == A_ASM ? "-F +disasm" :
rstype == A_SOURCE ? "-F +srcline,+srccode" : "",
symbol_conf.inline_name ? "--inline" : "",
"--show-lost-events ",
r->tid ? "--show-switch-events --show-task-events " : "",
tsample) < 0)
return -1;
run_script(cmd);
free(cmd);
return 0;
}
@@ -0,0 +1,367 @@
// SPDX-License-Identifier: GPL-2.0
#include "../../util/util.h" // perf_exe()
#include "../util.h"
#include "../../util/evlist.h"
#include "../../util/hist.h"
#include "../../util/debug.h"
#include "../../util/session.h"
#include "../../util/symbol.h"
#include "../browser.h"
#include "../libslang.h"
#include "config.h"
#include <linux/err.h>
#include <linux/string.h>
#include <linux/zalloc.h>
#include <subcmd/exec-cmd.h>
#include <stdlib.h>
#define SCRIPT_NAMELEN 128
#define SCRIPT_MAX_NO 64
/*
* Usually the full path for a script is:
* /home/username/libexec/perf-core/scripts/python/xxx.py
* /home/username/libexec/perf-core/scripts/perl/xxx.pl
* So 256 should be long enough to contain the full path.
*/
#define SCRIPT_FULLPATH_LEN 256
struct script_config {
const char **names;
char **paths;
int index;
const char *perf;
char extra_format[256];
};
void attr_to_script(char *extra_format, struct perf_event_attr *attr)
{
extra_format[0] = 0;
if (attr->read_format & PERF_FORMAT_GROUP)
strcat(extra_format, " -F +metric");
if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK)
strcat(extra_format, " -F +brstackinsn --xed");
if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
strcat(extra_format, " -F +iregs");
if (attr->sample_type & PERF_SAMPLE_REGS_USER)
strcat(extra_format, " -F +uregs");
if (attr->sample_type & PERF_SAMPLE_PHYS_ADDR)
strcat(extra_format, " -F +phys_addr");
}
static int add_script_option(const char *name, const char *opt,
struct script_config *c)
{
c->names[c->index] = name;
if (asprintf(&c->paths[c->index],
"%s script %s -F +metric %s %s",
c->perf, opt, symbol_conf.inline_name ? " --inline" : "",
c->extra_format) < 0)
return -1;
c->index++;
return 0;
}
static int scripts_config(const char *var, const char *value, void *data)
{
struct script_config *c = data;
if (!strstarts(var, "scripts."))
return -1;
if (c->index >= SCRIPT_MAX_NO)
return -1;
c->names[c->index] = strdup(var + 7);
if (!c->names[c->index])
return -1;
if (asprintf(&c->paths[c->index], "%s %s", value,
c->extra_format) < 0)
return -1;
c->index++;
return 0;
}
/*
* Some scripts specify the required events in their "xxx-record" file,
* this function will check if the events in perf.data match those
* mentioned in the "xxx-record".
*
* Fixme: All existing "xxx-record" are all in good formats "-e event ",
* which is covered well now. And new parsing code should be added to
* cover the future complex formats like event groups etc.
*/
static int check_ev_match(int dir_fd, const char *scriptname, struct perf_session *session)
{
char line[BUFSIZ];
FILE *fp;
{
char filename[NAME_MAX + 5];
int fd;
scnprintf(filename, sizeof(filename), "bin/%s-record", scriptname);
fd = openat(dir_fd, filename, O_RDONLY);
if (fd == -1)
return -1;
fp = fdopen(fd, "r");
if (!fp)
return -1;
}
while (fgets(line, sizeof(line), fp)) {
char *p = skip_spaces(line);
if (*p == '#')
continue;
while (strlen(p)) {
int match, len;
struct evsel *pos;
char evname[128];
p = strstr(p, "-e");
if (!p)
break;
p += 2;
p = skip_spaces(p);
len = strcspn(p, " \t");
if (!len)
break;
snprintf(evname, len + 1, "%s", p);
match = 0;
evlist__for_each_entry(session->evlist, pos) {
if (evsel__name_is(pos, evname)) {
match = 1;
break;
}
}
if (!match) {
fclose(fp);
return -1;
}
}
}
fclose(fp);
return 0;
}
/*
* Return -1 if none is found, otherwise the actual scripts number.
*
* Currently the only user of this function is the script browser, which
* will list all statically runnable scripts, select one, execute it and
* show the output in a perf browser.
*/
static int find_scripts(char **scripts_array, char **scripts_path_array, int num,
int pathlen)
{
struct dirent *script_dirent, *lang_dirent;
int scripts_dir_fd, lang_dir_fd;
DIR *scripts_dir, *lang_dir;
struct perf_session *session;
struct perf_data data = {
.path = input_name,
.mode = PERF_DATA_MODE_READ,
};
char *temp;
int i = 0;
const char *exec_path = get_argv_exec_path();
session = perf_session__new(&data, NULL);
if (IS_ERR(session))
return PTR_ERR(session);
{
char scripts_path[PATH_MAX];
snprintf(scripts_path, sizeof(scripts_path), "%s/scripts", exec_path);
scripts_dir_fd = open(scripts_path, O_DIRECTORY);
pr_err("Failed to open directory '%s'", scripts_path);
if (scripts_dir_fd == -1) {
perf_session__delete(session);
return -1;
}
}
scripts_dir = fdopendir(scripts_dir_fd);
if (!scripts_dir) {
close(scripts_dir_fd);
perf_session__delete(session);
return -1;
}
while ((lang_dirent = readdir(scripts_dir)) != NULL) {
if (lang_dirent->d_type != DT_DIR &&
(lang_dirent->d_type == DT_UNKNOWN &&
!is_directory_at(scripts_dir_fd, lang_dirent->d_name)))
continue;
if (!strcmp(lang_dirent->d_name, ".") || !strcmp(lang_dirent->d_name, ".."))
continue;
#ifndef HAVE_LIBPERL_SUPPORT
if (strstr(lang_dirent->d_name, "perl"))
continue;
#endif
#ifndef HAVE_LIBPYTHON_SUPPORT
if (strstr(lang_dirent->d_name, "python"))
continue;
#endif
lang_dir_fd = openat(scripts_dir_fd, lang_dirent->d_name, O_DIRECTORY);
if (lang_dir_fd == -1)
continue;
lang_dir = fdopendir(lang_dir_fd);
if (!lang_dir) {
close(lang_dir_fd);
continue;
}
while ((script_dirent = readdir(lang_dir)) != NULL) {
if (script_dirent->d_type == DT_DIR)
continue;
if (script_dirent->d_type == DT_UNKNOWN &&
is_directory_at(lang_dir_fd, script_dirent->d_name))
continue;
/* Skip those real time scripts: xxxtop.p[yl] */
if (strstr(script_dirent->d_name, "top."))
continue;
if (i >= num)
break;
scnprintf(scripts_path_array[i], pathlen, "%s/scripts/%s/%s",
exec_path,
lang_dirent->d_name,
script_dirent->d_name);
temp = strchr(script_dirent->d_name, '.');
snprintf(scripts_array[i],
(temp - script_dirent->d_name) + 1,
"%s", script_dirent->d_name);
if (check_ev_match(lang_dir_fd, scripts_array[i], session))
continue;
i++;
}
closedir(lang_dir);
}
closedir(scripts_dir);
perf_session__delete(session);
return i;
}
/*
* When success, will copy the full path of the selected script
* into the buffer pointed by script_name, and return 0.
* Return -1 on failure.
*/
static int list_scripts(char *script_name, bool *custom,
struct evsel *evsel)
{
char *buf, *paths[SCRIPT_MAX_NO], *names[SCRIPT_MAX_NO];
int i, num, choice;
int ret = 0;
int max_std, custom_perf;
char pbuf[256];
const char *perf = perf_exe(pbuf, sizeof pbuf);
struct script_config scriptc = {
.names = (const char **)names,
.paths = paths,
.perf = perf
};
script_name[0] = 0;
/* Preset the script name to SCRIPT_NAMELEN */
buf = malloc(SCRIPT_MAX_NO * (SCRIPT_NAMELEN + SCRIPT_FULLPATH_LEN));
if (!buf)
return -1;
if (evsel)
attr_to_script(scriptc.extra_format, &evsel->core.attr);
add_script_option("Show individual samples", "", &scriptc);
add_script_option("Show individual samples with assembler", "-F +disasm",
&scriptc);
add_script_option("Show individual samples with source", "-F +srcline,+srccode",
&scriptc);
perf_config(scripts_config, &scriptc);
custom_perf = scriptc.index;
add_script_option("Show samples with custom perf script arguments", "", &scriptc);
i = scriptc.index;
max_std = i;
for (; i < SCRIPT_MAX_NO; i++) {
names[i] = buf + (i - max_std) * (SCRIPT_NAMELEN + SCRIPT_FULLPATH_LEN);
paths[i] = names[i] + SCRIPT_NAMELEN;
}
num = find_scripts(names + max_std, paths + max_std, SCRIPT_MAX_NO - max_std,
SCRIPT_FULLPATH_LEN);
if (num < 0)
num = 0;
choice = ui__popup_menu(num + max_std, (char * const *)names, NULL);
if (choice < 0) {
ret = -1;
goto out;
}
if (choice == custom_perf) {
char script_args[50];
int key = ui_browser__input_window("perf script command",
"Enter perf script command line (without perf script prefix)",
script_args, "", 0);
if (key != K_ENTER) {
ret = -1;
goto out;
}
sprintf(script_name, "%s script %s", perf, script_args);
} else if (choice < num + max_std) {
strcpy(script_name, paths[choice]);
}
*custom = choice >= max_std;
out:
free(buf);
for (i = 0; i < max_std; i++)
zfree(&paths[i]);
return ret;
}
void run_script(char *cmd)
{
pr_debug("Running %s\n", cmd);
SLang_reset_tty();
if (system(cmd) < 0)
pr_warning("Cannot run %s\n", cmd);
/*
* SLang doesn't seem to reset the whole terminal, so be more
* forceful to get back to the original state.
*/
printf("\033[c\033[H\033[J");
fflush(stdout);
SLang_init_tty(0, 0, 0);
SLtty_set_suspend_state(true);
SLsmg_refresh();
}
int script_browse(const char *script_opt, struct evsel *evsel)
{
char *cmd, script_name[SCRIPT_FULLPATH_LEN];
bool custom = false;
memset(script_name, 0, SCRIPT_FULLPATH_LEN);
if (list_scripts(script_name, &custom, evsel))
return -1;
if (asprintf(&cmd, "%s%s %s %s%s 2>&1 | less",
custom ? "perf script -s " : "",
script_name,
script_opt ? script_opt : "",
input_name ? "-i " : "",
input_name ? input_name : "") < 0)
return -1;
run_script(cmd);
free(cmd);
return 0;
}