00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #if defined(__APPLE__) && defined(__GNUC__) // Mac OS X
00015 #include <mach/mach_init.h>
00016 #include <mach/task.h>
00017 #elif defined(_MSC_VER) // WINDOWS
00018 #include <windows.h>
00019 #include <psapi.h>
00020 #endif
00021
00022 #include <cstdio>
00023
00024 #include "base/sysinfo.h"
00025 #include "base/stringpiece.h"
00026
00027 namespace operations_research {
00028
00029
00030 #if defined(__APPLE__) && defined(__GNUC__) // Mac OS X
00031 int64 GetProcessMemoryUsage() {
00032 task_t task = MACH_PORT_NULL;
00033 struct task_basic_info t_info;
00034 mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
00035
00036 if (KERN_SUCCESS != task_info(mach_task_self(),
00037 TASK_BASIC_INFO,
00038 (task_info_t)&t_info,
00039 &t_info_count)) {
00040 return -1;
00041 }
00042 int64 resident_memory = t_info.resident_size;
00043 return resident_memory;
00044 }
00045 #elif defined(__GNUC__) // LINUX
00046 int64 GetProcessMemoryUsage() {
00047 unsigned size = 0;
00048 int result;
00049 char buf[30];
00050 snprintf(buf, sizeof(buf), "/proc/%u/statm", (unsigned)getpid());
00051 FILE* const pf = fopen(buf, "r");
00052 if (pf) {
00053 result = fscanf(pf, "%u", &size);
00054 }
00055 fclose(pf);
00056 return size * GG_LONGLONG(1024);
00057 }
00058 #elif defined(_MSC_VER) // WINDOWS
00059 int64 GetProcessMemoryUsage() {
00060 HANDLE hProcess;
00061 PROCESS_MEMORY_COUNTERS pmc;
00062 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
00063 FALSE,
00064 GetCurrentProcessId());
00065 int64 memory = 0;
00066 if (hProcess) {
00067 if (GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc))) {
00068 memory = pmc.WorkingSetSize;
00069 }
00070 CloseHandle(hProcess);
00071 }
00072 return memory;
00073 }
00074 #else // Unknown, returning 0.
00075 int64 GetProcessMemoryUsage() {
00076 return 0;
00077 }
00078 #endif
00079
00080 }