|
Packit |
6c4009 |
/* Get frequency of the system processor. i386/Linux version.
|
|
Packit |
6c4009 |
Copyright (C) 2000-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License as published by the Free Software Foundation; either
|
|
Packit |
6c4009 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6c4009 |
Lesser General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License along with the GNU C Library; if not, see
|
|
Packit |
6c4009 |
<http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <ctype.h>
|
|
Packit |
6c4009 |
#include <fcntl.h>
|
|
Packit |
6c4009 |
#include <string.h>
|
|
Packit |
6c4009 |
#include <unistd.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
hp_timing_t
|
|
Packit |
6c4009 |
__get_clockfreq (void)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* We read the information from the /proc filesystem. It contains at
|
|
Packit |
6c4009 |
least one line like
|
|
Packit |
6c4009 |
cpu MHz : 497.840237
|
|
Packit |
6c4009 |
or also
|
|
Packit |
6c4009 |
cpu MHz : 497.841
|
|
Packit |
6c4009 |
We search for this line and convert the number in an integer. */
|
|
Packit |
6c4009 |
static hp_timing_t result;
|
|
Packit |
6c4009 |
int fd;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* If this function was called before, we know the result. */
|
|
Packit |
6c4009 |
if (result != 0)
|
|
Packit |
6c4009 |
return result;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
fd = __open ("/proc/cpuinfo", O_RDONLY);
|
|
Packit |
6c4009 |
if (__glibc_likely (fd != -1))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* XXX AFAIK the /proc filesystem can generate "files" only up
|
|
Packit |
6c4009 |
to a size of 4096 bytes. */
|
|
Packit |
6c4009 |
char buf[4096];
|
|
Packit |
6c4009 |
ssize_t n;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
n = __read (fd, buf, sizeof buf);
|
|
Packit |
6c4009 |
if (__builtin_expect (n, 1) > 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
char *mhz = memmem (buf, n, "cpu MHz", 7);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (__glibc_likely (mhz != NULL))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
char *endp = buf + n;
|
|
Packit |
6c4009 |
int seen_decpoint = 0;
|
|
Packit |
6c4009 |
int ndigits = 0;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Search for the beginning of the string. */
|
|
Packit |
6c4009 |
while (mhz < endp && (*mhz < '0' || *mhz > '9') && *mhz != '\n')
|
|
Packit |
6c4009 |
++mhz;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
while (mhz < endp && *mhz != '\n')
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (*mhz >= '0' && *mhz <= '9')
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
result *= 10;
|
|
Packit |
6c4009 |
result += *mhz - '0';
|
|
Packit |
6c4009 |
if (seen_decpoint)
|
|
Packit |
6c4009 |
++ndigits;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else if (*mhz == '.')
|
|
Packit |
6c4009 |
seen_decpoint = 1;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
++mhz;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Compensate for missing digits at the end. */
|
|
Packit |
6c4009 |
while (ndigits++ < 6)
|
|
Packit |
6c4009 |
result *= 10;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
__close (fd);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return result;
|
|
Packit |
6c4009 |
}
|