OR-Tools  8.2
sysinfo.cc
Go to the documentation of this file.
1// Copyright 2010-2018 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__) // Mac OS X
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#elif defined(_MSC_VER) // WINDOWS
24// clang-format off
25#include <windows.h>
26#include <psapi.h>
27// clang-format on
28#endif
29
30#include <cstdio>
31
33
34namespace operations_research {
35// GetProcessMemoryUsage
36
37#if defined(__APPLE__) && defined(__GNUC__) // Mac OS X
39 task_t task = MACH_PORT_NULL;
40 struct task_basic_info t_info;
41 mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
42
43 if (KERN_SUCCESS != task_info(mach_task_self(), TASK_BASIC_INFO,
44 (task_info_t)&t_info, &t_info_count)) {
45 return -1;
46 }
47 int64 resident_memory = t_info.resident_size;
48 return resident_memory;
49}
50#elif defined(__GNUC__) && !defined(__FreeBSD__) // LINUX
52 unsigned size = 0;
53 char buf[30];
54 snprintf(buf, sizeof(buf), "/proc/%u/statm", (unsigned)getpid());
55 FILE* const pf = fopen(buf, "r");
56 if (pf) {
57 if (fscanf(pf, "%u", &size) != 1) return 0;
58 }
59 fclose(pf);
60 return size * int64{1024};
61}
62#elif defined(__FreeBSD__) // FreeBSD
64 int who = RUSAGE_SELF;
65 struct rusage rusage;
66 getrusage(who, &rusage);
67 return (int64)(rusage.ru_maxrss * int64{1024});
68}
69#elif defined(_MSC_VER) // WINDOWS
71 HANDLE hProcess;
72 PROCESS_MEMORY_COUNTERS pmc;
73 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE,
74 GetCurrentProcessId());
75 int64 memory = 0;
76 if (hProcess) {
77 if (GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc))) {
78 memory = pmc.WorkingSetSize;
79 }
80 CloseHandle(hProcess);
81 }
82 return memory;
83}
84#else // Unknown, returning 0.
86#endif
87
88} // namespace operations_research
int64_t int64
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
int64 GetProcessMemoryUsage()
Definition: sysinfo.cc:85