78 lines
2.2 KiB
C++
78 lines
2.2 KiB
C++
// Copyright 2010 Google
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
#include "base/util.h"
|
|
#include "base/stringpiece.h"
|
|
|
|
namespace operations_research {
|
|
// GetProcessMemoryUsage
|
|
|
|
#if defined(__APPLE__) && defined(__GNUC__)
|
|
#include <mach/mach_init.h>
|
|
#include <mach/task.h>
|
|
|
|
int64 GetProcessMemoryUsage () {
|
|
task_t task = MACH_PORT_NULL;
|
|
struct task_basic_info t_info;
|
|
mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
|
|
|
|
if (KERN_SUCCESS != task_info(mach_task_self(),
|
|
TASK_BASIC_INFO,
|
|
(task_info_t)&t_info,
|
|
&t_info_count)) {
|
|
return -1;
|
|
}
|
|
int64 resident_memory = t_info.resident_size;
|
|
return resident_memory;
|
|
}
|
|
#elif defined(__GNUC__) // LINUX
|
|
int64 GetProcessMemoryUsage() {
|
|
unsigned size = 0;
|
|
int result;
|
|
char buf[30];
|
|
snprintf(buf, 30, "/proc/%u/statm", (unsigned)getpid());
|
|
FILE* pf = fopen(buf, "r");
|
|
if (pf) {
|
|
result = fscanf(pf, "%u", &size);
|
|
}
|
|
fclose(pf);
|
|
return size * GG_LONGLONG(1024);
|
|
}
|
|
#elif defined(_MSC_VER) // WINDOWS
|
|
#include <windows.h>
|
|
#include <stdio.h>
|
|
#include <psapi.h>
|
|
|
|
int64 GetProcessMemoryUsage() {
|
|
HANDLE hProcess;
|
|
PROCESS_MEMORY_COUNTERS pmc;
|
|
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
|
|
FALSE,
|
|
GetCurrentProcessId());
|
|
int64 memory = 0;
|
|
if (hProcess) {
|
|
if (GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc))) {
|
|
memory = pmc.WorkingSetSize;
|
|
}
|
|
CloseHandle(hProcess);
|
|
}
|
|
return memory;
|
|
}
|
|
#else // Unknown, returning 0.
|
|
int64 GetProcessMemoryUsage() {
|
|
return 0;
|
|
}
|
|
#endif
|
|
|
|
} // namespace operations_research
|