The following are the states of the supported platforms.
Linux - Up to date.
AIX - Got the new hardware (5.1L), info coming soon
Tru64 - Up to date.
HP-UX - Up to date.
Solaris - Up to date.
Linux:
Most everything is in /proc. This is good and bad. It's good because everything is there and is easy to see. It's bad because it's all in text, which makes it hard to parse and get into a program, and because the format changes regularly. For what I'm trying to do, it absolutely stinks :(
I'm considering writing a library for extracting this information, but I'm not sure yet.
Solaris: kstat. You see if you can figure it out, because I can't.. even
with the examples I found, it's still a mystery to me.
#include <sys/utsname.h>
struct utsname {
char sysname [ SYS_NMLN ]; // operating system name
char nodename [ SYS_NMLN ]; // hostname of the machine
char release [ SYS_NMLN ]; // version of the operating system
char version [ SYS_NMLN ]; // build version of OS -- not version of OS
char machine [ SYS_NMLN ]; // platform (alpha, x86, sparc, etc)
};
int uname ( struct utsname * buf );
#include <sys/sysinfo.h>
#include <machine/hal_sysinfo.h>
int nCpus = -1;
getsysinfo ( GSI_CPUS_IN_BOX,
(caddr_t) &nCpus,
sizeof ( nCpus ),
NULL,
NULL,
NULL );
printf ( "System has %d CPUs\n", nCpus );
#include <sys/table.h> struct tbl_processor oProcessor; table ( TBL_PROCESSOR_INFO, 0, & oProcessor, 1, sizeof ( oProcessor ) ); printf ( "Speed of processor (0) is %d\n", oProcessor.mhz );
C++ Implementation
Here is a C++ implementation that gives the contents of /proc/cpuinfo in an easy-to-use std::map.
CPU Type=sparcv9 CPU Speed=200 Number of cpus=1 1 minute load average=66 ( 0.26) 5 minute load average=43 ( 0.17) 15 minute load average=27 ( 0.11) Total physical memory: pages=127808 (bytes=1047003136) Free physical memory: pages=25228 (bytes=206667776)Here is the sample. Compile as cc cpuinfo.c -lkstat. Note that there's an easier way to get load average information that is presented later on this page. Also, this isn't my code. I found it somehwere, because I couldn't figure out kstat (still can't -- I just use that sample code for what it can do)
#include <sys/param.h> #include <sys/pstat.h> struct pst_dynamic psd; // dynamic system info -- stuff that change change pstat_getdynamic ( &psd, sizeof ( psd ), (size_t)1 ); printf ( "There are %ld processors\n", psd.psd_proc_cnt );Here is a big program that prints out a bunch of stuff.
#include <sys/table.h> struct tbl_sysinfo oSystemInfo; // complete desc of tbl_sysinfo is in /usr/include/sys/table.h table ( TBL_SYSINFO, 0, & oSystemInfo, 1, sizeof ( oSystemInfo ) ); printf ( "System was booted %d seconds after the epoch\n", oSystemInfo.si_boottime );
When you look at /proc/uptime, you'll see two numbers. The first is the uptime in seconds of the system. The second is the total idle time of the system since boot. I verified this by running a simple program that takes 100% CPU and verified that the second number in /proc/uptime stopped changing.
C++ Implementation
Here is a C++ implementation that gives the uptime and total idle time in a structure with raw seconds and broken down days/hours/minutes/seconds for uptime and idle time. Whatever.
struct utmpx *getutxent(void);
a utmpx structure looks like this:
char ut_user[32]; /* user login name */
char ut_id[4]; /* /etc/inittab id (usually line #) */
char ut_line[32]; /* device name (console, lnxx) */
pid_t ut_pid; /* process id */
short ut_type; /* type of entry */
struct exit_status ut_exit; /* exit status of a process */
/* marked as DEAD_PROCESS */
struct timeval ut_tv; /* time entry was made */
long ut_session; /* session ID, used for windowing */
long pad[5]; /* reserved for future use */
short ut_syslen; /* significant length of ut_host */
/* including terminating null */
char ut_host[257]; /* host name, if remote */
Here's a sample program to get the boot time out of utmpx
#include <utmpx.h>
int main ( )
{
int nBootTime = 0;
int nCurrentTime = time ( NULL );
struct utmpx * ent;
while ( ( ent = getutxent ( ) ) ) {
if ( !strcmp ( "system boot", ent->ut_line ) ) {
nBootTime = ent->ut_tv.tv_sec;
}
}
printf ( "System was booted %d seconds ago\n", nCurrentTime - nBootTime );
}
Here is the total output of printing out each entry in the table..
#include <sys/param.h>
#include <sys/pstat.h>
struct pst_static pst; // data that can't change between boots
pstat_getstatic ( &Pst, sizeof ( pst ), (size_t) 1, 0 );
printf ( "System booted %d seconds ago\n",
time(NULL) - pst.boot_time );
Here is a program that prints out a
bunch of stuff including boot time.
struct tbl_pmemstats oMemStats; table ( TBL_PMEMSTATS, 0, & oMemStats, 1, sizeof ( oMemStats ) ); printf ( "Total memory on system: %ld\n", oMemStats.physmem ); // note it's a 64-bit signed
Here's the format for /proc/meminfo in 2.4.x
total: used: free: shared: buffers: cached:
Mem: 1580699648 1568923648 11776000 231284736 146837504 982405120
Swap: 2146787328 0 2146787328
MemTotal: 1543652 kB
MemFree: 11500 kB
MemShared: 225864 kB
Buffers: 143396 kB
Cached: 959380 kB
SwapCached: 0 kB
Active: 75932 kB
Inact_dirty: 785916 kB
Inact_clean: 466792 kB
Inact_target: 124 kB
HighTotal: 654528 kB
HighFree: 4532 kB
LowTotal: 889124 kB
LowFree: 6968 kB
SwapTotal: 2096472 kB
SwapFree: 2096472 kB
NrSwapPages: 524118 pages
#include <sys/sysconfig.h>
int main ( )
{
printf ( "Total memory installed is %d bytes\n",
_sysconfig(_CONFIG_PAGESIZE) *
_sysconfig(_CONFIG_PHYS_PAGES) );
}
#include <sys/param.h>
#include <sys/pstat.h>
struct pst_static pst; // data that can't change between boots
pstat_getstatic ( &Pst, sizeof ( pst ), (size_t) 1, 0 );
printf ( "System has %d KB of memory\n",
pst.physical_memory * pst.page_size );
Here is a big program that prints out a
bunch of stuff.
struct tbl_loadavg oLoadAverage;
table ( TBL_LOADAVG, 0, & oLoadAverage, 1, sizeof ( struct tbl_loadavg ) );
// if tl_scale is non-zero, it's in been multiplied out to make an int
printf ( "1-minute avg: %lf", oLoadAverage.tl_lscale == 0 ?
oLoadAverage.tl_avenrun.d [ 0 ] :
(double) ( oLoadAverage.tl_avenrun.l [ 0 ] / ( (double) oLoadAverage.tl_lscale ) ) );
// if tl_scale is non-zero, it's in been multiplied out to make an int
printf ( "5-minute avg: %lf", oLoadAverage.tl_lscale == 0 ?
oLoadAverage.tl_avenrun.d [ 1 ] :
(double) ( oLoadAverage.tl_avenrun.l [ 1 ] / ( (double) oLoadAverage.tl_lscale ) ) );
// if tl_scale is non-zero, it's in been multiplied out to make an int
printf ( "15-minute avg: %lf", oLoadAverage.tl_lscale == 0 ?
oLoadAverage.tl_avenrun.d [ 2 ] :
(double) ( oLoadAverage.tl_avenrun.l [ 2 ] / ( (double) oLoadAverage.tl_lscale ) ) );
#include <iostream>
#include <fstream>
#include <stdio.h>
int main()
{
std::ifstream loadavg ( "/proc/loadavg" );
double oneminute;
double fiveminute;
double fifteenminute;
int running;
char slash;
int total;
int highpid;
loadavg >> oneminute >> fiveminute >> fifteenminute >> running >> slash >>
total >> highpid;
printf ( "1-minute avg: %lf\n5-minute avg: %lf\n15-minute avg: %lf\n"
"running processes: %d\ntotal processes: %d\nhigh PID: %d\n",
oneminute, fiveminute, fifteenminute, running, total, highpid );
}
#includeJust make an array of 3 doubles, pass that in as the first argument, and send in "3" as the second argument, saying that you're giving it 3 doubles worth of space and *presto* you've got your 1,5, and 15 minute averages. Sun docs hereint getloadavg(double loadavg[], int nelem);
#include <sys/param.h>
#include <sys/pstat.h>
struct pst_dynamic psd; // dynamic system info -- stuff that change change
pstat_getdynamic ( &psd, sizeof ( psd ), (size_t)1 );
printf ( "1-minute: %lf, 5-minute: %lf, 15-minute: %lf\n",
psd.psd_avg_1_min,
psd.psd_avg_5_min,
psd.psd_avg_15_min );
Here is a big program that prints out a
bunch of stuff.