-
Notifications
You must be signed in to change notification settings - Fork 1
/
coretempstat.c
212 lines (177 loc) · 4.49 KB
/
coretempstat.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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <sys/processor.h>
#include <sys/procset.h>
#include <kstat.h>
#include <stdlib.h>
#include <coretemp.h>
#define MAX_CPUS 1024
typedef struct {
int cpu_index;
int chip_id;
int core_id;
} cpu_info_t;
long get_kstat_number(kstat_ctl_t *kernelDesc, char *moduleName,
int instance, char *recordName, char *fieldName);
char *get_kstat_string(kstat_ctl_t *kernelDesc, char *moduleName,
int instance, char *recordName, char *fieldName);
long read_value(kstat_ctl_t *kstat, int instance, char *field);
cpu_info_t *find_cpu(cpu_info_t *cpus, int cpus_size, int c_socket, int c_core);
int
main(int argc, char *argv[1])
{
kstat_ctl_t *kstat;
cpu_info_t cpus[MAX_CPUS];
int cpu_count, cpu_socket_count;
cpu_info_t *core_ptr;
int machine_readable, display_cpu;
int c_socket, c_core, c_index;
int temp;
int has_thermal_monitoring;
cpu_socket_count = 0;
machine_readable = 0;
display_cpu = 1;
if ((kstat = kstat_open()) == NULL) {
perror("kstat_open");
return (1);
}
if (argc > 1) {
if (strcmp(argv[1], "-p") == 0) {
machine_readable = 1;
}
if (argc > 2) {
display_cpu = atoi(argv[2]);
}
}
for (cpu_count = 0; cpu_count < MAX_CPUS; cpu_count++) {
int chip_id;
if ((chip_id = read_value(kstat, cpu_count, "chip_id")) < 0) {
break;
}
int core_id = read_value(kstat, cpu_count, "core_id");
cpus[cpu_count].cpu_index = cpu_count;
cpus[cpu_count].chip_id = chip_id;
cpus[cpu_count].core_id = core_id;
if (chip_id + 1 > cpu_socket_count) {
cpu_socket_count = chip_id + 1;
}
}
if (!machine_readable) {
printf("Found %d CPU%s in %d socket%s\n",
cpu_count, (cpu_count == 1) ? "" : "s",
cpu_socket_count, (cpu_socket_count == 1) ? "" : "s");
}
int package_displayed = 0;
for (c_socket = 0; c_socket < cpu_socket_count; c_socket++) {
package_displayed = 0;
int core_count = 0;
for (c_core = 0; c_core < MAX_CPUS; c_core++) {
core_ptr = find_cpu(cpus, cpu_count, c_socket, c_core);
if (core_ptr == NULL) {
continue;
}
c_index = core_ptr->cpu_index;
has_thermal_monitoring =
(read_value(kstat, c_index, "core_temp") >= 0);
if (!has_thermal_monitoring) {
continue;
}
if (!package_displayed && machine_readable == 0) {
package_displayed = 1;
/* first core. display package information */
temp =
read_value(kstat, c_index, "chip_temp");
printf("Socket #%d", core_ptr->chip_id);
if (temp >= 0) {
printf(" temp : %d \u00B0C\n", temp);
} else {
printf("\n");
}
}
temp = read_value(kstat, c_index, "core_temp");
if (machine_readable == 0) {
printf("\tCore #%d", core_count++);
if (temp >= 0) {
printf(" temp : %d \u00B0C\n", temp);
} else {
printf("\n");
}
} else {
if (display_cpu == -1) {
printf("%d %d\n",
core_ptr->cpu_index, temp);
} else if (display_cpu == core_ptr->cpu_index) {
printf("%d\n",
temp);
}
}
}
}
kstat_close(kstat);
return (0);
}
cpu_info_t *
find_cpu(cpu_info_t *cpus, int cpus_size, int c_socket, int c_core)
{
int i;
cpu_info_t *result;
for (i = 0; i < cpus_size; i++) {
result = &cpus[i];
if (result->chip_id == c_socket &&
result->core_id == c_core) {
return (result);
}
}
return (NULL);
}
long
read_value(kstat_ctl_t *kstat, int instance, char *field)
{
return (get_kstat_number(kstat, KSTAT_CORETEMP_MODULE,
instance, KSTAT_CORETEMP_NAME, field));
}
long
get_kstat_number(kstat_ctl_t *kernelDesc, char *moduleName, int instance,
char *recordName, char *fieldName)
{
kstat_t *kstatRecordPtr;
kstat_named_t *kstatFields;
long value;
int i;
kstatRecordPtr =
kstat_lookup(kernelDesc, moduleName, instance, recordName);
if (kstatRecordPtr == NULL) {
return (-1);
}
if (kstat_read(kernelDesc, kstatRecordPtr, NULL) < 0) {
return (-1);
}
kstatFields = KSTAT_NAMED_PTR(kstatRecordPtr);
for (i = 0; i < kstatRecordPtr->ks_ndata; i++) {
if (strcmp(kstatFields[i].name, fieldName) == 0) {
switch (kstatFields[i].data_type) {
case KSTAT_DATA_INT32:
value = kstatFields[i].value.i32;
break;
case KSTAT_DATA_UINT32:
value = kstatFields[i].value.ui32;
break;
case KSTAT_DATA_INT64:
value = kstatFields[i].value.i64;
break;
case KSTAT_DATA_UINT64:
value = kstatFields[i].value.ui64;
break;
default:
value = -1;
}
return (value);
}
}
return (-1);
}