Files
ortools-clone/ortools/base/sysinfo.cc
Corentin Le Molgat c34026b101 Bump copyright to 2025
note: done using
```sh
git grep -l "2010-2024 Google" | xargs sed -i 's/2010-2024 Google/2010-2025 Google/'
```
2025-01-10 11:33:35 +01:00

95 lines
3.0 KiB
C++

// Copyright 2010-2025 Google LLC
// 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.
#if defined(__GNUC__) && defined(__linux__)
#include <unistd.h>
#endif
#if defined(__APPLE__) && defined(__GNUC__) // MacOS
#include <mach/mach_init.h>
#include <mach/task.h>
#elif (defined(__FreeBSD__) || defined(__NetBSD__) || \
defined(__OpenBSD__)) // [Free,Net,Open]BSD
#include <sys/resource.h>
#include <sys/time.h>
// Windows
#elif defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
// clang-format off
#include <windows.h>
#include <psapi.h>
// clang-format on
#endif
#include <cstdio>
#include "ortools/base/sysinfo.h"
namespace operations_research {
// GetProcessMemoryUsage
#if defined(__APPLE__) && defined(__GNUC__) // MacOS
int64_t 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_t resident_memory = t_info.resident_size;
return resident_memory;
}
#elif defined(__GNUC__) && !defined(__FreeBSD__) && !defined(__NetBSD__) && \
!defined(__OpenBSD__) && !defined(__EMSCRIPTEN__) && \
!defined(_WIN32) // Linux
int64_t GetProcessMemoryUsage() {
unsigned size = 0;
char buf[30];
snprintf(buf, sizeof(buf), "/proc/%u/statm", (unsigned)getpid());
FILE* const pf = fopen(buf, "r");
if (pf) {
if (fscanf(pf, "%u", &size) != 1) return 0;
}
fclose(pf);
return int64_t{1024} * size;
}
#elif (defined(__FreeBSD__) || defined(__NetBSD__) || \
defined(__OpenBSD__)) // [Free,Net,Open]BSD
int64_t GetProcessMemoryUsage() {
int who = RUSAGE_SELF;
struct rusage rusage;
getrusage(who, &rusage);
return (int64_t)(int64_t{1024} * rusage.ru_maxrss);
}
#elif defined(_MSC_VER) || defined(__MINGW32__) || \
defined(__MINGW64__) // Windows
int64_t GetProcessMemoryUsage() {
HANDLE hProcess;
PROCESS_MEMORY_COUNTERS pmc;
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE,
GetCurrentProcessId());
int64_t memory = 0;
if (hProcess) {
if (GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc))) {
memory = pmc.WorkingSetSize;
}
CloseHandle(hProcess);
}
return memory;
}
#else // Unknown, returning 0.
int64_t GetProcessMemoryUsage() { return 0; }
#endif
} // namespace operations_research