File: globalslinux.cpp

package info (click to toggle)
libmuscle 3.7%2B4565-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,828 kB
  • ctags: 2,200
  • sloc: cpp: 27,959; makefile: 55; sh: 26
file content (167 lines) | stat: -rw-r--r-- 3,060 bytes parent folder | download | duplicates (5)
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
#include "libMUSCLE/muscle.h"
#include "libMUSCLE/threadstorage.h"

namespace muscle {

#if		defined(__linux__)
#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>

const int ONE_MB = 1000000;
const int MEM_WARNING_THRESHOLD = 20*ONE_MB;

double GetNAN()
	{
	static const unsigned long nan[2]={0xffffffff, 0x7fffffff};
	double dNAN = *( double* )nan;
	return dNAN;
	}

TLS<double> g_dNAN(GetNAN());

void chkmem(const char szMsg[])
	{
	//assert(_CrtCheckMemory());
	}

void Break()
	{
	//DebugBreak();
	}

static TLS<char[4096]> szCmdLine;

void *ptrStartBreak = sbrk(0);

const char *GetCmdLine()
	{
	return szCmdLine.get();
	}

double GetMemUseMB()
	{
	static TLS<char[64]> statm;
	static TLS<int> PageSize;
	if (0 == statm.get()[0])
		{
		PageSize.get() = sysconf(_SC_PAGESIZE);
		pid_t pid = getpid();
		sprintf(statm.get(), "/proc/%d/statm", (int) pid);
		}

	int fd = open(statm.get(), O_RDONLY);
	if (-1 == fd)
		return -1;
	char Buffer[64];
	int n = read(fd, Buffer, sizeof(Buffer) - 1);
	close(fd);
	fd = -1;

	if (n <= 0)
		{
		static bool Warned = false;
		if (!Warned)
			{
			Warned = true;
			Warning("*Warning* Cannot read %s errno=%d %s",
			  statm.get(), errno, strerror(errno));
			}
		return 0;
		}
	Buffer[n] = 0;
	int Pages = atoi(Buffer);

	return ((double) Pages * (double) PageSize.get())/1e6;
	}

void SaveCmdLine(int argc, char *argv[])
	{
	for (int i = 0; i < argc; ++i)
		{
		if (i > 0)
			strcat(szCmdLine.get(), " ");
		strcat(szCmdLine.get(), argv[i]);
		}
	}

double dPeakMemUseMB = 0;

double GetPeakMemUseMB()
	{
	CheckMemUse();
	return dPeakMemUseMB;
	}

double GetCPUGHz()
	{
	double dGHz = 2.5;
	const char *e = getenv("CPUGHZ");
	if (0 != e)
		dGHz = atof(e);
	return dGHz;
	}

void CheckMemUse()
	{
	double dMB = GetMemUseMB();
	if (dMB > dPeakMemUseMB)
		dPeakMemUseMB = dMB;
	}

double GetRAMSizeMB()
	{
	const double DEFAULT_RAM = 500;
	static double RAMMB = 0;
	if (RAMMB != 0)
		return RAMMB;

	int fd = open("/proc/meminfo", O_RDONLY);
	if (-1 == fd)
		{
		static bool Warned = false;
		if (!Warned)
			{
			Warned = true;
			Warning("*Warning* Cannot open /proc/meminfo errno=%d %s",
			  errno, strerror(errno));
			}
		return DEFAULT_RAM;
		}
	char Buffer[1024];
	int n = read(fd, Buffer, sizeof(Buffer) - 1);
	close(fd);
	fd = -1;

	if (n <= 0)
		{
		static bool Warned = false;
		if (!Warned)
			{
			Warned = true;
			Warning("*Warning* Cannot read /proc/meminfo errno=%d %s",
			  errno, strerror(errno));
			}
		return DEFAULT_RAM;
		}
	Buffer[n] = 0;
	char *pMem = strstr(Buffer, "MemTotal: ");
	if (0 == pMem)
		{
		static bool Warned = false;
		if (!Warned)
			{
			Warned = true;
			Warning("*Warning* 'MemTotal:' not found in /proc/meminfo");
			}
		return DEFAULT_RAM;
		}
	long Bytes = atoi(pMem+9)*1000;
	return ((double) Bytes)/1e6;
	}

#endif	// !WIN32
}