PowerPC: Rename __kernel_vdso_get_tbfreq to __kernel_get_tbfreq.
[platform/upstream/glibc.git] / sysdeps / unix / sysv / linux / powerpc / get_clockfreq.c
1 /* Get frequency of the system processor.  powerpc/Linux version.
2    Copyright (C) 2000-2013 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 <ctype.h>
20 #include <fcntl.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <libc-internal.h>
24 #include <sysdep.h>
25 #include <bits/libc-vdso.h>
26
27 hp_timing_t
28 __get_clockfreq (void)
29 {
30   /* We read the information from the /proc filesystem.  /proc/cpuinfo
31      contains at least one line like:
32      timebase        : 33333333
33      We search for this line and convert the number into an integer.  */
34   static hp_timing_t timebase_freq;
35   hp_timing_t result = 0L;
36
37   /* If this function was called before, we know the result.  */
38   if (timebase_freq != 0)
39     return timebase_freq;
40
41   /* If we can use the vDSO to obtain the timebase even better.  */
42 #ifdef SHARED
43   INTERNAL_SYSCALL_DECL (err);
44   timebase_freq =
45     INTERNAL_VSYSCALL_NO_SYSCALL_FALLBACK (get_tbfreq, err, hp_timing_t, 0);
46   if (INTERNAL_SYSCALL_ERROR_P (timebase_freq, err)
47       && INTERNAL_SYSCALL_ERRNO (timebase_freq, err) == ENOSYS)
48 #endif
49     {
50       int fd = __open ("/proc/cpuinfo", O_RDONLY);
51
52       if (__builtin_expect (fd != -1, 1))
53         {
54           /* The timebase will be in the 1st 1024 bytes for systems with up
55              to 8 processors.  If the first read returns less then 1024
56              bytes read,  we have the whole cpuinfo and can start the scan.
57              Otherwise we will have to read more to insure we have the
58              timebase value in the scan.  */
59           char buf[1024];
60           ssize_t n;
61
62           n = __read (fd, buf, sizeof (buf));
63           if (n == sizeof (buf))
64             {
65               /* We are here because the 1st read returned exactly sizeof
66                  (buf) bytes.  This implies that we are not at EOF and may
67                  not have read the timebase value yet.  So we need to read
68                  more bytes until we know we have EOF.  We copy the lower
69                  half of buf to the upper half and read sizeof (buf)/2
70                  bytes into the lower half of buf and repeat until we
71                  reach EOF.  We can assume that the timebase will be in
72                  the last 512 bytes of cpuinfo, so two 512 byte half_bufs
73                  will be sufficient to contain the timebase and will
74                  handle the case where the timebase spans the half_buf
75                  boundry.  */
76               const ssize_t half_buf = sizeof (buf) / 2;
77               while (n >= half_buf)
78                 {
79                   memcpy (buf, buf + half_buf, half_buf);
80                   n = __read (fd, buf + half_buf, half_buf);
81                 }
82               if (n >= 0)
83                 n += half_buf;
84             }
85
86           if (__builtin_expect (n, 1) > 0)
87             {
88               char *mhz = memmem (buf, n, "timebase", 7);
89
90               if (__builtin_expect (mhz != NULL, 1))
91                 {
92                   char *endp = buf + n;
93
94                   /* Search for the beginning of the string.  */
95                   while (mhz < endp && (*mhz < '0' || *mhz > '9')
96                          && *mhz != '\n')
97                     ++mhz;
98
99                   while (mhz < endp && *mhz != '\n')
100                     {
101                       if (*mhz >= '0' && *mhz <= '9')
102                         {
103                           result *= 10;
104                           result += *mhz - '0';
105                         }
106
107                       ++mhz;
108                     }
109                 }
110               timebase_freq = result;
111             }
112           __close (fd);
113         }
114     }
115
116   return timebase_freq;
117 }