FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
linux.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <vnet/vnet.h>
17 
18 #include <vlibapi/api.h>
19 #include <vlibmemory/api.h>
20 #include <vpp/app/version.h>
21 #include <linux/limits.h>
22 #include <sys/ioctl.h>
23 
24 #include <perfmon/perfmon.h>
25 
26 #include <linux/perf_event.h>
27 
28 #define foreach_perf_sw_counter \
29  _ (CONTEXT_SWITCHES, "context-switches") \
30  _ (PAGE_FAULTS_MIN, "page-faults-minor") \
31  _ (PAGE_FAULTS_MAJ, "page-faults-major")
32 
33 typedef enum
34 {
35 #define _(n, s) n,
37 #undef _
39 
40 static perfmon_event_t events[] = {
41 #define _(n, s) \
42  [n] = { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##n, .name = s },
44 #undef _
45 };
46 
47 PERFMON_REGISTER_SOURCE (linux) = {
48  .name = "linux",
49  .description = "Linux kernel performance counters",
50  .events = events,
51  .n_events = ARRAY_LEN (events),
52 };
53 
54 static u8 *
55 format_context_switches (u8 *s, va_list *args)
56 {
57  perfmon_reading_t *r = va_arg (*args, perfmon_reading_t *);
58  int row = va_arg (*args, int);
59  f64 t = (f64) r->time_running * 1e-9;
60 
61  switch (row)
62  {
63  case 0:
64  s = format (s, "%9.2f", t);
65  break;
66  case 1:
67  if (r->time_running)
68  s = format (s, "%9.2f", (f64) r->value[0] / t);
69  break;
70  }
71  return s;
72 }
73 
74 PERFMON_REGISTER_BUNDLE (context_switches) = {
75  .name = "context-switches",
76  .description = "per-thread context switches",
77  .source = "linux",
79  .events[0] = CONTEXT_SWITCHES,
80  .n_events = 1,
81  .format_fn = format_context_switches,
82  .column_headers = PERFMON_STRINGS ("RunTime", "ContextSwitches/Sec"),
83 };
84 
85 static u8 *
86 format_page_faults (u8 *s, va_list *args)
87 {
88  perfmon_reading_t *r = va_arg (*args, perfmon_reading_t *);
89  int row = va_arg (*args, int);
90  f64 t = (f64) r->time_running * 1e-9;
91 
92  switch (row)
93  {
94  case 0:
95  s = format (s, "%9.2f", t);
96  break;
97  case 1:
98  if (r->time_running)
99  s = format (s, "%9.2f", (f64) r->value[0] / t);
100  break;
101  case 2:
102  if (r->time_running)
103  s = format (s, "%9.2f", (f64) r->value[1] / t);
104  break;
105  }
106  return s;
107 }
108 
109 PERFMON_REGISTER_BUNDLE (page_faults) = {
110  .name = "page-faults",
111  .description = "per-thread page faults",
112  .source = "linux",
114  .events[0] = PAGE_FAULTS_MIN,
115  .events[1] = PAGE_FAULTS_MAJ,
116  .n_events = 2,
117  .format_fn = format_page_faults,
118  .column_headers = PERFMON_STRINGS ("RunTime", "MinorPageFaults/Sec",
119  "MajorPageFaults/Sec"),
120 };
api.h
foreach_perf_sw_counter
#define foreach_perf_sw_counter
Definition: linux.c:28
perfmon_reading_t
Definition: perfmon.h:132
perfmon_event_t
Definition: perfmon.h:44
format_page_faults
static u8 * format_page_faults(u8 *s, va_list *args)
Definition: linux.c:86
api.h
r
vnet_hw_if_output_node_runtime_t * r
Definition: interface_output.c:1089
PERFMON_BUNDLE_TYPE_THREAD
@ PERFMON_BUNDLE_TYPE_THREAD
Definition: perfmon.h:32
PERFMON_STRINGS
#define PERFMON_STRINGS(...)
Definition: perfmon.h:226
ARRAY_LEN
#define ARRAY_LEN(x)
Definition: clib.h:70
PERFMON_REGISTER_BUNDLE
PERFMON_REGISTER_BUNDLE(context_switches)
events
static perfmon_event_t events[]
Definition: linux.c:40
f64
double f64
Definition: types.h:142
format_context_switches
static u8 * format_context_switches(u8 *s, va_list *args)
Definition: linux.c:55
format
description fragment has unexpected format
Definition: map.api:433
linux_sw_events
linux_sw_events
Definition: linux.c:33
u8
unsigned char u8
Definition: types.h:56
PERFMON_REGISTER_SOURCE
PERFMON_REGISTER_SOURCE(linux)
vnet.h