1 /* Get frequency of the system processor. powerpc/Linux version.
2 Copyright (C) 2000, 2001, 2005 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
24 #include <libc-internal.h>
26 #include <bits/libc-vdso.h>
29 __get_clockfreq (void)
31 /* We read the information from the /proc filesystem. /proc/cpuinfo
32 contains at least one line like:
34 We search for this line and convert the number into an integer. */
35 static hp_timing_t timebase_freq
;
36 hp_timing_t result
= 0L;
38 /* If this function was called before, we know the result. */
39 if (timebase_freq
!= 0)
42 /* If we can use the vDSO to obtain the timebase even better. */
44 INTERNAL_SYSCALL_DECL (err
);
45 timebase_freq
= INTERNAL_VSYSCALL_NO_SYSCALL_FALLBACK (get_tbfreq
, err
, 0);
46 if (INTERNAL_SYSCALL_ERROR_P (timebase_freq
, err
)
47 && INTERNAL_SYSCALL_ERRNO (timebase_freq
, err
) == ENOSYS
)
50 int fd
= open ("/proc/cpuinfo", O_RDONLY
);
52 if (__builtin_expect (fd
!= -1, 1))
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. */
62 n
= read (fd
, buf
, sizeof (buf
));
63 if (n
== sizeof (buf
))
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
76 const ssize_t half_buf
= sizeof (buf
) / 2;
79 memcpy (buf
, buf
+ half_buf
, half_buf
);
80 n
= read (fd
, buf
+ half_buf
, half_buf
);
86 if (__builtin_expect (n
, 1) > 0)
88 char *mhz
= memmem (buf
, n
, "timebase", 7);
90 if (__builtin_expect (mhz
!= NULL
, 1))
94 /* Search for the beginning of the string. */
95 while (mhz
< endp
&& (*mhz
< '0' || *mhz
> '9')
99 while (mhz
< endp
&& *mhz
!= '\n')
101 if (*mhz
>= '0' && *mhz
<= '9')
104 result
+= *mhz
- '0';
110 timebase_freq
= result
;
116 return timebase_freq
;