Back to index

glibc  2.9
get_clockfreq.c
Go to the documentation of this file.
00001 /* Get frequency of the system processor.  IA-64/Linux version.
00002    Copyright (C) 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        itc MHz    : 733.390988
00033      We search for this line and convert the number in an integer.  */
00034   static hp_timing_t result;
00035   int fd;
00036 
00037   /* If this function was called before, we know the result.  */
00038   if (result != 0)
00039     return result;
00040 
00041   fd = open ("/proc/cpuinfo", O_RDONLY);
00042   if (__builtin_expect (fd != -1, 1))
00043     {
00044       /* XXX AFAIK the /proc filesystem can generate "files" only up
00045          to a size of 4096 bytes.  */
00046       char buf[4096];
00047       ssize_t n;
00048 
00049       n = read (fd, buf, sizeof buf);
00050       if (__builtin_expect (n, 1) > 0)
00051        {
00052          char *mhz = memmem (buf, n, "itc MHz", 7);
00053 
00054          if (__builtin_expect (mhz != NULL, 1))
00055            {
00056              char *endp = buf + n;
00057              int seen_decpoint = 0;
00058              int ndigits = 0;
00059 
00060              /* Search for the beginning of the string.  */
00061              while (mhz < endp && (*mhz < '0' || *mhz > '9') && *mhz != '\n')
00062               ++mhz;
00063 
00064              while (mhz < endp && *mhz != '\n')
00065               {
00066                 if (*mhz >= '0' && *mhz <= '9')
00067                   {
00068                     result *= 10;
00069                     result += *mhz - '0';
00070                     if (seen_decpoint)
00071                      ++ndigits;
00072                   }
00073                 else if (*mhz == '.')
00074                   seen_decpoint = 1;
00075 
00076                 ++mhz;
00077               }
00078 
00079              /* Compensate for missing digits at the end.  */
00080              while (ndigits++ < 6)
00081               result *= 10;
00082            }
00083        }
00084 
00085       close (fd);
00086     }
00087 
00088   return result;
00089 }