On Wed, 5 Jun 2002 23:40:55 +0300, Yedidyah Bar-David <[EMAIL PROTECTED]> wrote:
>
> On Wed, Jun 05, 2002 at 09:55:42PM +0200, [EMAIL PROTECTED] wrote:
> >
> > Which system calls can provide me detailed information (structures is preferable) 
>on RedHat 7.2 regarding:
> >
> > 1. Memory usage (similar to the info inculded in /proc/meminfo)
> > 2. CPU usage (SMP configuration - both CPUs, similar to the info we can get from 
>"gtop")
>
> Simply go ahead and read /proc/*. This is _the_ interface.

I have written some functions that get part of the information you want.
It works (debugged) on RH versions 6.0 to 7.3 (kernels 2.2.5 - 2.4.18).
Take whatever you like.

Ehud.



#define  BUFLNG   200                              /* to allow lines of 200 chars */
unsigned char buf [ BUFLNG + 3 ] ;                 /* tcp/ip read/write buffer */

int get_cpu_count ( void )                         /* return no. of cpus on this 
system */
{
#ifndef __linux__
   return ( 1 ) ;                                  /* assume 1 processor */
#else /* NOT __linux__ */
FILE *fd ;
int load_cpu_cnt = 1 ;                             /* start with 1 */

   if ( ( fd = fopen ( "/proc/stat" , "r" ) ) == NULL )    /* /proc/stat must be found 
*/
       return ( 1 ) ;                              /* assume 1 processor */

   fgets ( buf , BUFLNG , fd ) ;                   /* 1st line, accumulated cpu */
   while ( strncmp ( buf , "cpu" , 3 ) == 0 )      /* count cpu lines */
   {
       load_cpu_cnt ++ ;
       if ( fgets ( buf , BUFLNG , fd ) == NULL )
           *buf = 0 ;
   }
   fclose ( fd ) ;

   load_cpu_cnt -= 2 ;                             /* extra 2 lines (cpu , cpu0) */
   if ( load_cpu_cnt < 1 )                         /* correct for possible 0 */
       load_cpu_cnt = 1 ;

   return ( load_cpu_cnt ) ;                       /* no. of cpus */
#endif /* __linux__ */
}
/* ======================================================================== */

void get_cpu_pgs ( long *cpu , long *pgs )         /* get cpu avg * 100 & pages I/O * 
10 */
{
#ifdef __CYGWIN__
   *cpu = 0 ;                                      /* NO cpu avg on PC */
   *pgs = 0 ;                                      /* NO pages IO on PC */
#else  /* __CYGWIN__ */
FILE *fl ;                                         /* "/proc/stat" handle */
unsigned long tmp , tmp1 , tmp2 ;                  /* cpu, pages temp */
static int sv_cpu = 0 , sv_pgs = 0 , ncpus = 0 ;   /* saved cpu, pages, no. of cpus */
static time_t crnt, last = 0 ;

   if ( ! ncpus )                                  /* ncpus not set  ? */
        ncpus = get_cpu_count ( ) ;                /* set it now */

   time (& crnt) ;                                 /* get gmt seconds from 1/1/1970 */
   crnt -= last ;                                  /* ignore 1st time */
   last += crnt ;                                  /* for next time */

   if ( ( fl = fopen ( "/proc/stat" , "r" ) ) == NULL )/* /proc/stat must be found */
       error_msg ( "Open of /proc/stat failed" , 6 ) ; /* error message, exit */

   if ( fgets ( buf , BUFLNG , fl ) == NULL )      /* 1st record is cpu <user> <nice> 
<sys> <idle> */
       error_msg ( "1st read of /proc/stat failed" , 7 ) ; /* error message, exit */
   sscanf ( buf , "%*s %lu %lu %lu" , &tmp , &tmp1 , &tmp2 ) ;
   tmp = ( tmp + tmp1 + tmp2 ) * 10 ;              /* total cpu used * 100 */
   tmp1 = ( tmp - sv_cpu ) / ( crnt * ncpus ) ;    /* cpu avg for the time elapsed */
   if ( tmp1 > 999 )
       tmp1 = 999 ;                                /* max value allowed */
   *cpu = htonl ( tmp1 ) ;                         /* cpu avg * 1000 [0-999] */
   sv_cpu = tmp ;                                  /* for next time */

   while ( fgets ( buf , BUFLNG , fl ) != NULL )   /* next record of /proc/stat */
   {
       if ( strncmp ( buf , "page " , 5 ) != 0 )   /* is it page IO line ? */
           continue ;                              /* NO */
       sscanf ( buf , "%*s %lu %lu" , &tmp , &tmp1 ) ; /* IN pages, OUT pages */
       tmp = ( tmp + tmp1 ) << 4 ;                 /* total pages IO * 16 */
       *pgs = htonl ( ( tmp - sv_pgs ) / crnt ) ;  /* pages avg * 10 for the time 
elapsed */
       sv_pgs = tmp ;                              /* for next time */
       fclose ( fl ) ;                             /* end of /proc/stat processing */
       return ;                                    /* all OK */
   }
   error_msg ( "PAGES record of /proc/stat not found" , 8 ) ; /* error message, exit */
#endif /* __CYGWIN__ */
}
/*===========================================================================*/

void get_ld_procs ( long *ld_avg , long *procs )   /* get load avg & no. of procs */
{
#if __linux__
struct sysinfo si ;                                /* sys info values */

   if ( sysinfo ( & si ) < 0 )                     /* get sysinfo - must be successful 
*/
       error_msg ( "System error, sysinfo failed" , 5 ) ;  /* error message, exit */

   *ld_avg = htonl ( ( si . loads [0] + 128 ) >> 8 ) ; /* load average * 256 */
   *procs = htonl ( si . procs ) ;                 /* no. of procs */
#else /* __linux__ */
   *ld_avg = 0 ;                                    /* no load average */
   *procs = 0 ;                                     /* no proc count */
#endif /* __linux__ */
}
/*===========================================================================*/

long get_users ( void )                            /* no. of logins */
{
#ifdef __CYGWIN__
   return ( 1 ) ;                                  /* always 1 user on PC */
#else  /* __CYGWIN__ */
long users = 0 ;                                   /* users (logins) count */
struct utmp *U ;                                   /* utmp entry */

   setutent ( ) ;                                  /* rewind the utmp file */
   while ( ( U = getutent ( ) ) != NULL )          /* next utmp entry */
       if ( ( U -> ut_type == USER_PROCESS ) &&    /* real user */
            ( * ( U -> ut_user ) != 0 ) )          /* not exited */
           users ++ ;                              /* increase users number */

   return ( users ) ;
#endif /* __CYGWIN__ */
}
/*===========================================================================*/


--
 Ehud Karni           Tel: +972-3-7966-561  /"\
 Mivtach - Simon      Fax: +972-3-7966-667  \ /  ASCII Ribbon Campaign
 Insurance agencies   (USA) voice mail and   X   Against   HTML   Mail
 http://www.mvs.co.il  FAX:  1-815-5509341  / \
 mailto:[EMAIL PROTECTED]          Better  Safe  Than  Sorry

=================================================================
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]

Reply via email to