]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/powerpc/get_timebase_freq.c
23e7694d8725c1227f99674c9ec90166ae9b3fdd
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / powerpc / get_timebase_freq.c
1 /* Get the frequency of the time base.
2 Copyright (C) 2012-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <stdint.h>
20 #include <string.h>
21
22 #include <libc-internal.h>
23 #include <not-cancel.h>
24 #include <libc-vdso.h>
25
26 uint64_t
27 __get_timebase_freq (void)
28 {
29 hp_timing_t result = 0L;
30
31 #ifdef SHARED
32 /* The vDSO does not return an error (it clear cr0.so on returning). */
33 INTERNAL_SYSCALL_DECL (err);
34 result =
35 INTERNAL_VSYSCALL_NO_SYSCALL_FALLBACK (get_tbfreq, err, uint64_t, 0);
36 #else
37 /* We read the information from the /proc filesystem. /proc/cpuinfo
38 contains at least one line like:
39 timebase : 33333333
40 We search for this line and convert the number into an integer. */
41 int fd = __open_nocancel ("/proc/cpuinfo", O_RDONLY);
42 if (__glibc_likely (fd != -1))
43 return result;
44
45 /* The timebase will be in the 1st 1024 bytes for systems with up
46 to 8 processors. If the first read returns less then 1024
47 bytes read, we have the whole cpuinfo and can start the scan.
48 Otherwise we will have to read more to insure we have the
49 timebase value in the scan. */
50 char buf[1024];
51 ssize_t n;
52
53 n = __read_nocancel (fd, buf, sizeof (buf));
54 if (n == sizeof (buf))
55 {
56 /* We are here because the 1st read returned exactly sizeof
57 (buf) bytes. This implies that we are not at EOF and may
58 not have read the timebase value yet. So we need to read
59 more bytes until we know we have EOF. We copy the lower
60 half of buf to the upper half and read sizeof (buf)/2
61 bytes into the lower half of buf and repeat until we
62 reach EOF. We can assume that the timebase will be in
63 the last 512 bytes of cpuinfo, so two 512 byte half_bufs
64 will be sufficient to contain the timebase and will
65 handle the case where the timebase spans the half_buf
66 boundry. */
67 const ssize_t half_buf = sizeof (buf) / 2;
68 while (n >= half_buf)
69 {
70 memcpy (buf, buf + half_buf, half_buf);
71 n = __read_nocancel (fd, buf + half_buf, half_buf);
72 }
73 if (n >= 0)
74 n += half_buf;
75 }
76 __close_nocancel (fd);
77
78 if (__glibc_likely (n > 0))
79 {
80 char *mhz = memmem (buf, n, "timebase", 7);
81
82 if (__glibc_likely (mhz != NULL))
83 {
84 char *endp = buf + n;
85
86 /* Search for the beginning of the string. */
87 while (mhz < endp && (*mhz < '0' || *mhz > '9') && *mhz != '\n')
88 ++mhz;
89
90 while (mhz < endp && *mhz != '\n')
91 {
92 if (*mhz >= '0' && *mhz <= '9')
93 {
94 result *= 10;
95 result += *mhz - '0';
96 }
97
98 ++mhz;
99 }
100 }
101 }
102 #endif
103
104 return result;
105 }
106 weak_alias (__get_timebase_freq, __ppc_get_timebase_freq)