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