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