casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
HostInfoOsf1.h
Go to the documentation of this file.
00001 //# HostInfo_osf1.h: DEC OSF/1 specific memory, swap, and CPU code.
00002 //# $Id: HostInfoOsf1.h 20739 2009-09-29 01:15:15Z Malte.Marquarding $
00003 
00004  /*
00005  **  This is a greatly MODIFIED version of a "top" machine dependent file.
00006  **  The only resemblance it bears to the original is with respect to the
00007  **  mechanics of finding various system details. The copyright details
00008  **  follow.
00009  **
00010  **  --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
00011  **
00012  **  Top users/processes display for Unix
00013  **  Version 3
00014  **
00015  **  This program may be freely redistributed,
00016  **  but this entire comment MUST remain intact.
00017  **
00018  **  Copyright (c) 1984, 1989, William LeFebvre, Rice University
00019  **  Copyright (c) 1989 - 1994, William LeFebvre, Northwestern University
00020  **  Copyright (c) 1994, 1995, William LeFebvre, Argonne National Laboratory
00021  **  Copyright (c) 1996, William LeFebvre, Group sys Consulting
00022  **  Copyright (c) 2002, Associated Universities Inc.
00023  */
00024 
00025 /*
00026  * LIBS: -lmach -lpset
00027  *
00028  *          AUTHOR:       Darrell Schiebel  <drs@nrao.edu>
00029  *
00030  * ORIGINAL AUTHOR:       Anthony Baxter    <anthony@aaii.oz.au>
00031  * ORIGINAL CONTRIBUTORS: David S. Comay    <dsc@seismo.css.gov>
00032  *                        Claus Kalle
00033  *                        Pat Welch         <tpw@physics.orst.edu>
00034  *                        William LeFebvre  <lefebvre@dis.anl.gov>
00035  *                        Rainer Orth       <ro@techfak.uni-bielefeld.de>
00036  */
00037 
00038 #ifndef CASA_HOSTINFOOSF1_H
00039 #define CASA_HOSTINFOOSF1_H
00040 
00041 # if defined(HOSTINFO_DO_IMPLEMENT)
00042 
00043 //
00044 //--> /usr/include/mach/mach_interface.h:297:                                                        <-
00045 //--> previous declaration of `int vm_statistics(long int, struct vm_statistics *)' with C++ linkage <-
00046 //
00047 // so, we hack this by defining _mach to kick <mach/mach_interface.h> out of inclusion,
00048 // and then below we make this extern "C"... likely this is fixed in some version of
00049 // DEC OSF/1 which is newer than what we have...
00050 //
00051 #define _mach
00052 
00053 #include <stdio.h>
00054 
00055 #include <unistd.h>
00056 #include <mach.h>
00057 #include <mach/mach_types.h>
00058 #include <mach/vm_statistics.h>
00059 #include <mach/host_info.h>
00060 #include <sys/table.h>
00061 
00062 namespace casa { //# NAMESPACE CASA - BEGIN
00063 
00064 // <summary>
00065 // HostInfo for OSF1 machines.
00066 // </summary>
00067 
00068 // <use visibility=local>
00069 
00070 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
00071 // </reviewed>
00072 
00073 // <prerequisite>
00074 //   <li> <linkto class=HostInfo>HostInfo</linkto>
00075 // </prerequisite>
00076 
00077 // <synopsis> 
00078 // This file provides the OSF1 specific functions for HostInfo.
00079 // It is selectively included by HostInfo.cc.
00080 // </synopsis>
00081 //
00082 // <group name="HostInfo">
00083 
00084 extern "C" kern_return_t host_info(int, int, host_info_t, unsigned int *);
00085 extern "C" int host_self( );
00086 extern "C" int vm_statistics(long, struct vm_statistics *);
00087 
00088 /* Log base 2 of 1024 is 10 (2^10 == 1024) */
00089 #define LOG1024         10
00090 
00091 /* these are for getting the memory statistics */
00092 /* define pagetok in terms of pageshift */
00093 #define pagetok(size) ((size) << pageshift)
00094 
00095 class HostMachineInfo {
00096 friend class HostInfo;
00097 
00098     HostMachineInfo( );
00099     void update_info( );
00100 
00101     int valid;
00102     int cpus;
00103 
00104     ssize_t swap_total;
00105     ssize_t swap_used;
00106     ssize_t swap_free;
00107 
00108     ssize_t memory_total;
00109     ssize_t memory_used;
00110     ssize_t memory_free;
00111 
00112     int pageshift;              /* log base 2 of the pagesize */
00113 };
00114 
00115 // </group>
00116 
00117 
00118 HostMachineInfo::HostMachineInfo( ) : valid(1) {
00119 
00120     int i = 0;
00121     int pagesize;
00122     struct tbl_sysinfo sibuf;
00123 
00124     kern_return_t ret;
00125     struct host_basic_info basic_info;
00126     unsigned int count = HOST_BASIC_INFO_COUNT;
00127 
00128     /* get the page size with "getpagesize" and calculate pageshift from it */
00129     pagesize = getpagesize();
00130     pageshift = 0;
00131     while (pagesize > 1)
00132     {
00133         pageshift++;
00134         pagesize >>= 1;
00135     }
00136 
00137     /* we only need the amount of log(2)1024 for our conversion */
00138     pageshift -= LOG1024;
00139 
00140     ret = host_info( host_self(), HOST_BASIC_INFO, (host_info_t) &basic_info, &count );
00141     if ( ret != KERN_SUCCESS ) {
00142         valid = 0;
00143     } else {
00144         memory_total = basic_info.memory_size / 1024;
00145         cpus = basic_info.avail_cpus;
00146     }
00147 }
00148 
00149 void HostMachineInfo::update_info( ) {
00150 
00151     struct tbl_swapinfo swbuf;
00152     vm_statistics_data_t vmstats;
00153     int swappages=0,swapfree=0,i;
00154 
00155     /* memory information */
00156     /* this is possibly bogus - we work out total # pages by */
00157     /* adding up the free, active, inactive, wired down, and */
00158     /* zero filled. Anyone who knows a better way, TELL ME!  */
00159     /* Change: dont use zero filled. */
00160     (void) vm_statistics(task_self(),&vmstats);
00161 
00162     /* thanks DEC for the table() command. No thanks at all for   */
00163     /* omitting the man page for it from OSF/1 1.2, and failing   */
00164     /* to document SWAPINFO in the 1.3 man page. Lets hear it for */
00165     /* include files. */
00166     i=0;
00167     while(table(TBL_SWAPINFO,i,&swbuf,1,sizeof(struct tbl_swapinfo))>0) {
00168         swappages += swbuf.size;
00169         swapfree  += swbuf.free;
00170         i++;
00171     }
00172 
00173     swap_total = pagetok(swappages);
00174     swap_used = pagetok(swappages - swapfree);
00175     swap_free = pagetok(swapfree);
00176 
00177     memory_free = pagetok(vmstats.free_count);
00178     memory_used = memory_total - memory_free;
00179 //  some memory is left unaccounted for, using the following...
00180 //  memory_used = pagetok(vmstats.active_count + vmstats.inactive_count + vmstats.wire_count);
00181 }
00182 
00183 # endif
00184 
00185 } //# NAMESPACE CASA - END
00186 
00187 #endif