Google OR-Tools v9.9
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
sysinfo.cc
Go to the documentation of this file.
1// Copyright 2010-2024 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14#if defined(__GNUC__) && defined(__linux__)
15#include <unistd.h>
16#endif
17#if defined(__APPLE__) && defined(__GNUC__) // MacOS
18#include <mach/mach_init.h>
19#include <mach/task.h>
20#elif defined(__FreeBSD__) // FreeBSD
21#include <sys/resource.h>
22#include <sys/time.h>
23// Windows
24#elif defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
25// clang-format off
26#include <windows.h>
27#include <psapi.h>
28// clang-format on
29#endif
30
31#include <cstdio>
32
34
35namespace operations_research {
36// GetProcessMemoryUsage
37
38#if defined(__APPLE__) && defined(__GNUC__) // MacOS
39int64_t GetProcessMemoryUsage() {
40 task_t task = MACH_PORT_NULL;
41 struct task_basic_info t_info;
42 mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
43
44 if (KERN_SUCCESS != task_info(mach_task_self(), TASK_BASIC_INFO,
45 (task_info_t)&t_info, &t_info_count)) {
46 return -1;
47 }
48 int64_t resident_memory = t_info.resident_size;
49 return resident_memory;
50}
51#elif defined(__GNUC__) && !defined(__FreeBSD__) && \
52 !defined(__EMSCRIPTEN__) && !defined(_WIN32) // Linux
53int64_t GetProcessMemoryUsage() {
54 unsigned size = 0;
55 char buf[30];
56 snprintf(buf, sizeof(buf), "/proc/%u/statm", (unsigned)getpid());
57 FILE* const pf = fopen(buf, "r");
58 if (pf) {
59 if (fscanf(pf, "%u", &size) != 1) return 0;
60 }
61 fclose(pf);
62 return int64_t{1024} * size;
63}
64#elif defined(__FreeBSD__) // FreeBSD
65int64_t GetProcessMemoryUsage() {
66 int who = RUSAGE_SELF;
67 struct rusage rusage;
68 getrusage(who, &rusage);
69 return (int64_t)(int64_t{1024} * rusage.ru_maxrss);
70}
71// Windows
72#elif defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
73int64_t GetProcessMemoryUsage() {
74 HANDLE hProcess;
75 PROCESS_MEMORY_COUNTERS pmc;
76 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE,
77 GetCurrentProcessId());
78 int64_t memory = 0;
79 if (hProcess) {
80 if (GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc))) {
81 memory = pmc.WorkingSetSize;
82 }
83 CloseHandle(hProcess);
84 }
85 return memory;
86}
87#else // Unknown, returning 0.
88int64_t GetProcessMemoryUsage() { return 0; }
89#endif
90
91} // namespace operations_research
In SWIG mode, we don't want anything besides these top-level includes.
int64_t GetProcessMemoryUsage()
GetProcessMemoryUsage.
Definition sysinfo.cc:88