File size: 1,229 Bytes
684db41 | 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 | #ifndef PERF_HELPERS_H
#define PERF_HELPERS_H
#include <chrono>
#include <cstdio>
#include <cstdlib>
#if defined(__linux__) || defined(__FreeBSD__)
# include <sys/resource.h>
# include <unistd.h>
#endif
#include "kernel/yosys.h"
YOSYS_NAMESPACE_BEGIN
inline int env_int(const char *name, int fallback)
{
const char *value = getenv(name);
if (value == nullptr || *value == 0)
return fallback;
return atoi(value);
}
inline size_t rss_bytes()
{
#if defined(__linux__) || defined(__FreeBSD__)
size_t total_pages = 0, resident_pages = 0;
FILE *f = fopen("/proc/self/statm", "r");
if (f == nullptr)
return 0;
if (fscanf(f, "%zu %zu", &total_pages, &resident_pages) != 2)
resident_pages = 0;
fclose(f);
return resident_pages * (size_t)sysconf(_SC_PAGESIZE);
#else
return 0;
#endif
}
inline size_t peak_rss_bytes()
{
#if defined(__linux__) || defined(__FreeBSD__)
struct rusage ru;
if (getrusage(RUSAGE_SELF, &ru) != 0)
return 0;
return (size_t)ru.ru_maxrss * 1024;
#else
return 0;
#endif
}
inline double mb(size_t bytes)
{
return bytes / 1048576.0;
}
inline double ms(std::chrono::steady_clock::duration d)
{
return std::chrono::duration<double, std::milli>(d).count();
}
YOSYS_NAMESPACE_END
#endif
|