]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/unix/sysv/linux/powerpc/get_clockfreq.c
* sysdeps/powerpc/powerpc64/Makefile
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / powerpc / get_clockfreq.c
CommitLineData
7006f757
UD
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.
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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
19
20#include <ctype.h>
21#include <fcntl.h>
22#include <string.h>
23#include <unistd.h>
24#include <libc-internal.h>
25
26
27hp_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 int fd = open ("/proc/cpuinfo", O_RDONLY);
42 if (__builtin_expect (fd != -1, 1))
43 {
44 /* The timebase will be in the 1st 1024 bytes for systems with up
45 to 8 processors. If the first read returns less then 1024
46 bytes read, we have the whole cpuinfo and can start the scan.
47 Otherwise we will have to read more to insure we have the
48 timebase value in the scan. */
49 char buf[1024];
50 ssize_t n;
51
52 n = read (fd, buf, sizeof (buf));
53 if (n == sizeof (buf))
54 {
55 /* We are here because the 1st read returned exactly sizeof
56 (buf) bytes. This implies that we are not at EOF and may
57 not have read the timebase value yet. So we need to read
58 more bytes until we know we have EOF. We copy the lower
59 half of buf to the upper half and read sizeof (buf)/2
60 bytes into the lower half of buf and repeat until we
61 reach EOF. We can assume that the timebase will be in
62 the last 512 bytes of cpuinfo, so two 512 byte half_bufs
63 will be sufficient to contain the timebase and will
64 handle the case where the timebase spans the half_buf
65 boundry. */
66 const ssize_t half_buf = sizeof (buf) / 2;
67 while (n >= half_buf)
68 {
69 memcpy (buf, buf + half_buf, half_buf);
70 n = read (fd, buf + half_buf, half_buf);
71 }
72 if (n >= 0)
73 n += half_buf;
74 }
75
76 if (__builtin_expect (n, 1) > 0)
77 {
78 char *mhz = memmem (buf, n, "timebase", 7);
79
80 if (__builtin_expect (mhz != NULL, 1))
81 {
82 char *endp = buf + n;
83
84 /* Search for the beginning of the string. */
85 while (mhz < endp && (*mhz < '0' || *mhz > '9') && *mhz != '\n')
86 ++mhz;
87
88 while (mhz < endp && *mhz != '\n')
89 {
90 if (*mhz >= '0' && *mhz <= '9')
91 {
92 result *= 10;
93 result += *mhz - '0';
94 }
95
96 ++mhz;
97 }
98 }
99 timebase_freq = result;
100 }
101 close (fd);
102 }
103
104 return timebase_freq;
105}