// SPDX-License-Identifier: GPL-3.0-or-later
#include "libnetdata/libnetdata.h"
#include "libnetdata/required_dummies.h"
#include <linux/perf_event.h>
#define PLUGIN_PERF_NAME "perf.plugin"
// Hardware counters
#define NETDATA_CHART_PRIO_PERF_CPU_CYCLES 8800
#define NETDATA_CHART_PRIO_PERF_INSTRUCTIONS 8801
#define NETDATA_CHART_PRIO_PERF_IPC 8802
#define NETDATA_CHART_PRIO_PERF_BRANCH_INSTRUCTIONS 8803
#define NETDATA_CHART_PRIO_PERF_CACHE 8804
#define NETDATA_CHART_PRIO_PERF_BUS_CYCLES 8805
#define NETDATA_CHART_PRIO_PERF_FRONT_BACK_CYCLES 8806
// Software counters
#define NETDATA_CHART_PRIO_PERF_MIGRATIONS 8810
#define NETDATA_CHART_PRIO_PERF_ALIGNMENT 8811
#define NETDATA_CHART_PRIO_PERF_EMULATION 8812
// Hardware cache counters
#define NETDATA_CHART_PRIO_PERF_L1D 8820
#define NETDATA_CHART_PRIO_PERF_L1D_PREFETCH 8821
#define NETDATA_CHART_PRIO_PERF_L1I 8822
#define NETDATA_CHART_PRIO_PERF_LL 8823
#define NETDATA_CHART_PRIO_PERF_DTLB 8824
#define NETDATA_CHART_PRIO_PERF_ITLB 8825
#define NETDATA_CHART_PRIO_PERF_PBU 8826
#define RRD_TYPE_PERF "perf"
#define RRD_FAMILY_HW "hardware"
#define RRD_FAMILY_SW "software"
#define RRD_FAMILY_CACHE "cache"
#define NO_FD -1
#define ALL_PIDS -1
#define RUNNING_THRESHOLD 100
static int debug = 0;
static int update_every = 1;
static int freq = 0;
typedef enum perf_event_id {
// Hardware counters
EV_ID_CPU_CYCLES,
EV_ID_INSTRUCTIONS,
EV_ID_CACHE_REFERENCES,
EV_ID_CACHE_MISSES,
EV_ID_BRANCH_INSTRUCTIONS,
EV_ID_BRANCH_MISSES,
EV_ID_BUS_CYCLES,
EV_ID_STALLED_CYCLES_FRONTEND,
EV_ID_STALLED_CYCLES_BACKEND,
EV_ID_REF_CPU_CYCLES,
// Software counters
// EV_ID_CPU_CLOCK,
// EV_ID_TASK_CLOCK,
// EV_ID_PAGE_FAULTS,
// EV_ID_CONTEXT_SWITCHES,
EV_ID_CPU_MIGRATIONS,
// EV_ID_PAGE_FAULTS_MIN,
// EV_ID_PAGE_FAULTS_MAJ,
EV_ID_ALIGNMENT_FAULTS,
EV_ID_EMULATION_FAULTS,
// Hardware cache counters
EV_ID_L1D_READ_ACCESS,
EV_ID_L1D_READ_MISS,
EV_ID_L1D_WRITE_ACCESS,
EV_ID_L1D_WRITE_MISS,
EV_ID_L1D_PREFETCH_ACCESS,
EV_ID_L1I_READ_ACCESS,
EV_ID_L1I_READ_MISS,
EV_ID_LL_READ_ACCESS,
EV_ID_LL_READ_MISS,
EV_ID_LL_WRITE_ACCESS,
EV_ID_LL_WRITE_MISS,
EV_ID_DTLB_READ_ACCESS,
EV_ID_DTLB_READ_MISS,
EV_ID_DTLB_WRITE_ACCESS,
EV_ID_D