casacore
Loading...
Searching...
No Matches
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: casa-feedback@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** Author Tony Maher
26**
27** Based on HostInfoDarwin.h (just the scaffolding code).
28*/
29
30#ifndef CASA_HOSTINFOBSD_H
31#define CASA_HOSTINFOBSD_H
32
33#if defined(HOSTINFO_DO_IMPLEMENT)
34
35#include <sys/types.h>
36#include <sys/sysctl.h>
37#include <sys/vmmeter.h> // struct vmtotal
38
39#include <fcntl.h>
40#include <kvm.h>
41#include <paths.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <unistd.h>
45
46namespace casacore { // # NAMESPACE CASACORE - BEGIN
47
48// <summary>
49// HostInfo for BSD (FreeBSD) machines.
50// </summary>
51
52// <use visibility=local>
53
54// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
55// </reviewed>
56
57// <prerequisite>
58// <li> <linkto class=HostInfo>HostInfo</linkto>
59// </prerequisite>
60
61// <synopsis>
62// This file provides the BSD (FreeBSD) specific functions for HostInfo.
63// It is selectively included by HostInfo.cc.
64// </synopsis>
65//
66// <group name="HostInfo">
67
68// these are for getting the memory statistics
69static int pagesize;
70static int page_kb;
71
72class HostMachineInfo {
73 friend class HostInfo;
74
75 HostMachineInfo();
76 void update_info();
77
78 int valid;
79 int cpus;
80
81 ptrdiff_t memory_total;
82 ptrdiff_t memory_used;
83 ptrdiff_t memory_free;
84
85 ptrdiff_t swap_total;
86 ptrdiff_t swap_used;
87 ptrdiff_t swap_free;
88};
89
90// </group>
91
92HostMachineInfo::HostMachineInfo() : valid(1) {
93 size_t len;
94
95 pagesize = getpagesize(); // size of page in bytes.
96 page_kb = pagesize / 1024; // in kilobytes.
97
98 len = sizeof(cpus);
99 if (sysctlbyname("hw.ncpu", &cpus, &len, NULL, 0) == -1) perror("sysctl");
100
101 len = sizeof(memory_total);
102 if (sysctlbyname("hw.physmem", &memory_total, &len, NULL, 0) == -1)
103 perror("sysctl");
104 else
105 memory_total /= 1024; // in kilobytes
106}
107
108void HostMachineInfo::update_info() {
109 size_t len;
110 kvm_t *kd;
111 struct vmtotal total;
112 struct kvm_swap swapary[1];
113
114 // Memory and swap values are in pages.
115
116 len = sizeof(total);
117 if (sysctlbyname("vm.vmtotal", &total, &len, NULL, 0) == -1)
118 perror("sysctl");
119 else
120 memory_used = total.t_rm * page_kb;
121 memory_free = total.t_free * page_kb;
122
123 kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, "kvm_open");
124 if (kd != NULL) {
125 kvm_getswapinfo(kd, swapary, 1, 0);
126
127 swap_total = swapary[0].ksw_total * page_kb;
128 swap_used = swapary[0].ksw_used * page_kb;
129 swap_free = swap_total - swap_used;
130 }
131}
132
133} // namespace casacore
134
135#endif
136#endif
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28