Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
litecoincash-project
GitHub Repository: litecoincash-project/cpuminer-multi
Path: blob/master/sysinfos.c
548 views
1
/**
2
* Unit to read cpu informations
3
*
4
* tpruvot 2014
5
*/
6
7
#include <stdio.h>
8
#include <ctype.h>
9
#include <stdlib.h>
10
#include <string.h>
11
12
#include "miner.h"
13
14
#ifndef WIN32
15
16
#define HWMON_PATH \
17
"/sys/devices/platform/coretemp.0/hwmon/hwmon1/temp1_input"
18
#define HWMON_ALT \
19
"/sys/class/hwmon/hwmon1/temp1_input"
20
#define HWMON_ALT2 \
21
"/sys/class/hwmon/hwmon0/temp1_input"
22
#define HWMON_ALT3 \
23
"/sys/devices/platform/coretemp.0/hwmon/hwmon0/temp2_input"
24
#define HWMON_ALT4 \
25
"/sys/class/hwmon/hwmon0/temp2_input"
26
#define HWMON_ALT5 \
27
"/sys/class/hwmon/hwmon0/device/temp1_input"
28
29
static float linux_cputemp(int core)
30
{
31
float tc = 0.0;
32
FILE *fd = fopen(HWMON_PATH, "r");
33
uint32_t val = 0;
34
35
if (!fd)
36
fd = fopen(HWMON_ALT, "r");
37
38
if (!fd)
39
fd = fopen(HWMON_ALT2, "r");
40
41
if (!fd)
42
fd = fopen(HWMON_ALT3, "r");
43
44
if (!fd)
45
fd = fopen(HWMON_ALT4, "r");
46
47
if (!fd)
48
fd = fopen(HWMON_ALT5, "r");
49
50
if (!fd)
51
return tc;
52
53
if (fscanf(fd, "%d", &val))
54
tc = val / 1000.0;
55
56
fclose(fd);
57
return tc;
58
}
59
60
#define CPUFREQ_PATH \
61
"/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq"
62
static uint32_t linux_cpufreq(int core)
63
{
64
FILE *fd = fopen(CPUFREQ_PATH, "r");
65
uint32_t freq = 0;
66
67
if (!fd)
68
return freq;
69
70
if (!fscanf(fd, "%d", &freq))
71
return freq;
72
73
fclose(fd);
74
return freq;
75
}
76
77
#else /* WIN32 */
78
79
static float win32_cputemp(int core)
80
{
81
// todo
82
return 0.0;
83
}
84
85
#endif /* !WIN32 */
86
87
88
/* exports */
89
90
91
float cpu_temp(int core)
92
{
93
#ifdef WIN32
94
return win32_cputemp(core);
95
#else
96
return linux_cputemp(core);
97
#endif
98
}
99
100
uint32_t cpu_clock(int core)
101
{
102
#ifdef WIN32
103
return 0;
104
#else
105
return linux_cpufreq(core);
106
#endif
107
}
108
109
int cpu_fanpercent()
110
{
111
return 0;
112
}
113
114
#if !defined(__arm__) && !defined(__aarch64__)
115
static inline void cpuid(int functionnumber, int output[4]) {
116
#ifdef _MSC_VER
117
// Microsoft compiler, intrin.h included
118
__cpuidex(output, functionnumber, 0);
119
#elif defined(__INTEL_COMPILER)
120
__cpuid(output, functionnumber);
121
#elif defined(__GNUC__) || defined(__clang__)
122
// use inline assembly, Gnu/AT&T syntax
123
int a, b, c, d;
124
asm volatile("cpuid" : "=a"(a), "=b"(b), "=c"(c), "=d"(d) : "a"(functionnumber), "c"(0));
125
output[0] = a;
126
output[1] = b;
127
output[2] = c;
128
output[3] = d;
129
#else
130
// unknown platform. try inline assembly with masm/intel syntax
131
__asm {
132
mov eax, functionnumber
133
xor ecx, ecx
134
cpuid;
135
mov esi, output
136
mov[esi], eax
137
mov[esi + 4], ebx
138
mov[esi + 8], ecx
139
mov[esi + 12], edx
140
}
141
#endif
142
}
143
#else /* !__arm__ */
144
#define cpuid(fn, out) out[0] = 0;
145
#endif
146
147
// For the i7-5775C will output : Intel(R) Core(TM) i7-5775C CPU @ 3.30GHz
148
void cpu_getname(char *outbuf, size_t maxsz)
149
{
150
memset(outbuf, 0, maxsz);
151
#ifdef WIN32
152
char brand[0xC0] = { 0 };
153
int output[4] = { 0 }, ext;
154
cpuid(0x80000000, output);
155
ext = output[0];
156
if (ext >= 0x80000004) {
157
for (int i = 2; i <= (ext & 0xF); i++) {
158
cpuid(0x80000000+i, output);
159
memcpy(&brand[(i-2) * 4*sizeof(int)], output, 4*sizeof(int));
160
}
161
snprintf(outbuf, maxsz, "%s", brand);
162
} else {
163
// Fallback, for the i7-5775C will output
164
// Intel64 Family 6 Model 71 Stepping 1, GenuineIntel
165
snprintf(outbuf, maxsz, "%s", getenv("PROCESSOR_IDENTIFIER"));
166
}
167
#else
168
// Intel(R) Xeon(R) CPU E3-1245 V2 @ 3.40GHz
169
FILE *fd = fopen("/proc/cpuinfo", "rb");
170
char *buf = NULL, *p, *eol;
171
size_t size = 0;
172
if (!fd) return;
173
while(getdelim(&buf, &size, 0, fd) != -1) {
174
if (buf && (p = strstr(buf, "model name\t")) && strstr(p, ":")) {
175
p = strstr(p, ":");
176
if (p) {
177
p += 2;
178
eol = strstr(p, "\n"); if (eol) *eol = '\0';
179
snprintf(outbuf, maxsz, "%s", p);
180
}
181
break;
182
}
183
}
184
free(buf);
185
fclose(fd);
186
#endif
187
}
188
189
void cpu_getmodelid(char *outbuf, size_t maxsz)
190
{
191
memset(outbuf, 0, maxsz);
192
#ifdef WIN32
193
// For the i7-5775C will output 6:4701:8
194
snprintf(outbuf, maxsz, "%s:%s:%s", getenv("PROCESSOR_LEVEL"), // hexa ?
195
getenv("PROCESSOR_REVISION"), getenv("NUMBER_OF_PROCESSORS"));
196
#else
197
FILE *fd = fopen("/proc/cpuinfo", "rb");
198
char *buf = NULL, *p, *eol;
199
int cpufam = 0, model = 0, stepping = 0;
200
size_t size = 0;
201
if (!fd) return;
202
while(getdelim(&buf, &size, 0, fd) != -1) {
203
if (buf && (p = strstr(buf, "cpu family\t")) && strstr(p, ":")) {
204
p = strstr(p, ":");
205
if (p) {
206
p += 2;
207
cpufam = atoi(p);
208
}
209
}
210
if (buf && (p = strstr(buf, "model\t")) && strstr(p, ":")) {
211
p = strstr(p, ":");
212
if (p) {
213
p += 2;
214
model = atoi(p);
215
}
216
}
217
if (buf && (p = strstr(buf, "stepping\t")) && strstr(p, ":")) {
218
p = strstr(p, ":");
219
if (p) {
220
p += 2;
221
stepping = atoi(p);
222
}
223
}
224
if (cpufam && model && stepping) {
225
snprintf(outbuf, maxsz, "%x:%02x%02x:%d", cpufam, model, stepping, num_cpus);
226
outbuf[maxsz-1] = '\0';
227
break;
228
}
229
}
230
free(buf);
231
fclose(fd);
232
#endif
233
}
234
235
// http://en.wikipedia.org/wiki/CPUID
236
#define OSXSAVE_Flag (1 << 27)
237
#define AVX1_Flag ((1 << 28)|OSXSAVE_Flag)
238
#define XOP_Flag (1 << 11)
239
#define FMA3_Flag ((1 << 12)|AVX1_Flag|OSXSAVE_Flag)
240
#define AES_Flag (1 << 25)
241
#define SSE42_Flag (1 << 20)
242
243
#define SSE_Flag (1 << 25) // EDX
244
#define SSE2_Flag (1 << 26) // EDX
245
246
#define AVX2_Flag (1 << 5) // ADV EBX
247
248
bool has_aes_ni()
249
{
250
#if defined(__arm__) || defined(__aarch64__)
251
return false;
252
#else
253
int cpu_info[4] = { 0 };
254
cpuid(1, cpu_info);
255
return cpu_info[2] & AES_Flag;
256
#endif
257
}
258
259
void cpu_bestfeature(char *outbuf, size_t maxsz)
260
{
261
#if defined(__arm__) || defined(__aarch64__)
262
sprintf(outbuf, "ARM");
263
#else
264
int cpu_info[4] = { 0 };
265
int cpu_info_adv[4] = { 0 };
266
cpuid(1, cpu_info);
267
cpuid(7, cpu_info_adv);
268
if ((cpu_info[2] & AVX1_Flag) && (cpu_info_adv[1] & AVX2_Flag))
269
sprintf(outbuf, "AVX2");
270
else if (cpu_info[2] & AVX1_Flag)
271
sprintf(outbuf, "AVX");
272
else if (cpu_info[2] & FMA3_Flag)
273
sprintf(outbuf, "FMA3");
274
else if (cpu_info[2] & XOP_Flag)
275
sprintf(outbuf, "XOP");
276
else if (cpu_info[2] & SSE42_Flag)
277
sprintf(outbuf, "SSE42");
278
else if (cpu_info[3] & SSE2_Flag)
279
sprintf(outbuf, "SSE2");
280
else if (cpu_info[3] & SSE_Flag)
281
sprintf(outbuf, "SSE");
282
else
283
*outbuf = '\0';
284
#endif
285
}
286
287