OR-Tools  8.0
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(_MSC_VER) // WINDOWS
21 // clang-format off
22 #include <windows.h>
23 #include <psapi.h>
24 // clang-format on
25 #endif
26 
27 #include <cstdio>
28 
29 #include "ortools/base/sysinfo.h"
30 
31 namespace operations_research {
32 // GetProcessMemoryUsage
33 
34 #if defined(__APPLE__) && defined(__GNUC__) // Mac OS X
36  task_t task = MACH_PORT_NULL;
37  struct task_basic_info t_info;
38  mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
39 
40  if (KERN_SUCCESS != task_info(mach_task_self(), TASK_BASIC_INFO,
41  (task_info_t)&t_info, &t_info_count)) {
42  return -1;
43  }
44  int64 resident_memory = t_info.resident_size;
45  return resident_memory;
46 }
47 #elif defined(__GNUC__) // LINUX
49  unsigned size = 0;
50  char buf[30];
51  snprintf(buf, sizeof(buf), "/proc/%u/statm", (unsigned)getpid());
52  FILE* const pf = fopen(buf, "r");
53  if (pf) {
54  if (fscanf(pf, "%u", &size) != 1) return 0;
55  }
56  fclose(pf);
57  return size * GG_LONGLONG(1024);
58 }
59 #elif defined(_MSC_VER) // WINDOWS
61  HANDLE hProcess;
62  PROCESS_MEMORY_COUNTERS pmc;
63  hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE,
64  GetCurrentProcessId());
65  int64 memory = 0;
66  if (hProcess) {
67  if (GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc))) {
68  memory = pmc.WorkingSetSize;
69  }
70  CloseHandle(hProcess);
71  }
72  return memory;
73 }
74 #else // Unknown, returning 0.
76 #endif
77 
78 } // namespace operations_research
operations_research
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
Definition: dense_doubly_linked_list.h:21
int64
int64_t int64
Definition: integral_types.h:34
GG_LONGLONG
#define GG_LONGLONG(x)
Definition: integral_types.h:46
operations_research::GetProcessMemoryUsage
int64 GetProcessMemoryUsage()
Definition: sysinfo.cc:75
sysinfo.h