casacore
HostInfoLinux.h
Go to the documentation of this file.
1 //# HostInfo_linux.h: Linux specific memory, swap, and CPU code.
2 //# $Id$
3 
4  /*
5  ** This is a greatly MODIFIED version of a "top" machine dependent file.
6  ** The only resemblance it bears to the original is with respect to the
7  ** mechanics of finding various system details. The copyright details
8  ** follow.
9  **
10  ** --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
11  **
12  ** Top users/processes display for Unix
13  ** Version 3
14  **
15  ** This program may be freely redistributed,
16  ** but this entire comment MUST remain intact.
17  **
18  ** Copyright (c) 1984, 1989, William LeFebvre, Rice University
19  ** Copyright (c) 1989 - 1994, William LeFebvre, Northwestern University
20  ** Copyright (c) 1994, 1995, William LeFebvre, Argonne National Laboratory
21  ** Copyright (c) 1996, William LeFebvre, Group sys Consulting
22  ** Copyright (c) 2002, Associated Universities Inc.
23  */
24 
25 #ifndef CASA_HOSTINFOLINUX_H
26 #define CASA_HOSTINFOLINUX_H
27 
28 # if defined(HOSTINFO_DO_IMPLEMENT)
29 
30 /*
31  * AUTHOR: Darrell Schiebel <drs@nrao.edu>
32  *
33  * ORIGINAL AUTHORS: Richard Henderson <rth@tamu.edu>
34  * Alexey Klimkin <kad@klon.tme.mcst.ru>
35  *
36  *
37  */
38 
39 #include <string.h>
40 #include <ctype.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <sys/vfs.h>
44 #include <unistd.h>
45 #include <fcntl.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 
49 // <summary>
50 // HostInfo for Linux machines.
51 // </summary>
52 
53 // <use visibility=local>
54 
55 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
56 // </reviewed>
57 
58 // <prerequisite>
59 // <li> <linkto class=HostInfo>HostInfo</linkto>
60 // </prerequisite>
61 
62 // <synopsis>
63 // This file provides the Linux specific functions for HostInfo.
64 // It is selectively included by HostInfo.cc.
65 // </synopsis>
66 //
67 // <group name="HostInfo">
68 
69 #if 0
70 #include <linux/proc_fs.h> /* for PROC_SUPER_MAGIC */
71 #else
72 #define PROC_SUPER_MAGIC 0x9fa0
73 #endif
74 
75 #define PROCFS "/proc"
76 #define CPUINFO "/proc/cpuinfo"
77 #define MEMINFO "/proc/meminfo"
78 
79 #define bytetok(x) (((x) + 512) >> 10)
80 
81 namespace casacore { //# NAMESPACE CASACORE - BEGIN
82 
83 class HostMachineInfo {
84 friend class HostInfo;
85 
86  HostMachineInfo( );
87  void update_info( );
88 
89  int valid;
90  int cpus;
91 
92  ptrdiff_t memory_total;
93  ptrdiff_t memory_used;
94  ptrdiff_t memory_free;
95 
96  ptrdiff_t swap_total;
97  ptrdiff_t swap_used;
98  ptrdiff_t swap_free;
99 };
100 
101 // </group>
102 
103 
104 static inline char *
105 skip_ws(const char *p)
106 {
107  while (isspace(*p)) p++;
108  return (char *)p;
109 }
110 
111 static inline char *
112 skip_token(const char *p)
113 {
114  while (isspace(*p)) p++;
115  while (*p && !isspace(*p)) p++;
116  return (char *)p;
117 }
118 
119 HostMachineInfo::HostMachineInfo( ) : valid(1)
120 {
121  char buffer[4096+1];
122  int fd, len;
123  char *p;
124 
125 #ifndef AIPS_CRAY_PGI
126  /* make sure the proc filesystem is mounted */
127  {
128  struct statfs sb;
129  if (statfs(PROCFS, &sb) < 0 || sb.f_type != PROC_SUPER_MAGIC)
130  {
131  fprintf( stderr, "proc filesystem not mounted on " PROCFS "\n" );
132  valid = 0;
133  return;
134  }
135  }
136 #endif
137 
138  /* get number of CPUs */
139  {
140  cpus = 0;
141  FILE *fptr = fopen(CPUINFO, "r");
142  while ( (p = fgets( buffer, sizeof(buffer), fptr )) ) {
143  if ( ! strncmp( p, "processor", 9 ) ) ++cpus;
144  }
145  fclose(fptr);
146  }
147 
148  /* get system total memory */
149  /* See the sprintf() statements of the file
150  fs/proc/proc_misc.c in the kernel source tree */
151  {
152  fd = open(MEMINFO, O_RDONLY);
153  len = read(fd, buffer, sizeof(buffer)-1);
154  close(fd);
155  buffer[len] = '\0';
156 
157  int ret;
158  unsigned long mem_total, swp_total;
159  p = strstr(buffer, "MemTotal:");
160  if ((ret = sscanf (p, "MemTotal: %lu kB\n", &mem_total)) != 1)
161  cerr << "Error parsing MemTotal in /proc/meminfo\n";
162  memory_total = mem_total;
163  p = strstr(buffer, "SwapTotal:");
164  if ((ret = sscanf (p, "SwapTotal: %lu kB\n", &swp_total)) != 1)
165  cerr << "Error parsing SwapTotal in /proc/meminfo\n";
166  swap_total = swp_total;
167  }
168 }
169 
170 void HostMachineInfo::update_info( )
171 {
172  char buffer[4096+1];
173  int fd, len;
174 
175  /* get system wide memory usage */
176  {
177  char *p;
178  int ret;
179  unsigned long mem_total, mem_free, mem_cached, swp_total, swp_free;
180 
181  fd = open(MEMINFO, O_RDONLY);
182  len = read(fd, buffer, sizeof(buffer)-1);
183  close(fd);
184  buffer[len] = '\0';
185 
186  p = strstr(buffer, "MemTotal:");
187 
188  if ((ret = sscanf (p,"MemTotal: %lu kB\nMemFree: %lu kB\n",
189  &mem_total, &mem_free)) != 2)
190  cerr << "Error parsing MemTotal and MemFree in /proc/meminfo\n";
191 
192  p = strstr (buffer, "Cached:");
193  if ((ret = sscanf (p,"Cached: %lu kB\n", &mem_cached)) != 1)
194  cerr << "Error parsing Cached in /proc/meminfo\n";
195 
196  memory_total = mem_total;
197  memory_free = mem_free + mem_cached;
198  memory_used = memory_total - memory_free;
199 
200  p = strstr (buffer, "SwapTotal:");
201  if ((ret = sscanf (p, "SwapTotal: %lu kB\nSwapFree: %lu kB\n",
202  &swp_total, &swp_free)) != 2)
203  cerr << "Error parsing SwapTotal and SwapFree in /proc/meminfo\n";
204 
205  swap_total = swp_total;
206  swap_free = swp_free;
207  swap_used = swap_total-swap_free;
208  }
209 }
210 
211 } //# NAMESPACE CASACORE - END
212 
213 # endif
214 
215 #endif
this file contains all the compiler specific defines
Definition: mainpage.dox:28