Peano
Loading...
Searching...
No Matches
tarch.cpp
Go to the documentation of this file.
1#include "tarch.h"
2
3#include "config.h"
4#include "tarch/logging/Log.h"
6
7#include <limits.h>
8#include <iostream>
9
10#if defined(CompilerHasProcStat)
11#include <unistd.h>
12#endif
13
14#include <cstdlib>
15#include <cstring>
16#if defined(CompilerHasSysinfo) // for Linux OS
17#include <sys/sysinfo.h>
18#else // for Mac OS support
19#include <sys/sysctl.h>
20#include <sys/resource.h>
21#endif
22
23static_assert(sizeof(void*) == 8, "Assuming 64-bit build; i.e., 8-byte pointers");
24static_assert(std::numeric_limits<float>::is_iec559, "std::numeric_limits<float>::is_iec559");
25static_assert(std::numeric_limits<double>::is_iec559, "std::numeric_limits<double>::is_iec55");
26static_assert(CHAR_BIT == 8, "CHAR_BIT != 8");
27static_assert(sizeof(char) == 1, "sizeof(short) != 1");
28static_assert(sizeof(short) == 2, "sizeof(short) != 2");
29static_assert(sizeof(int) == 4, "sizeof(int) != 4");
30static_assert(sizeof(long long) == 8, "sizeof(long long) != 8");
31static_assert('A' == 65, "A != 65");
32
33namespace {
34 int convertMemorySize(long value, tarch::MemoryUsageFormat format) {
35 static tarch::logging::Log _log( "tarch" );
36 int result = 0;
37 switch (format) {
39 {
40 long megaByte = 1024 * 1024;
41 long usageMB ((value + (megaByte/2)) / megaByte );
42 const long maxInteger = std::numeric_limits<int>::max();
43 if (usageMB>maxInteger) {
44 logError( "convertMemorySize()", "cannot cast result to return value: " << usageMB );
45 }
46 result = static_cast<int>(usageMB);
47 }
48 }
49 return result;
50 }
51}
52
53#if defined(CompilerHasSysinfo)
54
55int tarch::getTotalMemory(MemoryUsageFormat format) {
56 struct sysinfo info;
57 sysinfo( &info );
58
59 return convertMemorySize(info.totalram,format);
60}
61
62
63int tarch::getFreeMemory(MemoryUsageFormat format) {
64 struct sysinfo info;
65 sysinfo( &info );
66
67 return convertMemorySize(info.freeram,format);
68}
69
70#else
71
73 int mib[] = { CTL_HW, HW_MEMSIZE };
74 int64_t physical_memory;
75 size_t length;
76
77 // Get the physical memory size
78 length = sizeof(int64_t);
79 sysctl(mib, 2, &physical_memory, &length, NULL, 0);
80 return convertMemorySize(physical_memory, format);
81}
82
84 struct rusage usage{};
85 if(0 == getrusage(RUSAGE_SELF, &usage)) {
86 long freeMem = tarch::getTotalMemory( tarch::MemoryUsageFormat::MByte ) - usage.ru_maxrss;
87 return convertMemorySize(freeMem, format);
88 }
89 else
90 return 0;
91}
92
93#endif
94
96 static tarch::logging::Log _log( "tarch" );
97
98#if defined(CompilerHasProcStat)
99 char work[4096];
100 FILE* f;
101 char* pCh;
102
103 pid_t pid = getpid();
104
105 sprintf(work, "/proc/%d/stat", (int)pid);
106 f = fopen(work, "r");
107
108 if (f == NULL) {
109 logError("getMemoryUsage()", "can't open file " << work );
110 return(0);
111 }
112 if(fgets(work, sizeof(work), f) == NULL) {
113 logError("getMemoryUsage","Error while reading from file");
114 }
115 fclose(f);
116 strtok(work, " ");
117
118 for (int i = 1; i < 23; i++) {
119 pCh = strtok(NULL, " ");
120 }
121
122 std::size_t rawInput = atol(pCh);
123#else
124 std::size_t rawInput = 0;
125#endif
126
127 return convertMemorySize(rawInput,format);
128}
double f(double p4, double p1, double p5, double rho1, double rho5, double gamma)
#define logError(methodName, logMacroMessageStream)
Wrapper macro around tarch::tarch::logging::Log to improve logging.
Definition Log.h:464
Log Device.
Definition Log.h:516
tarch::logging::Log _log("exahype2::fv")
MemoryUsageFormat
Definition tarch.h:35
int getTotalMemory(MemoryUsageFormat format)
Definition tarch.cpp:72
int getMemoryUsage(MemoryUsageFormat format)
Method for getting the application's memory footprint.
Definition tarch.cpp:95
int getFreeMemory(MemoryUsageFormat format)
Definition tarch.cpp:83