util: move os_time.[ch] to src/util
[mesa.git] / src / gallium / auxiliary / hud / hud_cpu.c
1 /**************************************************************************
2 *
3 * Copyright 2013 Marek Olšák <maraeo@gmail.com>
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL THE AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /* This file contains code for reading CPU load for displaying on the HUD.
29 */
30
31 #include "hud/hud_private.h"
32 #include "util/os_time.h"
33 #include "os/os_thread.h"
34 #include "util/u_memory.h"
35 #include "util/u_queue.h"
36 #include <stdio.h>
37 #include <inttypes.h>
38 #ifdef PIPE_OS_WINDOWS
39 #include <windows.h>
40 #endif
41
42
43 #ifdef PIPE_OS_WINDOWS
44
45 static inline uint64_t
46 filetime_to_scalar(FILETIME ft)
47 {
48 ULARGE_INTEGER uli;
49 uli.LowPart = ft.dwLowDateTime;
50 uli.HighPart = ft.dwHighDateTime;
51 return uli.QuadPart;
52 }
53
54 static boolean
55 get_cpu_stats(unsigned cpu_index, uint64_t *busy_time, uint64_t *total_time)
56 {
57 SYSTEM_INFO sysInfo;
58 FILETIME ftNow, ftCreation, ftExit, ftKernel, ftUser;
59
60 GetSystemInfo(&sysInfo);
61 assert(sysInfo.dwNumberOfProcessors >= 1);
62 if (cpu_index != ALL_CPUS && cpu_index >= sysInfo.dwNumberOfProcessors) {
63 /* Tell hud_get_num_cpus there are only this many CPUs. */
64 return FALSE;
65 }
66
67 /* Get accumulated user and sys time for all threads */
68 if (!GetProcessTimes(GetCurrentProcess(), &ftCreation, &ftExit,
69 &ftKernel, &ftUser))
70 return FALSE;
71
72 GetSystemTimeAsFileTime(&ftNow);
73
74 *busy_time = filetime_to_scalar(ftUser) + filetime_to_scalar(ftKernel);
75 *total_time = filetime_to_scalar(ftNow) - filetime_to_scalar(ftCreation);
76
77 /* busy_time already has the time accross all cpus.
78 * XXX: if we want 100% to mean one CPU, 200% two cpus, eliminate the
79 * following line.
80 */
81 *total_time *= sysInfo.dwNumberOfProcessors;
82
83 /* XXX: we ignore cpu_index, i.e, we assume that the individual CPU usage
84 * and the system usage are one and the same.
85 */
86 return TRUE;
87 }
88
89 #else
90
91 static boolean
92 get_cpu_stats(unsigned cpu_index, uint64_t *busy_time, uint64_t *total_time)
93 {
94 char cpuname[32];
95 char line[1024];
96 FILE *f;
97
98 if (cpu_index == ALL_CPUS)
99 strcpy(cpuname, "cpu");
100 else
101 sprintf(cpuname, "cpu%u", cpu_index);
102
103 f = fopen("/proc/stat", "r");
104 if (!f)
105 return FALSE;
106
107 while (!feof(f) && fgets(line, sizeof(line), f)) {
108 if (strstr(line, cpuname) == line) {
109 uint64_t v[12];
110 int i, num;
111
112 num = sscanf(line,
113 "%s %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64
114 " %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64
115 " %"PRIu64" %"PRIu64"",
116 cpuname, &v[0], &v[1], &v[2], &v[3], &v[4], &v[5],
117 &v[6], &v[7], &v[8], &v[9], &v[10], &v[11]);
118 if (num < 5) {
119 fclose(f);
120 return FALSE;
121 }
122
123 /* user + nice + system */
124 *busy_time = v[0] + v[1] + v[2];
125 *total_time = *busy_time;
126
127 /* ... + idle + iowait + irq + softirq + ... */
128 for (i = 3; i < num-1; i++) {
129 *total_time += v[i];
130 }
131 fclose(f);
132 return TRUE;
133 }
134 }
135 fclose(f);
136 return FALSE;
137 }
138 #endif
139
140
141 struct cpu_info {
142 unsigned cpu_index;
143 uint64_t last_cpu_busy, last_cpu_total, last_time;
144 };
145
146 static void
147 query_cpu_load(struct hud_graph *gr)
148 {
149 struct cpu_info *info = gr->query_data;
150 uint64_t now = os_time_get();
151
152 if (info->last_time) {
153 if (info->last_time + gr->pane->period <= now) {
154 uint64_t cpu_busy, cpu_total, cpu_load;
155
156 get_cpu_stats(info->cpu_index, &cpu_busy, &cpu_total);
157
158 cpu_load = (cpu_busy - info->last_cpu_busy) * 100 /
159 (double)(cpu_total - info->last_cpu_total);
160 hud_graph_add_value(gr, cpu_load);
161
162 info->last_cpu_busy = cpu_busy;
163 info->last_cpu_total = cpu_total;
164 info->last_time = now;
165 }
166 }
167 else {
168 /* initialize */
169 info->last_time = now;
170 get_cpu_stats(info->cpu_index, &info->last_cpu_busy,
171 &info->last_cpu_total);
172 }
173 }
174
175 static void
176 free_query_data(void *p)
177 {
178 FREE(p);
179 }
180
181 void
182 hud_cpu_graph_install(struct hud_pane *pane, unsigned cpu_index)
183 {
184 struct hud_graph *gr;
185 struct cpu_info *info;
186 uint64_t busy, total;
187
188 /* see if the cpu exists */
189 if (cpu_index != ALL_CPUS && !get_cpu_stats(cpu_index, &busy, &total)) {
190 return;
191 }
192
193 gr = CALLOC_STRUCT(hud_graph);
194 if (!gr)
195 return;
196
197 if (cpu_index == ALL_CPUS)
198 strcpy(gr->name, "cpu");
199 else
200 sprintf(gr->name, "cpu%u", cpu_index);
201
202 gr->query_data = CALLOC_STRUCT(cpu_info);
203 if (!gr->query_data) {
204 FREE(gr);
205 return;
206 }
207
208 gr->query_new_value = query_cpu_load;
209
210 /* Don't use free() as our callback as that messes up Gallium's
211 * memory debugger. Use simple free_query_data() wrapper.
212 */
213 gr->free_query_data = free_query_data;
214
215 info = gr->query_data;
216 info->cpu_index = cpu_index;
217
218 hud_pane_add_graph(pane, gr);
219 hud_pane_set_max_value(pane, 100);
220 }
221
222 int
223 hud_get_num_cpus(void)
224 {
225 uint64_t busy, total;
226 int i = 0;
227
228 while (get_cpu_stats(i, &busy, &total))
229 i++;
230
231 return i;
232 }
233
234 struct thread_info {
235 bool main_thread;
236 int64_t last_time;
237 int64_t last_thread_time;
238 };
239
240 static void
241 query_api_thread_busy_status(struct hud_graph *gr)
242 {
243 struct thread_info *info = gr->query_data;
244 int64_t now = os_time_get_nano();
245
246 if (info->last_time) {
247 if (info->last_time + gr->pane->period*1000 <= now) {
248 int64_t thread_now;
249
250 if (info->main_thread) {
251 thread_now = pipe_current_thread_get_time_nano();
252 } else {
253 struct util_queue_monitoring *mon = gr->pane->hud->monitored_queue;
254
255 if (mon && mon->queue)
256 thread_now = util_queue_get_thread_time_nano(mon->queue, 0);
257 else
258 thread_now = 0;
259 }
260
261 unsigned percent = (thread_now - info->last_thread_time) * 100 /
262 (now - info->last_time);
263
264 /* Check if the context changed a thread, so that we don't show
265 * a random value. When a thread is changed, the new thread clock
266 * is different, which can result in "percent" being very high.
267 */
268 if (percent > 100)
269 percent = 0;
270 hud_graph_add_value(gr, percent);
271
272 info->last_thread_time = thread_now;
273 info->last_time = now;
274 }
275 } else {
276 /* initialize */
277 info->last_time = now;
278 info->last_thread_time = pipe_current_thread_get_time_nano();
279 }
280 }
281
282 void
283 hud_thread_busy_install(struct hud_pane *pane, const char *name, bool main)
284 {
285 struct hud_graph *gr;
286
287 gr = CALLOC_STRUCT(hud_graph);
288 if (!gr)
289 return;
290
291 strcpy(gr->name, name);
292
293 gr->query_data = CALLOC_STRUCT(thread_info);
294 if (!gr->query_data) {
295 FREE(gr);
296 return;
297 }
298
299 ((struct thread_info*)gr->query_data)->main_thread = main;
300 gr->query_new_value = query_api_thread_busy_status;
301
302 /* Don't use free() as our callback as that messes up Gallium's
303 * memory debugger. Use simple free_query_data() wrapper.
304 */
305 gr->free_query_data = free_query_data;
306
307 hud_pane_add_graph(pane, gr);
308 hud_pane_set_max_value(pane, 100);
309 }
310
311 struct counter_info {
312 enum hud_counter counter;
313 unsigned last_value;
314 int64_t last_time;
315 };
316
317 static unsigned get_counter(struct hud_graph *gr, enum hud_counter counter)
318 {
319 struct util_queue_monitoring *mon = gr->pane->hud->monitored_queue;
320
321 if (!mon || !mon->queue)
322 return 0;
323
324 switch (counter) {
325 case HUD_COUNTER_OFFLOADED:
326 return mon->num_offloaded_items;
327 case HUD_COUNTER_DIRECT:
328 return mon->num_direct_items;
329 case HUD_COUNTER_SYNCS:
330 return mon->num_syncs;
331 default:
332 assert(0);
333 return 0;
334 }
335 }
336
337 static void
338 query_thread_counter(struct hud_graph *gr)
339 {
340 struct counter_info *info = gr->query_data;
341 int64_t now = os_time_get_nano();
342
343 if (info->last_time) {
344 if (info->last_time + gr->pane->period*1000 <= now) {
345 unsigned current_value = get_counter(gr, info->counter);
346
347 hud_graph_add_value(gr, current_value - info->last_value);
348 info->last_value = current_value;
349 info->last_time = now;
350 }
351 } else {
352 /* initialize */
353 info->last_value = get_counter(gr, info->counter);
354 info->last_time = now;
355 }
356 }
357
358 void hud_thread_counter_install(struct hud_pane *pane, const char *name,
359 enum hud_counter counter)
360 {
361 struct hud_graph *gr = CALLOC_STRUCT(hud_graph);
362 if (!gr)
363 return;
364
365 strcpy(gr->name, name);
366
367 gr->query_data = CALLOC_STRUCT(counter_info);
368 if (!gr->query_data) {
369 FREE(gr);
370 return;
371 }
372
373 ((struct counter_info*)gr->query_data)->counter = counter;
374 gr->query_new_value = query_thread_counter;
375
376 /* Don't use free() as our callback as that messes up Gallium's
377 * memory debugger. Use simple free_query_data() wrapper.
378 */
379 gr->free_query_data = free_query_data;
380
381 hud_pane_add_graph(pane, gr);
382 hud_pane_set_max_value(pane, 100);
383 }