gallium/hud: add monitoring of API thread busy status
[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 "os/os_time.h"
33 #include "os/os_thread.h"
34 #include "util/u_memory.h"
35 #include <stdio.h>
36 #include <inttypes.h>
37 #ifdef PIPE_OS_WINDOWS
38 #include <windows.h>
39 #endif
40
41
42 #ifdef PIPE_OS_WINDOWS
43
44 static inline uint64_t
45 filetime_to_scalar(FILETIME ft)
46 {
47 ULARGE_INTEGER uli;
48 uli.LowPart = ft.dwLowDateTime;
49 uli.HighPart = ft.dwHighDateTime;
50 return uli.QuadPart;
51 }
52
53 static boolean
54 get_cpu_stats(unsigned cpu_index, uint64_t *busy_time, uint64_t *total_time)
55 {
56 SYSTEM_INFO sysInfo;
57 FILETIME ftNow, ftCreation, ftExit, ftKernel, ftUser;
58
59 GetSystemInfo(&sysInfo);
60 assert(sysInfo.dwNumberOfProcessors >= 1);
61 if (cpu_index != ALL_CPUS && cpu_index >= sysInfo.dwNumberOfProcessors) {
62 /* Tell hud_get_num_cpus there are only this many CPUs. */
63 return FALSE;
64 }
65
66 /* Get accumulated user and sys time for all threads */
67 if (!GetProcessTimes(GetCurrentProcess(), &ftCreation, &ftExit,
68 &ftKernel, &ftUser))
69 return FALSE;
70
71 GetSystemTimeAsFileTime(&ftNow);
72
73 *busy_time = filetime_to_scalar(ftUser) + filetime_to_scalar(ftKernel);
74 *total_time = filetime_to_scalar(ftNow) - filetime_to_scalar(ftCreation);
75
76 /* busy_time already has the time accross all cpus.
77 * XXX: if we want 100% to mean one CPU, 200% two cpus, eliminate the
78 * following line.
79 */
80 *total_time *= sysInfo.dwNumberOfProcessors;
81
82 /* XXX: we ignore cpu_index, i.e, we assume that the individual CPU usage
83 * and the system usage are one and the same.
84 */
85 return TRUE;
86 }
87
88 #else
89
90 static boolean
91 get_cpu_stats(unsigned cpu_index, uint64_t *busy_time, uint64_t *total_time)
92 {
93 char cpuname[32];
94 char line[1024];
95 FILE *f;
96
97 if (cpu_index == ALL_CPUS)
98 strcpy(cpuname, "cpu");
99 else
100 sprintf(cpuname, "cpu%u", cpu_index);
101
102 f = fopen("/proc/stat", "r");
103 if (!f)
104 return FALSE;
105
106 while (!feof(f) && fgets(line, sizeof(line), f)) {
107 if (strstr(line, cpuname) == line) {
108 uint64_t v[12];
109 int i, num;
110
111 num = sscanf(line,
112 "%s %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64
113 " %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64
114 " %"PRIu64" %"PRIu64"",
115 cpuname, &v[0], &v[1], &v[2], &v[3], &v[4], &v[5],
116 &v[6], &v[7], &v[8], &v[9], &v[10], &v[11]);
117 if (num < 5) {
118 fclose(f);
119 return FALSE;
120 }
121
122 /* user + nice + system */
123 *busy_time = v[0] + v[1] + v[2];
124 *total_time = *busy_time;
125
126 /* ... + idle + iowait + irq + softirq + ... */
127 for (i = 3; i < num-1; i++) {
128 *total_time += v[i];
129 }
130 fclose(f);
131 return TRUE;
132 }
133 }
134 fclose(f);
135 return FALSE;
136 }
137 #endif
138
139
140 struct cpu_info {
141 unsigned cpu_index;
142 uint64_t last_cpu_busy, last_cpu_total, last_time;
143 };
144
145 static void
146 query_cpu_load(struct hud_graph *gr)
147 {
148 struct cpu_info *info = gr->query_data;
149 uint64_t now = os_time_get();
150
151 if (info->last_time) {
152 if (info->last_time + gr->pane->period <= now) {
153 uint64_t cpu_busy, cpu_total, cpu_load;
154
155 get_cpu_stats(info->cpu_index, &cpu_busy, &cpu_total);
156
157 cpu_load = (cpu_busy - info->last_cpu_busy) * 100 /
158 (double)(cpu_total - info->last_cpu_total);
159 hud_graph_add_value(gr, cpu_load);
160
161 info->last_cpu_busy = cpu_busy;
162 info->last_cpu_total = cpu_total;
163 info->last_time = now;
164 }
165 }
166 else {
167 /* initialize */
168 info->last_time = now;
169 get_cpu_stats(info->cpu_index, &info->last_cpu_busy,
170 &info->last_cpu_total);
171 }
172 }
173
174 static void
175 free_query_data(void *p)
176 {
177 FREE(p);
178 }
179
180 void
181 hud_cpu_graph_install(struct hud_pane *pane, unsigned cpu_index)
182 {
183 struct hud_graph *gr;
184 struct cpu_info *info;
185 uint64_t busy, total;
186
187 /* see if the cpu exists */
188 if (cpu_index != ALL_CPUS && !get_cpu_stats(cpu_index, &busy, &total)) {
189 return;
190 }
191
192 gr = CALLOC_STRUCT(hud_graph);
193 if (!gr)
194 return;
195
196 if (cpu_index == ALL_CPUS)
197 strcpy(gr->name, "cpu");
198 else
199 sprintf(gr->name, "cpu%u", cpu_index);
200
201 gr->query_data = CALLOC_STRUCT(cpu_info);
202 if (!gr->query_data) {
203 FREE(gr);
204 return;
205 }
206
207 gr->query_new_value = query_cpu_load;
208
209 /* Don't use free() as our callback as that messes up Gallium's
210 * memory debugger. Use simple free_query_data() wrapper.
211 */
212 gr->free_query_data = free_query_data;
213
214 info = gr->query_data;
215 info->cpu_index = cpu_index;
216
217 hud_graph_set_dump_file(gr);
218
219 hud_pane_add_graph(pane, gr);
220 hud_pane_set_max_value(pane, 100);
221 }
222
223 int
224 hud_get_num_cpus(void)
225 {
226 uint64_t busy, total;
227 int i = 0;
228
229 while (get_cpu_stats(i, &busy, &total))
230 i++;
231
232 return i;
233 }
234
235 struct thread_info {
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 = pipe_current_thread_get_time_nano();
249
250 hud_graph_add_value(gr,
251 (thread_now - info->last_thread_time) * 100 /
252 (now - info->last_time));
253
254 info->last_thread_time = thread_now;
255 info->last_time = now;
256 }
257 } else {
258 /* initialize */
259 info->last_time = now;
260 info->last_thread_time = pipe_current_thread_get_time_nano();
261 }
262 }
263
264 void
265 hud_api_thread_busy_install(struct hud_pane *pane)
266 {
267 struct hud_graph *gr;
268
269 gr = CALLOC_STRUCT(hud_graph);
270 if (!gr)
271 return;
272
273 strcpy(gr->name, "API-thread-busy");
274
275 gr->query_data = CALLOC_STRUCT(thread_info);
276 if (!gr->query_data) {
277 FREE(gr);
278 return;
279 }
280
281 gr->query_new_value = query_api_thread_busy_status;
282
283 /* Don't use free() as our callback as that messes up Gallium's
284 * memory debugger. Use simple free_query_data() wrapper.
285 */
286 gr->free_query_data = free_query_data;
287
288 hud_graph_set_dump_file(gr);
289
290 hud_pane_add_graph(pane, gr);
291 hud_pane_set_max_value(pane, 100);
292 }