casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
HostInfoBsd.h
Go to the documentation of this file.
00001 /*
00002 ** Author Tony Maher
00003 **
00004 ** Based on HostInfoDarwin.h (just the scaffolding code).
00005 **
00006 ** $Id: HostInfoBsd.h 20766 2009-10-06 00:52:29Z Malte.Marquarding $
00007 **
00008 **  Copyright (c) 2009, Associated Universities Inc.
00009 */
00010 
00011 #ifndef CASA_HOSTINFOBSD_H
00012 #define CASA_HOSTINFOBSD_H
00013 
00014 # if defined(HOSTINFO_DO_IMPLEMENT)
00015 
00016 #include <sys/types.h>
00017 #include <sys/sysctl.h>
00018 #include <sys/vmmeter.h>    // struct vmtotal
00019 
00020 #include <fcntl.h>
00021 #include <kvm.h>
00022 #include <paths.h>
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <unistd.h>
00026 
00027 
00028 namespace casa { //# NAMESPACE CASA - BEGIN
00029 
00030 // <summary>
00031 // HostInfo for BSD (FreeBSD) machines.
00032 // </summary>
00033 
00034 // <use visibility=local>
00035 
00036 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
00037 // </reviewed>
00038 
00039 // <prerequisite>
00040 //   <li> <linkto class=HostInfo>HostInfo</linkto>
00041 // </prerequisite>
00042 
00043 // <synopsis> 
00044 // This file provides the BSD (FreeBSD) specific functions for HostInfo.
00045 // It is selectively included by HostInfo.cc.
00046 // </synopsis>
00047 //
00048 // <group name="HostInfo">
00049 
00050 // these are for getting the memory statistics
00051 static int pagesize;
00052 static int page_kb;
00053 
00054 class HostMachineInfo {
00055 friend class HostInfo;
00056   
00057     HostMachineInfo( );
00058     void update_info( );
00059 
00060     int valid;
00061     int cpus;
00062 
00063     ssize_t memory_total;
00064     ssize_t memory_used;
00065     ssize_t memory_free;
00066 
00067     ssize_t swap_total;
00068     ssize_t swap_used;
00069     ssize_t swap_free;
00070 };
00071 
00072 // </group>
00073 
00074 
00075 HostMachineInfo::HostMachineInfo( ) : valid(1) {
00076     size_t len;
00077 
00078     pagesize = getpagesize();   // size of page in bytes.
00079     page_kb = pagesize / 1024;  // in kilobytes.
00080 
00081     len = sizeof(cpus);
00082     if (sysctlbyname("hw.ncpu", &cpus, &len, NULL, 0) == -1)
00083         perror("sysctl");
00084 
00085     len = sizeof(memory_total);
00086     if (sysctlbyname("hw.physmem", &memory_total, &len, NULL, 0) == -1)
00087         perror("sysctl");
00088     else
00089         memory_total /= 1024; // in kilobytes
00090 }
00091 
00092 
00093 void HostMachineInfo::update_info( ) {
00094     size_t len;
00095     kvm_t *kd;
00096     struct vmtotal total;
00097     struct kvm_swap swapary[1];
00098 
00099     // Memory and swap values are in pages.
00100 
00101     len = sizeof(total);
00102     if (sysctlbyname("vm.vmtotal", &total, &len, NULL, 0) == -1)
00103         perror("sysctl");
00104     else
00105         memory_used = total.t_rm * page_kb;
00106         memory_free = total.t_free * page_kb;
00107 
00108     kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, "kvm_open");
00109     if (kd != NULL) {
00110         kvm_getswapinfo(kd, swapary, 1, 0);
00111 
00112         swap_total = swapary[0].ksw_total * page_kb;
00113         swap_used  = swapary[0].ksw_used * page_kb;
00114         swap_free  = swap_total - swap_used;
00115         }
00116 }
00117 
00118 
00119 } //# NAMESPACE CASA - END
00120 
00121 # endif
00122 #endif