cregit-Linux how code gets into the kernel

Release 4.10 tools/perf/builtin-kvm.c

Directory: tools/perf
#include "builtin.h"
#include "perf.h"

#include "util/evsel.h"
#include "util/evlist.h"
#include "util/util.h"
#include "util/cache.h"
#include "util/symbol.h"
#include "util/thread.h"
#include "util/header.h"
#include "util/session.h"
#include "util/intlist.h"
#include <subcmd/parse-options.h>
#include "util/trace-event.h"
#include "util/debug.h"
#include "util/tool.h"
#include "util/stat.h"
#include "util/top.h"
#include "util/data.h"
#include "util/ordered-events.h"

#include <sys/prctl.h>
#ifdef HAVE_TIMERFD_SUPPORT
#include <sys/timerfd.h>
#endif

#include <linux/time64.h>
#include <termios.h>
#include <semaphore.h>
#include <pthread.h>
#include <math.h>

#ifdef HAVE_KVM_STAT_SUPPORT
#include "util/kvm-stat.h"


void exit_event_get_key(struct perf_evsel *evsel, struct perf_sample *sample, struct event_key *key) { key->info = 0; key->key = perf_evsel__intval(evsel, sample, kvm_exit_reason); }

Contributors

PersonTokensPropCommitsCommitProp
xiao guangrongxiao guangrong2256.41%125.00%
arnaldo carvalho de meloarnaldo carvalho de melo820.51%125.00%
alexander yaryginalexander yarygin820.51%125.00%
hemant kumarhemant kumar12.56%125.00%
Total39100.00%4100.00%


bool kvm_exit_event(struct perf_evsel *evsel) { return !strcmp(evsel->name, kvm_exit_trace); }

Contributors

PersonTokensPropCommitsCommitProp
alexander yaryginalexander yarygin1571.43%125.00%
xiao guangrongxiao guangrong419.05%125.00%
arnaldo carvalho de meloarnaldo carvalho de melo14.76%125.00%
hemant kumarhemant kumar14.76%125.00%
Total21100.00%4100.00%


bool exit_event_begin(struct perf_evsel *evsel, struct perf_sample *sample, struct event_key *key) { if (kvm_exit_event(evsel)) { exit_event_get_key(evsel, sample, key); return true; } return false; }

Contributors

PersonTokensPropCommitsCommitProp
xiao guangrongxiao guangrong3375.00%133.33%
arnaldo carvalho de meloarnaldo carvalho de melo818.18%133.33%
alexander yaryginalexander yarygin36.82%133.33%
Total44100.00%3100.00%


bool kvm_entry_event(struct perf_evsel *evsel) { return !strcmp(evsel->name, kvm_entry_trace); }

Contributors

PersonTokensPropCommitsCommitProp
alexander yaryginalexander yarygin2095.24%150.00%
hemant kumarhemant kumar14.76%150.00%
Total21100.00%2100.00%


bool exit_event_end(struct perf_evsel *evsel, struct perf_sample *sample __maybe_unused, struct event_key *key __maybe_unused) { return kvm_entry_event(evsel); }

Contributors

PersonTokensPropCommitsCommitProp
xiao guangrongxiao guangrong2071.43%133.33%
arnaldo carvalho de meloarnaldo carvalho de melo725.00%133.33%
alexander yaryginalexander yarygin13.57%133.33%
Total28100.00%3100.00%


static const char *get_exit_reason(struct perf_kvm_stat *kvm, struct exit_reasons_table *tbl, u64 exit_code) { while (tbl->reason != NULL) { if (tbl->exit_code == exit_code) return tbl->reason; tbl++; } pr_err("unknown kvm exit code:%lld on %s\n", (unsigned long long)exit_code, kvm->exit_reasons_isa); return "UNKNOWN"; }

Contributors

PersonTokensPropCommitsCommitProp
alexander yaryginalexander yarygin6598.48%150.00%
xiao guangrongxiao guangrong11.52%150.00%
Total66100.00%2100.00%


void exit_event_decode_key(struct perf_kvm_stat *kvm, struct event_key *key, char *decode) { const char *exit_reason = get_exit_reason(kvm, key->exit_reasons, key->key); scnprintf(decode, decode_str_len, "%s", exit_reason); }

