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 */
00039 //# $Id$
00040 
00041 #ifndef CASA_HOSTINFODARWIN_H
00042 #define CASA_HOSTINFODARWIN_H
00043 
00044 # if defined(HOSTINFO_DO_IMPLEMENT)
00045 
00046 
00047 #include <stdio.h>
00048 #include <stdlib.h>
00049 #include <unistd.h>
00050 
00051 #include <mach/mach.h>
00052 
00053 namespace casacore { //# NAMESPACE CASACORE - BEGIN
00054 
00055 // <summary>
00056 // HostInfo for Darwin machines.
00057 // </summary>
00058 
00059 // <use visibility=local>
00060 
00061 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
00062 // </reviewed>
00063 
00064 // <prerequisite>
00065 //   <li> <linkto class=HostInfo>HostInfo</linkto>
00066 // </prerequisite>
00067 
00068 // <synopsis> 
00069 // This file provides the Linux specific functions for HostInfo.
00070 // It is selectively included by HostInfo.cc.
00071 // </synopsis>
00072 //
00073 // <group name="HostInfo">
00074 
00075 /* Log base 2 of 1024 is 10 (2^10 == 1024) */
00076 #define LOG1024         10
00077 
00078 /* these are for getting the memory statistics */
00079 static int pageshift;           /* log base 2 of the pagesize */
00080 static int pagesize_;
00081 
00082 /* define pagetok in terms of pageshift */
00083 #define pagetok(size) ((size) << pageshift)
00084 
00085 class HostMachineInfo {
00086 friend class HostInfo;
00087   
00088     HostMachineInfo( );
00089     void update_info( );
00090 
00091     int valid;
00092     int cpus;
00093 
00094     ptrdiff_t memory_total;
00095     ptrdiff_t memory_used;
00096     ptrdiff_t memory_free;
00097 
00098     ptrdiff_t swap_total;
00099     ptrdiff_t swap_used;
00100     ptrdiff_t swap_free;
00101 };
00102 
00103 // </group>
00104 
00105 
00106 HostMachineInfo::HostMachineInfo( ) : valid(1) {
00107     int pagesize;
00108 
00109     kern_return_t ret;
00110     struct host_basic_info basic_info;
00111     unsigned int count = HOST_BASIC_INFO_COUNT;
00112 
00113     /* get the page size with "getpagesize" and calculate pageshift from it */
00114     pagesize_ = pagesize = getpagesize();
00115     pageshift = 0;
00116     while (pagesize > 1)
00117     {
00118         pageshift++;
00119         pagesize >>= 1;
00120     }
00121 
00122     /* we only need the amount of log(2)1024 for our conversion */
00123     pageshift -= LOG1024;
00124 
00125 #ifdef AIPS_64B
00126        ret = host_info( mach_host_self(), HOST_BASIC_INFO, (host_info64_t) &basic_info, &count );
00127 #else
00128        ret = host_info( mach_host_self(), HOST_BASIC_INFO, (host_info_t) &basic_info, &count );
00129 #endif
00130     if ( ret != KERN_SUCCESS ) {
00131         valid = 0;
00132     } else {
00133 #ifdef AIPS_64B
00134         memory_total = basic_info.max_mem / 1024;
00135 #else
00136         memory_total = basic_info.memory_size / 1024;
00137 #endif
00138         cpus = basic_info.avail_cpus;
00139     }
00140 }
00141 
00142 void HostMachineInfo::update_info( ) {
00143 
00144 #ifdef AIPS_64B
00145     struct vm_statistics64 vmstats;
00146 #else
00147     struct vm_statistics vmstats;
00148 #endif
00149     kern_return_t kr;
00150     unsigned int count;
00151 
00152     /* memory information */
00153     /* this is possibly bogus - we work out total # pages by */
00154     /* adding up the free, active, inactive, wired down, and */
00155     /* zero filled. Anyone who knows a better way, TELL ME!  */
00156     /* Change: dont use zero filled. */
00157     count = sizeof(vmstats)/sizeof(integer_t);
00158 
00159 #ifdef AIPS_64B
00160        kr = host_statistics64( mach_host_self(), HOST_VM_INFO64, (host_info64_t) &vmstats, &count );
00161 #else
00162        kr = host_statistics( mach_host_self(), HOST_VM_INFO, (host_info_t) &vmstats, &count );
00163 #endif
00164     if ( kr != KERN_SUCCESS ) {
00165       valid = 0;
00166       return;
00167     }
00168 
00169 //     /* thanks DEC for the table() command. No thanks at all for   */
00170 //     /* omitting the man page for it from OSF/1 1.2, and failing   */
00171 //     /* to document SWAPINFO in the 1.3 man page. Lets hear it for */
00172 //     /* include files. */
00173 //     i=0;
00174 //     while(table(TBL_SWAPINFO,i,&swbuf,1,sizeof(struct tbl_swapinfo))>0) {
00175 //      swappages += swbuf.size;
00176 //      swapfree  += swbuf.free;
00177 //      i++;
00178 //     }
00179 // 
00180 //     swap_used = pagetok(swappages - swapfree);
00181 //     swap_free = pagetok(swapfree);
00182 //     swap_total = pagetok(swappages);
00183 
00184     memory_used = pagetok(vmstats.active_count + vmstats.wire_count);
00185     memory_free = memory_total - memory_used;
00186     swap_used = pagetok( vmstats.active_count + vmstats.inactive_count + vmstats.wire_count );
00187     swap_free = pagetok( vmstats.free_count );
00188     swap_total = pagetok( vmstats.active_count + vmstats.inactive_count +
00189                           vmstats.wire_count + vmstats.free_count );
00190 }
00191 
00192 
00193 } //# NAMESPACE CASACORE - END
00194 
00195 # endif
00196 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 31 Aug 2016 for casa by  doxygen 1.6.1