|
| 1 | + |
| 2 | +#include <wsjcpp_core.h> |
| 3 | +#include <wsjcpp_unit_tests.h> |
| 4 | +#include <unistd.h> |
| 5 | +#include <ios> |
| 6 | +#include <iostream> |
| 7 | +#include <fstream> |
| 8 | +#include <string> |
| 9 | +#include <wsjcpp_yaml.h> |
| 10 | + |
| 11 | +////////////////////////////////////////////////////////////////////////////// |
| 12 | +// |
| 13 | +// process_mem_usage(double &, double &) - takes two doubles by reference, |
| 14 | +// attempts to read the system-dependent data for a process' virtual memory |
| 15 | +// size and resident set size, and return the results in KB. |
| 16 | +// |
| 17 | +// On failure, returns 0.0, 0.0 |
| 18 | + |
| 19 | +void process_mem_usage(double& vm_usage, double& resident_set) |
| 20 | +{ |
| 21 | + using std::ios_base; |
| 22 | + using std::ifstream; |
| 23 | + using std::string; |
| 24 | + |
| 25 | + vm_usage = 0.0; |
| 26 | + resident_set = 0.0; |
| 27 | + |
| 28 | + // 'file' stat seems to give the most reliable results |
| 29 | + // |
| 30 | + ifstream stat_stream("/proc/self/stat",ios_base::in); |
| 31 | + |
| 32 | + // dummy vars for leading entries in stat that we don't care about |
| 33 | + // |
| 34 | + string pid, comm, state, ppid, pgrp, session, tty_nr; |
| 35 | + string tpgid, flags, minflt, cminflt, majflt, cmajflt; |
| 36 | + string utime, stime, cutime, cstime, priority, nice; |
| 37 | + string O, itrealvalue, starttime; |
| 38 | + |
| 39 | + // the two fields we want |
| 40 | + // |
| 41 | + unsigned long vsize; |
| 42 | + long rss; |
| 43 | + |
| 44 | + stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr |
| 45 | + >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt |
| 46 | + >> utime >> stime >> cutime >> cstime >> priority >> nice |
| 47 | + >> O >> itrealvalue >> starttime >> vsize >> rss; // don't care about the rest |
| 48 | + |
| 49 | + stat_stream.close(); |
| 50 | + |
| 51 | + long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages |
| 52 | + vm_usage = vsize / 1024.0; |
| 53 | + resident_set = rss * page_size_kb; |
| 54 | +} |
| 55 | + |
| 56 | +// --------------------------------------------------------------------- |
| 57 | +// UnitTestMemoryLeaks |
| 58 | + |
| 59 | +class UnitTestMemoryLeaks : public WsjcppUnitTestBase { |
| 60 | + public: |
| 61 | + UnitTestMemoryLeaks(); |
| 62 | + virtual bool doBeforeTest() override; |
| 63 | + virtual void executeTest() override; |
| 64 | + virtual bool doAfterTest() override; |
| 65 | +}; |
| 66 | + |
| 67 | +REGISTRY_WSJCPP_UNIT_TEST(UnitTestMemoryLeaks) |
| 68 | + |
| 69 | +UnitTestMemoryLeaks::UnitTestMemoryLeaks() |
| 70 | + : WsjcppUnitTestBase("UnitTestMemoryLeaks") { |
| 71 | +} |
| 72 | + |
| 73 | +// --------------------------------------------------------------------- |
| 74 | + |
| 75 | +bool UnitTestMemoryLeaks::doBeforeTest() { |
| 76 | + // do something before test |
| 77 | + return true; |
| 78 | +} |
| 79 | + |
| 80 | +// --------------------------------------------------------------------- |
| 81 | + |
| 82 | +void UnitTestMemoryLeaks::executeTest() { |
| 83 | + double nBeforeVm, nBeforeRss; |
| 84 | + process_mem_usage(nBeforeVm, nBeforeRss); |
| 85 | + // std::cout << "nBeforeVm: " << nBeforeVm << std::endl; |
| 86 | + // std::cout << "nBeforeRss: " << nBeforeRss << std::endl; |
| 87 | + compare("memory vm not null", (int)nBeforeVm > 0, true); |
| 88 | + compare("memory vm not null", (int)nBeforeRss > 0, true); |
| 89 | + |
| 90 | + for (int i = 0; i < 1000; i++) { |
| 91 | + WsjcppYaml *pYaml = new WsjcppYaml(); |
| 92 | + std::string sError; |
| 93 | + if (!compare("Error parsing", pYaml->loadFromFile("./data-tests/for-memory-leak/some.yml", sError), true)) { |
| 94 | + WsjcppLog::err(TAG, sError); |
| 95 | + return; |
| 96 | + } |
| 97 | + delete pYaml; |
| 98 | + } |
| 99 | + |
| 100 | + double nAfterVm, nAfterRss; |
| 101 | + process_mem_usage(nAfterVm, nAfterRss); |
| 102 | + // std::cout << "nAfterVm: " << nAfterVm << std::endl; |
| 103 | + // std::cout << "nAfterRss: " << nAfterRss << std::endl; |
| 104 | + compare("memory vm", (int)nAfterVm, (int)nBeforeVm); |
| 105 | + compare("memory rss", (int)nAfterRss, (int)nBeforeRss); |
| 106 | +} |
| 107 | + |
| 108 | +// --------------------------------------------------------------------- |
| 109 | + |
| 110 | +bool UnitTestMemoryLeaks::doAfterTest() { |
| 111 | + // do somethig after test |
| 112 | + return true; |
| 113 | +} |
| 114 | + |
| 115 | + |
0 commit comments