Path: blob/master/tools/perf/arch/powerpc/util/kvm-stat.c
29274 views
// SPDX-License-Identifier: GPL-2.01#include <errno.h>2#include "util/kvm-stat.h"3#include "util/parse-events.h"4#include "util/debug.h"5#include "util/evsel.h"6#include "util/evlist.h"7#include "util/pmus.h"89#include "book3s_hv_exits.h"10#include "book3s_hcalls.h"11#include <subcmd/parse-options.h>1213#define NR_TPS 41415const char *vcpu_id_str = "vcpu_id";16const char *kvm_entry_trace = "kvm_hv:kvm_guest_enter";17const char *kvm_exit_trace = "kvm_hv:kvm_guest_exit";1819define_exit_reasons_table(hv_exit_reasons, kvm_trace_symbol_exit);20define_exit_reasons_table(hcall_reasons, kvm_trace_symbol_hcall);2122/* Tracepoints specific to ppc_book3s_hv */23const char *ppc_book3s_hv_kvm_tp[] = {24"kvm_hv:kvm_guest_enter",25"kvm_hv:kvm_guest_exit",26"kvm_hv:kvm_hcall_enter",27"kvm_hv:kvm_hcall_exit",28NULL,29};3031/* 1 extra placeholder for NULL */32const char *kvm_events_tp[NR_TPS + 1];33const char *kvm_exit_reason;3435static void hcall_event_get_key(struct evsel *evsel,36struct perf_sample *sample,37struct event_key *key)38{39key->info = 0;40key->key = evsel__intval(evsel, sample, "req");41}4243static const char *get_hcall_exit_reason(u64 exit_code)44{45struct exit_reasons_table *tbl = hcall_reasons;4647while (tbl->reason != NULL) {48if (tbl->exit_code == exit_code)49return tbl->reason;50tbl++;51}5253pr_debug("Unknown hcall code: %lld\n",54(unsigned long long)exit_code);55return "UNKNOWN";56}5758static bool hcall_event_end(struct evsel *evsel,59struct perf_sample *sample __maybe_unused,60struct event_key *key __maybe_unused)61{62return (evsel__name_is(evsel, kvm_events_tp[3]));63}6465static bool hcall_event_begin(struct evsel *evsel,66struct perf_sample *sample, struct event_key *key)67{68if (evsel__name_is(evsel, kvm_events_tp[2])) {69hcall_event_get_key(evsel, sample, key);70return true;71}7273return false;74}75static void hcall_event_decode_key(struct perf_kvm_stat *kvm __maybe_unused,76struct event_key *key,77char *decode)78{79const char *hcall_reason = get_hcall_exit_reason(key->key);8081scnprintf(decode, KVM_EVENT_NAME_LEN, "%s", hcall_reason);82}8384static struct kvm_events_ops hcall_events = {85.is_begin_event = hcall_event_begin,86.is_end_event = hcall_event_end,87.decode_key = hcall_event_decode_key,88.name = "HCALL-EVENT",89};9091static struct kvm_events_ops exit_events = {92.is_begin_event = exit_event_begin,93.is_end_event = exit_event_end,94.decode_key = exit_event_decode_key,95.name = "VM-EXIT"96};9798struct kvm_reg_events_ops kvm_reg_events_ops[] = {99{ .name = "vmexit", .ops = &exit_events },100{ .name = "hcall", .ops = &hcall_events },101{ NULL, NULL },102};103104const char * const kvm_skip_events[] = {105NULL,106};107108109static int is_tracepoint_available(const char *str, struct evlist *evlist)110{111struct parse_events_error err;112int ret;113114parse_events_error__init(&err);115ret = parse_events(evlist, str, &err);116if (ret)117parse_events_error__print(&err, "tracepoint");118parse_events_error__exit(&err);119return ret;120}121122static int ppc__setup_book3s_hv(struct perf_kvm_stat *kvm,123struct evlist *evlist)124{125const char **events_ptr;126int i, nr_tp = 0, err = -1;127128/* Check for book3s_hv tracepoints */129for (events_ptr = ppc_book3s_hv_kvm_tp; *events_ptr; events_ptr++) {130err = is_tracepoint_available(*events_ptr, evlist);131if (err)132return -1;133nr_tp++;134}135136for (i = 0; i < nr_tp; i++)137kvm_events_tp[i] = ppc_book3s_hv_kvm_tp[i];138139kvm_events_tp[i] = NULL;140kvm_exit_reason = "trap";141kvm->exit_reasons = hv_exit_reasons;142kvm->exit_reasons_isa = "HV";143144return 0;145}146147/* Wrapper to setup kvm tracepoints */148static int ppc__setup_kvm_tp(struct perf_kvm_stat *kvm)149{150struct evlist *evlist = evlist__new();151152if (evlist == NULL)153return -ENOMEM;154155/* Right now, only supported on book3s_hv */156return ppc__setup_book3s_hv(kvm, evlist);157}158159int setup_kvm_events_tp(struct perf_kvm_stat *kvm)160{161return ppc__setup_kvm_tp(kvm);162}163164int cpu_isa_init(struct perf_kvm_stat *kvm, const char *cpuid __maybe_unused)165{166int ret;167168ret = ppc__setup_kvm_tp(kvm);169if (ret) {170kvm->exit_reasons = NULL;171kvm->exit_reasons_isa = NULL;172}173174return ret;175}176177/*178* In case of powerpc architecture, pmu registers are programmable179* by guest kernel. So monitoring guest via host may not provide180* valid samples with default 'cycles' event. It is better to use181* 'trace_imc/trace_cycles' event for guest profiling, since it182* can track the guest instruction pointer in the trace-record.183*184* Function to parse the arguments and return appropriate values.185*/186int kvm_add_default_arch_event(int *argc, const char **argv)187{188const char **tmp;189bool event = false;190int i, j = *argc;191192const struct option event_options[] = {193OPT_BOOLEAN('e', "event", &event, NULL),194OPT_END()195};196197tmp = calloc(j + 1, sizeof(char *));198if (!tmp)199return -EINVAL;200201for (i = 0; i < j; i++)202tmp[i] = argv[i];203204parse_options(j, tmp, event_options, NULL, PARSE_OPT_KEEP_UNKNOWN);205if (!event) {206if (perf_pmus__have_event("trace_imc", "trace_cycles")) {207argv[j++] = strdup("-e");208argv[j++] = strdup("trace_imc/trace_cycles/");209*argc += 2;210} else {211free(tmp);212return -EINVAL;213}214}215216free(tmp);217return 0;218}219220221