Back to index

glibc  2.9
get_clockfreq.c
Go to the documentation of this file.
00001 /* Get frequency of the system processor.  sparc64 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 <dirent.h>
00025 #include <stdlib.h>
00026 #include <sys/ioctl.h>
00027 #include <libc-internal.h>
00028 #include <asm/openpromio.h>
00029 
00030 static hp_timing_t
00031 __get_clockfreq_via_cpuinfo (void)
00032 {
00033   hp_timing_t result;
00034   int fd;
00035 
00036   result = 0;
00037 
00038   fd = open ("/proc/cpuinfo", O_RDONLY);
00039   if (fd != -1)
00040     {
00041       char buf[8192];
00042       ssize_t n;
00043 
00044       n = read (fd, buf, sizeof buf);
00045       if (n > 0)
00046        {
00047          char *mhz = memmem (buf, n, "Cpu0ClkTck", 7);
00048 
00049          if (mhz != NULL)
00050            {
00051              char *endp = buf + n;
00052 
00053              /* Search for the beginning of the string.  */
00054              while (mhz < endp
00055                    && (*mhz < '0' || *mhz > '9')
00056                    && (*mhz < 'a' || *mhz > 'f')
00057                    && *mhz != '\n')
00058               ++mhz;
00059 
00060              while (mhz < endp && *mhz != '\n')
00061               {
00062                 if ((*mhz >= '0' && *mhz <= '9') ||
00063                     (*mhz >= 'a' && *mhz <= 'f'))
00064                   {
00065                     result <<= 4;
00066                     if (*mhz >= '0' && *mhz <= '9')
00067                      result += *mhz - '0';
00068                     else
00069                      result += (*mhz - 'a') + 10;
00070                   }
00071                 ++mhz;
00072               }
00073            }
00074        }
00075 
00076       close (fd);
00077     }
00078 
00079   return result;
00080 }
00081 
00082 static hp_timing_t
00083 __get_clockfreq_via_proc_openprom (void)
00084 {
00085   hp_timing_t result;
00086   int obp_fd;
00087 
00088   result = 0;
00089 
00090   obp_fd = open ("/proc/openprom", O_RDONLY);
00091   if (obp_fd != -1)
00092     {
00093       unsigned long int buf[4096 / sizeof (unsigned long int)];
00094       struct dirent *dirp = (struct dirent *) buf;
00095       off_t dbase = (off_t) 0;
00096       ssize_t len;
00097 
00098       while ((len = getdirentries (obp_fd, (char *) dirp,
00099                                sizeof (buf), &dbase)) > 0)
00100        {
00101          struct dirent *this_dirp = dirp;
00102 
00103          while (len > 0)
00104            {
00105              char node[strlen ("/proc/openprom/")
00106                      + _D_ALLOC_NAMLEN (this_dirp)
00107                      + strlen ("/clock-frequency")];
00108              char *prop;
00109              int fd;
00110 
00111              /* Note that
00112                  strlen("/clock-frequency") > strlen("/device_type")
00113              */
00114              __stpcpy (prop = __stpcpy (__stpcpy (node, "/proc/openprom/"),
00115                                     this_dirp->d_name),
00116                      "/device_type");
00117              fd = open (node, O_RDONLY);
00118              if (fd != -1)
00119               {
00120                 char type_string[128];
00121                 int ret;
00122 
00123                 ret = read (fd, type_string, sizeof (type_string));
00124                 if (ret > 0 && strncmp (type_string, "'cpu'", 5) == 0)
00125                   {
00126                     int clkfreq_fd;
00127 
00128                     __stpcpy (prop, "/clock-frequency");
00129                     clkfreq_fd = open (node, O_RDONLY);
00130                     if (fd != -1)
00131                      {
00132                        if (read (clkfreq_fd, type_string,
00133                                 sizeof (type_string)) > 0)
00134                          result = (hp_timing_t)
00135                            strtoull (type_string, NULL, 16);
00136                        close (clkfreq_fd);
00137                      }
00138                   }
00139                 close (fd);
00140               }
00141 
00142              if (result != 0)
00143               break;
00144 
00145              len -= this_dirp->d_reclen;
00146              this_dirp = (struct dirent *)
00147               ((char *) this_dirp + this_dirp->d_reclen);
00148            }
00149          if (result != 0)
00150            break;
00151        }
00152       close (obp_fd);
00153     }
00154 
00155   return result;
00156 }
00157 
00158 static hp_timing_t
00159 __get_clockfreq_via_dev_openprom (void)
00160 {
00161   hp_timing_t result;
00162   int obp_dev_fd;
00163 
00164   result = 0;
00165 
00166   obp_dev_fd = open ("/dev/openprom", O_RDONLY);
00167   if (obp_dev_fd != -1)
00168     {
00169       char obp_buf[8192];
00170       struct openpromio *obp_cmd = (struct openpromio *)obp_buf;
00171       int ret;
00172 
00173       obp_cmd->oprom_size =
00174        sizeof (obp_buf) - sizeof (unsigned int);
00175       *(int *) obp_cmd->oprom_array = 0;
00176       ret = ioctl (obp_dev_fd, OPROMCHILD, (char *) obp_cmd);
00177       if (ret == 0)
00178        {
00179          int cur_node = *(int *) obp_cmd->oprom_array;
00180 
00181          while (cur_node != 0 && cur_node != -1)
00182            {
00183              obp_cmd->oprom_size = sizeof (obp_buf) - sizeof (unsigned int);
00184              strcpy (obp_cmd->oprom_array, "device_type");
00185              ret = ioctl (obp_dev_fd, OPROMGETPROP, (char *) obp_cmd);
00186              if (ret == 0
00187                 && strncmp (obp_cmd->oprom_array, "cpu", 3) == 0)
00188               {
00189                 obp_cmd->oprom_size = (sizeof (obp_buf)
00190                                     - sizeof (unsigned int));
00191                 strcpy (obp_cmd->oprom_array, "clock-frequency");
00192                 ret = ioctl (obp_dev_fd, OPROMGETPROP, (char *) obp_cmd);
00193                 if (ret == 0)
00194                   result =
00195                     (hp_timing_t) *(unsigned int *) obp_cmd->oprom_array;
00196               }
00197              obp_cmd->oprom_size = sizeof (obp_buf) - sizeof (unsigned int);
00198              *(int *) obp_cmd->oprom_array = cur_node;
00199              ret = ioctl (obp_dev_fd, OPROMNEXT, (char *) obp_cmd);
00200              if (ret < 0)
00201               break;
00202              cur_node = *(int *)obp_cmd->oprom_array;
00203            }
00204        }
00205     }
00206 
00207   return result;
00208 }
00209 
00210 hp_timing_t
00211 __get_clockfreq (void)
00212 {
00213   static hp_timing_t result;
00214 
00215   /* If this function was called before, we know the result.  */
00216   if (result != 0)
00217     return result;
00218 
00219   /* We first read the information from the /proc/cpuinfo file.
00220      It contains at least one line like
00221        Cpu0ClkTick         : 000000002cb41780
00222      We search for this line and convert the number in an integer.  */
00223   result = __get_clockfreq_via_cpuinfo ();
00224   if (result != 0)
00225     return result;
00226 
00227   /* If that did not work, try to find an OpenPROM node
00228      with device_type equal to 'cpu' using /dev/openprom
00229      and fetch the clock-frequency property from there.  */
00230   result = __get_clockfreq_via_dev_openprom ();
00231   if (result != 0)
00232     return result;
00233 
00234   /* Finally, try the same lookup as above but using /proc/openprom.  */
00235   result = __get_clockfreq_via_proc_openprom ();
00236 
00237   return result;
00238 }