From 8b60fb0fe6e29aac1847c0b381c0f84165b27b61 Mon Sep 17 00:00:00 2001 From: Damjan Marion Date: Fri, 27 Nov 2020 20:15:17 +0100 Subject: perfmon: new perfmon plugin Type: feature Change-Id: I2c14f82393d11fc05c6d229f5c58603ab5c0f14d Signed-off-by: Damjan Marion --- src/plugins/perfmon/linux.c | 120 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 src/plugins/perfmon/linux.c (limited to 'src/plugins/perfmon/linux.c') diff --git a/src/plugins/perfmon/linux.c b/src/plugins/perfmon/linux.c new file mode 100644 index 00000000000..3715267266a --- /dev/null +++ b/src/plugins/perfmon/linux.c @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2020 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include +#include +#include +#include +#include + +#include + +#include + +#define foreach_perf_sw_counter \ + _ (CONTEXT_SWITCHES, "context-switches") \ + _ (PAGE_FAULTS_MIN, "page-faults-minor") \ + _ (PAGE_FAULTS_MAJ, "page-faults-major") + +typedef enum +{ +#define _(n, s) n, + foreach_perf_sw_counter +#undef _ +} linux_sw_events; + +static perfmon_event_t events[] = { +#define _(n, s) \ + [n] = { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##n, .name = s }, + foreach_perf_sw_counter +#undef _ +}; + +PERFMON_REGISTER_SOURCE (linux) = { + .name = "linux", + .description = "Linux kernel performance counters", + .events = events, + .n_events = ARRAY_LEN (events), +}; + +static u8 * +format_context_switches (u8 *s, va_list *args) +{ + perfmon_reading_t *r = va_arg (*args, perfmon_reading_t *); + int row = va_arg (*args, int); + f64 t = (f64) r->time_running * 1e-9; + + switch (row) + { + case 0: + s = format (s, "%9.2f", t); + break; + case 1: + if (r->time_running) + s = format (s, "%9.2f", (f64) r->value[0] / t); + break; + } + return s; +} + +PERFMON_REGISTER_BUNDLE (context_switches) = { + .name = "context-switches", + .description = "per-thread context switches", + .source = "linux", + .type = PERFMON_BUNDLE_TYPE_THREAD, + .events[0] = CONTEXT_SWITCHES, + .n_events = 1, + .format_fn = format_context_switches, + .column_headers = PERFMON_STRINGS ("RunTime", "ContextSwitches/Sec"), +}; + +static u8 * +format_page_faults (u8 *s, va_list *args) +{ + perfmon_reading_t *r = va_arg (*args, perfmon_reading_t *); + int row = va_arg (*args, int); + f64 t = (f64) r->time_running * 1e-9; + + switch (row) + { + case 0: + s = format (s, "%9.2f", t); + break; + case 1: + if (r->time_running) + s = format (s, "%9.2f", (f64) r->value[0] / t); + break; + case 2: + if (r->time_running) + s = format (s, "%9.2f", (f64) r->value[1] / t); + break; + } + return s; +} + +PERFMON_REGISTER_BUNDLE (page_faults) = { + .name = "page-faults", + .description = "per-thread page faults", + .source = "linux", + .type = PERFMON_BUNDLE_TYPE_THREAD, + .events[0] = PAGE_FAULTS_MIN, + .events[1] = PAGE_FAULTS_MAJ, + .n_events = 2, + .format_fn = format_page_faults, + .column_headers = PERFMON_STRINGS ("RunTime", "MinorPageFaults/Sec", + "MajorPageFaults/Sec"), +}; -- cgit 1.2.3-korg