LCOV - code coverage report
Current view: top level - plugins/perfmon - linux.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 3 33 9.1 %
Date: 2023-07-05 22:20:52 Functions: 3 5 60.0 %

          Line data    Source code
       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,
      36             :   foreach_perf_sw_counter
      37             : #undef _
      38             : } linux_sw_events;
      39             : 
      40             : static perfmon_event_t events[] = {
      41             : #define _(n, s)                                                               \
      42             :   [n] = {                                                                     \
      43             :     .type = PERF_TYPE_SOFTWARE,                                               \
      44             :     .config = PERF_COUNT_SW_##n,                                              \
      45             :     .name = s,                                                                \
      46             :     .implemented = 1,                                                         \
      47             :   },
      48             :   foreach_perf_sw_counter
      49             : #undef _
      50             : };
      51             : 
      52         559 : PERFMON_REGISTER_SOURCE (linux) = {
      53             :   .name = "linux",
      54             :   .description = "Linux kernel performance counters",
      55             :   .events = events,
      56             :   .n_events = ARRAY_LEN (events),
      57             : };
      58             : 
      59             : static u8 *
      60           0 : format_context_switches (u8 *s, va_list *args)
      61             : {
      62           0 :   perfmon_reading_t *r = va_arg (*args, perfmon_reading_t *);
      63           0 :   int row = va_arg (*args, int);
      64           0 :   f64 t = (f64) r->time_running * 1e-9;
      65             : 
      66           0 :   switch (row)
      67             :     {
      68           0 :     case 0:
      69           0 :       s = format (s, "%9.2f", t);
      70           0 :       break;
      71           0 :     case 1:
      72           0 :       if (r->time_running)
      73           0 :         s = format (s, "%9.2f", (f64) r->value[0] / t);
      74           0 :       break;
      75             :     }
      76           0 :   return s;
      77             : }
      78             : 
      79         559 : PERFMON_REGISTER_BUNDLE (context_switches) = {
      80             :   .name = "context-switches",
      81             :   .description = "per-thread context switches",
      82             :   .source = "linux",
      83             :   .type = PERFMON_BUNDLE_TYPE_THREAD,
      84             :   .events[0] = CONTEXT_SWITCHES,
      85             :   .n_events = 1,
      86             :   .format_fn = format_context_switches,
      87             :   .column_headers = PERFMON_STRINGS ("RunTime", "ContextSwitches/Sec"),
      88             : };
      89             : 
      90             : static u8 *
      91           0 : format_page_faults (u8 *s, va_list *args)
      92             : {
      93           0 :   perfmon_reading_t *r = va_arg (*args, perfmon_reading_t *);
      94           0 :   int row = va_arg (*args, int);
      95           0 :   f64 t = (f64) r->time_running * 1e-9;
      96             : 
      97           0 :   switch (row)
      98             :     {
      99           0 :     case 0:
     100           0 :       s = format (s, "%9.2f", t);
     101           0 :       break;
     102           0 :     case 1:
     103           0 :       if (r->time_running)
     104           0 :         s = format (s, "%9.2f", (f64) r->value[0] / t);
     105           0 :       break;
     106           0 :     case 2:
     107           0 :       if (r->time_running)
     108           0 :         s = format (s, "%9.2f", (f64) r->value[1] / t);
     109           0 :       break;
     110             :     }
     111           0 :   return s;
     112             : }
     113             : 
     114         559 : PERFMON_REGISTER_BUNDLE (page_faults) = {
     115             :   .name = "page-faults",
     116             :   .description = "per-thread page faults",
     117             :   .source = "linux",
     118             :   .type = PERFMON_BUNDLE_TYPE_THREAD,
     119             :   .events[0] = PAGE_FAULTS_MIN,
     120             :   .events[1] = PAGE_FAULTS_MAJ,
     121             :   .n_events = 2,
     122             :   .format_fn = format_page_faults,
     123             :   .column_headers = PERFMON_STRINGS ("RunTime", "MinorPageFaults/Sec",
     124             :                                      "MajorPageFaults/Sec"),
     125             : };

Generated by: LCOV version 1.14