blob: 36afd6490fc0be5c8ba9ab16cc3cc5c14d462352 [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) {
James Kuszmaul860a94c2022-04-06 16:35:07 -070095 LOG(INFO) << "In child process.";
James Kuszmaul418fd062022-03-22 15:22:27 -070096 while (true) {
James Kuszmaul860a94c2022-04-06 16:35:07 -070097 // This is a "please don't optimize me out" thing for the compiler.
98 // Otherwise, the entire if (pid == 0) block can get optimized away...
99 asm("");
100 continue;
James Kuszmaul418fd062022-03-22 15:22:27 -0700101 }
James Kuszmaul860a94c2022-04-06 16:35:07 -0700102 LOG(FATAL) << "This should be unreachable.";
James Kuszmaul418fd062022-03-22 15:22:27 -0700103 } else {
James Kuszmaul860a94c2022-04-06 16:35:07 -0700104 CHECK_NE(0, pid) << "The compiler is messing with you.";
James Kuszmaul418fd062022-03-22 15:22:27 -0700105 children.push_back(pid);
106 }
107 }
108
109 Top top(&event_loop_);
110 top.set_track_top_processes(true);
111 event_loop_.AddTimer([this]() { event_loop_.Exit(); })
112 ->Setup(event_loop_.monotonic_now() + std::chrono::seconds(2));
113 event_loop_.SkipTimingReport();
114 event_loop_.SkipAosLog();
115 event_loop_.Run();
116 flatbuffers::FlatBufferBuilder fbb;
117 fbb.ForceDefaults(true);
118 fbb.Finish(top.TopProcesses(&fbb, kNProcesses));
119 aos::FlatbufferDetachedBuffer<TopProcessesFbs> info = fbb.Release();
120 ASSERT_EQ(kNProcesses, info.message().processes()->size());
121 double last_cpu = std::numeric_limits<double>::infinity();
122 std::set<pid_t> observed_pids;
123 int process_index = 0;
124 for (const ProcessInfo *info : *info.message().processes()) {
125 SCOPED_TRACE(aos::FlatbufferToJson(info));
126 ASSERT_EQ(0, observed_pids.count(info->pid()));
127 observed_pids.insert(info->pid());
128 ASSERT_TRUE(info->has_name());
129 // Confirm that the top process has non-zero CPU usage, but allow the
130 // lower-down processes to have not been scheduled in the last measurement
131 // cycle.
132 if (process_index < 1) {
133 ASSERT_LT(0.0, info->cpu_usage());
134 } else {
135 ASSERT_LE(0.0, info->cpu_usage());
136 }
137 ++process_index;
138 ASSERT_GE(last_cpu, info->cpu_usage());
139 last_cpu = info->cpu_usage();
140 ASSERT_LT(0, info->physical_memory());
141 }
142
143 for (const pid_t child : children) {
144 kill(child, SIGINT);
145 }
146}
147
148// Test thgat if we request arbitrarily many processes that we only get back as
149// many processes as actually exist and that nothing breaks.
150TEST_F(TopTest, AllTopProcesses) {
151 constexpr int kNProcesses = 1000000;
152
153 Top top(&event_loop_);
154 top.set_track_top_processes(true);
155 event_loop_.AddTimer([this]() { event_loop_.Exit(); })
156 ->Setup(event_loop_.monotonic_now() + std::chrono::seconds(2));
157 event_loop_.Run();
158 flatbuffers::FlatBufferBuilder fbb;
159 fbb.ForceDefaults(true);
160 // There should only be at most 2-3 processes visible inside the bazel
161 // sandbox.
162 fbb.Finish(top.TopProcesses(&fbb, kNProcesses));
163 aos::FlatbufferDetachedBuffer<TopProcessesFbs> info = fbb.Release();
164 ASSERT_GT(kNProcesses, info.message().processes()->size());
165 double last_cpu = std::numeric_limits<double>::infinity();
166 std::set<pid_t> observed_pids;
167 for (const ProcessInfo *info : *info.message().processes()) {
168 SCOPED_TRACE(aos::FlatbufferToJson(info));
169 LOG(INFO) << aos::FlatbufferToJson(info);
170 ASSERT_EQ(0, observed_pids.count(info->pid()));
171 observed_pids.insert(info->pid());
172 ASSERT_TRUE(info->has_name());
173 ASSERT_LE(0.0, info->cpu_usage());
174 ASSERT_GE(last_cpu, info->cpu_usage());
175 last_cpu = info->cpu_usage();
176 ASSERT_LE(0, info->physical_memory());
177 }
178}
179
180} // namespace aos::util::testing