-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathUtils.cpp
More file actions
422 lines (396 loc) · 11 KB
/
Copy pathUtils.cpp
File metadata and controls
422 lines (396 loc) · 11 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
This file is part of the Util library.
Copyright (C) 2007-2013 Benjamin Eikel <benjamin@eikel.org>
Copyright (C) 2007-2012 Claudius Jähn <claudius@uni-paderborn.de>
Copyright (C) 2007-2012 Ralf Petring <ralf@petring.net>
This library is subject to the terms of the Mozilla Public License, v. 2.0.
You should have received a copy of the MPL along with this library; see the
file LICENSE. If not, you can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "Utils.h"
#include "Macros.h"
#include "MicroXML.h"
#include "StringUtils.h"
#include <iomanip>
#include <iostream>
#include <cstdio>
#if defined(_WIN32) || defined(_WIN64)
#ifndef WINVER
#define WINVER 0x0501
#endif
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0501
#endif
#include <windows.h>
#include <psapi.h>
#elif defined(__linux__) || defined(__unix__) || defined(ANDROID)
#include <fstream>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#elif defined(__APPLE__)
#include <mach-o/dyld.h>
#include <unistd.h>
#endif
#include <algorithm>
#include <array>
#include <cstdint>
#include <ctime>
#include <deque>
#include <numeric>
#include <sstream>
#include <utility>
#include <cstring>
#ifdef UTIL_HAVE_EXECINFO_H
#include <execinfo.h>
#endif
#ifdef UTIL_HAVE_MALLOC_H
#include <malloc.h>
#endif
namespace Util {
namespace Utils {
uint64_t getResidentSetMemorySize() {
#if defined(_WIN32) || defined(_WIN64)
HANDLE hProcess = GetCurrentProcess();
PROCESS_MEMORY_COUNTERS pmc;
GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)); // Needs psapi-library!
return pmc.WorkingSetSize;
#elif defined(__linux__) || defined(__unix__) || defined(ANDROID)
try {
uint64_t sizeNumPages, residentNumPages;
const long pageSize = sysconf(_SC_PAGESIZE);
std::ifstream fs("/proc/self/statm");
fs >> std::skipws >> sizeNumPages >> residentNumPages;
return residentNumPages * pageSize;
} catch (...) {
return 0;
}
#else
return 0;
#endif
}
uint64_t getVirtualMemorySize() {
#if defined(_WIN32) || defined(_WIN64)
HANDLE hProcess = GetCurrentProcess();
PROCESS_MEMORY_COUNTERS pmc;
GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)); // Needs psapi-library!
return pmc.PagefileUsage;
#elif defined(__linux__) || defined(__unix__) || defined(ANDROID)
try {
uint64_t sizeNumPages;
const long pageSize = sysconf(_SC_PAGESIZE);
std::ifstream fs("/proc/self/statm");
fs >> std::skipws >> sizeNumPages;
return sizeNumPages * pageSize;
} catch (...) {
return 0;
}
#else
return 0;
#endif
}
uint64_t getAllocatedMemorySize() {
#if defined(UTIL_HAVE_MALLOC_H) && defined(UTIL_HAVE_MALLOC_INFO) && defined(UTIL_HAVE_OPEN_MEMSTREAM)
char * ptr;
uint64_t size;
auto memStream = open_memstream(&ptr, &size);
if(memStream == nullptr) {
WARN("open_memstream failed.");
return 0;
}
malloc_info(0, memStream);
fclose(memStream);
std::istringstream stream(std::string(ptr, size));
free(ptr);
uint64_t allocatedSize = 0;
bool insideHeapTag = false;
MicroXML::Reader::traverse(
stream,
[&insideHeapTag, &allocatedSize](const std::string & tagName,
const Util::MicroXML::attributes_t & attributes) {
if(tagName == "malloc") {
const auto versionIt = attributes.find("version");
if(versionIt == attributes.cend()) {
WARN("No malloc_info version found.");
return false;
} else if(versionIt->second != "1") {
WARN("Unsupported malloc_info version found.");
return false;
}
} else if(tagName == "heap") {
if(insideHeapTag) {
WARN("Nested heap tags found in malloc_info.");
return false;
}
insideHeapTag = true;
} else if(!insideHeapTag && tagName == "system") {
const auto typeIt = attributes.find("type");
if(typeIt == attributes.cend()) {
WARN("No attribute type in tag system found in malloc_info.");
return false;
}
if(typeIt->second == "current") {
const auto sizeIt = attributes.find("size");
if(typeIt == attributes.cend()) {
WARN("No attribute size in tag system found in malloc_info.");
return false;
}
allocatedSize = Util::StringUtils::toNumber<uint64_t>(sizeIt->second);
return false;
}
}
return true;
},
[&insideHeapTag](const std::string & tagName) {
if(tagName == "heap") {
if(!insideHeapTag) {
WARN("Closing without opening heap tag found in malloc_info.");
return false;
}
insideHeapTag = false;
}
return true;
},
[](const std::string &, const std::string &) {
return true;
}
);
return allocatedSize;
#elif defined(UTIL_HAVE_MALLOC_H) && defined(UTIL_HAVE_MALLINFO)
const auto mallocInfo = mallinfo();
return static_cast<uint64_t>(mallocInfo.hblkhd) + static_cast<uint64_t>(mallocInfo.uordblks);
#else
return 0;
#endif
}
void outputProcessMemory() {
const double Mebi = 1024.0 * 1024.0;
std::cout << std::fixed << std::setprecision(3);
std::cout << "Memory:\tVirtual memory size =\t" << std::setw(8) << getVirtualMemorySize() / Mebi << " MiBytes\n";
std::cout << "Memory:\tResident set size = \t" << std::setw(8) << getResidentSetMemorySize() / Mebi << " MiBytes\n";
std::cout << "Memory:\tAllocated memory size = \t" << std::setw(8) << getAllocatedMemorySize() / Mebi << " MiBytes" << std::endl;
}
uint64_t getIOBytesRead() {
#if defined(_WIN32) || defined(_WIN64)
HANDLE hProcess = GetCurrentProcess();
IO_COUNTERS pioc;
GetProcessIoCounters(hProcess, &pioc);
return pioc.ReadTransferCount;
#elif defined(__linux__) || defined(__unix__) || defined(ANDROID)
try {
std::ifstream fs("/proc/self/io");
char buffer[256];
while(fs.good()) {
fs.getline(buffer, 256, ':');
if(std::string(buffer) == "rchar") {
uint64_t bytesRead;
fs >> std::skipws >> bytesRead;
return bytesRead;
}
fs.ignore(256, '\n');
}
return 0;
} catch (...) {
return 0;
}
#else
return 0;
#endif
}
uint64_t getIOBytesWritten() {
#if defined(_WIN32) || defined(_WIN64)
HANDLE hProcess = GetCurrentProcess();
IO_COUNTERS pioc;
GetProcessIoCounters(hProcess, &pioc);
return pioc.WriteTransferCount;
#elif defined(__linux__) || defined(__unix__) || defined(ANDROID)
try {
std::ifstream fs("/proc/self/io");
char buffer[256];
while(fs.good()) {
fs.getline(buffer, 256, ':');
if(std::string(buffer) == "wchar") {
uint64_t bytesRead;
fs >> std::skipws >> bytesRead;
return bytesRead;
}
fs.ignore(256, '\n');
}
return 0;
} catch (...) {
return 0;
}
#else
return 0;
#endif
}
void outputProcessIO() {
const float Mebi = 1024.0f * 1024.0f;
const float bytesRead = static_cast<float>(getIOBytesRead()) / Mebi;
const float bytesWritten = static_cast<float>(getIOBytesWritten()) / Mebi;
std::cout << std::fixed << std::setprecision(3);
std::cout << "IO:\t\tRead =\t" << std::setw(8) << bytesRead << " MiBytes\n";
std::cout << "IO:\t\tWrite = \t" << std::setw(8) << bytesWritten << " MiBytes" << std::endl;
}
void sleep(unsigned long int ms) {
#if defined(_WIN32) || defined(_WIN64)
Sleep(ms);
#elif defined(__USE_XOPEN2K) && defined(_POSIX_MONOTONIC_CLOCK)
timespec t;
const unsigned long int numSeconds = ms / 1000ul;
ms -= numSeconds * 1000ul;
t.tv_sec = numSeconds;
t.tv_nsec = 1000000ul * ms;
clock_nanosleep(CLOCK_MONOTONIC, 0, &t, nullptr);
#elif defined(ANDROID)
timespec t;
const unsigned long int numSeconds = ms / 1000ul;
ms -= numSeconds * 1000ul;
t.tv_sec = numSeconds;
t.tv_nsec = 1000000ul * ms;
nanosleep(&t, nullptr);
#elif defined(__USE_XOPEN_EXTENDED) || defined(__APPLE__) || defined(__unix__)
usleep(1000ul * ms);
#else
#error "Not implemented for your system.";
#endif
}
int32_t getProcessId() {
#if defined(_WIN32) || defined(_WIN64)
const DWORD pid = GetCurrentProcessId();
return static_cast<int32_t>(pid);
#elif defined(__linux__) || defined(__unix__) || defined(ANDROID) || defined(__APPLE__)
const pid_t pid = getpid();
return static_cast<int32_t>(pid);
#else
WARN("Not implemented for your system.");
return -1;
#endif
}
std::string getExecutablePath() {
#if defined(_WIN32) || defined(_WIN64)
std::string buffer(MAX_PATH, '\0');
const auto numChars = GetModuleFileName(nullptr, &buffer.front(), static_cast<DWORD>(buffer.size()));
if(numChars == 0) {
WARN("GetModuleFileName failed.");
return std::string();
}
buffer.resize(numChars);
return buffer;
#elif defined(__linux__)
std::string buffer(2048, '\0');
const auto numChars = readlink("/proc/self/exe",
&buffer.front(),
buffer.size());
if(numChars == -1) {
WARN("readlink failed.");
return std::string();
}
buffer.resize(numChars);
return buffer;
#elif defined(__APPLE__)
uint32_t bufferSize = 0;
_NSGetExecutablePath(nullptr, &bufferSize);
std::string buffer(bufferSize, '\0');
const auto result = _NSGetExecutablePath(&buffer.front(), &bufferSize);
if(result != 0) {
WARN("_NSGetExecutablePath failed.");
return std::string();
}
return buffer;
#else
WARN("Not implemented for your system.");
return std::string();
#endif
}
#if defined(__linux__) || defined(__unix__)
/**
* Read CPU data from "/proc/stat" and return the overall sum and the sum of
* the first three values (user, nice, system).
*/
static std::pair<uint32_t, uint32_t> getCPUWorkAndOverall() {
std::ifstream fs("/proc/stat");
while(fs.good()) {
std::string prefix;
fs >> prefix;
if(prefix == "cpu") {
std::deque<double> values;
while(fs.peek() != '\n') {
uint32_t value;
fs >> value;
values.push_back(value);
}
const uint32_t work = values[0] + values[1] + values[2];
const uint32_t sum = std::accumulate(values.begin(), values.end(), 0);
return std::make_pair(work, sum);
}
}
return std::make_pair(0, 0);
}
#endif
double getCPUUsage(unsigned long int timespan) {
#if defined(__linux__) || defined(__unix__)
const auto pairBefore = getCPUWorkAndOverall();
sleep(timespan);
const auto pairAfter = getCPUWorkAndOverall();
const auto workDiff = pairAfter.first - pairBefore.first;
const auto overallDiff = pairAfter.second - pairBefore.second;
return static_cast<double>(workDiff) / static_cast<double>(overallDiff);
#else
WARN("Not implemented for your system.");
return -1.0;
#endif
}
std::vector<std::string> getBacktrace() {
std::vector<std::string> result;
#ifdef UTIL_HAVE_EXECINFO_H
std::array<void *, 128> buffer;
const int num = backtrace(buffer.data(), buffer.size());
result.reserve(static_cast<uint64_t>(num));
char ** strings = backtrace_symbols(buffer.data(), num);
for (int i = 0; i < num; ++i) {
result.emplace_back(strings[i]);
}
free(strings);
#else
WARN("Not implemented for your system.");
#endif
return result;
}
std::string createTimeStamp() {
const auto time = std::time(nullptr);
std::string buffer(64, '\0');
const auto numBytes = std::strftime(&buffer.front(),
buffer.size(),
"%Y-%m-%d_%H-%M-%S",
std::localtime(&time));
buffer.resize(numBytes);
return buffer;
}
}
DebugOutput info;
void enableInfo()
{
info.setStream(&std::cout);
}
void disableInfo()
{
info.setStream(nullptr);
}
//
//void useStdInfo() {
// info=&std::cout;
//}
//static std::ostringstream infoString;
//
//void useInternalInfo() {
// info=&infoString;
//}
//
//std::string getInfo() {
// return infoString.str();
//}
//void clearInfo() {
// infoString.clear();
//}
}