Contributors

PersonTokensPropCommitsCommitProp
alexander yaryginalexander yarygin2245.83%342.86%
xiao guangrongxiao guangrong2143.75%228.57%
david aherndavid ahern48.33%114.29%
hemant kumarhemant kumar12.08%114.29%
Total48100.00%7100.00%


static bool register_kvm_events_ops(struct perf_kvm_stat *kvm) { struct kvm_reg_events_ops *events_ops = kvm_reg_events_ops; for (events_ops = kvm_reg_events_ops; events_ops->name; events_ops++) { if (!strcmp(events_ops->name, kvm->report_event)) { kvm->events_ops = events_ops->ops; return true; } } return false; }

Contributors

PersonTokensPropCommitsCommitProp
xiao guangrongxiao guangrong2742.86%250.00%
alexander yaryginalexander yarygin2742.86%125.00%
david aherndavid ahern914.29%125.00%
Total63100.00%4100.00%

struct vcpu_event_record { int vcpu_id; u64 start_time; struct kvm_event *last_event; };
static void init_kvm_event_record(struct perf_kvm_stat *kvm) { unsigned int i; for (i = 0; i < EVENTS_CACHE_SIZE; i++) INIT_LIST_HEAD(&kvm->kvm_events_cache[i]); }

Contributors

PersonTokensPropCommitsCommitProp
xiao guangrongxiao guangrong3384.62%250.00%
david aherndavid ahern615.38%250.00%
Total39100.00%4100.00%

#ifdef HAVE_TIMERFD_SUPPORT
static void clear_events_cache_stats(struct list_head *kvm_events_cache) { struct list_head *head; struct kvm_event *event; unsigned int i; int j; for (i = 0; i < EVENTS_CACHE_SIZE; i++) { head = &kvm_events_cache[i]; list_for_each_entry(event, head, hash_entry) { /* reset stats for event */ event->total.time = 0; init_stats(&event->total.stats); for (j = 0; j < event->max_vcpu; ++j) { event->vcpu[j].time = 0; init_stats(&event->vcpu[j].stats); } } } }

Contributors

PersonTokensPropCommitsCommitProp
david aherndavid ahern121100.00%2100.00%
Total121100.00%2100.00%

#endif
static int kvm_events_hash_fn(u64 key) { return key & (EVENTS_CACHE_SIZE - 1); }

Contributors

PersonTokensPropCommitsCommitProp
xiao guangrongxiao guangrong18100.00%1100.00%
Total18100.00%1100.00%


static bool kvm_event_expand(struct kvm_event *event, int vcpu_id) { int old_max_vcpu = event->max_vcpu; void *prev; if (vcpu_id < event->max_vcpu) return true; while (event->max_vcpu <= vcpu_id) event->max_vcpu += DEFAULT_VCPU_NUM; prev = event->vcpu; event->vcpu = realloc(event->vcpu, event->max_vcpu * sizeof(*event->vcpu)); if (!event->vcpu) { free(prev); pr_err("Not enough memory\n"); return false; } memset(event->vcpu + old_max_vcpu, 0, (event->max_vcpu - old_max_vcpu) * sizeof(*event->vcpu)); return true; }

Contributors

PersonTokensPropCommitsCommitProp
xiao guangrongxiao guangrong11688.55%150.00%
david aherndavid ahern1511.45%150.00%
Total131100.00%2100.00%


static struct kvm_event *kvm_alloc_init_event(struct event_key *key) { struct kvm_event *event; event = zalloc(sizeof(*event)); if (!event) { pr_err("Not enough memory\n"); return NULL; } event->key = *key; init_stats(&event->total.stats); return event; }

Contributors

PersonTokensPropCommitsCommitProp
xiao guangrongxiao guangrong5484.38%150.00%
alexander yaryginalexander yarygin1015.62%150.00%
Total64100.00%2100.00%


