Debian Codegraph @

madrisan-nagios-plugins-linux/nagios-plugins-linux/lib/interrupts.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2014 Davide Madrisan <[email protected]>
5 *
6 * A library for getting informations on the system interrupts
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #ifndef _GNU_SOURCE
22 # define _GNU_SOURCE /* activate extra prototypes for glibc */
23 #endif
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "common.h"
30 #include "cputopology.h"
31 #include "logging.h"
32 #include "system.h"
33 #include "xalloc.h"
34
35 #define PROC_ROOT "/proc"
36 #define PROC_INTR PROC_ROOT "/interrupts"
37
38 /* Return an array containing the number of interrupts per cpu per IO device.
39 * Since Linux 2.6.24, for the i386 and x86_64 architectures at least,
40 * /proc/interrupts also includes interrupts internal to the system (that is,
41 * not associated with a device as such). */
42 unsigned long *
43 proc_interrupts_get_nintr_per_cpu (unsigned int *ncpus)
44 {
45 FILE *fp;
46 char *p, *end, *line = NULL;
47 size_t len = 0;
48 ssize_t chread;
49 bool header = true;
50 unsigned int cpu;
51
52 if ((fp = fopen (PROC_INTR, "r")) == NULL)
53 return NULL;
54
55 *ncpus = get_processor_number_online ();
56 unsigned long value,
57 *vintr = xnmalloc (*ncpus, sizeof (unsigned long));
58
59 while ((chread = getline (&line, &len, fp)) != -1)
60 {
61 /* skip the first line */
62 if (header)
63 {
64 header = false;
65 continue;
66 }
67
68 p = strchr(line, ':');
69 if (NULL == p) /* this should never happen */
70 continue;
71
72 dbg ("%s", line);
73 for (cpu = 0; cpu < *ncpus; cpu++)
74 {
75 if (*p == '\0' || *p == '\n')
76 continue;
77 value = strtoul (p + 1, &end, 10);
78 dbg (" --> cpu%u = %lu\n", cpu, value);
79 vintr[cpu] += value;
80 p = end + 1;
81 }
82 dbg ("\n");
83 }
84
85 free (line);
86
87 return vintr;
88 }