summaryrefslogtreecommitdiffstats
path: root/src/vppinfra/time.c
diff options
context:
space:
mode:
authorDamjan Marion <damarion@cisco.com>2023-08-06 20:39:38 +0200
committerFlorin Coras <florin.coras@gmail.com>2023-08-07 17:33:09 +0000
commit8d0c0c68215a2972053ba7fd8fa0f82aa14f8624 (patch)
tree035f14012dfd939890cf360898be6d4e71021504 /src/vppinfra/time.c
parent696db20e332fcbecaaef9c5505cc2e132bfaa9e2 (diff)
vppinfra: add unformat_init_path
More conveninet way to unformat file by providing filesystem path. Takes format string for easier constuction of path... Type: improvement Change-Id: I433204fa20dc98e2b11c53914883d047a7fc62c6 Signed-off-by: Damjan Marion <damarion@cisco.com>
Diffstat (limited to 'src/vppinfra/time.c')
-rw-r--r--src/vppinfra/time.c51
1 files changed, 23 insertions, 28 deletions
diff --git a/src/vppinfra/time.c b/src/vppinfra/time.c
index 3377828bbc5..5a6aaf182e4 100644
--- a/src/vppinfra/time.c
+++ b/src/vppinfra/time.c
@@ -74,7 +74,6 @@ clock_frequency_from_proc_filesystem (void)
{
f64 cpu_freq = 1e9; /* better than 40... */
f64 ppc_timebase = 0; /* warnings be gone */
- int fd;
unformat_input_t input;
/* $$$$ aarch64 kernel doesn't report "cpu MHz" */
@@ -83,26 +82,24 @@ clock_frequency_from_proc_filesystem (void)
#endif
cpu_freq = 0;
- fd = open ("/proc/cpuinfo", 0);
- if (fd < 0)
- return cpu_freq;
-
- unformat_init_clib_file (&input, fd);
ppc_timebase = 0;
- while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
+ if (unformat_init_file (&input, "/proc/cpuinfo"))
{
- if (unformat (&input, "cpu MHz : %f", &cpu_freq))
- cpu_freq *= 1e6;
- else if (unformat (&input, "timebase : %f", &ppc_timebase))
- ;
- else
- unformat_skip_line (&input);
- }
-
- unformat_free (&input);
+ while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
+ {
+ if (unformat (&input, "cpu MHz : %f", &cpu_freq))
+ cpu_freq *= 1e6;
+ else if (unformat (&input, "timebase : %f", &ppc_timebase))
+ ;
+ else
+ unformat_skip_line (&input);
+ }
- close (fd);
+ unformat_free (&input);
+ }
+ else
+ return cpu_freq;
/* Override CPU frequency with time base for PPC. */
if (ppc_timebase != 0)
@@ -117,21 +114,19 @@ static f64
clock_frequency_from_sys_filesystem (void)
{
f64 cpu_freq = 0.0;
- int fd;
unformat_input_t input;
/* Time stamp always runs at max frequency. */
cpu_freq = 0;
- fd = open ("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq", 0);
- if (fd < 0)
- goto done;
-
- unformat_init_clib_file (&input, fd);
- (void) unformat (&input, "%f", &cpu_freq);
- cpu_freq *= 1e3; /* measured in kHz */
- unformat_free (&input);
- close (fd);
-done:
+
+ if (unformat_init_file (
+ &input, "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq"))
+ {
+ if (unformat (&input, "%f", &cpu_freq))
+ cpu_freq *= 1e3; /* measured in kHz */
+ unformat_free (&input);
+ }
+
return cpu_freq;
}