static struct kvm_event *find_create_kvm_event(struct perf_kvm_stat *kvm, struct event_key *key) { struct kvm_event *event; struct list_head *head; BUG_ON(key->key == INVALID_KEY); head = &kvm->kvm_events_cache[kvm_events_hash_fn(key->key)]; list_for_each_entry(event, head, hash_entry) { if (event->key.key == key->key && event->key.info == key->info) return event; } event = kvm_alloc_init_event(key); if (!event) return NULL; list_add(&event->hash_entry, head); return event; }

Contributors

PersonTokensPropCommitsCommitProp
xiao guangrongxiao guangrong10793.04%250.00%
david aherndavid ahern86.96%250.00%
Total115100.00%4100.00%


static bool handle_begin_event(struct perf_kvm_stat *kvm, struct vcpu_event_record *vcpu_record, struct event_key *key, u64 timestamp) { struct kvm_event *event = NULL; if (key->key != INVALID_KEY) event = find_create_kvm_event(kvm, key); vcpu_record->last_event = event; vcpu_record->start_time = timestamp; return true; }

Contributors

PersonTokensPropCommitsCommitProp
xiao guangrongxiao guangrong5790.48%266.67%
david aherndavid ahern69.52%133.33%
Total63100.00%3100.00%


static void kvm_update_event_stats(struct kvm_event_stats *kvm_stats, u64 time_diff) { kvm_stats->time += time_diff; update_stats(&kvm_stats->stats, time_diff); }

Contributors

PersonTokensPropCommitsCommitProp
xiao guangrongxiao guangrong30100.00%1100.00%
Total30100.00%1100.00%


static double kvm_event_rel_stddev(int vcpu_id, struct kvm_event *event) { struct kvm_event_stats *kvm_stats = &event->total; if (vcpu_id != -1) kvm_stats = &event->vcpu[vcpu_id]; return rel_stddev_stats(stddev_stats(&kvm_stats->stats), avg_stats(&kvm_stats->stats)); }

Contributors

PersonTokensPropCommitsCommitProp
xiao guangrongxiao guangrong61100.00%1100.00%
Total61100.00%1100.00%


static bool update_kvm_event(struct kvm_event *event, int vcpu_id, u64 time_diff) { if (vcpu_id == -1) { kvm_update_event_stats(&event->total, time_diff); return true; } if (!kvm_event_expand(event, vcpu_id)) return false; kvm_update_event_stats(&event->vcpu[vcpu_id], time_diff); return true; }

Contributors

PersonTokensPropCommitsCommitProp
xiao guangrongxiao guangrong5682.35%150.00%
david aherndavid ahern1217.65%150.00%
Total68100.00%2100.00%


static bool is_child_event(struct perf_kvm_stat *kvm, struct perf_evsel *evsel, struct perf_sample *sample, struct event_key *key) { struct child_event_ops *child_ops; child_ops = kvm->events_ops->child_ops; if (!child_ops) return false; for (; child_ops->name; child_ops++) { if (!strcmp(evsel->name, child_ops->name)) { child_ops->get_key(evsel, sample, key); return true; } } return false; }

Contributors

PersonTokensPropCommitsCommitProp
alexander yaryginalexander yarygin92100.00%1100.00%
Total92100.00%1100.00%


static bool handle_child_event(struct perf_kvm_stat *kvm, struct vcpu_event_record *vcpu_record, struct event_key *key, struct perf_sample *sample __maybe_unused) { struct kvm_event *event = NULL; if (key->key != INVALID_KEY) event = find_create_kvm_event(kvm, key); vcpu_record->last_event = event; return true; }

Contributors

PersonTokensPropCommitsCommitProp
alexander yaryginalexander yarygin60100.00%1100.00%
Total60100.00%1100.00%


static bool skip_event(const char *event) { const char * const *skip_events; for (skip_events = kvm_skip_events; *skip_events; skip_events++) if (!strcmp(event, *skip_events)) return true; return false; }

Contributors

PersonTokensPropCommitsCommitProp
alexander yaryginalexander yarygin47100.00%1100.00%
Total47100.00%1100.00%


