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