File: sysinfo.C

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 239,848 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; makefile: 93
file content (347 lines) | stat: -rw-r--r-- 6,570 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
// $Id: sysinfo.C,v 1.22 2005/12/23 17:03:06 amoll Exp $

#include <BALL/SYSTEM/sysinfo.h>

#ifdef BALL_OS_SOLARIS
# undef BALL_HAS_SYS_SYSINFO_H
# include <cstdlib>
# include <cstdio>
# include <kstat.h>
# include <sys/sysinfo.h>
# include <sys/stat.h>
# include <sys/swap.h>
# include <sys/param.h>
# include <unistd.h>
#endif

#ifdef BALL_HAS_SYS_SYSINFO_H
#	 include <sys/sysinfo.h>
#	 include <BALL/SYSTEM/file.h>
#else
# ifdef BALL_COMPILER_MSVC
#	  define WIN32_LEAN_AND_MEAN
#	  include <windows.h>
# endif
# ifdef BALL_OS_DARWIN
#   include <cstdlib>
#		include <sys/sysctl.h>
#   include <mach/mach.h>
# endif
#endif

namespace BALL
{
	namespace SysInfo
	{

#if defined(BALL_OS_LINUX) && defined(BALL_HAS_SYS_SYSINFO_H)

		LongIndex getAvailableMemory()
		{
			LongIndex mem = getFreeMemory();
			return mem;
		}

		LongIndex getFreeMemory()
		{
			struct sysinfo info;
			LongIndex result = sysinfo(&info);
			if (result == -1) 
			{
				return result;
			}
			return info.freeram * info.mem_unit;
		}

		LongIndex getTotalMemory()
		{
			struct sysinfo info;
			LongIndex result = sysinfo(&info);
			if (result == -1) 
			{
				return result;
			}
			return info.totalram * info.mem_unit;
		}

		LongIndex getBufferedMemory()
		{
			struct sysinfo info;
			LongIndex result = sysinfo(&info);
			if (result == -1) return result;
			return info.bufferram * info.mem_unit;
		}

		Time getUptime()
		{
			struct sysinfo info;
			LongIndex result = sysinfo(&info);
			if (result == -1) return result;
			return info.uptime;
		}

		Index getNumberOfProcessors()
		{
			return get_nprocs();
		}


		LongIndex getFreeSwapSpace()
		{
			struct sysinfo info;
			LongIndex result = sysinfo(&info);
			if (result == -1) return result;
			return info.freeswap * info.mem_unit;
		}
#else
#ifdef BALL_COMPILER_MSVC

		/// internal helper method used on Windows Platform, not available extern!
		MEMORYSTATUSEX getMemoryStatus()
		{
			MEMORYSTATUSEX statex;
			statex.dwLength = sizeof (statex);
			GlobalMemoryStatusEx (&statex);
			return statex;
		}

		LongIndex getAvailableMemory()
		{
			return getFreeMemory();
		}

		LongIndex getFreeMemory()
		{
			MEMORYSTATUSEX statex = getMemoryStatus();
			return static_cast<LongIndex>(statex.ullAvailPhys);
		}

		LongIndex getTotalMemory()
		{
			MEMORYSTATUSEX statex = getMemoryStatus();
			return static_cast<LongIndex>(statex.ullTotalPhys);
		}

		LongIndex getBufferedMemory()
		{
			return 0;
		}

		Time getUptime()
		{
			return -1;
		}

		Index getNumberOfProcessors()
		{
			SYSTEM_INFO sysinfo;
			GetSystemInfo(&sysinfo);
			return sysinfo.dwNumberOfProcessors;
		}

		LongIndex getFreeSwapSpace()
		{
			MEMORYSTATUSEX statex = getMemoryStatus();
			return (LongIndex) statex.ullAvailPageFile;
		}

#else
#ifdef BALL_OS_SOLARIS

	LongIndex getFreeMemory()
	{
	 	return ((LongIndex)sysconf(_SC_AVPHYS_PAGES)) * sysconf(_SC_PAGE_SIZE);
	}

	Index getNumberOfProcessors()
	{
		return sysconf(_SC_NPROCESSORS_CONF);
	}

	LongIndex getTotalMemory()
	{
		return ((LongIndex)sysconf(_SC_PHYS_PAGES)) * sysconf(_SC_PAGE_SIZE);
	}