static bool handle_end_event(struct perf_kvm_stat *kvm, struct vcpu_event_record *vcpu_record, struct event_key *key, struct perf_sample *sample) { struct kvm_event *event; u64 time_begin, time_diff; int vcpu; if (kvm->trace_vcpu == -1) vcpu = -1; else vcpu = vcpu_record->vcpu_id; event = vcpu_record->last_event; time_begin = vcpu_record->start_time; /* The begin event is not caught. */ if (!time_begin) return true; /* * In some case, the 'begin event' only records the start timestamp, * the actual event is recognized in the 'end event' (e.g. mmio-event). */ /* Both begin and end events did not get the key. */ if (!event && key->key == INVALID_KEY) return true; if (!event) event = find_create_kvm_event(kvm, key); if (!event) return false; vcpu_record->last_event = NULL; vcpu_record->start_time = 0; /* seems to happen once in a while during live mode */ if (sample->time < time_begin) { pr_debug("End time before begin time; skipping event.\n"); return true; } time_diff = sample->time - time_begin; if (kvm->duration && time_diff > kvm->duration) { char decode[decode_str_len]; kvm->events_ops->decode_key(kvm, &event->key, decode); if (!skip_event(decode)) { pr_info("%" PRIu64 " VM %d, vcpu %d: %s event took %" PRIu64 "usec\n", sample->time, sample->pid, vcpu_record->vcpu_id, decode, time_diff / NSEC_PER_USEC); } } return update_kvm_event(event, vcpu, time_diff); }

Contributors

PersonTokensPropCommitsCommitProp
david aherndavid ahern12250.62%444.44%
xiao guangrongxiao guangrong11547.72%222.22%
alexander yaryginalexander yarygin20.83%111.11%
arnaldo carvalho de meloarnaldo carvalho de melo10.41%111.11%
hemant kumarhemant kumar10.41%111.11%
Total241100.00%9100.00%


static struct vcpu_event_record *per_vcpu_record(struct thread *thread, struct perf_evsel *evsel, struct perf_sample *sample) { /* Only kvm_entry records vcpu id. */ if (!thread__priv(thread) && kvm_entry_event(evsel)) { struct vcpu_event_record *vcpu_record; vcpu_record = zalloc(sizeof(*vcpu_record)); if (!vcpu_record) { pr_err("%s: Not enough memory\n", __func__); return NULL; } vcpu_record->vcpu_id = perf_evsel__intval(evsel, sample, vcpu_id_str); thread__set_priv(thread, vcpu_record); } return thread__priv(thread); }

Contributors

PersonTokensPropCommitsCommitProp
xiao guangrongxiao guangrong7374.49%125.00%
arnaldo carvalho de meloarnaldo carvalho de melo1414.29%125.00%
namhyung kimnamhyung kim1010.20%125.00%
hemant kumarhemant kumar11.02%125.00%
Total98100.00%4100.00%


static bool handle_kvm_event(struct perf_kvm_stat *kvm, struct thread *thread, struct perf_evsel *evsel, struct perf_sample *sample) { struct vcpu_event_record *vcpu_record; struct event_key key = { .key = INVALID_KEY, .exit_reasons = kvm->exit_reasons }; vcpu_record = per_vcpu_record(thread, evsel, sample); if (!vcpu_record) return true; /* only process events for vcpus user cares about */ if ((kvm->trace_vcpu != -1) && (kvm->trace_vcpu != vcpu_record->vcpu_id)) return true; if (kvm->events_ops->is_begin_event(evsel, sample, &key)) return handle_begin_event(kvm, vcpu_record, &key, sample->time); if (is_child_event(kvm, evsel, sample, &key)) return handle_child_event(kvm, vcpu_record, &key, sample); if (kvm->events_ops->is_end_event(evsel, sample, &key)) return handle_end_event(kvm, vcpu_record, &key, sample); return true; }

Contributors

PersonTokensPropCommitsCommitProp
xiao guangrongxiao guangrong7540.98%233.33%
david aherndavid ahern6233.88%233.33%
alexander yaryginalexander yarygin3418.58%116.67%
arnaldo carvalho de meloarnaldo carvalho de melo126.56%116.67%
Total183100.00%6100.00%

