1 /**************************************************************************
3 * Copyright (C) 2016 Steven Toth <stoth@kernellabs.com>
4 * Copyright (C) 2016 Zodiac Inflight Innovations
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:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
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.
27 **************************************************************************/
29 #if HAVE_GALLIUM_EXTRA_HUD
32 * Reading /sys/devices/system/cpu/cpu?/cpufreq/scaling_???_freq
33 * cpu frequency (KHz), displaying on the HUD in Hz.
36 #include "hud/hud_private.h"
37 #include "util/list.h"
38 #include "os/os_time.h"
39 #include "util/u_memory.h"
46 #include <sys/types.h>
53 struct list_head list
;
54 int mode
; /* CPUFREQ_MINIMUM, CPUFREQ_CURRENT, CPUFREQ_MAXIMUM */
55 char name
[16]; /* EG. cpu0 */
58 /* EG. /sys/devices/system/cpu/cpu?/cpufreq/scaling_cur_freq */
59 char sysfs_filename
[128];
64 static int gcpufreq_count
= 0;
65 static struct list_head gcpufreq_list
;
67 static struct cpufreq_info
*
68 find_cfi_by_index(int cpu_index
, int mode
)
70 list_for_each_entry(struct cpufreq_info
, cfi
, &gcpufreq_list
, list
) {
71 if (cfi
->mode
!= mode
)
73 if (cfi
->cpu_index
== cpu_index
)
80 get_file_value(const char *fn
, uint64_t *KHz
)
82 FILE *fh
= fopen(fn
, "r");
84 fprintf(stderr
, "%s error: %s\n", fn
, strerror(errno
));
87 int ret
= fscanf(fh
, "%" PRIu64
"", KHz
);
94 query_cfi_load(struct hud_graph
*gr
)
96 struct cpufreq_info
*cfi
= gr
->query_data
;
98 uint64_t now
= os_time_get();
100 if (cfi
->last_time
+ gr
->pane
->period
<= now
) {
102 case CPUFREQ_MINIMUM
:
103 case CPUFREQ_CURRENT
:
104 case CPUFREQ_MAXIMUM
:
105 get_file_value(cfi
->sysfs_filename
, &cfi
->KHz
);
106 hud_graph_add_value(gr
, (uint64_t)cfi
->KHz
* 1000);
108 cfi
->last_time
= now
;
112 get_file_value(cfi
->sysfs_filename
, &cfi
->KHz
);
113 cfi
->last_time
= now
;
118 free_query_data(void *p
)
120 struct cpufreq_info
*cfi
= (struct cpufreq_info
*)p
;
121 list_del(&cfi
->list
);
126 * Create and initialize a new object for a specific CPU.
127 * \param pane parent context.
128 * \param cpu_index CPU identifier Eg. 0 (CPU0)
129 * \param mode query CPUFREQ_MINIMUM | CURRENT | MAXIMUM statistic.
132 hud_cpufreq_graph_install(struct hud_pane
*pane
, int cpu_index
,
135 struct hud_graph
*gr
;
136 struct cpufreq_info
*cfi
;
138 int num_cpus
= hud_get_num_cpufreq(0);
143 printf("%s(%d, %s) - Creating HUD object\n", __func__
, cpu_index
,
144 mode
== CPUFREQ_MINIMUM
? "MIN" :
145 mode
== CPUFREQ_CURRENT
? "CUR" :
146 mode
== CPUFREQ_MAXIMUM
? "MAX" : "UNDEFINED");
149 cfi
= find_cfi_by_index(cpu_index
, mode
);
153 gr
= CALLOC_STRUCT(hud_graph
);
159 case CPUFREQ_MINIMUM
:
160 snprintf(gr
->name
, sizeof(gr
->name
), "%s-Min", cfi
->name
);
162 case CPUFREQ_CURRENT
:
163 snprintf(gr
->name
, sizeof(gr
->name
), "%s-Cur", cfi
->name
);
165 case CPUFREQ_MAXIMUM
:
166 snprintf(gr
->name
, sizeof(gr
->name
), "%s-Max", cfi
->name
);
171 gr
->query_data
= cfi
;
172 gr
->query_new_value
= query_cfi_load
;
174 /* Don't use free() as our callback as that messes up Gallium's
175 * memory debugger. Use simple free_query_data() wrapper.
177 gr
->free_query_data
= free_query_data
;
179 hud_pane_add_graph(pane
, gr
);
180 hud_pane_set_max_value(pane
, 3000000 /* 3 GHz */);
184 add_object(const char *name
, const char *fn
, int objmode
, int cpu_index
)
186 struct cpufreq_info
*cfi
= CALLOC_STRUCT(cpufreq_info
);
188 strcpy(cfi
->name
, name
);
189 strcpy(cfi
->sysfs_filename
, fn
);
191 cfi
->cpu_index
= cpu_index
;
192 list_addtail(&cfi
->list
, &gcpufreq_list
);
197 * Initialize internal object arrays and display cpu freq HUD help.
198 * \param displayhelp true if the list of detected cpus should be
199 displayed on the console.
200 * \return number of detected CPU metrics (CPU count * 3)
203 hud_get_num_cpufreq(bool displayhelp
)
206 struct stat stat_buf
;
210 /* Return the number of CPU metrics we support. */
212 return gcpufreq_count
;
214 /* Scan /sys/devices.../cpu, for every object type we support, create
215 * and persist an object to represent its different metrics.
217 list_inithead(&gcpufreq_list
);
218 DIR *dir
= opendir("/sys/devices/system/cpu");
222 while ((dp
= readdir(dir
)) != NULL
) {
224 /* Avoid 'lo' and '..' and '.' */
225 if (strlen(dp
->d_name
) <= 2)
228 if (sscanf(dp
->d_name
, "cpu%d\n", &cpu_index
) != 1)
232 snprintf(basename
, sizeof(basename
), "/sys/devices/system/cpu/%s", dp
->d_name
);
234 snprintf(fn
, sizeof(fn
), "%s/cpufreq/scaling_cur_freq", basename
);
235 if (stat(fn
, &stat_buf
) < 0)
238 if (!S_ISREG(stat_buf
.st_mode
))
239 continue; /* Not a regular file */
241 snprintf(fn
, sizeof(fn
), "%s/cpufreq/scaling_min_freq", basename
);
242 add_object(dp
->d_name
, fn
, CPUFREQ_MINIMUM
, cpu_index
);
244 snprintf(fn
, sizeof(fn
), "%s/cpufreq/scaling_cur_freq", basename
);
245 add_object(dp
->d_name
, fn
, CPUFREQ_CURRENT
, cpu_index
);
247 snprintf(fn
, sizeof(fn
), "%s/cpufreq/scaling_max_freq", basename
);
248 add_object(dp
->d_name
, fn
, CPUFREQ_MAXIMUM
, cpu_index
);
252 list_for_each_entry(struct cpufreq_info
, cfi
, &gcpufreq_list
, list
) {
254 snprintf(line
, sizeof(line
), " cpufreq-%s-%s",
255 cfi
->mode
== CPUFREQ_MINIMUM
? "min" :
256 cfi
->mode
== CPUFREQ_CURRENT
? "cur" :
257 cfi
->mode
== CPUFREQ_MAXIMUM
? "max" : "undefined", cfi
->name
);
263 return gcpufreq_count
;
266 #endif /* HAVE_GALLIUM_EXTRA_HUD */