blob: cf7e03ede7b54e7ddf9eca13f6abf20d085d35b4 [file] [log] [blame]
James Kuszmaul418fd062022-03-22 15:22:27 -07001#include "aos/util/top.h"
2
3#include <unistd.h>
4
5#include <array>
6#include <string>
7#include <thread>
8
9#include "aos/events/shm_event_loop.h"
10#include "aos/json_to_flatbuffer.h"
11#include "aos/testing/path.h"
12#include "aos/testing/tmpdir.h"
13#include "gtest/gtest.h"
14
15namespace aos::util::testing {
16
17class TopTest : public ::testing::Test {
18 protected:
19 TopTest()
20 : shm_dir_(aos::testing::TestTmpDir() + "/aos"),
21 cpu_consumer_([this]() {
22 while (!stop_flag_.load()) {
23 }
24 }),
25 config_file_(
26 aos::testing::ArtifactPath("aos/events/pingpong_config.json")),
27 config_(aos::configuration::ReadConfig(config_file_)),
28 event_loop_(&config_.message()) {
29 FLAGS_shm_base = shm_dir_;
30
31 // Nuke the shm dir, to ensure we aren't being affected by any preexisting tests.
32 aos::util::UnlinkRecursive(shm_dir_);
33 }
34 ~TopTest() {
35 stop_flag_ = true;
36 cpu_consumer_.join();
37 }
38
39 gflags::FlagSaver flag_saver_;
40 std::string shm_dir_;
41
42 std::thread cpu_consumer_;
43 std::atomic<bool> stop_flag_{false};
44 const std::string config_file_;
45 const aos::FlatbufferDetachedBuffer<aos::Configuration> config_;
46 aos::ShmEventLoop event_loop_;
47};
48
49TEST_F(TopTest, TestSelfStat) {
50 const pid_t pid = getpid();
51 std::optional<ProcStat> proc_stat = ReadProcStat(pid);
52 ASSERT_TRUE(proc_stat.has_value());
53 ASSERT_EQ(pid, proc_stat->pid);
54 ASSERT_EQ("top_test", proc_stat->name);
55 ASSERT_EQ('R', proc_stat->state);
56 ASSERT_LT(1, proc_stat->num_threads);
57}
58
59TEST_F(TopTest, QuerySingleProcess) {
60 const pid_t pid = getpid();
61 Top top(&event_loop_);
62 top.set_track_pids({pid});
63 event_loop_.AddTimer([this]() { event_loop_.Exit(); })
64 ->Setup(event_loop_.monotonic_now() + std::chrono::seconds(2));
65 event_loop_.Run();
66 flatbuffers::FlatBufferBuilder fbb;
67 fbb.ForceDefaults(true);
68 fbb.Finish(top.InfoForProcess(&fbb, pid));
69 aos::FlatbufferDetachedBuffer<ProcessInfo> info = fbb.Release();
70 ASSERT_EQ(pid, info.message().pid());
71 ASSERT_TRUE(info.message().has_name());
72 ASSERT_EQ("top_test", info.message().name()->string_view());
73 // Check that we did indeed consume ~1 CPU core (because we're multi-threaded,
74 // we could've consumed a bit more; and on systems where we are competing with
75 // other processes for CPU time, we may not get a full 100% load).
76 ASSERT_LT(0.5, info.message().cpu_usage());
77 ASSERT_GT(1.1, info.message().cpu_usage());
78 // Sanity check memory usage.
79 ASSERT_LT(1000000, info.message().physical_memory());
80 ASSERT_GT(1000000000, info.message().physical_memory());
81}
82
83TEST_F(TopTest, TopProcesses) {
84 // Make some dummy processes that will just spin and get killed off at the
85 // end, so that we actually have things to query.
86 constexpr int kNProcesses = 2;
87 std::vector<pid_t> children;
88 // This will create kNProcesses children + ourself, which means we have enough
89 // processes to test that we correctly exclude extras when requesting fewer
90 // processes than exist.
91 for (int ii = 0; ii < kNProcesses; ++ii) {
92 const pid_t pid = fork();
93 PCHECK(pid >= 0);
94 if (pid == 0) {
95 while (true) {
96 }
97 } else {
98 children.push_back(pid);
99 }
100 }
101
102 Top top(&event_loop_);
103 top.set_track_top_processes(true);
104 event_loop_.AddTimer([this]() { event_loop_.Exit(); })
105 ->Setup(event_loop_.monotonic_now() + std::chrono::seconds(2));
106 event_loop_.SkipTimingReport();
107 event_loop_.SkipAosLog();
108 event_loop_.Run();
109 flatbuffers::FlatBufferBuilder fbb;
110 fbb.ForceDefaults(true);
111 fbb.Finish(top.TopProcesses(&fbb, kNProcesses));
112 aos::FlatbufferDetachedBuffer<TopProcessesFbs> info = fbb.Release();
113 ASSERT_EQ(kNProcesses, info.message().processes()->size());
114 double last_cpu = std::numeric_limits<double>::infinity();
115 std::set<pid_t> observed_pids;
116 int process_index = 0;
117 for (const ProcessInfo *info : *info.message().processes()) {
118 SCOPED_TRACE(aos::FlatbufferToJson(info));
119 ASSERT_EQ(0, observed_pids.count(info->pid()));
120 observed_pids.insert(info->pid());
121 ASSERT_TRUE(info->has_name());
122 // Confirm that the top process has non-zero CPU usage, but allow the
123 // lower-down processes to have not been scheduled in the last measurement
124 // cycle.
125 if (process_index < 1) {
126 ASSERT_LT(0.0, info->cpu_usage());
127 } else {
128 ASSERT_LE(0.0, info->cpu_usage());
129 }
130 ++process_index;
131 ASSERT_GE(last_cpu, info->cpu_usage());
132 last_cpu = info->cpu_usage();
133 ASSERT_LT(0, info->physical_memory());
134 }
135
136 for (const pid_t child : children) {
137 kill(child, SIGINT);
138 }
139}
140
141// Test thgat if we request arbitrarily many processes that we only get back as
142// many processes as actually exist and that nothing breaks.
143TEST_F(TopTest, AllTopProcesses) {
144 constexpr int kNProcesses = 1000000;
145
146 Top top(&event_loop_);
147 top.set_track_top_processes(true);
148 event_loop_.AddTimer([this]() { event_loop_.Exit(); })
149 ->Setup(event_loop_.monotonic_now() + std::chrono::seconds(2));
150 event_loop_.Run();
151 flatbuffers::FlatBufferBuilder fbb;
152 fbb.ForceDefaults(true);
153 // There should only be at most 2-3 processes visible inside the bazel
154 // sandbox.
155 fbb.Finish(top.TopProcesses(&fbb, kNProcesses));
156 aos::FlatbufferDetachedBuffer<TopProcessesFbs> info = fbb.Release();
157 ASSERT_GT(kNProcesses, info.message().processes()->size());
158 double last_cpu = std::numeric_limits<double>::infinity();
159 std::set<pid_t> observed_pids;
160 for (const ProcessInfo *info : *info.message().processes()) {
161 SCOPED_TRACE(aos::FlatbufferToJson(info));
162 LOG(INFO) << aos::FlatbufferToJson(info);
163 ASSERT_EQ(0, observed_pids.count(info->pid()));
164 observed_pids.insert(info->pid());
165 ASSERT_TRUE(info->has_name());
166 ASSERT_LE(0.0, info->cpu_usage());
167 ASSERT_GE(last_cpu, info->cpu_usage());
168 last_cpu = info->cpu_usage();
169 ASSERT_LE(0, info->physical_memory());
170 }
171}
172
173} // namespace aos::util::testing