#define GET_EVENT_KEY(func, field) \ static u64 get_event_ ##func(struct kvm_event *event, int vcpu) \ { \ if (vcpu == -1) \ return event->total.field; \ \ if (vcpu >= event->max_vcpu) \ return 0; \ \ return event->vcpu[vcpu].field; \ } #define COMPARE_EVENT_KEY(func, field) \ GET_EVENT_KEY(func, field) \ static int compare_kvm_event_ ## func(struct kvm_event *one, \ struct kvm_event *two, int vcpu)\ { \ return get_event_ ##func(one, vcpu) > \ get_event_ ##func(two, vcpu); \ } GET_EVENT_KEY(time, time); COMPARE_EVENT_KEY(count, stats.n); COMPARE_EVENT_KEY(mean, stats.mean); GET_EVENT_KEY(max, stats.max); GET_EVENT_KEY(min, stats.min); #define DEF_SORT_NAME_KEY(name, compare_key) \ { #name, compare_kvm_event_ ## compare_key } static struct kvm_event_key keys[] = { DEF_SORT_NAME_KEY(sample, count), DEF_SORT_NAME_KEY(time, mean), { NULL, NULL } };
static bool select_key(struct perf_kvm_stat *kvm) { int i; for (i = 0; keys[i].name; i++) { if (!strcmp(keys[i].name, kvm->sort_key)) { kvm->compare = keys[i].key; return true; } } pr_err("Unknown compare key:%s\n", kvm->sort_key); return false; }

Contributors

PersonTokensPropCommitsCommitProp
xiao guangrongxiao guangrong6888.31%266.67%
david aherndavid ahern911.69%133.33%
Total77100.00%3100.00%


static void insert_to_result(struct rb_root *result, struct kvm_event *event, key_cmp_fun bigger, int vcpu) { struct rb_node **rb = &result->rb_node; struct rb_node *parent = NULL; struct kvm_event *p; while (*rb) { p = container_of(*rb, struct kvm_event, rb); parent = *rb; if (bigger(event, p, vcpu)) rb = &(*rb)->rb_left; else rb = &(*rb)->rb_right; } rb_link_node(&event->rb, parent, rb); rb_insert_color(&event->rb, result); }

Contributors

PersonTokensPropCommitsCommitProp
xiao guangrongxiao guangrong11895.16%150.00%
david aherndavid ahern64.84%150.00%
Total124100.00%2100.00%


static void update_total_count(struct perf_kvm_stat *kvm, struct kvm_event *event) { int vcpu = kvm->trace_vcpu; kvm->total_count += get_event_count(event, vcpu); kvm->total_time += get_event_time(event, vcpu); }

Contributors

PersonTokensPropCommitsCommitProp
xiao guangrongxiao guangrong3066.67%266.67%
david aherndavid ahern1533.33%133.33%
Total45100.00%3100.00%


static bool event_is_valid(struct kvm_event *event, int vcpu) { return !!get_event_count(event, vcpu); }

Contributors

PersonTokensPropCommitsCommitProp
xiao guangrongxiao guangrong24100.00%1100.00%
Total24100.00%1100.00%


static void sort_result(struct perf_kvm_stat *kvm) { unsigned int i; int vcpu = kvm->trace_vcpu; struct kvm_event *event; for (i = 0; i < EVENTS_CACHE_SIZE; i++) { list_for_each_entry(event, &kvm->kvm_events_cache[i], hash_entry) { if (event_is_valid(event, vcpu)) { update_total_count(kvm, event); insert_to_result(&kvm->result, event, kvm->compare, vcpu); } } } }

Contributors

PersonTokensPropCommitsCommitProp
xiao guangrongxiao guangrong6272.09%250.00%
david aherndavid ahern2427.91%250.00%
Total86100.00%4100.00%

/* returns left most element of result, and erase it */
static struct kvm_event *pop_from_result(struct rb_root *result) { struct rb_node *node = rb_first(result); if (!node) return NULL; rb_erase(node, result); return container_of(node, struct kvm_event, rb); }

Contributors

PersonTokensPropCommitsCommitProp
xiao guangrongxiao guangrong4591.84%150.00%
david aherndavid ahern48.16%150.00%
Total49100.00%2100.00%


