HostInfoBsd.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
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>
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 {
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
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
00094
00095
00096 HostMachineInfo::HostMachineInfo( ) : valid(1) {
00097 size_t len;
00098
00099 pagesize = getpagesize();
00100 page_kb = pagesize / 1024;
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;
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
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 }
00141
00142 # endif
00143 #endif