casacore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HostInfoBsd.h
Go to the documentation of this file.
1 //# Copyright (C) 2009
2 //# Associated Universities, Inc. Washington DC, USA.
3 //#
4 //# This library is free software; you can redistribute it and/or modify it
5 //# under the terms of the GNU Library General Public License as published by
6 //# the Free Software Foundation; either version 2 of the License, or (at your
7 //# option) any later version.
8 //#
9 //# This library is distributed in the hope that it will be useful, but WITHOUT
10 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 //# License for more details.
13 //#
14 //# You should have received a copy of the GNU Library General Public License
15 //# along with this library; if not, write to the Free Software Foundation,
16 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
17 //#
18 //# Correspondence concerning AIPS++ should be addressed as follows:
19 //# Internet email: aips2-request@nrao.edu.
20 //# Postal address: AIPS++ Project Office
21 //# National Radio Astronomy Observatory
22 //# 520 Edgemont Road
23 //# Charlottesville, VA 22903-2475 USA
24 //#
25 //# $Id$
26 /*
27 ** Author Tony Maher
28 **
29 ** Based on HostInfoDarwin.h (just the scaffolding code).
30 */
31 
32 #ifndef CASA_HOSTINFOBSD_H
33 #define CASA_HOSTINFOBSD_H
34 
35 # if defined(HOSTINFO_DO_IMPLEMENT)
36 
37 #include <sys/types.h>
38 #include <sys/sysctl.h>
39 #include <sys/vmmeter.h> // struct vmtotal
40 
41 #include <fcntl.h>
42 #include <kvm.h>
43 #include <paths.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47 
48 
49 namespace casacore { //# NAMESPACE CASACORE - BEGIN
50 
51 // <summary>
52 // HostInfo for BSD (FreeBSD) machines.
53 // </summary>
54 
55 // <use visibility=local>
56 
57 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
58 // </reviewed>
59 
60 // <prerequisite>
61 // <li> <linkto class=HostInfo>HostInfo</linkto>
62 // </prerequisite>
63 
64 // <synopsis>
65 // This file provides the BSD (FreeBSD) specific functions for HostInfo.
66 // It is selectively included by HostInfo.cc.
67 // </synopsis>
68 //
69 // <group name="HostInfo">
70 
71 // these are for getting the memory statistics
72 static int pagesize;
73 static int page_kb;
74 
75 class HostMachineInfo {
76 friend class HostInfo;
77 
78  HostMachineInfo( );
79  void update_info( );
80 
81  int valid;
82  int cpus;
83 
84  ptrdiff_t memory_total;
85  ptrdiff_t memory_used;
86  ptrdiff_t memory_free;
87 
88  ptrdiff_t swap_total;
89  ptrdiff_t swap_used;
90  ptrdiff_t swap_free;
91 };
92 
93 // </group>
94 
95 
96 HostMachineInfo::HostMachineInfo( ) : valid(1) {
97  size_t len;
98 
99  pagesize = getpagesize(); // size of page in bytes.
100  page_kb = pagesize / 1024; // in kilobytes.
101 
102  len = sizeof(cpus);
103  if (sysctlbyname("hw.ncpu", &cpus, &len, NULL, 0) == -1)
104  perror("sysctl");
105 
106  len = sizeof(memory_total);
107  if (sysctlbyname("hw.physmem", &memory_total, &len, NULL, 0) == -1)
108  perror("sysctl");
109  else
110  memory_total /= 1024; // in kilobytes
111 }
112 
113 
114 void HostMachineInfo::update_info( ) {
115  size_t len;
116  kvm_t *kd;
117  struct vmtotal total;
118  struct kvm_swap swapary[1];
119 
120  // Memory and swap values are in pages.
121 
122  len = sizeof(total);
123  if (sysctlbyname("vm.vmtotal", &total, &len, NULL, 0) == -1)
124  perror("sysctl");
125  else
126  memory_used = total.t_rm * page_kb;
127  memory_free = total.t_free * page_kb;
128 
129  kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, "kvm_open");
130  if (kd != NULL) {
131  kvm_getswapinfo(kd, swapary, 1, 0);
132 
133  swap_total = swapary[0].ksw_total * page_kb;
134  swap_used = swapary[0].ksw_used * page_kb;
135  swap_free = swap_total - swap_used;
136  }
137 }
138 
139 
140 } //# NAMESPACE CASACORE - END
141 
142 # endif
143 #endif