Debian Codegraph @

madrisan-nagios-plugins-linux/nagios-plugins-linux/lib/cpufreq.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2014 Davide Madrisan <[email protected]>
5 *
6 * A library for checking the CPU frequency configuration
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "common.h"
27 #include "cpufreq.h"
28 #include "sysfsparser.h"
29 #include "xalloc.h"
30 #include "xasprintf.h"
31
32 struct cpufreq_available_frequencies
33 {
34 unsigned long value;
35 struct cpufreq_available_frequencies *next;
36 };
37
38 unsigned long
39 cpufreq_get_freq_kernel (unsigned int cpu)
40 {
41 return sysfsparser_cpufreq_get_freq_kernel (cpu);
42 }
43
44 struct cpufreq_available_frequencies *
45 cpufreq_get_available_freqs (unsigned int cpu)
46 {
47 char *freqs =
48 sysfsparser_cpufreq_get_available_freqs (cpu);
49
50 if (NULL == freqs)
51 return NULL;
52
53 struct cpufreq_available_frequencies *first = NULL, *curr = NULL;
54 char *str, *saveptr;
55
56 for (str = freqs; ; str = NULL)
57 {
58 char *token = strtok_r (str, " ", &saveptr);
59 if (token == NULL)
60 break;
61
62 if (curr)
63 {
64 curr->next = xmalloc (sizeof (*curr));
65 curr = curr->next;
66 }
67 else
68 {
69 first = xmalloc (sizeof (*first));
70 curr = first;
71 }
72 curr->next = NULL;
73 curr->value = strtoul (token, NULL, 10);
74 }
75
76 return first;
77 }
78
79 struct cpufreq_available_frequencies *
80 cpufreq_get_available_freqs_next (struct cpufreq_available_frequencies *curr)
81 {
82 return curr->next;
83 }
84
85 unsigned long
86 cpufreq_get_available_freqs_value (struct cpufreq_available_frequencies *curr)
87 {
88 return curr->value;
89 }
90
91 void
92 cpufreq_available_frequencies_unref(struct cpufreq_available_frequencies *first)
93 {
94 struct cpufreq_available_frequencies *curr = first;
95 while (curr)
96 {
97 struct cpufreq_available_frequencies *tmp = curr;
98 curr = curr->next;
99 free(tmp);
100 }
101 }
102
103 int
104 cpufreq_get_hardware_limits (unsigned int cpu,
105 unsigned long *min, unsigned long *max)
106 {
107 return sysfsparser_cpufreq_get_hardware_limits (cpu, min, max);
108 }
109
110 unsigned long
111 cpufreq_get_transition_latency (unsigned int cpu)
112 {
113 return sysfsparser_cpufreq_get_transition_latency (cpu);
114 }
115
116 char *
117 cpufreq_get_driver (unsigned int cpu)
118 {
119 return sysfsparser_cpufreq_get_driver (cpu);
120 }
121
122 char *
123 cpufreq_get_governor (unsigned int cpu)
124 {
125 return sysfsparser_cpufreq_get_governor (cpu);
126 }
127
128 char *
129 cpufreq_get_available_governors (unsigned int cpu)
130 {
131 return sysfsparser_cpufreq_get_available_governors (cpu);
132 }
133
134 char *
135 cpufreq_freq_to_string (unsigned long freq)
136 {
137 unsigned long tmp;
138
139 if (freq > 1000000)
140 {
141 tmp = freq % 10000;
142 if (tmp >= 5000)
143 freq += 10000;
144 return xasprintf ("%u.%02uGHz", ((unsigned int) freq / 1000000),
145 ((unsigned int) (freq % 1000000) / 10000));
146 }
147 else if (freq > 100000)
148 {
149 tmp = freq % 1000;
150 if (tmp >= 500)
151 freq += 1000;
152 return xasprintf ("%uMHz", ((unsigned int) freq / 1000));
153 }
154 else if (freq > 1000)
155 {
156 tmp = freq % 100;
157 if (tmp >= 50)
158 freq += 100;
159 return xasprintf ("%u.%01uMHz", ((unsigned int) freq / 1000),
160 ((unsigned int) (freq % 1000) / 100));
161 }
162 else
163 return xasprintf ("%lukHz", freq);
164 }
165
166 char *
167 cpufreq_duration_to_string (unsigned long duration)
168 {
169 unsigned long tmp;
170
171 if (duration > 1000000)
172 {
173 tmp = duration % 10000;
174 if (tmp >= 5000)
175 duration += 10000;
176 return xasprintf ("%u.%02ums", ((unsigned int) duration / 1000000),
177 ((unsigned int) (duration % 1000000) / 10000));
178 }
179 else if (duration > 100000)
180 {
181 tmp = duration % 1000;
182 if (tmp >= 500)
183 duration += 1000;
184 return xasprintf ("%uus", ((unsigned int) duration / 1000));
185 }
186 else if (duration > 1000)
187 {
188 tmp = duration % 100;
189 if (tmp >= 50)
190 duration += 100;
191 return xasprintf ("%u.%01uus", ((unsigned int) duration / 1000),
192 ((unsigned int) (duration % 1000) / 100));
193 }
194 else
195 return xasprintf ("%luns", duration);
196 }