aboutsummaryrefslogtreecommitdiffstats
path: root/proc_interrupt.c
blob: 1942802a6057360d9bba07529ae39518f8588083 (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
// SPDX-License-Identifier: GPL-2.0

/*
 * Copyright (C) 2015 Intel Corporation
 * Author: Tony Luck
 *
 * This software may be redistributed and/or modified under the terms of
 * the GNU General Public License ("GPL") version 2 only as published by the
 * Free Software Foundation.
 */

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

static long sumint(char *s)
{
	long total = 0;
	char	*se;

	for (;;) {
		total += strtol(s, &se, 10);
		if (s == se)
			break;
		s = se;
	}

	return total;
}

/*
 * Parse /proc/interrupts to sum the number of observed
 * machine checks and corrected machine check interrupts
 * across all cpus
 */
void proc_interrupts(long *nmce, long *ncmci)
{
	FILE *fp = fopen("/proc/interrupts", "r");
	char	*p, line[4096];

	*ncmci = *nmce = -1;
	if (fp == NULL)
		return;

	while (fgets(line, sizeof(line), fp) != NULL) {
		for (p = line; isspace(*p); p++)
			;
		if (strncmp(p, "MCE:", 4) == 0)
			*nmce = sumint(p+4);
		else if (strncmp(p, "THR:", 4) == 0)
			*ncmci = sumint(p+4);
	}

	fclose(fp);
}