]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/unix/sysv/linux/i386/get_clockfreq.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / i386 / get_clockfreq.c
CommitLineData
6689a188 1/* Get frequency of the system processor. i386/Linux version.
f7a9f785 2 Copyright (C) 2000-2016 Free Software Foundation, Inc.
6689a188
UD
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
41bdb6e2
AJ
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.
6689a188
UD
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
41bdb6e2 13 Lesser General Public License for more details.
6689a188 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
6689a188
UD
18
19#include <ctype.h>
20#include <fcntl.h>
4a199526 21#include <string.h>
6689a188 22#include <unistd.h>
66539a73 23#include <libc-internal.h>
6689a188
UD
24
25
3b5c1b57 26hp_timing_t
6689a188
UD
27__get_clockfreq (void)
28{
29 /* We read the information from the /proc filesystem. It contains at
30 least one line like
31 cpu MHz : 497.840237
01608292
UD
32 or also
33 cpu MHz : 497.841
6689a188 34 We search for this line and convert the number in an integer. */
3b5c1b57 35 static hp_timing_t result;
6689a188
UD
36 int fd;
37
e7c036b3
UD
38 /* If this function was called before, we know the result. */
39 if (result != 0)
40 return result;
41
6e6249d0 42 fd = __open ("/proc/cpuinfo", O_RDONLY);
a1ffb40e 43 if (__glibc_likely (fd != -1))
6689a188
UD
44 {
45 /* XXX AFAIK the /proc filesystem can generate "files" only up
46 to a size of 4096 bytes. */
47 char buf[4096];
48 ssize_t n;
49
6e6249d0 50 n = __read (fd, buf, sizeof buf);
6689a188
UD
51 if (__builtin_expect (n, 1) > 0)
52 {
53 char *mhz = memmem (buf, n, "cpu MHz", 7);
54
a1ffb40e 55 if (__glibc_likely (mhz != NULL))
6689a188
UD
56 {
57 char *endp = buf + n;
01608292
UD
58 int seen_decpoint = 0;
59 int ndigits = 0;
6689a188
UD
60
61 /* Search for the beginning of the string. */
62 while (mhz < endp && (*mhz < '0' || *mhz > '9') && *mhz != '\n')
63 ++mhz;
64
65 while (mhz < endp && *mhz != '\n')
66 {
67 if (*mhz >= '0' && *mhz <= '9')
68 {
69 result *= 10;
70 result += *mhz - '0';
01608292
UD
71 if (seen_decpoint)
72 ++ndigits;
6689a188 73 }
01608292
UD
74 else if (*mhz == '.')
75 seen_decpoint = 1;
6689a188
UD
76
77 ++mhz;
78 }
01608292
UD
79
80 /* Compensate for missing digits at the end. */
81 while (ndigits++ < 6)
82 result *= 10;
6689a188
UD
83 }
84 }
85
6e6249d0 86 __close (fd);
6689a188
UD
87 }
88
89 return result;
90}