File: cache.c

package info (click to toggle)
mcelog 104-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 996 kB
  • ctags: 1,508
  • sloc: ansic: 7,739; sh: 481; makefile: 87
file content (185 lines) | stat: -rw-r--r-- 4,454 bytes parent folder | download
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* Copyright (C) 2008 Intel Corporation 
   Author: Andi Kleen
   Parse sysfs exported CPU cache topology

   mcelog is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation; version
   2.

   mcelog is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should find a copy of v2 of the GNU General Public License somewhere
   on your Linux system; if not, write to the Free Software Foundation, 
   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#define _GNU_SOURCE 1
#include <string.h>
#include <dirent.h>
#include <stdio.h>
#include <sys/stat.h>
#include <assert.h>
#include <ctype.h>
#include "mcelog.h"
#include "memutil.h"
#include "sysfs.h"
#include "cache.h"

struct cache { 
	unsigned level;
	/* Numerical values must match MCACOD */
	enum { INSTR, DATA, UNIFIED } type; 
	unsigned *cpumap;
	unsigned cpumaplen;
};

struct cache **caches;
static unsigned cachelen;

#define PREFIX "/sys/devices/system/cpu"
#define MIN_CPUS 8
#define MIN_INDEX 4

static struct map type_map[] = {
	{ "Instruction", INSTR },
	{ "Data", DATA },
	{ "Unified", UNIFIED },
	{ },
};

static void more_cpus(int cpu)
{
	int old = cachelen;
	if (!cachelen)
		cachelen = MIN_CPUS/2;	
	cachelen *= 2;
	caches = xrealloc(caches, cachelen * sizeof(struct cache *));
	memset(caches + old, 0, (cachelen - old) * sizeof(struct cache *));
}

static unsigned cpumap_len(char *s)
{
	unsigned len = 0, width = 0;
	do {
		if (isxdigit(*s))
			width++;
		else {
			len += round_up(width * 4, BITS_PER_INT) / 8;
			width = 0;
		}
	} while (*s++);
	return len;
}

static void parse_cpumap(char *map, unsigned *buf, unsigned len)
{
	char *s;
	int c;

	c = 0;
	s = map + strlen(map);
	for (;;) { 
		s = memrchr(map, ',', s - map);
		if (!s) 
			s = map;
		else
			s++;
		buf[c++] = strtoul(s, NULL, 16);
		if (s == map)
			break;
		s--;
	}
	assert(len == c * sizeof(unsigned));
}

static void read_cpu_map(struct cache *c, char *cfn)
{
	char *map = read_field(cfn, "shared_cpu_map");
	c->cpumaplen = cpumap_len(map);
	c->cpumap = xalloc(c->cpumaplen);
	parse_cpumap(map, c->cpumap, c->cpumaplen);
	free(map);
}

static int read_caches(void)
{
	DIR *cpus = opendir(PREFIX);
	struct dirent *de;
	if (!cpus) { 
		Wprintf("Cannot read cache topology from %s", PREFIX);
		return -1;
	}
	while ((de = readdir(cpus)) != NULL) {
		unsigned cpu;
		if (sscanf(de->d_name, "cpu%u", &cpu) == 1) { 
			struct stat st;
			char *fn;
			int i;
			int numindex;

			asprintf(&fn, "%s/%s/cache", PREFIX, de->d_name);
			if (!stat(fn, &st)) {
				numindex = st.st_nlink - 2;
				if (numindex < 0)
					numindex = MIN_INDEX;
				if (cachelen <= cpu)
					more_cpus(cpu);
				caches[cpu] = xalloc(sizeof(struct cache) * 
						     (numindex+1));
				for (i = 0; i < numindex; i++) {
					char *cfn;
					struct cache *c = caches[cpu] + i;
					asprintf(&cfn, "%s/index%d", fn, i);
					c->type = read_field_map(cfn, "type", type_map);
					c->level = read_field_num(cfn, "level");
					read_cpu_map(c, cfn);
					free(cfn);
				}
			}
			free(fn);
		}
	}
	closedir(cpus);
	return 0;
}

int cache_to_cpus(int cpu, unsigned level, unsigned type, 
		   int *cpulen, unsigned **cpumap)
{
	struct cache *c;
	if (!caches) {
		if (read_caches() < 0)
			return -1;
		if (!caches) { 
			Wprintf("No caches found in sysfs");
			return -1;
		}
	}
	for (c = caches[cpu]; c && c->cpumap; c++) { 
		//printf("%d level %d type %d\n", cpu, c->level, c->type);
		if (c->level == level && c->type == type) { 
			*cpumap = c->cpumap;
			*cpulen = c->cpumaplen;
			return 0;
		}
	}
	Wprintf("Cannot find sysfs cache for CPU %d", cpu);
	return -1;
}

#ifdef TEST
main()
{
	int cpulen;
	unsigned *cpumap;
	cache_to_cpus(1, 1, INSTR, &cpulen, &cpumap); printf("%d %x\n", cpulen, cpumap[0]);
	cache_to_cpus(1, 1, DATA, &cpulen, &cpumap); printf("%d %x\n", cpulen, cpumap[0]);
	cache_to_cpus(1, 2, UNIFIED, &cpulen, &cpumap); printf("%d %x\n", cpulen, cpumap[0]);
	cache_to_cpus(0, 1, INSTR, &cpulen, &cpumap); printf("%d %x\n", cpulen, cpumap[0]);
	cache_to_cpus(0, 1, DATA, &cpulen, &cpumap); printf("%d %x\n", cpulen, cpumap[0]);
	cache_to_cpus(0, 2, UNIFIED, &cpulen, &cpumap); printf("%d %x\n", cpulen, cpumap[0]);
}
#endif