1 #include <U2Core/disable-warnings.h>
2 U2_DISABLE_WARNINGS
3 
4 #include "muscle.h"
5 
6 #if		defined(__linux__)
7 #include <sys/time.h>
8 #include <sys/resource.h>
9 #include <unistd.h>
10 #include <errno.h>
11 #include <stdio.h>
12 #include <fcntl.h>
13 
14 const int ONE_MB = 1000000;
15 const int MEM_WARNING_THRESHOLD = 20*ONE_MB;
16 
GetNAN()17 double GetNAN()
18 	{
19 	static unsigned long nan[2]={0xffffffff, 0x7fffffff};
20 	double dNAN = *( double* )nan;
21 	return dNAN;
22 	}
23 
24 double g_dNAN = GetNAN();
25 
chkmem(const char szMsg[])26 void chkmem(const char szMsg[])
27 	{
28 	//assert(_CrtCheckMemory());
29 	}
30 
Break()31 void Break()
32 	{
33 	//DebugBreak();
34 	}
35 
36 static char szCmdLine[4096];
37 
38 void *ptrStartBreak = sbrk(0);
39 
GetCmdLine()40 const char *GetCmdLine()
41 	{
42 	return szCmdLine;
43 	}
44 //warning: unsafe
45 //double GetMemUseMB()
46 //	{
47 //	static char statm[64];
48 //	static int PageSize;
49 //	if (0 == statm[0])
50 //		{
51 //		PageSize = sysconf(_SC_PAGESIZE);
52 //		pid_t pid = getpid();
53 //		sprintf(statm, "/proc/%d/statm", (int) pid);
54 //		}
55 //
56 //	int fd = open(statm, O_RDONLY);
57 //	if (-1 == fd)
58 //		return -1;
59 //	char Buffer[64];
60 //	int n = read(fd, Buffer, sizeof(Buffer) - 1);
61 //	close(fd);
62 //	fd = -1;
63 //
64 //	if (n <= 0)
65 //		{
66 //		static bool Warned = false;
67 //		if (!Warned)
68 //			{
69 //			Warned = true;
70 //			Warning("*Warning* Cannot read %s errno=%d %s",
71 //			  statm, errno, strerror(errno));
72 //			}
73 //		return 0;
74 //		}
75 //	Buffer[n] = 0;
76 //	int Pages = atoi(Buffer);
77 //
78 //	return ((double) Pages * (double) PageSize)/1e6;
79 //	}
80 
SaveCmdLine(int argc,char * argv[])81 void SaveCmdLine(int argc, char *argv[])
82 	{
83 	for (int i = 0; i < argc; ++i)
84 		{
85 		if (i > 0)
86 			strcat(szCmdLine, " ");
87 		strcat(szCmdLine, argv[i]);
88 		}
89 	}
90 
91 double dPeakMemUseMB = 0;
92 
93 //commented out because of CheckMemUse disabling
94 //double GetPeakMemUseMB()
95 //	{
96 //	CheckMemUse();
97 //	return dPeakMemUseMB;
98 //	}
99 
GetCPUGHz()100 double GetCPUGHz()
101 	{
102 	double dGHz = 2.5;
103 	const char *e = getenv("CPUGHZ");
104 	if (0 != e)
105 		dGHz = atof(e);
106 	return dGHz;
107 	}
108 
109 //commented out because of GetMemUseMB disabling
110 //void CheckMemUse()
111 //	{
112 //	double dMB = GetMemUseMB();
113 //	if (dMB > dPeakMemUseMB)
114 //		dPeakMemUseMB = dMB;
115 //	}
116 
GetRAMSizeMB()117 double GetRAMSizeMB()
118 	{
119 	const double DEFAULT_RAM = 500;
120 	static double RAMMB = 0;
121 	if (RAMMB != 0)
122 		return RAMMB;
123 
124 	int fd = open("/proc/meminfo", O_RDONLY);
125 	if (-1 == fd)
126 		{
127 		static bool Warned = false;
128 		if (!Warned)
129 			{
130 			Warned = true;
131 			Warning("*Warning* Cannot open /proc/meminfo errno=%d %s",
132 			  errno, strerror(errno));
133 			}
134 		return DEFAULT_RAM;
135 		}
136 	char Buffer[1024];
137 	int n = read(fd, Buffer, sizeof(Buffer) - 1);
138 	close(fd);
139 	fd = -1;
140 
141 	if (n <= 0)
142 		{
143 		static bool Warned = false;
144 		if (!Warned)
145 			{
146 			Warned = true;
147 			Warning("*Warning* Cannot read /proc/meminfo errno=%d %s",
148 			  errno, strerror(errno));
149 			}
150 		return DEFAULT_RAM;
151 		}
152 	Buffer[n] = 0;
153 	char *pMem = strstr(Buffer, "MemTotal: ");
154 	if (0 == pMem)
155 		{
156 		static bool Warned = false;
157 		if (!Warned)
158 			{
159 			Warned = true;
160 			Warning("*Warning* 'MemTotal:' not found in /proc/meminfo");
161 			}
162 		return DEFAULT_RAM;
163 		}
164 	int Bytes = atoi(pMem+9)*1000;
165 	return ((double) Bytes)/1e6;
166 	}
167 
168 #endif	// !WIN32
169