summaryrefslogtreecommitdiffstats
path: root/src/plugins/perfmon/linux.c
blob: 3715267266ac4966176030d0bac1c95b77b8c7a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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 <vnet/vnet.h>

#include <vlibapi/api.h>
#include <vlibmemory/api.h>
#include <vpp/app/version.h>
#include <linux/limits.h>
#include <sys/ioctl.h>

#include <perfmon/perfmon.h>

#include <linux/perf_event.h>

#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"),
};