gallium/hud: add support for PIPE_QUERY_PIPELINE_STATISTICS
[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 "util/u_memory.h"
34 #include <stdio.h>
35 #include <inttypes.h>
36
37 static boolean
38 get_cpu_stats(unsigned cpu_index, uint64_t *busy_time, uint64_t *total_time)
39 {
40 char cpuname[32];
41 char line[1024];
42 FILE *f;
43
44 if (cpu_index == ALL_CPUS)
45 strcpy(cpuname, "cpu");
46 else
47 sprintf(cpuname, "cpu%u", cpu_index);
48
49 f = fopen("/proc/stat", "r");
50 if (!f)
51 return FALSE;
52
53 while (!feof(f) && fgets(line, sizeof(line), f)) {
54 if (strstr(line, cpuname) == line) {
55 uint64_t v[12];
56 int i, num;
57
58 num = sscanf(line,
59 "%s %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64
60 " %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64
61 " %"PRIu64" %"PRIu64"",
62 cpuname, &v[0], &v[1], &v[2], &v[3], &v[4], &v[5],
63 &v[6], &v[7], &v[8], &v[9], &v[10], &v[11]);
64 if (num < 5) {
65 fclose(f);
66 return FALSE;
67 }
68
69 /* user + nice + system */
70 *busy_time = v[0] + v[1] + v[2];
71 *total_time = *busy_time;
72
73 /* ... + idle + iowait + irq + softirq + ... */
74 for (i = 3; i < num-1; i++) {
75 *total_time += v[i];
76 }
77 fclose(f);
78 return TRUE;
79 }
80 }
81 fclose(f);
82 return FALSE;
83 }
84
85 struct cpu_info {
86 unsigned cpu_index;
87 uint64_t last_cpu_busy, last_cpu_total, last_time;
88 };
89
90 static void
91 query_cpu_load(struct hud_graph *gr)
92 {
93 struct cpu_info *info = gr->query_data;
94 uint64_t now = os_time_get();
95
96 if (info->last_time) {
97 if (info->last_time + gr->pane->period <= now) {
98 uint64_t cpu_busy, cpu_total, cpu_load;
99
100 get_cpu_stats(info->cpu_index, &cpu_busy, &cpu_total);
101
102 cpu_load = (cpu_busy - info->last_cpu_busy) * 100 /
103 (double)(cpu_total - info->last_cpu_total);
104 hud_graph_add_value(gr, cpu_load);
105
106 info->last_cpu_busy = cpu_busy;
107 info->last_cpu_total = cpu_total;
108 info->last_time = now;
109 }
110 }
111 else {
112 /* initialize */
113 info->last_time = now;
114 get_cpu_stats(info->cpu_index, &info->last_cpu_busy,
115 &info->last_cpu_total);
116 }
117 }
118
119 void
120 hud_cpu_graph_install(struct hud_pane *pane, unsigned cpu_index)
121 {
122 struct hud_graph *gr;
123 struct cpu_info *info;
124 uint64_t busy, total;
125
126 /* see if the cpu exists */
127 if (cpu_index != ALL_CPUS && !get_cpu_stats(cpu_index, &busy, &total)) {
128 return;
129 }
130
131 gr = CALLOC_STRUCT(hud_graph);
132 if (!gr)
133 return;
134
135 if (cpu_index == ALL_CPUS)
136 strcpy(gr->name, "cpu");
137 else
138 sprintf(gr->name, "cpu%u", cpu_index);
139
140 gr->query_data = CALLOC_STRUCT(cpu_info);
141 if (!gr->query_data) {
142 FREE(gr);
143 return;
144 }
145
146 gr->query_new_value = query_cpu_load;
147 gr->free_query_data = free;
148
149 info = gr->query_data;
150 info->cpu_index = cpu_index;
151
152 hud_pane_add_graph(pane, gr);
153 hud_pane_set_max_value(pane, 100);
154 }
155
156 int
157 hud_get_num_cpus(void)
158 {
159 uint64_t busy, total;
160 int i = 0;
161
162 while (get_cpu_stats(i, &busy, &total))
163 i++;
164
165 return i;
166 }