gallium/hud: fix a problem where objects are free'd while in use.
[mesa.git] / src / gallium / auxiliary / hud / hud_cpufreq.c
1 /**************************************************************************
2 *
3 * Copyright (C) 2016 Steven Toth <stoth@kernellabs.com>
4 * Copyright (C) 2016 Zodiac Inflight Innovations
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL THE AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
29 #if HAVE_GALLIUM_EXTRA_HUD
30
31 /* Purpose:
32 * Reading /sys/devices/system/cpu/cpu?/cpufreq/scaling_???_freq
33 * cpu frequency (KHz), displaying on the HUD in Hz.
34 */
35
36 #include "hud/hud_private.h"
37 #include "util/list.h"
38 #include "os/os_time.h"
39 #include "util/u_memory.h"
40 #include <stdio.h>
41 #include <unistd.h>
42 #include <dirent.h>
43 #include <stdlib.h>
44 #include <errno.h>
45 #include <inttypes.h>
46 #include <sys/types.h>
47 #include <sys/stat.h>
48
49 struct cpufreq_info
50 {
51 struct list_head list;
52 int mode; /* CPUFREQ_MINIMUM, CPUFREQ_CURRENT, CPUFREQ_MAXIMUM */
53 char name[16]; /* EG. cpu0 */
54 int cpu_index;
55
56 /* EG. /sys/devices/system/cpu/cpu?/cpufreq/scaling_cur_freq */
57 char sysfs_filename[128];
58 uint64_t KHz;
59 uint64_t last_time;
60 };
61
62 static int gcpufreq_count = 0;
63 static struct list_head gcpufreq_list;
64
65 static struct cpufreq_info *
66 find_cfi_by_index(int cpu_index, int mode)
67 {
68 list_for_each_entry(struct cpufreq_info, cfi, &gcpufreq_list, list) {
69 if (cfi->mode != mode)
70 continue;
71 if (cfi->cpu_index == cpu_index)
72 return cfi;
73 }
74 return 0;
75 }
76
77 static int
78 get_file_value(const char *fn, uint64_t *KHz)
79 {
80 FILE *fh = fopen(fn, "r");
81 if (!fh) {
82 fprintf(stderr, "%s error: %s\n", fn, strerror(errno));
83 return -1;
84 }
85 int ret = fscanf(fh, "%" PRIu64 "", KHz);
86 fclose(fh);
87
88 return ret;
89 }
90
91 static void
92 query_cfi_load(struct hud_graph *gr)
93 {
94 struct cpufreq_info *cfi = gr->query_data;
95
96 uint64_t now = os_time_get();
97 if (cfi->last_time) {
98 if (cfi->last_time + gr->pane->period <= now) {
99 switch (cfi->mode) {
100 case CPUFREQ_MINIMUM:
101 case CPUFREQ_CURRENT:
102 case CPUFREQ_MAXIMUM:
103 get_file_value(cfi->sysfs_filename, &cfi->KHz);
104 hud_graph_add_value(gr, (uint64_t)cfi->KHz * 1000);
105 }
106 cfi->last_time = now;
107 }
108 } else {
109 /* initialize */
110 get_file_value(cfi->sysfs_filename, &cfi->KHz);
111 cfi->last_time = now;
112 }
113 }
114
115 /**
116 * Create and initialize a new object for a specific CPU.
117 * \param pane parent context.
118 * \param cpu_index CPU identifier Eg. 0 (CPU0)
119 * \param mode query CPUFREQ_MINIMUM | CURRENT | MAXIMUM statistic.
120 */
121 void
122 hud_cpufreq_graph_install(struct hud_pane *pane, int cpu_index,
123 unsigned int mode)
124 {
125 struct hud_graph *gr;
126 struct cpufreq_info *cfi;
127
128 int num_cpus = hud_get_num_cpufreq(0);
129 if (num_cpus <= 0)
130 return;
131
132 cfi = find_cfi_by_index(cpu_index, mode);
133 if (!cfi)
134 return;
135
136 gr = CALLOC_STRUCT(hud_graph);
137 if (!gr)
138 return;
139
140 cfi->mode = mode;
141 switch(cfi->mode) {
142 case CPUFREQ_MINIMUM:
143 snprintf(gr->name, sizeof(gr->name), "%s-Min", cfi->name);
144 break;
145 case CPUFREQ_CURRENT:
146 snprintf(gr->name, sizeof(gr->name), "%s-Cur", cfi->name);
147 break;
148 case CPUFREQ_MAXIMUM:
149 snprintf(gr->name, sizeof(gr->name), "%s-Max", cfi->name);
150 default:
151 return;
152 }
153
154 gr->query_data = cfi;
155 gr->query_new_value = query_cfi_load;
156
157 hud_pane_add_graph(pane, gr);
158 hud_pane_set_max_value(pane, 3000000 /* 3 GHz */);
159 }
160
161 static void
162 add_object(const char *name, const char *fn, int objmode, int cpu_index)
163 {
164 struct cpufreq_info *cfi = CALLOC_STRUCT(cpufreq_info);
165
166 strcpy(cfi->name, name);
167 strcpy(cfi->sysfs_filename, fn);
168 cfi->mode = objmode;
169 cfi->cpu_index = cpu_index;
170 list_addtail(&cfi->list, &gcpufreq_list);
171 gcpufreq_count++;
172 }
173
174 /**
175 * Initialize internal object arrays and display cpu freq HUD help.
176 * \param displayhelp true if the list of detected cpus should be
177 displayed on the console.
178 * \return number of detected CPU metrics (CPU count * 3)
179 */
180 int
181 hud_get_num_cpufreq(bool displayhelp)
182 {
183 struct dirent *dp;
184 struct stat stat_buf;
185 char fn[128];
186 int cpu_index;
187
188 /* Return the number of CPU metrics we support. */
189 if (gcpufreq_count)
190 return gcpufreq_count;
191
192 /* Scan /sys/devices.../cpu, for every object type we support, create
193 * and persist an object to represent its different metrics.
194 */
195 list_inithead(&gcpufreq_list);
196 DIR *dir = opendir("/sys/devices/system/cpu");
197 if (!dir)
198 return 0;
199
200 while ((dp = readdir(dir)) != NULL) {
201
202 /* Avoid 'lo' and '..' and '.' */
203 if (strlen(dp->d_name) <= 2)
204 continue;
205
206 if (sscanf(dp->d_name, "cpu%d\n", &cpu_index) != 1)
207 continue;
208
209 char basename[256];
210 snprintf(basename, sizeof(basename), "/sys/devices/system/cpu/%s", dp->d_name);
211
212 snprintf(fn, sizeof(fn), "%s/cpufreq/scaling_cur_freq", basename);
213 if (stat(fn, &stat_buf) < 0)
214 continue;
215
216 if (!S_ISREG(stat_buf.st_mode))
217 continue; /* Not a regular file */
218
219 snprintf(fn, sizeof(fn), "%s/cpufreq/scaling_min_freq", basename);
220 add_object(dp->d_name, fn, CPUFREQ_MINIMUM, cpu_index);
221
222 snprintf(fn, sizeof(fn), "%s/cpufreq/scaling_cur_freq", basename);
223 add_object(dp->d_name, fn, CPUFREQ_CURRENT, cpu_index);
224
225 snprintf(fn, sizeof(fn), "%s/cpufreq/scaling_max_freq", basename);
226 add_object(dp->d_name, fn, CPUFREQ_MAXIMUM, cpu_index);
227 }
228
229 if (displayhelp) {
230 list_for_each_entry(struct cpufreq_info, cfi, &gcpufreq_list, list) {
231 char line[128];
232 snprintf(line, sizeof(line), " cpufreq-%s-%s",
233 cfi->mode == CPUFREQ_MINIMUM ? "min" :
234 cfi->mode == CPUFREQ_CURRENT ? "cur" :
235 cfi->mode == CPUFREQ_MAXIMUM ? "max" : "undefined", cfi->name);
236
237 puts(line);
238 }
239 }
240
241 return gcpufreq_count;
242 }
243
244 #endif /* HAVE_GALLIUM_EXTRA_HUD */