Google OR-Tools v9.12
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-2025 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__) || defined(__NetBSD__) || \
21 defined(__OpenBSD__)) // [Free,Net,Open]BSD
22#include <sys/resource.h>
23#include <sys/time.h>
24// Windows
25#elif defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
26// clang-format off
27#include <windows.h>
28#include <psapi.h>
29// clang-format on
30#endif
31
32#include <cstdio>
33
35
36namespace operations_research {
37// GetProcessMemoryUsage
38
39#if defined(__APPLE__) && defined(__GNUC__) // MacOS
40int64_t GetProcessMemoryUsage() {
41 task_t task = MACH_PORT_NULL;
42 struct task_basic_info t_info;
43 mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
44
45 if (KERN_SUCCESS != task_info(mach_task_self(), TASK_BASIC_INFO,
46 (task_info_t)&t_info, &t_info_count)) {
47 return -1;
48 }
49 int64_t resident_memory = t_info.resident_size;
50 return resident_memory;
51}
52#elif defined(__GNUC__) && !defined(__FreeBSD__) && !defined(__NetBSD__) && \
53 !defined(__OpenBSD__) && !defined(__EMSCRIPTEN__) && \
54 !defined(_WIN32) // Linux
55int64_t GetProcessMemoryUsage() {
56 unsigned size = 0;
57 char buf[30];
58 snprintf(buf, sizeof(buf), "/proc/%u/statm", (unsigned)getpid());
59 FILE* const pf = fopen(buf, "r");
60 if (pf) {
61 if (fscanf(pf, "%u", &size) != 1) return 0;
62 }
63 fclose(pf);
64 return int64_t{1024} * size;
65}
66#elif (defined(__FreeBSD__) || defined(__NetBSD__) || \
67 defined(__OpenBSD__)) // [Free,Net,Open]BSD
68int64_t GetProcessMemoryUsage() {
69 int who = RUSAGE_SELF;
70 struct rusage rusage;
71 getrusage(who, &rusage);
72 return (int64_t)(int64_t{1024} * rusage.ru_maxrss);
73}
74#elif defined(_MSC_VER) || defined(__MINGW32__) || \
75 defined(__MINGW64__) // Windows
76int64_t GetProcessMemoryUsage() {
77 HANDLE hProcess;
78 PROCESS_MEMORY_COUNTERS pmc;
79 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE,
80 GetCurrentProcessId());
81 int64_t memory = 0;
82 if (hProcess) {
83 if (GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc))) {
84 memory = pmc.WorkingSetSize;
85 }
86 CloseHandle(hProcess);
87 }
88 return memory;
89}
90#else // Unknown, returning 0.
91int64_t GetProcessMemoryUsage() { return 0; }
92#endif
93
94} // namespace operations_research
In SWIG mode, we don't want anything besides these top-level includes.
int64_t GetProcessMemoryUsage()
GetProcessMemoryUsage.
Definition sysinfo.cc:91