xref: /aosp_15_r20/external/mesa3d/src/gallium/auxiliary/hud/hud_cpufreq.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /**************************************************************************
2  *
3  * Copyright (C) 2016 Steven Toth <[email protected]>
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 #ifdef 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 "util/os_time.h"
39 #include "util/simple_mtx.h"
40 #include "util/u_thread.h"
41 #include "util/u_memory.h"
42 #include <stdio.h>
43 #include <unistd.h>
44 #include <dirent.h>
45 #include <stdlib.h>
46 #include <errno.h>
47 #include <inttypes.h>
48 #include <sys/types.h>
49 #include <sys/stat.h>
50 
51 struct cpufreq_info
52 {
53    struct list_head list;
54    int mode; /* CPUFREQ_MINIMUM, CPUFREQ_CURRENT, CPUFREQ_MAXIMUM */
55    char name[16]; /* EG. cpu0 */
56    int cpu_index;
57 
58    /* EG. /sys/devices/system/cpu/cpu?/cpufreq/scaling_cur_freq */
59    char sysfs_filename[128];
60    uint64_t KHz;
61    uint64_t last_time;
62 };
63 
64 static int gcpufreq_count = 0;
65 static struct list_head gcpufreq_list;
66 static simple_mtx_t gcpufreq_mutex = SIMPLE_MTX_INITIALIZER;
67 
68 static struct cpufreq_info *
find_cfi_by_index(int cpu_index,int mode)69 find_cfi_by_index(int cpu_index, int mode)
70 {
71    list_for_each_entry(struct cpufreq_info, cfi, &gcpufreq_list, list) {
72       if (cfi->mode != mode)
73          continue;
74       if (cfi->cpu_index == cpu_index)
75          return cfi;
76    }
77    return 0;
78 }
79 
80 static int
get_file_value(const char * fn,uint64_t * KHz)81 get_file_value(const char *fn, uint64_t *KHz)
82 {
83    FILE *fh = fopen(fn, "r");
84    if (!fh) {
85       fprintf(stderr, "%s error: %s\n", fn, strerror(errno));
86       return -1;
87    }
88    int ret = fscanf(fh, "%" PRIu64 "", KHz);
89    fclose(fh);
90 
91    return ret;
92 }
93 
94 static void
query_cfi_load(struct hud_graph * gr,struct pipe_context * pipe)95 query_cfi_load(struct hud_graph *gr, struct pipe_context *pipe)
96 {
97    struct cpufreq_info *cfi = gr->query_data;
98 
99    uint64_t now = os_time_get();
100    if (cfi->last_time) {
101       if (cfi->last_time + gr->pane->period <= now) {
102          switch (cfi->mode) {
103          case CPUFREQ_MINIMUM:
104          case CPUFREQ_CURRENT:
105          case CPUFREQ_MAXIMUM:
106             get_file_value(cfi->sysfs_filename, &cfi->KHz);
107             hud_graph_add_value(gr, (uint64_t)cfi->KHz * 1000);
108          }
109          cfi->last_time = now;
110       }
111    } else {
112       /* initialize */
113       get_file_value(cfi->sysfs_filename, &cfi->KHz);
114       cfi->last_time = now;
115    }
116 }
117 
118 /**
119   * Create and initialize a new object for a specific CPU.
120   * \param  pane  parent context.
121   * \param  cpu_index  CPU identifier Eg. 0 (CPU0)
122   * \param  mode  query CPUFREQ_MINIMUM | CURRENT | MAXIMUM statistic.
123   */
124 void
hud_cpufreq_graph_install(struct hud_pane * pane,int cpu_index,unsigned int mode)125 hud_cpufreq_graph_install(struct hud_pane *pane, int cpu_index,
126                            unsigned int mode)
127 {
128    struct hud_graph *gr;
129    struct cpufreq_info *cfi;
130 
131    int num_cpus = hud_get_num_cpufreq(0);
132    if (num_cpus <= 0)
133       return;
134 
135    cfi = find_cfi_by_index(cpu_index, mode);
136    if (!cfi)
137       return;
138 
139    gr = CALLOC_STRUCT(hud_graph);
140    if (!gr)
141       return;
142 
143    cfi->mode = mode;
144    switch(cfi->mode) {
145    case CPUFREQ_MINIMUM:
146       snprintf(gr->name, sizeof(gr->name), "%s-Min", cfi->name);
147       break;
148    case CPUFREQ_CURRENT:
149       snprintf(gr->name, sizeof(gr->name), "%s-Cur", cfi->name);
150       break;
151    case CPUFREQ_MAXIMUM:
152       snprintf(gr->name, sizeof(gr->name), "%s-Max", cfi->name);
153       break;
154    default:
155       free(gr);
156       return;
157    }
158 
159    gr->query_data = cfi;
160    gr->query_new_value = query_cfi_load;
161 
162    hud_pane_add_graph(pane, gr);
163    hud_pane_set_max_value(pane, 3000000 /* 3 GHz */);
164 }
165 
166 static void
add_object(const char * name,const char * fn,int objmode,int cpu_index)167 add_object(const char *name, const char *fn, int objmode, int cpu_index)
168 {
169    struct cpufreq_info *cfi = CALLOC_STRUCT(cpufreq_info);
170 
171    strcpy(cfi->name, name);
172    strcpy(cfi->sysfs_filename, fn);
173    cfi->mode = objmode;
174    cfi->cpu_index = cpu_index;
175    list_addtail(&cfi->list, &gcpufreq_list);
176    gcpufreq_count++;
177 }
178 
179 /**
180   * Initialize internal object arrays and display cpu freq HUD help.
181   * \param  displayhelp  true if the list of detected cpus should be
182                          displayed on the console.
183   * \return  number of detected CPU metrics (CPU count * 3)
184   */
185 int
hud_get_num_cpufreq(bool displayhelp)186 hud_get_num_cpufreq(bool displayhelp)
187 {
188    struct dirent *dp;
189    struct stat stat_buf;
190    char fn[128];
191    int cpu_index;
192 
193    /* Return the number of CPU metrics we support. */
194    simple_mtx_lock(&gcpufreq_mutex);
195    if (gcpufreq_count) {
196       simple_mtx_unlock(&gcpufreq_mutex);
197       return gcpufreq_count;
198    }
199 
200    /* Scan /sys/devices.../cpu, for every object type we support, create
201     * and persist an object to represent its different metrics.
202     */
203    list_inithead(&gcpufreq_list);
204    DIR *dir = opendir("/sys/devices/system/cpu");
205    if (!dir) {
206       simple_mtx_unlock(&gcpufreq_mutex);
207       return 0;
208    }
209 
210    while ((dp = readdir(dir)) != NULL) {
211 
212       size_t d_name_len = strlen(dp->d_name);
213 
214       /* Avoid 'lo' and '..' and '.', and avoid overlong names that
215        * would  result in a buffer overflow in add_object.
216        */
217       if (d_name_len <= 2 || d_name_len > 15)
218          continue;
219 
220       if (sscanf(dp->d_name, "cpu%d\n", &cpu_index) != 1)
221          continue;
222 
223       char basename[256];
224       snprintf(basename, sizeof(basename), "/sys/devices/system/cpu/%s", dp->d_name);
225 
226       snprintf(fn, sizeof(fn), "%s/cpufreq/scaling_cur_freq", basename);
227       if (stat(fn, &stat_buf) < 0)
228          continue;
229 
230       if (!S_ISREG(stat_buf.st_mode))
231          continue;              /* Not a regular file */
232 
233       snprintf(fn, sizeof(fn), "%s/cpufreq/scaling_min_freq", basename);
234       add_object(dp->d_name, fn, CPUFREQ_MINIMUM, cpu_index);
235 
236       snprintf(fn, sizeof(fn), "%s/cpufreq/scaling_cur_freq", basename);
237       add_object(dp->d_name, fn, CPUFREQ_CURRENT, cpu_index);
238 
239       snprintf(fn, sizeof(fn), "%s/cpufreq/scaling_max_freq", basename);
240       add_object(dp->d_name, fn, CPUFREQ_MAXIMUM, cpu_index);
241    }
242    closedir(dir);
243 
244    if (displayhelp) {
245       list_for_each_entry(struct cpufreq_info, cfi, &gcpufreq_list, list) {
246          char line[128];
247          snprintf(line, sizeof(line), "    cpufreq-%s-%s",
248                  cfi->mode == CPUFREQ_MINIMUM ? "min" :
249                  cfi->mode == CPUFREQ_CURRENT ? "cur" :
250                  cfi->mode == CPUFREQ_MAXIMUM ? "max" : "undefined", cfi->name);
251 
252          puts(line);
253       }
254    }
255 
256    simple_mtx_unlock(&gcpufreq_mutex);
257    return gcpufreq_count;
258 }
259 
260 #endif /* HAVE_GALLIUM_EXTRA_HUD */
261