forked from alexander-nadel/intel_sat_solver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicMemoryUsage.h
69 lines (60 loc) · 1.21 KB
/
BasicMemoryUsage.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright(C) 2021 Intel Corporation
// SPDX - License - Identifier: MIT
#pragma once
#include <iostream>
#include <iomanip>
#ifdef _WIN32
#include <windows.h>
#include <psapi.h>
#else
#include <unistd.h>
#include <sys/resource.h>
#include <stdio.h>
#endif
namespace BasicMemUsage
{
static size_t GetPeakRSS()
{
#ifdef _WIN32
PROCESS_MEMORY_COUNTERS info;
GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info));
return (size_t)info.PeakWorkingSetSize;
#else
struct rusage rusage;
getrusage(RUSAGE_SELF, &rusage);
return (size_t)(rusage.ru_maxrss * 1024L);
#endif
}
static size_t GetPeakRSSMb()
{
return GetPeakRSS() >> (size_t)20;
}
static size_t GetCurrentRSS()
{
#ifdef _WIN32
PROCESS_MEMORY_COUNTERS info;
GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info));
return (size_t)info.WorkingSetSize;
#else
long rss = 0L;
FILE* fp = NULL;
if ((fp = fopen("/proc/self/statm", "r")) == NULL)
{
// Can't open
return 0;
}
if (fscanf(fp, "%*s%ld", &rss) != 1)
{
fclose(fp);
// Can't read
return 0;
}
fclose(fp);
return (size_t)rss * (size_t)sysconf(_SC_PAGESIZE);
#endif
}
static size_t GetCurrentRSSMb()
{
return GetCurrentRSS() >> (size_t)20;
}
};