	LongIndex getAvailableMemory()
	{
		return getFreeMemory();
	}

	LongIndex getFreeSwapSpace()
	{
		long pgsize_in_kbytes = sysconf(_SC_PAGE_SIZE) / 1024L;

		int swap_count=swapctl(SC_GETNSWP, NULL);
		if (swap_count == -1 || swap_count == 0) return swap_count;

		/*
		 *  Although it's not particularly clear in the documentation, you're
		 *  responsible for creating a variable length structure (ie. the
		 *  array is within the struct rather than being pointed to
		 *  by the struct).  Also, it is necessary for you to allocate space
		 *  for the path strings (see /usr/include/sys/swap.h).
		 */
		swaptbl_t* st = (swaptbl_t*)malloc(sizeof(int) + swap_count * sizeof(struct swapent));
		if (st == NULL) return -1;

		st->swt_n = swap_count;
		for (int i=0; i < swap_count; i++) 
		{
			if ((st->swt_ent[i].ste_path = (char*)malloc(MAXPATHLEN)) == NULL)
			{
				free(st);
				return -1;
			}
		}

		swap_count = swapctl(SC_LIST, (void*)st);
		if (swap_count == -1) return -1;

		long swap_avail = 0;
		for (int i = 0; i < swap_count; i++) 
		{
			swap_avail += st->swt_ent[i].ste_free * pgsize_in_kbytes;
		}

		free(st);

		return ((LongIndex)swap_avail) * 1024;
	}

	LongIndex getBufferedMemory()
	{
		return 0;
	}

	Time getUptime()
	{
		return -1;
	}


#else
#ifdef BALL_OS_DARWIN

		LongIndex getAvailableMemory()
		{
			return getFreeMemory();
		}
 
		bool getDarwinMemstats(LongSize& free, LongSize& total, LongSize& used)
		{
			static mach_port_t host;
			static mach_msg_type_number_t host_size = sizeof(vm_statistics_data_t) / sizeof(Index);
			static vm_size_t page_size;
			static vm_statistics_data_t vm_stats;

			host = mach_host_self();
			host_page_size(host, &page_size);
			if (host_statistics(host, HOST_VM_INFO, (host_info_t)&vm_stats, &host_size) != KERN_SUCCESS)
			{
				return false;
			}
			used = (vm_stats.active_count + vm_stats.inactive_count + vm_stats.wire_count) * page_size;
			free = vm_stats.free_count * page_size;
			total = free + used;
			
			return true;
		}

		LongIndex getFreeMemory()
		{
			static LongSize free, total, used;
			getDarwinMemstats(free, total, used);
			return free;
		}

		LongIndex getTotalMemory()
		{
    	unsigned int physmem;
    	size_t len = sizeof(physmem);
    	static int mib[2] = { CTL_HW, HW_PHYSMEM };

    	if (sysctl (mib, 2, &physmem, &len, NULL, 0) == 0
					&& len == sizeof(physmem))
			{
    	  return (LongIndex) physmem;
			}
			return -1;
		}

		LongIndex getBufferedMemory()
		{
			return -1;

		}

		Time getUptime()
		{
			return -1;
		}

		Index getNumberOfProcessors()
		{
			int mib[2] = { CTL_HW, HW_NCPU };
			int num_proc = 0;
			size_t len = sizeof(num_proc);
			sysctl(mib, 2, &num_proc, &len, NULL, 0);
			return (Index)num_proc;
		}

		LongIndex getFreeSwapSpace()
		{
			return getFreeMemory(); // It's at least this much. If you better, please tell me! [OK]
		}


#else // We have no idea how to retrieve that information on this
			// platform, so we just return -1 everywhere

		LongIndex getAvailableMemory()
		{
			return -1;
		}

		LongIndex getFreeMemory()
		{
			return -1;
		}

		LongIndex getTotalMemory()
		{
			return -1;
		}

		LongIndex getBufferedMemory()
		{
			return -1;
		}

		Time getUptime()
		{
			return -1;
		}

		Index getNumberOfProcessors()
		{
			return -1;
		}
	
		LongIndex getFreeSwapSpace()
		{
			return -1;
		}

#endif
#endif
#endif
#endif

	} // namespace SysInfo

} // namespace BALL