1 | /* Get frequency of the system processor. i386/Linux version. |
2 | Copyright (C) 2000-2017 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 | |
24 | hp_timing_t |
25 | __get_clockfreq (void) |
26 | { |
27 | /* We read the information from the /proc filesystem. It contains at |
28 | least one line like |
29 | cpu MHz : 497.840237 |
30 | or also |
31 | cpu MHz : 497.841 |
32 | We search for this line and convert the number in an integer. */ |
33 | static hp_timing_t result; |
34 | int fd; |
35 | |
36 | /* If this function was called before, we know the result. */ |
37 | if (result != 0) |
38 | return result; |
39 | |
40 | fd = __open ("/proc/cpuinfo" , O_RDONLY); |
41 | if (__glibc_likely (fd != -1)) |
42 | { |
43 | /* XXX AFAIK the /proc filesystem can generate "files" only up |
44 | to a size of 4096 bytes. */ |
45 | char buf[4096]; |
46 | ssize_t n; |
47 | |
48 | n = __read (fd, buf, sizeof buf); |
49 | if (__builtin_expect (n, 1) > 0) |
50 | { |
51 | char *mhz = memmem (buf, n, "cpu MHz" , 7); |
52 | |
53 | if (__glibc_likely (mhz != NULL)) |
54 | { |
55 | char *endp = buf + n; |
56 | int seen_decpoint = 0; |
57 | int ndigits = 0; |
58 | |
59 | /* Search for the beginning of the string. */ |
60 | while (mhz < endp && (*mhz < '0' || *mhz > '9') && *mhz != '\n') |
61 | ++mhz; |
62 | |
63 | while (mhz < endp && *mhz != '\n') |
64 | { |
65 | if (*mhz >= '0' && *mhz <= '9') |
66 | { |
67 | result *= 10; |
68 | result += *mhz - '0'; |
69 | if (seen_decpoint) |
70 | ++ndigits; |
71 | } |
72 | else if (*mhz == '.') |
73 | seen_decpoint = 1; |
74 | |
75 | ++mhz; |
76 | } |
77 | |
78 | /* Compensate for missing digits at the end. */ |
79 | while (ndigits++ < 6) |
80 | result *= 10; |
81 | } |
82 | } |
83 | |
84 | __close (fd); |
85 | } |
86 | |
87 | return result; |
88 | } |
89 | |