static void print_vcpu_info(struct perf_kvm_stat *kvm) { int vcpu = kvm->trace_vcpu; pr_info("Analyze events for "); if (kvm->opts.target.system_wide) pr_info("all VMs, "); else if (kvm->opts.target.pid) pr_info("pid(s) %s, ", kvm->opts.target.pid); else pr_info("dazed and confused on what is monitored, "); if (vcpu == -1) pr_info("all VCPUs:\n\n"); else pr_info("VCPU %d:\n\n", vcpu); }

Contributors

PersonTokensPropCommitsCommitProp
david aherndavid ahern5663.64%150.00%
xiao guangrongxiao guangrong3236.36%150.00%
Total88100.00%2100.00%


static void show_timeofday(void) { char date[64]; struct timeval tv; struct tm ltime; gettimeofday(&tv, NULL); if (localtime_r(&tv.tv_sec, &ltime)) { strftime(date, sizeof(date), "%H:%M:%S", &ltime); pr_info("%s.%06ld", date, tv.tv_usec); } else pr_info("00:00:00.000000"); return; }

Contributors

PersonTokensPropCommitsCommitProp
david aherndavid ahern4760.26%266.67%
xiao guangrongxiao guangrong3139.74%133.33%
Total78100.00%3100.00%


static void print_result(struct perf_kvm_stat *kvm) { char decode[decode_str_len]; struct kvm_event *event; int vcpu = kvm->trace_vcpu; if (kvm->live) { puts(CONSOLE_CLEAR); show_timeofday(); } pr_info("\n\n"); print_vcpu_info(kvm); pr_info("%*s ", decode_str_len, kvm->events_ops->name); pr_info("%10s ", "Samples"); pr_info("%9s ", "Samples%"); pr_info("%9s ", "Time%"); pr_info("%11s ", "Min Time"); pr_info("%11s ", "Max Time"); pr_info("%16s ", "Avg time"); pr_info("\n\n"); while ((event = pop_from_result(&kvm->result))) { u64 ecount, etime, max, min; ecount = get_event_count(event, vcpu); etime = get_event_time(event, vcpu); max = get_event_max(event, vcpu); min = get_event_min(event, vcpu); kvm->events_ops->decode_key(kvm, &event->key, decode); pr_info("%*s ", decode_str_len, decode); pr_info("%10llu ", (unsigned long long)ecount); pr_info("%8.2f%% ", (double)ecount / kvm->total_count * 100); pr_info("%8.2f%% ", (double)etime / kvm->total_time * 100); pr_info("%9.2fus ", (double)min / NSEC_PER_USEC); pr_info("%9.2fus ", (double)max / NSEC_PER_USEC); pr_info("%9.2fus ( +-%7.2f%% )", (double)etime / ecount / NSEC_PER_USEC, kvm_event_rel_stddev(vcpu, event)); pr_info("\n"); } pr_info("\nTotal Samples:%" PRIu64 ", Total events handled time:%.2fus.\n\n", kvm->total_count, kvm->total_time / (double)NSEC_PER_USEC); if (kvm->lost_events) pr_info("\nLost events: %" PRIu64 "\n\n", kvm->lost_events); }

Contributors

PersonTokensPropCommitsCommitProp
david aherndavid ahern17552.71%444.44%
xiao guangrongxiao guangrong13340.06%111.11%
christian borntraegerchristian borntraeger103.01%111.11%
arnaldo carvalho de meloarnaldo carvalho de melo72.11%111.11%
alexander yaryginalexander yarygin41.20%111.11%
hemant kumarhemant kumar30.90%111.11%
Total332100.00%9100.00%

#ifdef HAVE_TIMERFD_SUPPORT
static int process_lost_event(struct perf_tool *tool, union perf_event *event __maybe_unused, struct perf_sample *sample __maybe_unused, struct machine *machine __maybe_unused) { struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat, tool); kvm->lost_events++; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
david aherndavid ahern5198.08%150.00%
xiao guangrongxiao guangrong11.92%150.00%
Total52100.00%2100.00%

#endif
static bool skip_sample(struct perf_kvm_stat *kvm, struct perf_sample *sample) { if (kvm->pid_list && intlist__find(kvm->pid_list, sample->pid) == NULL) return