Back to index

glibc  2.9
get_clockfreq.c
Go to the documentation of this file.
00001 /* Get frequency of the system processor.  i386/Linux version.
00002    Copyright (C) 2000, 2001 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <ctype.h>
00021 #include <fcntl.h>
00022 #include <string.h>
00023 #include <unistd.h>
00024 #include <libc-internal.h>
00025 
00026 
00027 hp_timing_t
00028 __get_clockfreq (void)
00029 {
00030   /* We read the information from the /proc filesystem.  It contains at
00031      least one line like
00032        cpu MHz         : 497.840237
00033      or also
00034        cpu MHz         : 497.841
00035      We search for this line and convert the number in an integer.  */
00036   static hp_timing_t result;
00037   int fd;
00038 
00039   /* If this function was called before, we know the result.  */
00040   if (result != 0)
00041     return result;
00042 
00043   fd = open ("/proc/cpuinfo", O_RDONLY);
00044   if (__builtin_expect (fd != -1, 1))
00045     {
00046       /* XXX AFAIK the /proc filesystem can generate "files" only up
00047          to a size of 4096 bytes.  */
00048       char buf[4096];
00049       ssize_t n;
00050 
00051       n = read (fd, buf, sizeof buf);
00052       if (__builtin_expect (n, 1) > 0)
00053        {
00054          char *mhz = memmem (buf, n, "cpu MHz", 7);
00055 
00056          if (__builtin_expect (mhz != NULL, 1))
00057            {
00058              char *endp = buf + n;
00059              int seen_decpoint = 0;
00060              int ndigits = 0;
00061 
00062              /* Search for the beginning of the string.  */
00063              while (mhz < endp && (*mhz < '0' || *mhz > '9') && *mhz != '\n')
00064               ++mhz;
00065 
00066              while (mhz < endp && *mhz != '\n')
00067               {
00068                 if (*mhz >= '0' && *mhz <= '9')
00069                   {
00070                     result *= 10;
00071                     result += *mhz - '0';
00072                     if (seen_decpoint)
00073                      ++ndigits;
00074                   }
00075                 else if (*mhz == '.')
00076                   seen_decpoint = 1;
00077 
00078                 ++mhz;
00079               }
00080 
00081              /* Compensate for missing digits at the end.  */
00082              while (ndigits++ < 6)
00083               result *= 10;
00084            }
00085        }
00086 
00087       close (fd);
00088     }
00089 
00090   return result;
00091 }