summaryrefslogtreecommitdiff
path: root/tools/perf/util/tool_pmu.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/perf/util/tool_pmu.c')
-rw-r--r--tools/perf/util/tool_pmu.c406
1 files changed, 312 insertions, 94 deletions
diff --git a/tools/perf/util/tool_pmu.c b/tools/perf/util/tool_pmu.c
index 4fb097578479..5c30854b4644 100644
--- a/tools/perf/util/tool_pmu.c
+++ b/tools/perf/util/tool_pmu.c
@@ -6,6 +6,7 @@
#include "pmu.h"
#include "print-events.h"
#include "smt.h"
+#include "stat.h"
#include "time-utils.h"
#include "tool_pmu.h"
#include "tsc.h"
@@ -16,6 +17,8 @@
#include <fcntl.h>
#include <strings.h>
+#define INVALID_START_TIME ~0ULL
+
static const char *const tool_pmu__event_names[TOOL_PMU__EVENT_MAX] = {
NULL,
"duration_time",
@@ -30,6 +33,8 @@ static const char *const tool_pmu__event_names[TOOL_PMU__EVENT_MAX] = {
"slots",
"smt_on",
"system_tsc_freq",
+ "core_wide",
+ "target_cpu",
};
bool tool_pmu__skip_event(const char *name __maybe_unused)
@@ -62,7 +67,8 @@ int tool_pmu__num_skip_events(void)
const char *tool_pmu__event_to_str(enum tool_pmu_event ev)
{
- if (ev > TOOL_PMU__EVENT_NONE && ev < TOOL_PMU__EVENT_MAX)
+ if ((ev > TOOL_PMU__EVENT_NONE && ev < TOOL_PMU__EVENT_MAX) &&
+ !tool_pmu__skip_event(tool_pmu__event_names[ev]))
return tool_pmu__event_names[ev];
return NULL;
@@ -201,20 +207,57 @@ int evsel__tool_pmu_prepare_open(struct evsel *evsel,
struct perf_cpu_map *cpus,
int nthreads)
{
- if ((evsel__tool_event(evsel) == TOOL_PMU__EVENT_SYSTEM_TIME ||
- evsel__tool_event(evsel) == TOOL_PMU__EVENT_USER_TIME) &&
- !evsel->start_times) {
- evsel->start_times = xyarray__new(perf_cpu_map__nr(cpus),
- nthreads,
- sizeof(__u64));
- if (!evsel->start_times)
- return -ENOMEM;
+ enum tool_pmu_event ev = evsel__tool_event(evsel);
+
+ if (ev == TOOL_PMU__EVENT_SYSTEM_TIME || ev == TOOL_PMU__EVENT_USER_TIME) {
+ if (!evsel->process_time.start_times) {
+ evsel->process_time.start_times =
+ xyarray__new(perf_cpu_map__nr(cpus), nthreads, sizeof(__u64));
+ if (!evsel->process_time.start_times)
+ return -ENOMEM;
+ }
+ if (!evsel->process_time.accumulated_times) {
+ evsel->process_time.accumulated_times =
+ xyarray__new(perf_cpu_map__nr(cpus), nthreads, sizeof(__u64));
+ if (!evsel->process_time.accumulated_times)
+ return -ENOMEM;
+ }
}
return 0;
}
#define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y))
+static int tool_pmu__read_stat(struct evsel *evsel, int cpu_map_idx, int thread, __u64 *val)
+{
+ enum tool_pmu_event ev = evsel__tool_event(evsel);
+ bool system = ev == TOOL_PMU__EVENT_SYSTEM_TIME;
+ int fd = FD(evsel, cpu_map_idx, thread);
+ int err = 0;
+
+ if (fd < 0) {
+ *val = 0;
+ return 0;
+ }
+
+ lseek(fd, 0, SEEK_SET);
+ if (evsel->pid_stat) {
+ if (cpu_map_idx == 0)
+ err = read_pid_stat_field(fd, system ? 15 : 14, val);
+ else
+ *val = 0;
+ } else {
+ if (thread == 0) {
+ struct perf_cpu cpu = perf_cpu_map__cpu(evsel->core.cpus, cpu_map_idx);
+
+ err = read_stat_field(fd, cpu, system ? 3 : 1, val);
+ } else {
+ *val = 0;
+ }
+ }
+ return err;
+}
+
int evsel__tool_pmu_open(struct evsel *evsel,
struct perf_thread_map *threads,
int start_cpu_map_idx, int end_cpu_map_idx)
@@ -228,7 +271,14 @@ int evsel__tool_pmu_open(struct evsel *evsel,
if (ev == TOOL_PMU__EVENT_DURATION_TIME) {
if (evsel->core.attr.sample_period) /* no sampling */
return -EINVAL;
- evsel->start_time = rdclock();
+ evsel->duration_time.accumulated_time = 0;
+ if (evsel->core.attr.disabled) {
+ evsel->disabled = true;
+ evsel->duration_time.start_time = INVALID_START_TIME;
+ } else {
+ evsel->disabled = false;
+ evsel->duration_time.start_time = rdclock();
+ }
return 0;
}
@@ -238,15 +288,12 @@ int evsel__tool_pmu_open(struct evsel *evsel,
nthreads = perf_thread_map__nr(threads);
for (idx = start_cpu_map_idx; idx < end_cpu_map_idx; idx++) {
for (thread = 0; thread < nthreads; thread++) {
- if (thread >= nthreads)
- break;
-
if (!evsel->cgrp && !evsel->core.system_wide)
pid = perf_thread_map__pid(threads, thread);
if (ev == TOOL_PMU__EVENT_USER_TIME || ev == TOOL_PMU__EVENT_SYSTEM_TIME) {
- bool system = ev == TOOL_PMU__EVENT_SYSTEM_TIME;
__u64 *start_time = NULL;
+ __u64 *accumulated_time = NULL;
int fd;
if (evsel->core.attr.sample_period) {
@@ -268,21 +315,25 @@ int evsel__tool_pmu_open(struct evsel *evsel,
err = -errno;
goto out_close;
}
- start_time = xyarray__entry(evsel->start_times, idx, thread);
- if (pid > -1) {
- err = read_pid_stat_field(fd, system ? 15 : 14,
- start_time);
+ start_time = xyarray__entry(evsel->process_time.start_times, idx,
+ thread);
+ accumulated_time = xyarray__entry(
+ evsel->process_time.accumulated_times, idx, thread);
+ *accumulated_time = 0;
+
+ if (evsel->core.attr.disabled) {
+ evsel->disabled = true;
+ *start_time = INVALID_START_TIME;
} else {
- struct perf_cpu cpu;
-
- cpu = perf_cpu_map__cpu(evsel->core.cpus, idx);
- err = read_stat_field(fd, cpu, system ? 3 : 1,
- start_time);
+ evsel->disabled = false;
+ err = tool_pmu__read_stat(evsel, idx, thread, start_time);
+ if (err) {
+ close(fd);
+ FD(evsel, idx, thread) = -1;
+ goto out_close;
+ }
}
- if (err)
- goto out_close;
}
-
}
}
return 0;
@@ -331,7 +382,11 @@ static bool has_pmem(void)
return has_pmem;
}
-bool tool_pmu__read_event(enum tool_pmu_event ev, u64 *result)
+bool tool_pmu__read_event(enum tool_pmu_event ev,
+ struct evsel *evsel,
+ bool system_wide,
+ const char *user_requested_cpu_list,
+ u64 *result)
{
const struct cpu_topology *topology;
@@ -346,17 +401,60 @@ bool tool_pmu__read_event(enum tool_pmu_event ev, u64 *result)
return true;
case TOOL_PMU__EVENT_NUM_CPUS:
- *result = cpu__max_present_cpu().cpu;
+ if (!evsel || perf_cpu_map__is_empty(evsel->core.cpus)) {
+ /* No evsel to be specific to. */
+ *result = cpu__max_present_cpu().cpu;
+ } else if (!perf_cpu_map__has_any_cpu(evsel->core.cpus)) {
+ /* Evsel just has specific CPUs. */
+ *result = perf_cpu_map__nr(evsel->core.cpus);
+ } else {
+ /*
+ * "Any CPU" event that can be scheduled on any CPU in
+ * the PMU's cpumask. The PMU cpumask should be saved in
+ * pmu_cpus. If not present fall back to max.
+ */
+ if (!perf_cpu_map__is_empty(evsel->core.pmu_cpus))
+ *result = perf_cpu_map__nr(evsel->core.pmu_cpus);
+ else
+ *result = cpu__max_present_cpu().cpu;
+ }
return true;
case TOOL_PMU__EVENT_NUM_CPUS_ONLINE: {
struct perf_cpu_map *online = cpu_map__online();
- if (online) {
+ if (!online)
+ return false;
+
+ if (!evsel || perf_cpu_map__is_empty(evsel->core.cpus)) {
+ /* No evsel to be specific to. */
*result = perf_cpu_map__nr(online);
- return true;
+ } else if (!perf_cpu_map__has_any_cpu(evsel->core.cpus)) {
+ /* Evsel just has specific CPUs. */
+ struct perf_cpu_map *tmp =
+ perf_cpu_map__intersect(online, evsel->core.cpus);
+
+ *result = perf_cpu_map__nr(tmp);
+ perf_cpu_map__put(tmp);
+ } else {
+ /*
+ * "Any CPU" event that can be scheduled on any CPU in
+ * the PMU's cpumask. The PMU cpumask should be saved in
+ * pmu_cpus, if not present then just the online cpu
+ * mask.
+ */
+ if (!perf_cpu_map__is_empty(evsel->core.pmu_cpus)) {
+ struct perf_cpu_map *tmp =
+ perf_cpu_map__intersect(online, evsel->core.pmu_cpus);
+
+ *result = perf_cpu_map__nr(tmp);
+ perf_cpu_map__put(tmp);
+ } else {
+ *result = perf_cpu_map__nr(online);
+ }
}
- return false;
+ perf_cpu_map__put(online);
+ return true;
}
case TOOL_PMU__EVENT_NUM_DIES:
topology = online_topology();
@@ -380,6 +478,14 @@ bool tool_pmu__read_event(enum tool_pmu_event ev, u64 *result)
*result = arch_get_tsc_freq();
return true;
+ case TOOL_PMU__EVENT_CORE_WIDE:
+ *result = core_wide(system_wide, user_requested_cpu_list) ? 1 : 0;
+ return true;
+
+ case TOOL_PMU__EVENT_TARGET_CPU:
+ *result = system_wide || (user_requested_cpu_list != NULL) ? 1 : 0;
+ return true;
+
case TOOL_PMU__EVENT_NONE:
case TOOL_PMU__EVENT_DURATION_TIME:
case TOOL_PMU__EVENT_USER_TIME:
@@ -390,16 +496,140 @@ bool tool_pmu__read_event(enum tool_pmu_event ev, u64 *result)
}
}
+static void perf_counts__update(struct perf_counts_values *count,
+ const struct perf_counts_values *old_count,
+ bool raw, u64 val)
+{
+ /*
+ * The values of enabled and running must make a ratio of 100%. The
+ * exact values don't matter as long as they are non-zero to avoid
+ * issues with evsel__count_has_error.
+ */
+ if (old_count) {
+ count->val = raw ? val : old_count->val + val;
+ count->run = old_count->run + 1;
+ count->ena = old_count->ena + 1;
+ count->lost = old_count->lost;
+ } else {
+ count->val = val;
+ count->run++;
+ count->ena++;
+ count->lost = 0;
+ }
+}
+int evsel__tool_pmu_enable_cpu(struct evsel *evsel, int cpu_map_idx)
+{
+ enum tool_pmu_event ev = evsel__tool_event(evsel);
+ int thread, nthreads;
+
+ if (!evsel->disabled)
+ return 0;
+
+ if (ev == TOOL_PMU__EVENT_DURATION_TIME) {
+ if (cpu_map_idx == 0)
+ evsel->duration_time.start_time = rdclock();
+ return 0;
+ }
+
+ if (ev == TOOL_PMU__EVENT_USER_TIME || ev == TOOL_PMU__EVENT_SYSTEM_TIME) {
+ nthreads = xyarray__max_y(evsel->process_time.start_times);
+ for (thread = 0; thread < nthreads; thread++) {
+ __u64 *start_time = xyarray__entry(evsel->process_time.start_times,
+ cpu_map_idx, thread);
+ __u64 val;
+ int err;
+
+ err = tool_pmu__read_stat(evsel, cpu_map_idx, thread, &val);
+ if (!err)
+ *start_time = val;
+ else
+ *start_time = INVALID_START_TIME;
+ }
+ }
+ return 0;
+}
+
+int evsel__tool_pmu_enable(struct evsel *evsel)
+{
+ unsigned int idx;
+ int err = 0;
+
+ if (!evsel->disabled)
+ return 0;
+
+ for (idx = 0; idx < perf_cpu_map__nr(evsel->core.cpus); idx++) {
+ err = evsel__tool_pmu_enable_cpu(evsel, idx);
+ if (err)
+ break;
+ }
+ return err;
+}
+
+int evsel__tool_pmu_disable_cpu(struct evsel *evsel, int cpu_map_idx)
+{
+ enum tool_pmu_event ev = evsel__tool_event(evsel);
+ int thread, nthreads;
+
+ if (evsel->disabled)
+ return 0;
+
+ if (ev == TOOL_PMU__EVENT_DURATION_TIME) {
+ if (cpu_map_idx == 0) {
+ __u64 delta = rdclock() - evsel->duration_time.start_time;
+
+ evsel->duration_time.accumulated_time += delta;
+ }
+ return 0;
+ }
+
+ if (ev == TOOL_PMU__EVENT_USER_TIME || ev == TOOL_PMU__EVENT_SYSTEM_TIME) {
+ nthreads = xyarray__max_y(evsel->process_time.start_times);
+ for (thread = 0; thread < nthreads; thread++) {
+ __u64 *start_time = xyarray__entry(evsel->process_time.start_times,
+ cpu_map_idx, thread);
+ __u64 *accumulated_time = xyarray__entry(
+ evsel->process_time.accumulated_times, cpu_map_idx, thread);
+ __u64 val;
+ int err;
+
+ err = tool_pmu__read_stat(evsel, cpu_map_idx, thread, &val);
+ if (!err) {
+ if (*start_time != INVALID_START_TIME && val >= *start_time)
+ *accumulated_time += (val - *start_time);
+ }
+ *start_time = INVALID_START_TIME;
+ }
+ }
+ return 0;
+}
+
+int evsel__tool_pmu_disable(struct evsel *evsel)
+{
+ unsigned int idx;
+ int err = 0;
+
+ if (evsel->disabled)
+ return 0;
+
+ for (idx = 0; idx < perf_cpu_map__nr(evsel->core.cpus); idx++) {
+ err = evsel__tool_pmu_disable_cpu(evsel, idx);
+ if (err)
+ break;
+ }
+ return err;
+}
+
int evsel__tool_pmu_read(struct evsel *evsel, int cpu_map_idx, int thread)
{
- __u64 *start_time, cur_time, delta_start;
- u64 val;
- int fd, err = 0;
+ __u64 delta_start = 0;
+ int err = 0;
struct perf_counts_values *count, *old_count = NULL;
bool adjust = false;
enum tool_pmu_event ev = evsel__tool_event(evsel);
count = perf_counts(evsel->counts, cpu_map_idx, thread);
+ if (evsel->prev_raw_counts)
+ old_count = perf_counts(evsel->prev_raw_counts, cpu_map_idx, thread);
switch (ev) {
case TOOL_PMU__EVENT_HAS_PMEM:
@@ -410,60 +640,51 @@ int evsel__tool_pmu_read(struct evsel *evsel, int cpu_map_idx, int thread)
case TOOL_PMU__EVENT_NUM_PACKAGES:
case TOOL_PMU__EVENT_SLOTS:
case TOOL_PMU__EVENT_SMT_ON:
- case TOOL_PMU__EVENT_SYSTEM_TSC_FREQ:
- if (evsel->prev_raw_counts)
- old_count = perf_counts(evsel->prev_raw_counts, cpu_map_idx, thread);
- val = 0;
+ case TOOL_PMU__EVENT_CORE_WIDE:
+ case TOOL_PMU__EVENT_TARGET_CPU:
+ case TOOL_PMU__EVENT_SYSTEM_TSC_FREQ: {
+ u64 val = 0;
+
if (cpu_map_idx == 0 && thread == 0) {
- if (!tool_pmu__read_event(ev, &val)) {
+ if (!tool_pmu__read_event(ev, evsel,
+ stat_config.system_wide,
+ stat_config.user_requested_cpu_list,
+ &val)) {
count->lost++;
val = 0;
}
}
- if (old_count) {
- count->val = old_count->val + val;
- count->run = old_count->run + 1;
- count->ena = old_count->ena + 1;
- } else {
- count->val = val;
- count->run++;
- count->ena++;
- }
+ perf_counts__update(count, old_count, /*raw=*/false, val);
return 0;
+ }
case TOOL_PMU__EVENT_DURATION_TIME:
- /*
- * Pretend duration_time is only on the first CPU and thread, or
- * else aggregation will scale duration_time by the number of
- * CPUs/threads.
- */
- start_time = &evsel->start_time;
- if (cpu_map_idx == 0 && thread == 0)
- cur_time = rdclock();
- else
- cur_time = *start_time;
+ if (cpu_map_idx == 0 && thread == 0) {
+ delta_start = evsel->duration_time.accumulated_time;
+ if (!evsel->disabled &&
+ evsel->duration_time.start_time != INVALID_START_TIME)
+ delta_start += (rdclock() - evsel->duration_time.start_time);
+ } else {
+ delta_start = 0;
+ }
break;
case TOOL_PMU__EVENT_USER_TIME:
case TOOL_PMU__EVENT_SYSTEM_TIME: {
- bool system = evsel__tool_event(evsel) == TOOL_PMU__EVENT_SYSTEM_TIME;
-
- start_time = xyarray__entry(evsel->start_times, cpu_map_idx, thread);
- fd = FD(evsel, cpu_map_idx, thread);
- lseek(fd, SEEK_SET, 0);
- if (evsel->pid_stat) {
- /* The event exists solely on 1 CPU. */
- if (cpu_map_idx == 0)
- err = read_pid_stat_field(fd, system ? 15 : 14, &cur_time);
- else
- cur_time = 0;
- } else {
- /* The event is for all threads. */
- if (thread == 0) {
- struct perf_cpu cpu = perf_cpu_map__cpu(evsel->core.cpus,
- cpu_map_idx);
+ __u64 accumulated = *(__u64 *)xyarray__entry(evsel->process_time.accumulated_times,
+ cpu_map_idx, thread);
- err = read_stat_field(fd, cpu, system ? 3 : 1, &cur_time);
- } else {
- cur_time = 0;
+ if (evsel->disabled) {
+ delta_start = accumulated;
+ } else {
+ __u64 *start_time = xyarray__entry(evsel->process_time.start_times,
+ cpu_map_idx, thread);
+ __u64 cur_time;
+
+ err = tool_pmu__read_stat(evsel, cpu_map_idx, thread, &cur_time);
+ if (!err) {
+ if (*start_time != INVALID_START_TIME && cur_time >= *start_time)
+ delta_start = accumulated + (cur_time - *start_time);
+ else
+ delta_start = accumulated;
}
}
adjust = true;
@@ -477,29 +698,26 @@ int evsel__tool_pmu_read(struct evsel *evsel, int cpu_map_idx, int thread)
if (err)
return err;
- delta_start = cur_time - *start_time;
if (adjust) {
__u64 ticks_per_sec = sysconf(_SC_CLK_TCK);
- delta_start *= 1000000000 / ticks_per_sec;
+ delta_start *= 1e9 / ticks_per_sec;
}
- count->val = delta_start;
- count->ena = count->run = delta_start;
- count->lost = 0;
+ perf_counts__update(count, old_count, /*raw=*/true, delta_start);
return 0;
}
-struct perf_pmu *perf_pmus__tool_pmu(void)
+struct perf_pmu *tool_pmu__new(void)
{
- static struct perf_pmu tool = {
- .name = "tool",
- .type = PERF_PMU_TYPE_TOOL,
- .aliases = LIST_HEAD_INIT(tool.aliases),
- .caps = LIST_HEAD_INIT(tool.caps),
- .format = LIST_HEAD_INIT(tool.format),
- };
- if (!tool.events_table)
- tool.events_table = find_core_events_table("common", "common");
-
- return &tool;
+ struct perf_pmu *tool = zalloc(sizeof(struct perf_pmu));
+
+ if (!tool)
+ return NULL;
+
+ if (perf_pmu__init(tool, PERF_PMU_TYPE_TOOL, "tool") != 0) {
+ perf_pmu__delete(tool);
+ return NULL;
+ }
+ tool->events_table = find_core_events_table("common", "common");
+ return tool;
}