gallium/hud: create files after graphs are created to get final names
[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_pane_add_graph(pane, gr);
218 hud_pane_set_max_value(pane, 100);
219 }
220
221 int
222 hud_get_num_cpus(void)
223 {
224 uint64_t busy, total;
225 int i = 0;
226
227 while (get_cpu_stats(i, &busy, &total))
228 i++;
229
230 return i;
231 }
232
233 struct thread_info {
234 int64_t last_time;
235 int64_t last_thread_time;
236 };
237
238 static void
239 query_api_thread_busy_status(struct hud_graph *gr)
240 {
241 struct thread_info *info = gr->query_data;
242 int64_t now = os_time_get_nano();
243
244 if (info->last_time) {
245 if (info->last_time + gr->pane->period*1000 <= now) {
246 int64_t thread_now = pipe_current_thread_get_time_nano();
247
248 hud_graph_add_value(gr,
249 (thread_now - info->last_thread_time) * 100 /
250 (now - info->last_time));
251
252 info->last_thread_time = thread_now;
253 info->last_time = now;
254 }
255 } else {
256 /* initialize */
257 info->last_time = now;
258 info->last_thread_time = pipe_current_thread_get_time_nano();
259 }
260 }
261
262 void
263 hud_api_thread_busy_install(struct hud_pane *pane)
264 {
265 struct hud_graph *gr;
266
267 gr = CALLOC_STRUCT(hud_graph);
268 if (!gr)
269 return;
270
271 strcpy(gr->name, "API-thread-busy");
272
273 gr->query_data = CALLOC_STRUCT(thread_info);
274 if (!gr->query_data) {
275 FREE(gr);
276 return;
277 }
278
279 gr->query_new_value = query_api_thread_busy_status;
280
281 /* Don't use free() as our callback as that messes up Gallium's
282 * memory debugger. Use simple free_query_data() wrapper.
283 */
284 gr->free_query_data = free_query_data;
285
286 hud_pane_add_graph(pane, gr);
287 hud_pane_set_max_value(pane, 100);
288 }