|
| 1 | +/* |
| 2 | + * Author: David Robert Nadeau |
| 3 | + * Site: http://NadeauSoftware.com/ |
| 4 | + * License: Creative Commons Attribution 3.0 Unported License |
| 5 | + * http://creativecommons.org/licenses/by/3.0/deed.en_US |
| 6 | + */ |
| 7 | + |
| 8 | +#if defined(_WIN32) |
| 9 | +# include <windows.h> |
| 10 | +# include <psapi.h> |
| 11 | + |
| 12 | +#elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__)) |
| 13 | +# include <unistd.h> |
| 14 | +# include <sys/resource.h> |
| 15 | + |
| 16 | +# if defined(__APPLE__) && defined(__MACH__) |
| 17 | +# include <mach/mach.h> |
| 18 | + |
| 19 | +# elif (defined(_AIX) || defined(__TOS__AIX__)) || (defined(__sun__) || defined(__sun) || defined(sun) && (defined(__SVR4) || defined(__svr4__))) |
| 20 | +# include <fcntl.h> |
| 21 | +# include <procfs.h> |
| 22 | + |
| 23 | +# elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__) |
| 24 | +# include <stdio.h> |
| 25 | + |
| 26 | +# endif |
| 27 | + |
| 28 | +#else |
| 29 | +# error "Cannot define getPeakRSS( ) or getCurrentRSS( ) for an unknown OS." |
| 30 | +#endif |
| 31 | + |
| 32 | + |
| 33 | +/** |
| 34 | + * Returns the peak (maximum so far) resident set size (physical |
| 35 | + * memory use) measured in bytes, or zero if the value cannot be |
| 36 | + * determined on this OS. |
| 37 | + */ |
| 38 | +size_t getPeakRSS() |
| 39 | +{ |
| 40 | +#if defined(_WIN32) |
| 41 | + /* Windows -------------------------------------------------- */ |
| 42 | + PROCESS_MEMORY_COUNTERS info; |
| 43 | + GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info)); |
| 44 | + return (size_t)info.PeakWorkingSetSize; |
| 45 | + |
| 46 | +#elif (defined(_AIX) || defined(__TOS__AIX__)) || (defined(__sun__) || defined(__sun) || defined(sun) && (defined(__SVR4) || defined(__svr4__))) |
| 47 | + /* AIX and Solaris ------------------------------------------ */ |
| 48 | + struct psinfo psinfo; |
| 49 | + int fd = -1; |
| 50 | + if ((fd = open("/proc/self/psinfo", O_RDONLY)) == -1) |
| 51 | + return (size_t)0L; /* Can't open? */ |
| 52 | + if (read(fd, &psinfo, sizeof(psinfo)) != sizeof(psinfo)) |
| 53 | + { |
| 54 | + close(fd); |
| 55 | + return (size_t)0L; /* Can't read? */ |
| 56 | + } |
| 57 | + close(fd); |
| 58 | + return (size_t)(psinfo.pr_rssize * 1024L); |
| 59 | + |
| 60 | +#elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__)) |
| 61 | + /* BSD, Linux, and OSX -------------------------------------- */ |
| 62 | + struct rusage rusage; |
| 63 | + getrusage(RUSAGE_SELF, &rusage); |
| 64 | +# if defined(__APPLE__) && defined(__MACH__) |
| 65 | + return (size_t)rusage.ru_maxrss; |
| 66 | +# else |
| 67 | + return (size_t)(rusage.ru_maxrss * 1024L); |
| 68 | +# endif |
| 69 | + |
| 70 | +#else |
| 71 | + /* Unknown OS ----------------------------------------------- */ |
| 72 | + return (size_t)0L; /* Unsupported. */ |
| 73 | +#endif |
| 74 | +} |
| 75 | + |
| 76 | + |
| 77 | +/** |
| 78 | + * Returns the current resident set size (physical memory use) measured |
| 79 | + * in bytes, or zero if the value cannot be determined on this OS. |
| 80 | + */ |
| 81 | +size_t getCurrentRSS() |
| 82 | +{ |
| 83 | +#if defined(_WIN32) |
| 84 | + /* Windows -------------------------------------------------- */ |
| 85 | + PROCESS_MEMORY_COUNTERS info; |
| 86 | + GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info)); |
| 87 | + return (size_t)info.WorkingSetSize; |
| 88 | + |
| 89 | +#elif defined(__APPLE__) && defined(__MACH__) |
| 90 | + /* OSX ------------------------------------------------------ */ |
| 91 | + struct mach_task_basic_info info; |
| 92 | + mach_msg_type_number_t infoCount = MACH_TASK_BASIC_INFO_COUNT; |
| 93 | + if (task_info(mach_task_self(), MACH_TASK_BASIC_INFO, |
| 94 | + (task_info_t)&info, &infoCount) != KERN_SUCCESS) |
| 95 | + return (size_t)0L; /* Can't access? */ |
| 96 | + return (size_t)info.resident_size; |
| 97 | + |
| 98 | +#elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__) |
| 99 | + /* Linux ---------------------------------------------------- */ |
| 100 | + long rss = 0L; |
| 101 | + FILE* fp = NULL; |
| 102 | + if ((fp = fopen("/proc/self/statm", "r")) == NULL) |
| 103 | + return (size_t)0L; /* Can't open? */ |
| 104 | + if (fscanf(fp, "%*s%ld", &rss) != 1) |
| 105 | + { |
| 106 | + fclose(fp); |
| 107 | + return (size_t)0L; /* Can't read? */ |
| 108 | + } |
| 109 | + fclose(fp); |
| 110 | + return (size_t)rss * (size_t)sysconf(_SC_PAGESIZE); |
| 111 | + |
| 112 | +#else |
| 113 | + /* AIX, BSD, Solaris, and Unknown OS ------------------------ */ |
| 114 | + return (size_t)0L; /* Unsupported. */ |
| 115 | +#endif |
| 116 | +} |
| 117 | + |
| 118 | +#include <Ark/Module.hpp> |
| 119 | + |
| 120 | +// cppcheck-suppress [constParameterReference, constParameterPointer] |
| 121 | +Ark::Value ArkGetCurrentRSS(std::vector<Ark::Value>& n [[maybe_unused]], Ark::VM* vm [[maybe_unused]]) |
| 122 | +{ |
| 123 | + return Ark::Value(static_cast<double>(getCurrentRSS())); |
| 124 | +} |
| 125 | + |
| 126 | +// cppcheck-suppress [constParameterReference, constParameterPointer] |
| 127 | +Ark::Value ArkGetPeakRSS(std::vector<Ark::Value>& n, Ark::VM* vm [[maybe_unused]]) |
| 128 | +{ |
| 129 | + return Ark::Value(static_cast<double>(getPeakRSS())); |
| 130 | +} |
| 131 | + |
| 132 | +ARK_API Ark::mapping* getFunctionsMapping() |
| 133 | +{ |
| 134 | + static Ark::mapping map[] = { |
| 135 | + { "playground:getCurrentRss", ArkGetCurrentRSS}, |
| 136 | + { "playground:getPeakRss", ArkGetPeakRSS}, |
| 137 | + { nullptr, nullptr } |
| 138 | + }; |
| 139 | + |
| 140 | + return map; |
| 141 | +} |
0 commit comments