-
Notifications
You must be signed in to change notification settings - Fork 17
/
CacheLineSize.c
54 lines (44 loc) · 1.18 KB
/
CacheLineSize.c
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
#include "CacheLineSize.h"
#if defined(__APPLE__)
#include <sys/sysctl.h>
size_t CacheLineSize() {
size_t lineSize = 0;
size_t sizeOfLineSize = sizeof(lineSize);
sysctlbyname("hw.cachelinesize", &lineSize, &sizeOfLineSize, 0, 0);
return lineSize;
}
#elif defined(_WIN32)
#include <stdlib.h>
#include <windows.h>
size_t CacheLineSize() {
size_t lineSize = 0;
DWORD bufferSize = 0;
DWORD i = 0;
SYSTEM_LOGICAL_PROCESSOR_INFORMATION * buffer = 0;
GetLogicalProcessorInformation(0, &bufferSize);
buffer = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION *) malloc(bufferSize);
GetLogicalProcessorInformation(&buffer[0], &bufferSize);
for (i = 0; i != bufferSize / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); ++i) {
if (buffer[i].Relationship == RelationCache && buffer[i].Cache.Level == 1) {
lineSize = buffer[i].Cache.LineSize;
break;
}
}
free(buffer);
return lineSize;
}
#elif defined(__linux__)
#include <stdio.h>
size_t CacheLineSize() {
FILE * p = 0;
p = fopen("/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size", "r");
unsigned int lineSize = 0;
if (p) {
fscanf(p, "%d", &lineSize);
fclose(p);
}
return lineSize;
}
#else
#error Unrecognized platform
#endif