HostInfoBsd.h

Go to the documentation of this file.
00001 //# Copyright (C) 2009
00002 //# Associated Universities, Inc. Washington DC, USA.
00003 //#
00004 //# This library is free software; you can redistribute it and/or modify it
00005 //# under the terms of the GNU Library General Public License as published by
00006 //# the Free Software Foundation; either version 2 of the License, or (at your
00007 //# option) any later version.
00008 //#
00009 //# This library is distributed in the hope that it will be useful, but WITHOUT
00010 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00011 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00012 //# License for more details.
00013 //#
00014 //# You should have received a copy of the GNU Library General Public License
00015 //# along with this library; if not, write to the Free Software Foundation,
00016 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00017 //#
00018 //# Correspondence concerning AIPS++ should be addressed as follows:
00019 //#        Internet email: aips2-request@nrao.edu.
00020 //#        Postal address: AIPS++ Project Office
00021 //#                        National Radio Astronomy Observatory
00022 //#                        520 Edgemont Road
00023 //#                        Charlottesville, VA 22903-2475 USA
00024 //#
00025 //# $Id$
00026 /*
00027 ** Author Tony Maher
00028 **
00029 ** Based on HostInfoDarwin.h (just the scaffolding code).
00030 */
00031 
00032 #ifndef CASA_HOSTINFOBSD_H
00033 #define CASA_HOSTINFOBSD_H
00034 
00035 # if defined(HOSTINFO_DO_IMPLEMENT)
00036 
00037 #include <sys/types.h>
00038 #include <sys/sysctl.h>
00039 #include <sys/vmmeter.h>    // struct vmtotal
00040 
00041 #include <fcntl.h>
00042 #include <kvm.h>
00043 #include <paths.h>
00044 #include <stdio.h>
00045 #include <stdlib.h>
00046 #include <unistd.h>
00047 
00048 
00049 namespace casacore { //# NAMESPACE CASACORE - BEGIN
00050 
00051 // <summary>
00052 // HostInfo for BSD (FreeBSD) machines.
00053 // </summary>
00054 
00055 // <use visibility=local>
00056 
00057 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
00058 // </reviewed>
00059 
00060 // <prerequisite>
00061 //   <li> <linkto class=HostInfo>HostInfo</linkto>
00062 // </prerequisite>
00063 
00064 // <synopsis> 
00065 // This file provides the BSD (FreeBSD) specific functions for HostInfo.
00066 // It is selectively included by HostInfo.cc.
00067 // </synopsis>
00068 //
00069 // <group name="HostInfo">
00070 
00071 // these are for getting the memory statistics
00072 static int pagesize;
00073 static int page_kb;
00074 
00075 class HostMachineInfo {
00076 friend class HostInfo;
00077   
00078     HostMachineInfo( );
00079     void update_info( );
00080 
00081     int valid;
00082     int cpus;
00083 
00084     ptrdiff_t memory_total;
00085     ptrdiff_t memory_used;
00086     ptrdiff_t memory_free;
00087 
00088     ptrdiff_t swap_total;
00089     ptrdiff_t swap_used;
00090     ptrdiff_t swap_free;
00091 };
00092 
00093 // </group>
00094 
00095 
00096 HostMachineInfo::HostMachineInfo( ) : valid(1) {
00097     size_t len;
00098 
00099     pagesize = getpagesize();   // size of page in bytes.
00100     page_kb = pagesize / 1024;  // in kilobytes.
00101 
00102     len = sizeof(cpus);
00103     if (sysctlbyname("hw.ncpu", &cpus, &len, NULL, 0) == -1)
00104         perror("sysctl");
00105 
00106     len = sizeof(memory_total);
00107     if (sysctlbyname("hw.physmem", &memory_total, &len, NULL, 0) == -1)
00108         perror("sysctl");
00109     else
00110         memory_total /= 1024; // in kilobytes
00111 }
00112 
00113 
00114 void HostMachineInfo::update_info( ) {
00115     size_t len;
00116     kvm_t *kd;
00117     struct vmtotal total;
00118     struct kvm_swap swapary[1];
00119 
00120     // Memory and swap values are in pages.
00121 
00122     len = sizeof(total);
00123     if (sysctlbyname("vm.vmtotal", &total, &len, NULL, 0) == -1)
00124         perror("sysctl");
00125     else
00126         memory_used = total.t_rm * page_kb;
00127         memory_free = total.t_free * page_kb;
00128 
00129     kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, "kvm_open");
00130     if (kd != NULL) {
00131         kvm_getswapinfo(kd, swapary, 1, 0);
00132 
00133         swap_total = swapary[0].ksw_total * page_kb;
00134         swap_used  = swapary[0].ksw_used * page_kb;
00135         swap_free  = swap_total - swap_used;
00136         }
00137 }
00138 
00139 
00140 } //# NAMESPACE CASACORE - END
00141 
00142 # endif
00143 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 31 Aug 2016 for casa by  doxygen 1.6.1