casacore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HostInfoDarwin.h
Go to the documentation of this file.
1 /*
2 ** This is a greatly MODIFIED version of a "top" machine dependent file.
3 ** The only resemblance it bears to the original is with respect to the
4 ** mechanics of finding various system details. The copyright details
5 ** follow.
6 **
7 ** This is a modified version of the osf1 version. Initially, I tried
8 ** to use the m_macosx.c version by Andrew S. Townley, but ran into
9 ** problems...
10 **
11 ** --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
12 **
13 ** Top users/processes display for Unix
14 ** Version 3
15 **
16 ** This program may be freely redistributed,
17 ** but this entire comment MUST remain intact.
18 **
19 ** Copyright (c) 1984, 1989, William LeFebvre, Rice University
20 ** Copyright (c) 1989 - 1994, William LeFebvre, Northwestern University
21 ** Copyright (c) 1994, 1995, William LeFebvre, Argonne National Laboratory
22 ** Copyright (c) 1996, William LeFebvre, Group sys Consulting
23 ** Copyright (c) 2002, Associated Universities Inc.
24 */
25 
26 /*
27 ** LIBS: -lstdc++
28 **
29 ** AUTHOR: Darrell Schiebel <drs@nrao.edu>
30 **
31 ** ORIGINAL AUTHOR: Anthony Baxter <anthony@aaii.oz.au>
32 ** ORIGINAL CONTRIBUTORS: David S. Comay <dsc@seismo.css.gov>
33 ** Claus Kalle
34 ** Pat Welch <tpw@physics.orst.edu>
35 ** William LeFebvre <lefebvre@dis.anl.gov>
36 ** Rainer Orth <ro@techfak.uni-bielefeld.de>
37 **
38 */
39 //# $Id$
40 
41 #ifndef CASA_HOSTINFODARWIN_H
42 #define CASA_HOSTINFODARWIN_H
43 
44 # if defined(HOSTINFO_DO_IMPLEMENT)
45 
46 
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <unistd.h>
50 
51 #include <mach/mach.h>
52 
53 namespace casacore { //# NAMESPACE CASACORE - BEGIN
54 
55 // <summary>
56 // HostInfo for Darwin machines.
57 // </summary>
58 
59 // <use visibility=local>
60 
61 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
62 // </reviewed>
63 
64 // <prerequisite>
65 // <li> <linkto class=HostInfo>HostInfo</linkto>
66 // </prerequisite>
67 
68 // <synopsis>
69 // This file provides the Linux specific functions for HostInfo.
70 // It is selectively included by HostInfo.cc.
71 // </synopsis>
72 //
73 // <group name="HostInfo">
74 
75 /* Log base 2 of 1024 is 10 (2^10 == 1024) */
76 #define LOG1024 10
77 
78 /* these are for getting the memory statistics */
79 static int pageshift; /* log base 2 of the pagesize */
80 static int pagesize_;
81 
82 /* define pagetok in terms of pageshift */
83 #define pagetok(size) ((size) << pageshift)
84 
85 class HostMachineInfo {
86 friend class HostInfo;
87 
88  HostMachineInfo( );
89  void update_info( );
90 
91  int valid;
92  int cpus;
93 
94  ptrdiff_t memory_total;
95  ptrdiff_t memory_used;
96  ptrdiff_t memory_free;
97 
98  ptrdiff_t swap_total;
99  ptrdiff_t swap_used;
100  ptrdiff_t swap_free;
101 };
102 
103 // </group>
104 
105 
106 HostMachineInfo::HostMachineInfo( ) : valid(1) {
107  int pagesize;
108 
109  kern_return_t ret;
110  struct host_basic_info basic_info;
111  unsigned int count = HOST_BASIC_INFO_COUNT;
112 
113  /* get the page size with portable sysconf and calculate pageshift from it */
114  pagesize_ = pagesize = sysconf(_SC_PAGESIZE);
115  pageshift = 0;
116  while (pagesize > 1)
117  {
118  pageshift++;
119  pagesize >>= 1;
120  }
121 
122  /* we only need the amount of log(2)1024 for our conversion */
123  pageshift -= LOG1024;
124 
125 #ifdef AIPS_64B
126  ret = host_info( mach_host_self(), HOST_BASIC_INFO, (host_info64_t) &basic_info, &count );
127 #else
128  ret = host_info( mach_host_self(), HOST_BASIC_INFO, (host_info_t) &basic_info, &count );
129 #endif
130  if ( ret != KERN_SUCCESS ) {
131  valid = 0;
132  } else {
133 #ifdef AIPS_64B
134  memory_total = basic_info.max_mem / 1024;
135 #else
136  memory_total = basic_info.memory_size / 1024;
137 #endif
138  cpus = basic_info.avail_cpus;
139  }
140 }
141 
142 void HostMachineInfo::update_info( ) {
143 
144 #ifdef AIPS_64B
145  struct vm_statistics64 vmstats;
146 #else
147  struct vm_statistics vmstats;
148 #endif
149  kern_return_t kr;
150  unsigned int count;
151 
152  /* memory information */
153  /* this is possibly bogus - we work out total # pages by */
154  /* adding up the free, active, inactive, wired down, and */
155  /* zero filled. Anyone who knows a better way, TELL ME! */
156  /* Change: dont use zero filled. */
157  count = sizeof(vmstats)/sizeof(integer_t);
158 
159 #ifdef AIPS_64B
160  kr = host_statistics64( mach_host_self(), HOST_VM_INFO64, (host_info64_t) &vmstats, &count );
161 #else
162  kr = host_statistics( mach_host_self(), HOST_VM_INFO, (host_info_t) &vmstats, &count );
163 #endif
164  if ( kr != KERN_SUCCESS ) {
165  valid = 0;
166  return;
167  }
168 
169 // /* thanks DEC for the table() command. No thanks at all for */
170 // /* omitting the man page for it from OSF/1 1.2, and failing */
171 // /* to document SWAPINFO in the 1.3 man page. Lets hear it for */
172 // /* include files. */
173 // i=0;
174 // while(table(TBL_SWAPINFO,i,&swbuf,1,sizeof(struct tbl_swapinfo))>0) {
175 // swappages += swbuf.size;
176 // swapfree += swbuf.free;
177 // i++;
178 // }
179 //
180 // swap_used = pagetok(swappages - swapfree);
181 // swap_free = pagetok(swapfree);
182 // swap_total = pagetok(swappages);
183 
184  memory_used = pagetok(vmstats.active_count + vmstats.wire_count);
185  memory_free = memory_total - memory_used;
186  swap_used = pagetok( vmstats.active_count + vmstats.inactive_count + vmstats.wire_count );
187  swap_free = pagetok( vmstats.free_count );
188  swap_total = pagetok( vmstats.active_count + vmstats.inactive_count +
189  vmstats.wire_count + vmstats.free_count );
190 }
191 
192 
193 } //# NAMESPACE CASACORE - END
194 
195 # endif
196 #endif