]> git.proxmox.com Git - ceph.git/blob - ceph/src/test/mon/test_mon_memory_target.cc
import ceph 14.2.5
[ceph.git] / ceph / src / test / mon / test_mon_memory_target.cc
1 #include <algorithm>
2 #include <iostream>
3 #include <string>
4 #include <numeric>
5 #include <regex>
6 #include <system_error>
7
8 #include <boost/process.hpp>
9 #include <boost/tokenizer.hpp>
10
11 namespace bp = boost::process;
12 using namespace std;
13
14 int main(int argc, char** argv)
15 {
16 cout << "Mon Memory Target Test" << endl;
17
18 if (argc != 2) {
19 cout << "Syntax: "
20 << "ceph_test_mon_memory_target <mon-memory-target-bytes>"
21 << endl;
22 exit(EINVAL);
23 }
24
25 string target_directory("/var/log/ceph/");
26 unsigned long maxallowed = stoul(argv[1], nullptr, 10);
27 regex reg(R"(cache_size:(\d*)\s)");
28
29 string grep_command("grep _set_new_cache_sizes " + target_directory
30 + "ceph-mon.a.log");
31 bp::ipstream is;
32 error_code ec;
33 bp::child grep(grep_command, bp::std_out > is, ec);
34 if (ec) {
35 cout << "Error grepping logs! Exiting" << endl;
36 cout << "Error: " << ec.value() << " " << ec.message() << endl;
37 exit(ec.value());
38 }
39
40 string line;
41 vector<unsigned long> results;
42 while (grep.running() && getline(is, line) && !line.empty()) {
43 smatch match;
44 if (regex_search(line, match, reg)) {
45 results.push_back(stoul(match[1].str()));
46 }
47 }
48
49 if (results.empty()) {
50 cout << "Error: No grep results found!" << endl;
51 exit(ENOENT);
52 }
53
54 auto maxe = *(max_element(results.begin(), results.end()));
55 cout << "Results for mon_memory_target:" << endl;
56 cout << "Max: " << maxe << endl;
57 cout << "Min: " << *(min_element(results.begin(), results.end())) << endl;
58 auto sum = accumulate(results.begin(), results.end(),
59 static_cast<unsigned long long>(0));
60 auto mean = sum / results.size();
61 cout << "Mean average: " << mean << endl;
62 vector<unsigned long> diff(results.size());
63 transform(results.begin(), results.end(), diff.begin(),
64 [mean](unsigned long x) { return x - mean; });
65 auto sump = inner_product(diff.begin(), diff.end(), diff.begin(), 0.0);
66 auto stdev = sqrt(sump / results.size());
67 cout << "Standard deviation: " << stdev << endl;
68
69 if (maxe > maxallowed) {
70 cout << "Error: Mon memory consumption exceeds maximum allowed!" << endl;
71 exit(ENOMEM);
72 }
73
74 grep.wait();
75
76 cout << "Completed successfully" << endl;
77 return 0;
78 }