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
|
#include "muscle.h"
#ifndef WIN32
#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 unsigned long nan[2]={0xffffffff, 0x7fffffff};
double dNAN = *( double* )nan;
return dNAN;
}
double g_dNAN = GetNAN();
void chkmem(const char szMsg[])
{
//assert(_CrtCheckMemory());
}
void Break()
{
//DebugBreak();
}
static char szCmdLine[4096];
void *ptrStartBreak = sbrk(0);
const char *GetCmdLine()
{
return szCmdLine;
}
double GetMemUseMB()
{
static char statm[64];
static int PageSize;
if (0 == statm[0])
{
PageSize = sysconf(_SC_PAGESIZE);
pid_t pid = getpid();
sprintf(statm, "/proc/%d/statm", (int) pid);
}
int fd = open(statm, O_RDONLY);
if (-1 == fd)
{
static bool Warned = false;
if (!Warned)
{
Warned = true;
Warning("*Warning* Cannot open %s errno=%d %s",
statm, errno, strerror(errno));
}
return 0;
}
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, errno, strerror(errno));
}
return 0;
}
Buffer[n] = 0;
int Pages = atoi(Buffer);
return ((double) Pages * (double) PageSize)/1e6;
}
void SaveCmdLine(int argc, char *argv[])
{
for (int i = 0; i < argc; ++i)
{
if (i > 0)
strcat(szCmdLine, " ");
strcat(szCmdLine, 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;
}
int Bytes = atoi(pMem+9)*1000;
return ((double) Bytes)/1e6;
}
#endif // !WIN32
|