Back to index

glibc  2.9
Defines | Functions
getsysstats.c File Reference
#include <alloca.h>
#include <assert.h>
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <mntent.h>
#include <paths.h>
#include <stdio.h>
#include <stdio_ext.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/sysinfo.h>
#include <atomic.h>
#include <not-cancel.h>
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define GET_NPROCS_PARSER(FP, BUFFER, RESULT)

Functions

int __get_nprocs ()
 weak_alias (__get_nprocs, get_nprocs)
 weak_alias (__get_nprocs_conf, get_nprocs_conf)
long int __get_phys_pages ()
 weak_alias (__get_phys_pages, get_phys_pages)

Define Documentation

#define GET_NPROCS_PARSER (   FP,
  BUFFER,
  RESULT 
)
Value:
do                                                             \
    {                                                          \
      (RESULT) = 0;                                            \
      /* Read all lines and count the lines starting with the string  \
        "processor".  We don't have to fear extremely long lines since       \
        the kernel will not generate them.  8192 bytes are really     \
        enough.  */                                            \
      while (fgets_unlocked (BUFFER, sizeof (BUFFER), FP) != NULL)    \
       if (strncmp (BUFFER, "processor", 9) == 0)                     \
         ++(RESULT);                                           \
    }                                                          \
  while (0)

Definition at line 55 of file getsysstats.c.


Function Documentation

int __get_nprocs ( void  )

Definition at line 69 of file getsysstats.c.

{
  /* XXX Here will come a test for the new system call.  */

  char buffer[8192];
  int result = 1;

  /* The /proc/stat format is more uniform, use it by default.  */
  FILE *fp = fopen ("/proc/stat", "rc");
  if (fp != NULL)
    {
      /* No threads use this stream.  */
      __fsetlocking (fp, FSETLOCKING_BYCALLER);

      result = 0;
      while (fgets_unlocked (buffer, sizeof (buffer), fp) != NULL)
       if (strncmp (buffer, "cpu", 3) == 0 && isdigit (buffer[3]))
         ++result;

      fclose (fp);
    }
  else
    {
      fp = fopen ("/proc/cpuinfo", "rc");
      if (fp != NULL)
       {
         /* No threads use this stream.  */
         __fsetlocking (fp, FSETLOCKING_BYCALLER);
         GET_NPROCS_PARSER (fp, buffer, result);
         fclose (fp);
       }
    }

Here is the call graph for this function:

Here is the caller graph for this function:

long int __get_phys_pages ( void  )

Definition at line 209 of file getsysstats.c.

{
  /* XXX Here will come a test for the new system call.  */

Here is the caller graph for this function:

Definition at line 104 of file getsysstats.c.

{
  /* XXX Here will come a test for the new system call.  */

  /* Try to use the sysfs filesystem.  It has actual information about
     online processors.  */
  DIR *dir = __opendir ("/sys/devices/system/cpu");
  if (dir != NULL)
    {
      int count = 0;
      struct dirent64 *d;

      while ((d = __readdir64 (dir)) != NULL)
       /* NB: the sysfs has d_type support.  */
       if (d->d_type == DT_DIR && strncmp (d->d_name, "cpu", 3) == 0)
         {
           char *endp;
           unsigned long int nr = strtoul (d->d_name + 3, &endp, 10);
           if (nr != ULONG_MAX && endp != d->d_name + 3 && *endp == '\0')
             ++count;
         }

      __closedir (dir);

      return count;
    }

  int result = 1;

#ifdef GET_NPROCS_CONF_PARSER
  /* If we haven't found an appropriate entry return 1.  */
  FILE *fp = fopen ("/proc/cpuinfo", "rc");
  if (fp != NULL)
    {
      char buffer[8192];

      /* No threads use this stream.  */
      __fsetlocking (fp, FSETLOCKING_BYCALLER);
      GET_NPROCS_CONF_PARSER (fp, buffer, result);
      fclose (fp);
    }
#else
  result = __get_nprocs ();
#endif

Definition at line 157 of file getsysstats.c.

{
  char buffer[8192];
  long int result = -1;

  /* If we haven't found an appropriate entry return 1.  */
  FILE *fp = fopen ("/proc/meminfo", "rc");
  if (fp != NULL)
    {
      /* No threads use this stream.  */
      __fsetlocking (fp, FSETLOCKING_BYCALLER);

      result = 0;
      /* Read all lines and count the lines starting with the
        string "processor".  We don't have to fear extremely long
        lines since the kernel will not generate them.  8192
        bytes are really enough.  */
      while (fgets_unlocked (buffer, sizeof buffer, fp) != NULL)
       if (sscanf (buffer, format, &result) == 1)
         {
           result /= (__getpagesize () / 1024);
           break;
         }

      fclose (fp);
    }

  if (result == -1)
    /* We cannot get the needed value: signal an error.  */
    __set_errno (ENOSYS);

Definition at line 215 of file getsysstats.c.

                                   : %ld kB");
}
weak_alias (__get_phys_pages, get_phys_pages)


/* Return the number of available pages of physical memory in the
   system.  There is currently (as of version 2.0.21) no system call
   to determine the number.  It is planned for the 2.1.x series to add
   this, though.

   One possibility to implement it for systems using Linux 2.0 is to
   examine the pseudo file /proc/cpuinfo.  Here we have one entry for
   each processor.

   But not all systems have support for the /proc filesystem.  If it
   is not available we return -1 as an error signal.  */
long int
__get_avphys_pages ()
{
  /* XXX Here will come a test for the new system call.  */