• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "src/traced/probes/ftrace/cpu_reader.h"
18 
19 #include <string.h>
20 #include <sys/stat.h>
21 #include <sys/syscall.h>
22 
23 #include "perfetto/base/build_config.h"
24 #include "perfetto/ext/base/utils.h"
25 #include "perfetto/protozero/proto_utils.h"
26 #include "perfetto/protozero/scattered_heap_buffer.h"
27 #include "perfetto/protozero/scattered_stream_writer.h"
28 #include "src/traced/probes/ftrace/event_info.h"
29 #include "src/traced/probes/ftrace/ftrace_config_muxer.h"
30 #include "src/traced/probes/ftrace/ftrace_procfs.h"
31 #include "src/traced/probes/ftrace/proto_translation_table.h"
32 #include "src/traced/probes/ftrace/test/cpu_reader_support.h"
33 #include "src/tracing/core/trace_writer_for_testing.h"
34 #include "test/gtest_and_gmock.h"
35 
36 #include "protos/perfetto/trace/ftrace/dpu.gen.h"
37 #include "protos/perfetto/trace/ftrace/f2fs.gen.h"
38 #include "protos/perfetto/trace/ftrace/ftrace.gen.h"
39 #include "protos/perfetto/trace/ftrace/ftrace_event.gen.h"
40 #include "protos/perfetto/trace/ftrace/ftrace_event.pbzero.h"
41 #include "protos/perfetto/trace/ftrace/ftrace_event_bundle.gen.h"
42 #include "protos/perfetto/trace/ftrace/ftrace_event_bundle.pbzero.h"
43 #include "protos/perfetto/trace/ftrace/ftrace_stats.gen.h"
44 #include "protos/perfetto/trace/ftrace/ftrace_stats.pbzero.h"
45 #include "protos/perfetto/trace/ftrace/power.gen.h"
46 #include "protos/perfetto/trace/ftrace/raw_syscalls.gen.h"
47 #include "protos/perfetto/trace/ftrace/sched.gen.h"
48 #include "protos/perfetto/trace/ftrace/task.gen.h"
49 #include "protos/perfetto/trace/trace_packet.gen.h"
50 #include "src/traced/probes/ftrace/test/test_messages.gen.h"
51 #include "src/traced/probes/ftrace/test/test_messages.pbzero.h"
52 
53 using protozero::proto_utils::ProtoSchemaType;
54 using testing::_;
55 using testing::AnyNumber;
56 using testing::Contains;
57 using testing::Each;
58 using testing::ElementsAre;
59 using testing::ElementsAreArray;
60 using testing::EndsWith;
61 using testing::Eq;
62 using testing::IsEmpty;
63 using testing::NiceMock;
64 using testing::Not;
65 using testing::Pair;
66 using testing::Property;
67 using testing::Return;
68 using testing::SizeIs;
69 using testing::StartsWith;
70 
71 namespace perfetto {
72 namespace {
73 
74 using FtraceParseStatus = protos::pbzero::FtraceParseStatus;
75 
EmptyConfig()76 FtraceDataSourceConfig EmptyConfig() {
77   return FtraceDataSourceConfig{EventFilter{},
78                                 EventFilter{},
79                                 DisabledCompactSchedConfigForTesting(),
80                                 std::nullopt,
81                                 {},
82                                 {},
83                                 false /*symbolize_ksyms*/,
84                                 50u,
85                                 {}};
86 }
87 
88 constexpr uint64_t kNanoInSecond = 1000 * 1000 * 1000;
89 constexpr uint64_t kNanoInMicro = 1000;
90 
WithinOneMicrosecond(uint64_t actual_ns,uint64_t expected_s,uint64_t expected_us)91 ::testing::AssertionResult WithinOneMicrosecond(uint64_t actual_ns,
92                                                 uint64_t expected_s,
93                                                 uint64_t expected_us) {
94   // Round to closest us.
95   uint64_t actual_us = (actual_ns + kNanoInMicro / 2) / kNanoInMicro;
96   uint64_t total_expected_us = expected_s * 1000 * 1000 + expected_us;
97   if (actual_us == total_expected_us)
98     return ::testing::AssertionSuccess();
99 
100   return ::testing::AssertionFailure()
101          << actual_ns / kNanoInSecond << "."
102          << (actual_ns % kNanoInSecond) / kNanoInMicro << " vs. " << expected_s
103          << "." << expected_us;
104 }
105 
106 class MockFtraceProcfs : public FtraceProcfs {
107  public:
MockFtraceProcfs()108   MockFtraceProcfs() : FtraceProcfs("/root/") {
109     ON_CALL(*this, NumberOfCpus()).WillByDefault(Return(1));
110     ON_CALL(*this, WriteToFile(_, _)).WillByDefault(Return(true));
111     ON_CALL(*this, ClearFile(_)).WillByDefault(Return(true));
112     EXPECT_CALL(*this, NumberOfCpus()).Times(AnyNumber());
113   }
114 
115   MOCK_METHOD(bool,
116               WriteToFile,
117               (const std::string& path, const std::string& str),
118               (override));
119   MOCK_METHOD(char, ReadOneCharFromFile, (const std::string& path), (override));
120   MOCK_METHOD(bool, ClearFile, (const std::string& path), (override));
121   MOCK_METHOD(std::string,
122               ReadFileIntoString,
123               (const std::string& path),
124               (const, override));
125   MOCK_METHOD(size_t, NumberOfCpus, (), (const, override));
126 };
127 
128 class CpuReaderTableTest : public ::testing::Test {
129  protected:
130   NiceMock<MockFtraceProcfs> ftrace_;
131 };
132 
133 // Single class to manage the whole protozero -> scattered stream -> chunks ->
134 // single buffer -> real proto dance. Has a method: writer() to get an
135 // protozero ftrace bundle writer and a method ParseProto() to attempt to
136 // parse whatever has been written so far into a proto message.
137 template <class ZeroT, class ProtoT>
138 class ProtoProvider {
139  public:
ProtoProvider(size_t chunk_size)140   explicit ProtoProvider(size_t chunk_size) : chunk_size_(chunk_size) {}
141   ~ProtoProvider() = default;
142 
writer()143   ZeroT* writer() { return writer_.get(); }
ResetWriter()144   void ResetWriter() { writer_.Reset(); }
145 
146   // Stitch together the scattered chunks into a single buffer then attempt
147   // to parse the buffer as a FtraceEventBundle. Returns the FtraceEventBundle
148   // on success and nullptr on failure.
ParseProto()149   std::unique_ptr<ProtoT> ParseProto() {
150     auto bundle = std::make_unique<ProtoT>();
151     std::vector<uint8_t> buffer = writer_.SerializeAsArray();
152     if (!bundle->ParseFromArray(buffer.data(), buffer.size()))
153       return nullptr;
154     return bundle;
155   }
156 
157  private:
158   ProtoProvider(const ProtoProvider&) = delete;
159   ProtoProvider& operator=(const ProtoProvider&) = delete;
160 
161   size_t chunk_size_;
162   protozero::HeapBuffered<ZeroT> writer_;
163 };
164 
165 using BundleProvider = ProtoProvider<protos::pbzero::FtraceEventBundle,
166                                      protos::gen::FtraceEventBundle>;
167 
168 class BinaryWriter {
169  public:
BinaryWriter()170   BinaryWriter()
171       : size_(base::GetSysPageSize()),
172         page_(new uint8_t[size_]),
173         ptr_(page_.get()) {}
174 
175   template <typename T>
Write(T t)176   void Write(T t) {
177     memcpy(ptr_, &t, sizeof(T));
178     ptr_ += sizeof(T);
179     PERFETTO_CHECK(ptr_ < ptr_ + size_);
180   }
181 
WriteFixedString(size_t n,const char * s)182   void WriteFixedString(size_t n, const char* s) {
183     size_t length = strlen(s);
184     PERFETTO_CHECK(length < n);
185     char c;
186     while ((c = *s++)) {
187       Write<char>(c);
188     }
189     Write<char>('\0');
190     for (size_t i = 0; i < n - length - 1; i++) {
191       Write<char>('\xff');
192     }
193   }
194 
GetCopy()195   std::unique_ptr<uint8_t[]> GetCopy() {
196     std::unique_ptr<uint8_t[]> buffer(new uint8_t[written()]);
197     memcpy(buffer.get(), page_.get(), written());
198     return buffer;
199   }
200 
written()201   size_t written() { return static_cast<size_t>(ptr_ - page_.get()); }
202 
203  private:
204   size_t size_;
205   std::unique_ptr<uint8_t[]> page_;
206   uint8_t* ptr_;
207 };
208 
TEST(PageFromXxdTest,OneLine)209 TEST(PageFromXxdTest, OneLine) {
210   std::string text = R"(
211     00000000: 0000 0000 0000 0000 0000 0000 0000 0000  ................
212     00000000: 0000 0000 5600 0000 0000 0000 0000 0000  ................
213   )";
214   auto page = PageFromXxd(text);
215   EXPECT_EQ(page.get()[0x14], 0x56);
216 }
217 
TEST(PageFromXxdTest,ManyLines)218 TEST(PageFromXxdTest, ManyLines) {
219   std::string text = R"(
220     00000000: 1234 0000 0000 0000 0000 0000 0000 0056  ................
221     00000010: 7800 0000 0000 0000 0000 0000 0000 009a  ................
222     00000020: 0000 0000 bc00 0000 00de 0000 0000 009a  ................
223   )";
224   auto page = PageFromXxd(text);
225   EXPECT_EQ(page.get()[0x00], 0x12);
226   EXPECT_EQ(page.get()[0x01], 0x34);
227   EXPECT_EQ(page.get()[0x0f], 0x56);
228   EXPECT_EQ(page.get()[0x10], 0x78);
229   EXPECT_EQ(page.get()[0x1f], 0x9a);
230   EXPECT_EQ(page.get()[0x24], 0xbc);
231   EXPECT_EQ(page.get()[0x29], 0xde);
232 }
233 
TEST(CpuReaderTest,BinaryWriter)234 TEST(CpuReaderTest, BinaryWriter) {
235   BinaryWriter writer;
236   writer.Write<uint64_t>(1);
237   writer.Write<uint32_t>(2);
238   writer.Write<uint16_t>(3);
239   writer.Write<uint8_t>(4);
240   auto buffer = writer.GetCopy();
241   EXPECT_EQ(buffer.get()[0], 1);
242   EXPECT_EQ(buffer.get()[1], 0);
243   EXPECT_EQ(buffer.get()[2], 0);
244   EXPECT_EQ(buffer.get()[3], 0);
245   EXPECT_EQ(buffer.get()[4], 0);
246   EXPECT_EQ(buffer.get()[5], 0);
247   EXPECT_EQ(buffer.get()[6], 0);
248   EXPECT_EQ(buffer.get()[7], 0);
249   EXPECT_EQ(buffer.get()[8], 2);
250 }
251 
TEST(ReadAndAdvanceTest,Number)252 TEST(ReadAndAdvanceTest, Number) {
253   uint64_t expected = 42;
254   uint64_t actual = 0;
255   uint8_t buffer[8] = {};
256   const uint8_t* start = buffer;
257   const uint8_t* ptr = buffer;
258   memcpy(&buffer, &expected, 8);
259   EXPECT_TRUE(CpuReader::ReadAndAdvance<uint64_t>(&ptr, ptr + 8, &actual));
260   EXPECT_EQ(ptr, start + 8);
261   EXPECT_EQ(actual, expected);
262 }
263 
TEST(ReadAndAdvanceTest,PlainStruct)264 TEST(ReadAndAdvanceTest, PlainStruct) {
265   struct PlainStruct {
266     uint64_t timestamp;
267     uint64_t length;
268   };
269 
270   uint64_t expected[2] = {42, 999};
271   PlainStruct actual;
272   uint8_t buffer[16] = {};
273   const uint8_t* start = buffer;
274   const uint8_t* ptr = buffer;
275   memcpy(&buffer, &expected, 16);
276   EXPECT_TRUE(CpuReader::ReadAndAdvance<PlainStruct>(&ptr, ptr + 16, &actual));
277   EXPECT_EQ(ptr, start + 16);
278   EXPECT_EQ(actual.timestamp, 42ul);
279   EXPECT_EQ(actual.length, 999ul);
280 }
281 
TEST(ReadAndAdvanceTest,ComplexStruct)282 TEST(ReadAndAdvanceTest, ComplexStruct) {
283   struct ComplexStruct {
284     uint64_t timestamp;
285     uint32_t length;
286     uint32_t : 24;
287     uint32_t overwrite : 8;
288   };
289 
290   uint64_t expected[2] = {42, 0xcdffffffabababab};
291   ComplexStruct actual = {};
292   uint8_t buffer[16] = {};
293   const uint8_t* start = buffer;
294   const uint8_t* ptr = buffer;
295   memcpy(&buffer, &expected, 16);
296   EXPECT_TRUE(
297       CpuReader::ReadAndAdvance<ComplexStruct>(&ptr, ptr + 16, &actual));
298   EXPECT_EQ(ptr, start + 16);
299   EXPECT_EQ(actual.timestamp, 42ul);
300   EXPECT_EQ(actual.length, 0xabababab);
301   EXPECT_EQ(actual.overwrite, 0xCDu);
302 }
303 
TEST(ReadAndAdvanceTest,Overruns)304 TEST(ReadAndAdvanceTest, Overruns) {
305   uint64_t result = 42;
306   uint8_t buffer[7] = {};
307   const uint8_t* start = buffer;
308   const uint8_t* ptr = buffer;
309   EXPECT_FALSE(CpuReader::ReadAndAdvance<uint64_t>(&ptr, ptr + 7, &result));
310   EXPECT_EQ(ptr, start);
311   EXPECT_EQ(result, 42ul);
312 }
313 
TEST(ReadAndAdvanceTest,AtEnd)314 TEST(ReadAndAdvanceTest, AtEnd) {
315   uint8_t result = 42;
316   uint8_t buffer[8] = {};
317   const uint8_t* start = buffer;
318   const uint8_t* ptr = buffer;
319   EXPECT_FALSE(CpuReader::ReadAndAdvance<uint8_t>(&ptr, ptr, &result));
320   EXPECT_EQ(ptr, start);
321   EXPECT_EQ(result, 42);
322 }
323 
TEST(ReadAndAdvanceTest,Underruns)324 TEST(ReadAndAdvanceTest, Underruns) {
325   uint64_t expected = 42;
326   uint64_t actual = 0;
327   uint8_t buffer[9] = {};
328   const uint8_t* start = buffer;
329   const uint8_t* ptr = buffer;
330   memcpy(&buffer, &expected, 8);
331   EXPECT_TRUE(CpuReader::ReadAndAdvance<uint64_t>(&ptr, ptr + 8, &actual));
332   EXPECT_EQ(ptr, start + 8);
333   EXPECT_EQ(actual, expected);
334 }
335 
TEST(ParsePageHeaderTest,WithOverrun)336 TEST(ParsePageHeaderTest, WithOverrun) {
337   std::string text = R"(
338     00000000: 3ef3 db77 67a2 0100 f00f 0080 ffff ffff
339     )";
340   auto page = PageFromXxd(text);
341 
342   // parse as if we're on a 32 bit kernel (4 byte "commit" field)
343   {
344     const uint8_t* ptr = page.get();
345     auto ret = CpuReader::ParsePageHeader(&ptr, 4u);
346     ASSERT_TRUE(ret.has_value());
347     CpuReader::PageHeader parsed = ret.value();
348 
349     ASSERT_EQ(parsed.timestamp, 0x0001A26777DBF33Eull);  // first 8 bytes
350     ASSERT_EQ(parsed.size, 0x0ff0u);                     // 4080
351     ASSERT_TRUE(parsed.lost_events);
352 
353     // pointer advanced past the header (8+4 bytes)
354     ASSERT_EQ(ptr, page.get() + 12);
355   }
356 
357   // parse as if we're on a 64 bit kernel (8 byte "commit" field)
358   {
359     const uint8_t* ptr = page.get();
360     auto ret = CpuReader::ParsePageHeader(&ptr, 8u);
361     ASSERT_TRUE(ret.has_value());
362     CpuReader::PageHeader parsed = ret.value();
363 
364     ASSERT_EQ(parsed.timestamp, 0x0001A26777DBF33Eull);  // first 8 bytes
365     ASSERT_EQ(parsed.size, 0x0ff0u);                     // 4080
366     ASSERT_TRUE(parsed.lost_events);
367 
368     // pointer advanced past the header (8+8 bytes)
369     ASSERT_EQ(ptr, page.get() + 16);
370   }
371 }
372 
373 // clang-format off
374 // # tracer: nop
375 // #
376 // # entries-in-buffer/entries-written: 1/1   #P:8
377 // #
378 // #                              _-----=> irqs-off
379 // #                             / _----=> need-resched
380 // #                            | / _---=> hardirq/softirq
381 // #                            || / _--=> preempt-depth
382 // #                            ||| /     delay
383 // #           TASK-PID   CPU#  ||||    TIMESTAMP  FUNCTION
384 // #              | |       |   ||||       |         |
385 //               sh-28712 [000] ...1 608934.535199: tracing_mark_write: Hello, world!
386 // clang-format on
387 
388 static ExamplePage g_single_print{
389     "synthetic",
390     R"(
391     00000000: ba12 6a33 c628 0200 2c00 0000 0000 0000  ..j3.(..,.......
392     00000010: def0 ec67 8d21 0000 0800 0000 0500 0001  ...g.!..........
393     00000020: 2870 0000 ac5d 1661 86ff ffff 4865 6c6c  (p...].a....Hell
394     00000030: 6f2c 2077 6f72 6c64 210a 00ff 0000 0000  o, world!.......
395   )",
396 };
397 
398 class CpuReaderParsePagePayloadTest : public testing::Test {
399  protected:
CreateBundler(const FtraceDataSourceConfig & ds_config)400   CpuReader::Bundler* CreateBundler(const FtraceDataSourceConfig& ds_config) {
401     PERFETTO_CHECK(!bundler_.has_value());
402     writer_.emplace();
403     compact_sched_buf_ = std::make_unique<CompactSchedBuffer>();
404     bundler_.emplace(&writer_.value(), &metadata_, /*symbolizer=*/nullptr,
405                      /*cpu=*/0,
406                      /*ftrace_clock_snapshot=*/nullptr,
407                      protos::pbzero::FTRACE_CLOCK_UNSPECIFIED,
408                      compact_sched_buf_.get(), ds_config.compact_sched.enabled,
409                      /*last_read_event_ts=*/0);
410     return &bundler_.value();
411   }
412 
GetBundle()413   protos::gen::FtraceEventBundle GetBundle() {
414     PERFETTO_CHECK(bundler_.has_value());
415     PERFETTO_CHECK(writer_.has_value());
416     bundler_.reset();
417     protos::gen::FtraceEventBundle bundle =
418         writer_->GetOnlyTracePacket().ftrace_events();
419     writer_.reset();
420     return bundle;
421   }
422 
AllTracePackets()423   std::vector<protos::gen::TracePacket> AllTracePackets() {
424     PERFETTO_CHECK(bundler_.has_value());
425     PERFETTO_CHECK(writer_.has_value());
426     bundler_.reset();
427     std::vector<protos::gen::TracePacket> packets =
428         writer_->GetAllTracePackets();
429     writer_.reset();
430     return packets;
431   }
432 
433   FtraceMetadata metadata_;
434   std::optional<TraceWriterForTesting> writer_;
435   std::unique_ptr<CompactSchedBuffer> compact_sched_buf_;
436   std::optional<CpuReader::Bundler> bundler_;
437   uint64_t last_read_event_ts_ = 0;
438 };
439 
TEST_F(CpuReaderParsePagePayloadTest,ParseSinglePrint)440 TEST_F(CpuReaderParsePagePayloadTest, ParseSinglePrint) {
441   const ExamplePage* test_case = &g_single_print;
442 
443   ProtoTranslationTable* table = GetTable(test_case->name);
444   auto page = PageFromXxd(test_case->data);
445 
446   FtraceDataSourceConfig ds_config = EmptyConfig();
447   ds_config.event_filter.AddEnabledEvent(
448       table->EventToFtraceId(GroupAndName("ftrace", "print")));
449 
450   const uint8_t* parse_pos = page.get();
451   std::optional<CpuReader::PageHeader> page_header =
452       CpuReader::ParsePageHeader(&parse_pos, table->page_header_size_len());
453 
454   const uint8_t* page_end = page.get() + base::GetSysPageSize();
455   ASSERT_TRUE(page_header.has_value());
456   EXPECT_EQ(44ul, page_header->size);
457   EXPECT_FALSE(page_header->lost_events);
458   EXPECT_LE(parse_pos + page_header->size, page_end);
459 
460   FtraceParseStatus status = CpuReader::ParsePagePayload(
461       parse_pos, &page_header.value(), table, &ds_config,
462       CreateBundler(ds_config), &metadata_, &last_read_event_ts_);
463 
464   EXPECT_EQ(status, FtraceParseStatus::FTRACE_STATUS_OK);
465 
466   auto bundle = GetBundle();
467   ASSERT_EQ(bundle.event().size(), 1u);
468   const protos::gen::FtraceEvent& event = bundle.event()[0];
469   EXPECT_EQ(event.pid(), 28712ul);
470   EXPECT_TRUE(WithinOneMicrosecond(event.timestamp(), 608934, 535199));
471   EXPECT_EQ(event.print().buf(), "Hello, world!\n");
472 }
473 
474 // clang-format off
475 // # tracer: nop
476 // #
477 // # entries-in-buffer/entries-written: 2/2   #P:8
478 // #
479 // #                                      _-----=> irqs-off
480 // #                                     / _----=> need-resched
481 // #                                    | / _---=> hardirq/softirq
482 // #                                    || / _--=> preempt-depth
483 // #                                    ||| /     delay
484 // #           TASK-PID    TGID   CPU#  ||||    TIMESTAMP  FUNCTION
485 // #              | |        |      |   ||||       |         |
486 //             echo-6908  ( 6908) [000] ...1 282762.884473: tracing_mark_write: qwertyuiopqwrtyuiopqwertyuiopqwertyuiopqwer[...]
487 //             echo-6908  ( 6908) [000] ...1 282762.884492: tracing_mark_write:
488 // clang-format on
489 
490 static ExamplePage g_really_long_event{
491     "synthetic",
492     R"(
493       00000000: 6be0 48dd 2b01 0100 e403 0000 0000 0000  k.H.+...........
494       00000010: 1e00 0000 0000 0000 0000 0000 c003 0000  ................
495       00000020: 0500 0001 fc1a 0000 4096 3615 9cff ffff  ........@.6.....
496       00000030: 7177 6572 7479 7569 6f70 7177 7274 7975  qwertyuiopqwrtyu
497       00000040: 696f 7071 7765 7274 7975 696f 7071 7765  iopqwertyuiopqwe
498       00000050: 7274 7975 696f 7071 7765 7274 7975 696f  rtyuiopqwertyuio
499       00000060: 7071 7772 7479 7569 6f70 7177 6572 7479  pqwrtyuiopqwerty
500       00000070: 7569 6f70 7177 6572 7479 7569 6f71 7765  uiopqwertyuioqwe
501       00000080: 7274 7975 696f 7071 7772 7479 7569 6f70  rtyuiopqwrtyuiop
502       00000090: 7177 6572 7479 7569 6f70 7177 6572 7479  qwertyuiopqwerty
503       000000a0: 7569 6f71 7765 7274 7975 696f 7071 7772  uioqwertyuiopqwr
504       000000b0: 7479 7569 6f70 7177 6572 7479 7569 6f70  tyuiopqwertyuiop
505       000000c0: 7177 6572 7479 7569 6f70 7070 7177 6572  qwertyuiopppqwer
506       000000d0: 7479 7569 6f70 7177 7274 7975 696f 7071  tyuiopqwrtyuiopq
507       000000e0: 7765 7274 7975 696f 7071 7765 7274 7975  wertyuiopqwertyu
508       000000f0: 696f 7071 7765 7274 7975 696f 7071 7772  iopqwertyuiopqwr
509       00000100: 7479 7569 6f70 7177 6572 7479 7569 6f70  tyuiopqwertyuiop
510       00000110: 7177 6572 7479 7569 6f71 7765 7274 7975  qwertyuioqwertyu
511       00000120: 696f 7071 7772 7479 7569 6f70 7177 6572  iopqwrtyuiopqwer
512       00000130: 7479 7569 6f70 7177 6572 7479 7569 6f71  tyuiopqwertyuioq
513       00000140: 7765 7274 7975 696f 7071 7772 7479 7569  wertyuiopqwrtyui
514       00000150: 6f70 7177 6572 7479 7569 6f70 7177 6572  opqwertyuiopqwer
515       00000160: 7479 7569 6f70 7070 7177 6572 7479 7569  tyuiopppqwertyui
516       00000170: 6f70 7177 7274 7975 696f 7071 7765 7274  opqwrtyuiopqwert
517       00000180: 7975 696f 7071 7765 7274 7975 696f 7071  yuiopqwertyuiopq
518       00000190: 7765 7274 7975 696f 7071 7772 7479 7569  wertyuiopqwrtyui
519       000001a0: 6f70 7177 6572 7479 7569 6f70 7177 6572  opqwertyuiopqwer
520       000001b0: 7479 7569 6f71 7765 7274 7975 696f 7071  tyuioqwertyuiopq
521       000001c0: 7772 7479 7569 6f70 7177 6572 7479 7569  wrtyuiopqwertyui
522       000001d0: 6f70 7177 6572 7479 7569 6f71 7765 7274  opqwertyuioqwert
523       000001e0: 7975 696f 7071 7772 7479 7569 6f70 7177  yuiopqwrtyuiopqw
524       000001f0: 6572 7479 7569 6f70 7177 6572 7479 7569  ertyuiopqwertyui
525       00000200: 6f70 7070 7177 6572 7479 7569 6f70 7177  opppqwertyuiopqw
526       00000210: 7274 7975 696f 7071 7765 7274 7975 696f  rtyuiopqwertyuio
527       00000220: 7071 7765 7274 7975 696f 7071 7765 7274  pqwertyuiopqwert
528       00000230: 7975 696f 7071 7772 7479 7569 6f70 7177  yuiopqwrtyuiopqw
529       00000240: 6572 7479 7569 6f70 7177 6572 7479 7569  ertyuiopqwertyui
530       00000250: 6f71 7765 7274 7975 696f 7071 7772 7479  oqwertyuiopqwrty
531       00000260: 7569 6f70 7177 6572 7479 7569 6f70 7177  uiopqwertyuiopqw
532       00000270: 6572 7479 7569 6f71 7765 7274 7975 696f  ertyuioqwertyuio
533       00000280: 7071 7772 7479 7569 6f70 7177 6572 7479  pqwrtyuiopqwerty
534       00000290: 7569 6f70 7177 6572 7479 7569 6f70 7070  uiopqwertyuioppp
535       000002a0: 7177 6572 7479 7569 6f70 7177 7274 7975  qwertyuiopqwrtyu
536       000002b0: 696f 7071 7765 7274 7975 696f 7071 7765  iopqwertyuiopqwe
537       000002c0: 7274 7975 696f 7071 7765 7274 7975 696f  rtyuiopqwertyuio
538       000002d0: 7071 7772 7479 7569 6f70 7177 6572 7479  pqwrtyuiopqwerty
539       000002e0: 7569 6f70 7177 6572 7479 7569 6f71 7765  uiopqwertyuioqwe
540       000002f0: 7274 7975 696f 7071 7772 7479 7569 6f70  rtyuiopqwrtyuiop
541       00000300: 7177 6572 7479 7569 6f70 7177 6572 7479  qwertyuiopqwerty
542       00000310: 7569 6f71 7765 7274 7975 696f 7071 7772  uioqwertyuiopqwr
543       00000320: 7479 7569 6f70 7177 6572 7479 7569 6f70  tyuiopqwertyuiop
544       00000330: 7177 6572 7479 7569 6f70 7070 7177 6572  qwertyuiopppqwer
545       00000340: 7479 7569 6f70 7177 7274 7975 696f 7071  tyuiopqwrtyuiopq
546       00000350: 7765 7274 7975 696f 7071 7765 7274 7975  wertyuiopqwertyu
547       00000360: 696f 7071 7765 7274 7975 696f 7071 7772  iopqwertyuiopqwr
548       00000370: 7479 7569 6f70 7177 6572 7479 7569 6f70  tyuiopqwertyuiop
549       00000380: 7177 6572 7479 7569 6f71 7765 7274 7975  qwertyuioqwertyu
550       00000390: 696f 7071 7772 7479 7569 6f70 7177 6572  iopqwrtyuiopqwer
551       000003a0: 7479 7569 6f70 7177 6572 7479 7569 6f71  tyuiopqwertyuioq
552       000003b0: 7765 7274 7975 696f 7071 7772 7479 7569  wertyuiopqwrtyui
553       000003c0: 6f70 7177 6572 7479 7569 6f70 7177 6572  opqwertyuiopqwer
554       000003d0: 7479 7569 6f70 7070 0a00 5115 6562 0900  tyuioppp..Q.eb..
555       000003e0: 0500 0001 fc1a 0000 4096 3615 9cff ffff  ........@.6.....
556       000003f0: 0a00 0000 0000 0000 0000 0000 0000 0000  ................
557       00000400: 0000 0000 0000 0000 0000 0000 0000 0000  ................
558       00000410: 0000 0000 0000 0000 0000 0000 0000 0000  ................
559       00000420: 0000 0000 0000 0000 0000 0000 0000 0000  ................
560   )",
561 };
562 
TEST_F(CpuReaderParsePagePayloadTest,ReallyLongEvent)563 TEST_F(CpuReaderParsePagePayloadTest, ReallyLongEvent) {
564   const ExamplePage* test_case = &g_really_long_event;
565 
566   ProtoTranslationTable* table = GetTable(test_case->name);
567   auto page = PageFromXxd(test_case->data);
568 
569   FtraceDataSourceConfig ds_config = EmptyConfig();
570   ds_config.event_filter.AddEnabledEvent(
571       table->EventToFtraceId(GroupAndName("ftrace", "print")));
572 
573   const uint8_t* parse_pos = page.get();
574   std::optional<CpuReader::PageHeader> page_header =
575       CpuReader::ParsePageHeader(&parse_pos, table->page_header_size_len());
576 
577   const uint8_t* page_end = page.get() + base::GetSysPageSize();
578   ASSERT_TRUE(page_header.has_value());
579   EXPECT_FALSE(page_header->lost_events);
580   EXPECT_LE(parse_pos + page_header->size, page_end);
581 
582   CpuReader::ParsePagePayload(parse_pos, &page_header.value(), table,
583                               &ds_config, CreateBundler(ds_config), &metadata_,
584                               &last_read_event_ts_);
585 
586   auto bundle = GetBundle();
587   const protos::gen::FtraceEvent& long_print = bundle.event()[0];
588   EXPECT_THAT(long_print.print().buf(), StartsWith("qwerty"));
589   EXPECT_THAT(long_print.print().buf(), EndsWith("ppp\n"));
590   const protos::gen::FtraceEvent& newline = bundle.event()[1];
591   EXPECT_EQ(newline.print().buf(), "\n");
592 }
593 
594 // This event is as the event for ParseSinglePrint above except the string
595 // is extended and not null terminated.
596 static ExamplePage g_single_print_non_null_terminated{
597     "synthetic",
598     R"(
599     00000000: ba12 6a33 c628 0200 2c00 0000 0000 0000  ..j3.(..,.......
600     00000010: def0 ec67 8d21 0000 0800 0000 0500 0001  ...g.!..........
601     00000020: 2870 0000 ac5d 1661 86ff ffff 4865 6c6c  (p...].a....Hell
602     00000030: 6f2c 2077 6f72 6c64 2161 6161 6161 6161  o, world!aaaaaaa
603     00000040: 6161 6161 6161 6161 6161 6161 6161 6161  aaaaaaaaaaaaaaaa
604   )",
605 };
606 
TEST_F(CpuReaderParsePagePayloadTest,ParseSinglePrintNonNullTerminated)607 TEST_F(CpuReaderParsePagePayloadTest, ParseSinglePrintNonNullTerminated) {
608   const ExamplePage* test_case = &g_single_print_non_null_terminated;
609 
610   ProtoTranslationTable* table = GetTable(test_case->name);
611   auto page = PageFromXxd(test_case->data);
612 
613   FtraceDataSourceConfig ds_config = EmptyConfig();
614   ds_config.event_filter.AddEnabledEvent(
615       table->EventToFtraceId(GroupAndName("ftrace", "print")));
616 
617   const uint8_t* parse_pos = page.get();
618   std::optional<CpuReader::PageHeader> page_header =
619       CpuReader::ParsePageHeader(&parse_pos, table->page_header_size_len());
620 
621   const uint8_t* page_end = page.get() + base::GetSysPageSize();
622   ASSERT_TRUE(page_header.has_value());
623   EXPECT_FALSE(page_header->lost_events);
624   EXPECT_LE(parse_pos + page_header->size, page_end);
625 
626   FtraceParseStatus status = CpuReader::ParsePagePayload(
627       parse_pos, &page_header.value(), table, &ds_config,
628       CreateBundler(ds_config), &metadata_, &last_read_event_ts_);
629 
630   EXPECT_EQ(status, FtraceParseStatus::FTRACE_STATUS_OK);
631 
632   auto bundle = GetBundle();
633   ASSERT_EQ(bundle.event().size(), 1u);
634   const protos::gen::FtraceEvent& event = bundle.event()[0];
635   EXPECT_EQ(event.pid(), 28712ul);
636   EXPECT_TRUE(WithinOneMicrosecond(event.timestamp(), 608934, 535199));
637   EXPECT_EQ(event.print().buf(), "Hello, world!aaa");
638 }
639 
640 static ExamplePage g_single_print_zero_size{
641     "synthetic",
642     R"(
643     00000000: ba12 6a33 c628 0200 2c00 0000 0000 0000  ..j3.(..,.......
644     00000010: def0 ec67 8d21 0000 0800 0000 0500 0001  ...g.!..........
645     00000020: 2870 0000 ac5d 1661 86ff ffff 0000 0000  (p...].a........
646     00000030: 0000 0000 0000 0000 0000 0000 0000 0000  ................
647     00000040: 0000 0000 0000 0000 0000 0000 0000 0000  ................
648   )",
649 };
650 
TEST_F(CpuReaderParsePagePayloadTest,ParseSinglePrintZeroSize)651 TEST_F(CpuReaderParsePagePayloadTest, ParseSinglePrintZeroSize) {
652   const ExamplePage* test_case = &g_single_print_zero_size;
653 
654   ProtoTranslationTable* table = GetTable(test_case->name);
655   auto page = PageFromXxd(test_case->data);
656 
657   FtraceDataSourceConfig ds_config = EmptyConfig();
658   ds_config.event_filter.AddEnabledEvent(
659       table->EventToFtraceId(GroupAndName("ftrace", "print")));
660 
661   const uint8_t* parse_pos = page.get();
662   std::optional<CpuReader::PageHeader> page_header =
663       CpuReader::ParsePageHeader(&parse_pos, table->page_header_size_len());
664 
665   const uint8_t* page_end = page.get() + base::GetSysPageSize();
666   ASSERT_TRUE(page_header.has_value());
667   EXPECT_FALSE(page_header->lost_events);
668   EXPECT_LE(parse_pos + page_header->size, page_end);
669 
670   FtraceParseStatus status = CpuReader::ParsePagePayload(
671       parse_pos, &page_header.value(), table, &ds_config,
672       CreateBundler(ds_config), &metadata_, &last_read_event_ts_);
673 
674   EXPECT_EQ(status, FtraceParseStatus::FTRACE_STATUS_OK);
675 
676   auto bundle = GetBundle();
677   ASSERT_EQ(bundle.event().size(), 1u);
678   const protos::gen::FtraceEvent& event = bundle.event()[0];
679   EXPECT_EQ(event.pid(), 28712ul);
680   EXPECT_TRUE(WithinOneMicrosecond(event.timestamp(), 608934, 535199));
681   EXPECT_TRUE(event.print().has_buf());
682   EXPECT_EQ(event.print().buf(), "");
683 }
684 
TEST_F(CpuReaderParsePagePayloadTest,FilterByEvent)685 TEST_F(CpuReaderParsePagePayloadTest, FilterByEvent) {
686   const ExamplePage* test_case = &g_single_print;
687 
688   ProtoTranslationTable* table = GetTable(test_case->name);
689   auto page = PageFromXxd(test_case->data);
690 
691   FtraceDataSourceConfig ds_config = EmptyConfig();
692 
693   const uint8_t* parse_pos = page.get();
694   std::optional<CpuReader::PageHeader> page_header =
695       CpuReader::ParsePageHeader(&parse_pos, table->page_header_size_len());
696 
697   ASSERT_TRUE(page_header.has_value());
698   EXPECT_FALSE(page_header->lost_events);
699 
700   FtraceParseStatus status = CpuReader::ParsePagePayload(
701       parse_pos, &page_header.value(), table, &ds_config,
702       CreateBundler(ds_config), &metadata_, &last_read_event_ts_);
703 
704   EXPECT_EQ(status, FtraceParseStatus::FTRACE_STATUS_OK);
705 
706   EXPECT_THAT(AllTracePackets(), IsEmpty());
707 }
708 
709 // clang-format off
710 // # tracer: nop
711 // #
712 // # entries-in-buffer/entries-written: 3/3   #P:8
713 // #
714 // #                              _-----=> irqs-off
715 // #                             / _----=> need-resched
716 // #                            | / _---=> hardirq/softirq
717 // #                            || / _--=> preempt-depth
718 // #                            ||| /     delay
719 // #           TASK-PID   CPU#  ||||    TIMESTAMP  FUNCTION
720 // #              | |       |   ||||       |         |
721 //               sh-30693 [000] ...1 615436.216806: tracing_mark_write: Hello, world!
722 //               sh-30693 [000] ...1 615486.377232: tracing_mark_write: Good afternoon, world!
723 //               sh-30693 [000] ...1 615495.632679: tracing_mark_write: Goodbye, world!
724 // clang-format on
725 
726 static ExamplePage g_three_prints{
727     "synthetic",
728     R"(
729     00000000: a3ab 1569 bc2f 0200 9400 0000 0000 0000  ...i./..........
730     00000010: 1e00 0000 0000 0000 0800 0000 0500 0001  ................
731     00000020: e577 0000 ac5d 1661 86ff ffff 4865 6c6c  .w...].a....Hell
732     00000030: 6f2c 2077 6f72 6c64 210a 0000 5e32 6bb9  o, world!...^2k.
733     00000040: 7501 0000 0b00 0000 0500 0001 e577 0000  u............w..
734     00000050: ac5d 1661 86ff ffff 476f 6f64 2061 6674  .].a....Good aft
735     00000060: 6572 6e6f 6f6e 2c20 776f 726c 6421 0a00  ernoon, world!..
736     00000070: 0000 0000 9e6a 5df5 4400 0000 0900 0000  .....j].D.......
737     00000080: 0500 0001 e577 0000 ac5d 1661 86ff ffff  .....w...].a....
738     00000090: 476f 6f64 6279 652c 2077 6f72 6c64 210a  Goodbye, world!.
739     000000a0: 0051 0000 0000 0000 0000 0000 0000 0000  .Q..............
740   )",
741 };
742 
TEST_F(CpuReaderParsePagePayloadTest,ParseThreePrint)743 TEST_F(CpuReaderParsePagePayloadTest, ParseThreePrint) {
744   const ExamplePage* test_case = &g_three_prints;
745 
746   ProtoTranslationTable* table = GetTable(test_case->name);
747   auto page = PageFromXxd(test_case->data);
748 
749   FtraceDataSourceConfig ds_config = EmptyConfig();
750   ds_config.event_filter.AddEnabledEvent(
751       table->EventToFtraceId(GroupAndName("ftrace", "print")));
752 
753   const uint8_t* parse_pos = page.get();
754   std::optional<CpuReader::PageHeader> page_header =
755       CpuReader::ParsePageHeader(&parse_pos, table->page_header_size_len());
756 
757   const uint8_t* page_end = page.get() + base::GetSysPageSize();
758   ASSERT_TRUE(page_header.has_value());
759   EXPECT_FALSE(page_header->lost_events);
760   EXPECT_LE(parse_pos + page_header->size, page_end);
761 
762   FtraceParseStatus status = CpuReader::ParsePagePayload(
763       parse_pos, &page_header.value(), table, &ds_config,
764       CreateBundler(ds_config), &metadata_, &last_read_event_ts_);
765 
766   EXPECT_EQ(status, FtraceParseStatus::FTRACE_STATUS_OK);
767 
768   auto bundle = GetBundle();
769   ASSERT_EQ(bundle.event().size(), 3u);
770 
771   {
772     const protos::gen::FtraceEvent& event = bundle.event()[0];
773     EXPECT_EQ(event.pid(), 30693ul);
774     EXPECT_TRUE(WithinOneMicrosecond(event.timestamp(), 615436, 216806));
775     EXPECT_EQ(event.print().buf(), "Hello, world!\n");
776   }
777 
778   {
779     const protos::gen::FtraceEvent& event = bundle.event()[1];
780     EXPECT_EQ(event.pid(), 30693ul);
781     EXPECT_TRUE(WithinOneMicrosecond(event.timestamp(), 615486, 377232));
782     EXPECT_EQ(event.print().buf(), "Good afternoon, world!\n");
783   }
784 
785   {
786     const protos::gen::FtraceEvent& event = bundle.event()[2];
787     EXPECT_EQ(event.pid(), 30693ul);
788     EXPECT_TRUE(WithinOneMicrosecond(event.timestamp(), 615495, 632679));
789     EXPECT_EQ(event.print().buf(), "Goodbye, world!\n");
790   }
791 }
792 
TEST_F(CpuReaderParsePagePayloadTest,ParsePrintWithAndWithoutFilter)793 TEST_F(CpuReaderParsePagePayloadTest, ParsePrintWithAndWithoutFilter) {
794   using FtraceEventBundle = protos::gen::FtraceEventBundle;
795   using FtraceEvent = protos::gen::FtraceEvent;
796   using PrintFtraceEvent = protos::gen::PrintFtraceEvent;
797 
798   const ExamplePage* test_case = &g_three_prints;
799 
800   ProtoTranslationTable* table = GetTable(test_case->name);
801   auto page = PageFromXxd(test_case->data);
802 
803   const uint8_t* parse_pos = page.get();
804   std::optional<CpuReader::PageHeader> page_header =
805       CpuReader::ParsePageHeader(&parse_pos, table->page_header_size_len());
806 
807   const uint8_t* page_end = page.get() + base::GetSysPageSize();
808   ASSERT_TRUE(page_header.has_value());
809   ASSERT_FALSE(page_header->lost_events);
810   ASSERT_LE(parse_pos + page_header->size, page_end);
811   {
812     FtraceDataSourceConfig ds_config_no_filter = EmptyConfig();
813     ds_config_no_filter.event_filter.AddEnabledEvent(
814         table->EventToFtraceId(GroupAndName("ftrace", "print")));
815 
816     FtraceParseStatus status = CpuReader::ParsePagePayload(
817         parse_pos, &page_header.value(), table, &ds_config_no_filter,
818         CreateBundler(ds_config_no_filter), &metadata_, &last_read_event_ts_);
819     EXPECT_EQ(status, FtraceParseStatus::FTRACE_STATUS_OK);
820 
821     auto bundle = GetBundle();
822     EXPECT_THAT(
823         bundle,
824         Property(
825             &FtraceEventBundle::event,
826             ElementsAre(
827                 Property(&FtraceEvent::print,
828                          Property(&PrintFtraceEvent::buf, "Hello, world!\n")),
829                 Property(&FtraceEvent::print,
830                          Property(&PrintFtraceEvent::buf,
831                                   "Good afternoon, world!\n")),
832                 Property(&FtraceEvent::print, Property(&PrintFtraceEvent::buf,
833                                                        "Goodbye, world!\n")))));
834   }
835 
836   {
837     FtraceDataSourceConfig ds_config_with_filter = EmptyConfig();
838     ds_config_with_filter.event_filter.AddEnabledEvent(
839         table->EventToFtraceId(GroupAndName("ftrace", "print")));
840 
841     FtraceConfig::PrintFilter conf;
842     auto* rule = conf.add_rules();
843     rule->set_prefix("Good ");
844     rule->set_allow(false);
845     ds_config_with_filter.print_filter =
846         FtracePrintFilterConfig::Create(conf, table);
847     ASSERT_TRUE(ds_config_with_filter.print_filter.has_value());
848 
849     FtraceParseStatus status = CpuReader::ParsePagePayload(
850         parse_pos, &page_header.value(), table, &ds_config_with_filter,
851         CreateBundler(ds_config_with_filter), &metadata_, &last_read_event_ts_);
852     EXPECT_EQ(status, FtraceParseStatus::FTRACE_STATUS_OK);
853 
854     auto bundle = GetBundle();
855     EXPECT_THAT(
856         bundle,
857         Property(
858             &FtraceEventBundle::event,
859             ElementsAre(
860                 Property(&FtraceEvent::print,
861                          Property(&PrintFtraceEvent::buf, "Hello, world!\n")),
862                 Property(&FtraceEvent::print, Property(&PrintFtraceEvent::buf,
863                                                        "Goodbye, world!\n")))));
864   }
865 }
866 
TEST(CpuReaderTest,ProcessPagesForDataSourceNoEmptyPackets)867 TEST(CpuReaderTest, ProcessPagesForDataSourceNoEmptyPackets) {
868   const ExamplePage* test_case = &g_three_prints;
869 
870   ProtoTranslationTable* table = GetTable(test_case->name);
871   auto page = PageFromXxd(test_case->data);
872 
873   std::vector<const void*> test_page_order = {
874       page.get(), page.get(), page.get(), page.get(),
875       page.get(), page.get(), page.get(), page.get()};
876 
877   // Prepare a buffer with 8 contiguous pages, with the above contents.
878   static constexpr size_t kTestPages = 8;
879 
880   std::unique_ptr<uint8_t[]> buf(
881       new uint8_t[base::GetSysPageSize() * kTestPages]());
882   for (size_t i = 0; i < kTestPages; i++) {
883     void* dest = buf.get() + (i * base::GetSysPageSize());
884     memcpy(dest, static_cast<const void*>(test_page_order[i]),
885            base::GetSysPageSize());
886   }
887   auto compact_sched_buf = std::make_unique<CompactSchedBuffer>();
888 
889   {
890     FtraceMetadata metadata{};
891     FtraceDataSourceConfig with_filter = EmptyConfig();
892     with_filter.event_filter.AddEnabledEvent(
893         table->EventToFtraceId(GroupAndName("ftrace", "print")));
894 
895     FtraceConfig::PrintFilter conf;
896     auto* rule = conf.add_rules();
897     rule->set_prefix("");
898     rule->set_allow(false);
899     with_filter.print_filter = FtracePrintFilterConfig::Create(conf, table);
900     ASSERT_TRUE(with_filter.print_filter.has_value());
901 
902     TraceWriterForTesting trace_writer;
903     base::FlatSet<protos::pbzero::FtraceParseStatus> parse_errors;
904     uint64_t last_read_event_ts = 0;
905     bool success = CpuReader::ProcessPagesForDataSource(
906         &trace_writer, &metadata, /*cpu=*/1, &with_filter, &parse_errors,
907         &last_read_event_ts, buf.get(), kTestPages, compact_sched_buf.get(),
908         table,
909         /*symbolizer=*/nullptr,
910         /*ftrace_clock_snapshot=*/nullptr,
911         protos::pbzero::FTRACE_CLOCK_UNSPECIFIED);
912 
913     EXPECT_TRUE(success);
914 
915     // Check that the data source doesn't emit any packet, not even empty
916     // packets.
917     EXPECT_THAT(trace_writer.GetAllTracePackets(), IsEmpty());
918   }
919 
920   {
921     FtraceMetadata metadata{};
922     FtraceDataSourceConfig without_filter = EmptyConfig();
923     without_filter.event_filter.AddEnabledEvent(
924         table->EventToFtraceId(GroupAndName("ftrace", "print")));
925 
926     TraceWriterForTesting trace_writer;
927     base::FlatSet<protos::pbzero::FtraceParseStatus> parse_errors;
928     uint64_t last_read_event_ts = 0;
929     bool success = CpuReader::ProcessPagesForDataSource(
930         &trace_writer, &metadata, /*cpu=*/1, &without_filter, &parse_errors,
931         &last_read_event_ts, buf.get(), kTestPages, compact_sched_buf.get(),
932         table,
933         /*symbolizer=*/nullptr,
934         /*ftrace_clock_snapshot=*/nullptr,
935         protos::pbzero::FTRACE_CLOCK_UNSPECIFIED);
936 
937     EXPECT_TRUE(success);
938 
939     EXPECT_THAT(trace_writer.GetAllTracePackets(), Not(IsEmpty()));
940   }
941 }
942 
943 // clang-format off
944 // # tracer: nop
945 // #
946 // # entries-in-buffer/entries-written: 6/6   #P:8
947 // #
948 // #                              _-----=> irqs-off
949 // #                             / _----=> need-resched
950 // #                            | / _---=> hardirq/softirq
951 // #                            || / _--=> preempt-depth
952 // #                            ||| /     delay
953 // #           TASK-PID   CPU#  ||||    TIMESTAMP  FUNCTION
954 // #              | |       |   ||||       |         |
955 //      ksoftirqd/0-3     [000] d..3 1045157.722134: sched_switch: prev_comm=ksoftirqd/0 prev_pid=3 prev_prio=120 prev_state=S ==> next_comm=sleep next_pid=3733 next_prio=120
956 //            sleep-3733  [000] d..3 1045157.725035: sched_switch: prev_comm=sleep prev_pid=3733 prev_prio=120 prev_state=R+ ==> next_comm=rcuop/0 next_pid=10 next_prio=120
957 //      rcu_preempt-7     [000] d..3 1045157.725182: sched_switch: prev_comm=rcu_preempt prev_pid=7 prev_prio=120 prev_state=S ==> next_comm=sleep next_pid=3733 next_prio=120
958 //            sleep-3733  [000] d..3 1045157.725671: sched_switch: prev_comm=sleep prev_pid=3733 prev_prio=120 prev_state=R+ ==> next_comm=sh next_pid=3513 next_prio=120
959 //               sh-3513  [000] d..3 1045157.726668: sched_switch: prev_comm=sh prev_pid=3513 prev_prio=120 prev_state=S ==> next_comm=sleep next_pid=3733 next_prio=120
960 //            sleep-3733  [000] d..3 1045157.726697: sched_switch: prev_comm=sleep prev_pid=3733 prev_prio=120 prev_state=x ==> next_comm=kworker/u16:3 next_pid=3681 next_prio=120
961 // clang-format on
962 
963 static ExamplePage g_six_sched_switch{
964     "synthetic",
965     R"(
966     00000000: 2b16 c3be 90b6 0300 a001 0000 0000 0000  +...............
967     00000010: 1e00 0000 0000 0000 1000 0000 2f00 0103  ............/...
968     00000020: 0300 0000 6b73 6f66 7469 7271 642f 3000  ....ksoftirqd/0.
969     00000030: 0000 0000 0300 0000 7800 0000 0100 0000  ........x.......
970     00000040: 0000 0000 736c 6565 7000 722f 3000 0000  ....sleep.r/0...
971     00000050: 0000 0000 950e 0000 7800 0000 b072 8805  ........x....r..
972     00000060: 2f00 0103 950e 0000 736c 6565 7000 722f  /.......sleep.r/
973     00000070: 3000 0000 0000 0000 950e 0000 7800 0000  0...........x...
974     00000080: 0008 0000 0000 0000 7263 756f 702f 3000  ........rcuop/0.
975     00000090: 0000 0000 0000 0000 0a00 0000 7800 0000  ............x...
976     000000a0: f0b0 4700 2f00 0103 0700 0000 7263 755f  ..G./.......rcu_
977     000000b0: 7072 6565 6d70 7400 0000 0000 0700 0000  preempt.........
978     000000c0: 7800 0000 0100 0000 0000 0000 736c 6565  x...........slee
979     000000d0: 7000 722f 3000 0000 0000 0000 950e 0000  p.r/0...........
980     000000e0: 7800 0000 1001 ef00 2f00 0103 950e 0000  x......./.......
981     000000f0: 736c 6565 7000 722f 3000 0000 0000 0000  sleep.r/0.......
982     00000100: 950e 0000 7800 0000 0008 0000 0000 0000  ....x...........
983     00000110: 7368 0064 0065 722f 3000 0000 0000 0000  sh.d.er/0.......
984     00000120: b90d 0000 7800 0000 f0c7 e601 2f00 0103  ....x......./...
985     00000130: b90d 0000 7368 0064 0065 722f 3000 0000  ....sh.d.er/0...
986     00000140: 0000 0000 b90d 0000 7800 0000 0100 0000  ........x.......
987     00000150: 0000 0000 736c 6565 7000 722f 3000 0000  ....sleep.r/0...
988     00000160: 0000 0000 950e 0000 7800 0000 d030 0e00  ........x....0..
989     00000170: 2f00 0103 950e 0000 736c 6565 7000 722f  /.......sleep.r/
990     00000180: 3000 0000 0000 0000 950e 0000 7800 0000  0...........x...
991     00000190: 4000 0000 0000 0000 6b77 6f72 6b65 722f  @.......kworker/
992     000001a0: 7531 363a 3300 0000 610e 0000 7800 0000  u16:3...a...x...
993     000001b0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
994     )",
995 };
996 
TEST_F(CpuReaderParsePagePayloadTest,ParseSixSchedSwitch)997 TEST_F(CpuReaderParsePagePayloadTest, ParseSixSchedSwitch) {
998   const ExamplePage* test_case = &g_six_sched_switch;
999 
1000   ProtoTranslationTable* table = GetTable(test_case->name);
1001   auto page = PageFromXxd(test_case->data);
1002 
1003   FtraceDataSourceConfig ds_config = EmptyConfig();
1004   ds_config.event_filter.AddEnabledEvent(
1005       table->EventToFtraceId(GroupAndName("sched", "sched_switch")));
1006 
1007   const uint8_t* parse_pos = page.get();
1008   std::optional<CpuReader::PageHeader> page_header =
1009       CpuReader::ParsePageHeader(&parse_pos, table->page_header_size_len());
1010 
1011   const uint8_t* page_end = page.get() + base::GetSysPageSize();
1012   ASSERT_TRUE(page_header.has_value());
1013   EXPECT_FALSE(page_header->lost_events);
1014   EXPECT_LE(parse_pos + page_header->size, page_end);
1015 
1016   FtraceParseStatus status = CpuReader::ParsePagePayload(
1017       parse_pos, &page_header.value(), table, &ds_config,
1018       CreateBundler(ds_config), &metadata_, &last_read_event_ts_);
1019 
1020   EXPECT_EQ(status, FtraceParseStatus::FTRACE_STATUS_OK);
1021   EXPECT_EQ(last_read_event_ts_, 1'045'157'726'697'236ULL);
1022 
1023   auto bundle = GetBundle();
1024   EXPECT_EQ(0u, bundle.last_read_event_timestamp());
1025   ASSERT_EQ(bundle.event().size(), 6u);
1026   {
1027     const protos::gen::FtraceEvent& event = bundle.event()[1];
1028     EXPECT_EQ(event.pid(), 3733ul);
1029     EXPECT_TRUE(WithinOneMicrosecond(event.timestamp(), 1045157, 725035));
1030     EXPECT_EQ(event.sched_switch().prev_comm(), "sleep");
1031     EXPECT_EQ(event.sched_switch().prev_pid(), 3733);
1032     EXPECT_EQ(event.sched_switch().prev_prio(), 120);
1033     EXPECT_EQ(event.sched_switch().next_comm(), "rcuop/0");
1034     EXPECT_EQ(event.sched_switch().next_pid(), 10);
1035     EXPECT_EQ(event.sched_switch().next_prio(), 120);
1036   }
1037 }
1038 
TEST_F(CpuReaderParsePagePayloadTest,ParseSixSchedSwitchCompactFormat)1039 TEST_F(CpuReaderParsePagePayloadTest, ParseSixSchedSwitchCompactFormat) {
1040   const ExamplePage* test_case = &g_six_sched_switch;
1041 
1042   ProtoTranslationTable* table = GetTable(test_case->name);
1043   auto page = PageFromXxd(test_case->data);
1044 
1045   FtraceDataSourceConfig ds_config{EventFilter{},
1046                                    EventFilter{},
1047                                    EnabledCompactSchedConfigForTesting(),
1048                                    std::nullopt,
1049                                    {},
1050                                    {},
1051                                    false /* symbolize_ksyms*/,
1052                                    false /*preserve_ftrace_buffer*/,
1053                                    {}};
1054   ds_config.event_filter.AddEnabledEvent(
1055       table->EventToFtraceId(GroupAndName("sched", "sched_switch")));
1056 
1057   const uint8_t* parse_pos = page.get();
1058   std::optional<CpuReader::PageHeader> page_header =
1059       CpuReader::ParsePageHeader(&parse_pos, table->page_header_size_len());
1060 
1061   const uint8_t* page_end = page.get() + base::GetSysPageSize();
1062   ASSERT_TRUE(page_header.has_value());
1063   EXPECT_FALSE(page_header->lost_events);
1064   EXPECT_LE(parse_pos + page_header->size, page_end);
1065 
1066   FtraceParseStatus status = CpuReader::ParsePagePayload(
1067       parse_pos, &page_header.value(), table, &ds_config,
1068       CreateBundler(ds_config), &metadata_, &last_read_event_ts_);
1069 
1070   EXPECT_EQ(status, FtraceParseStatus::FTRACE_STATUS_OK);
1071   EXPECT_EQ(last_read_event_ts_, 1'045'157'726'697'236ULL);
1072 
1073   // sched switch fields were buffered:
1074   EXPECT_LT(0u, bundler_->compact_sched_buf()->sched_switch().size());
1075   EXPECT_LT(0u,
1076             bundler_->compact_sched_buf()->interner().interned_comms_size());
1077 
1078   // Write the buffer out & check the serialized format:
1079   auto bundle = GetBundle();
1080 
1081   const auto& compact_sched = bundle.compact_sched();
1082   EXPECT_EQ(0u, bundle.last_read_event_timestamp());
1083 
1084   EXPECT_EQ(6u, compact_sched.switch_timestamp().size());
1085   EXPECT_EQ(6u, compact_sched.switch_prev_state().size());
1086   EXPECT_EQ(6u, compact_sched.switch_next_pid().size());
1087   EXPECT_EQ(6u, compact_sched.switch_next_prio().size());
1088   // 4 unique interned next_comm strings:
1089   EXPECT_EQ(4u, compact_sched.intern_table().size());
1090   EXPECT_EQ(6u, compact_sched.switch_next_comm_index().size());
1091 
1092   // First event exactly as expected (absolute timestamp):
1093   EXPECT_TRUE(WithinOneMicrosecond(compact_sched.switch_timestamp()[0], 1045157,
1094                                    722134));
1095   EXPECT_EQ(1, compact_sched.switch_prev_state()[0]);
1096   EXPECT_EQ(3733, compact_sched.switch_next_pid()[0]);
1097   EXPECT_EQ(120, compact_sched.switch_next_prio()[0]);
1098   auto comm_intern_idx = compact_sched.switch_next_comm_index()[0];
1099   std::string next_comm = compact_sched.intern_table()[comm_intern_idx];
1100   EXPECT_EQ("sleep", next_comm);
1101 }
1102 
1103 // clang-format off
1104 // # tracer: nop
1105 // #
1106 // # entries-in-buffer/entries-written: 23/23   #P:8
1107 // #
1108 // #                                _-----=> irqs-off/BH-disabled
1109 // #                               / _----=> need-resched
1110 // #                              | / _---=> hardirq/softirq
1111 // #                              || / _--=> preempt-depth
1112 // #                              ||| / _-=> migrate-disable
1113 // #                              |||| /     delay
1114 // #           TASK-PID     CPU#  |||||  TIMESTAMP  FUNCTION
1115 // #              | |         |   |||||     |         |
1116 //           <idle>-0       [000] d..2. 701500.111507: sched_switch: prev_comm=swapper/0 prev_pid=0 prev_prio=120 prev_state=R ==> next_comm=bash next_pid=219057 next_prio=120
1117 //               ls-219057  [000] d..3. 701500.115222: sched_waking: comm=kworker/u16:17 pid=203967 prio=120 target_cpu=006
1118 //               ls-219057  [000] d..3. 701500.115327: sched_waking: comm=kworker/u16:17 pid=203967 prio=120 target_cpu=006
1119 //               ls-219057  [000] d..3. 701500.115412: sched_waking: comm=kworker/u16:5 pid=205556 prio=120 target_cpu=004
1120 //               ls-219057  [000] d..3. 701500.115416: sched_waking: comm=kworker/u16:17 pid=203967 prio=120 target_cpu=006
1121 //               ls-219057  [000] dN.5. 701500.115801: sched_waking: comm=bash pid=217958 prio=120 target_cpu=006
1122 //               ls-219057  [000] d..2. 701500.115817: sched_switch: prev_comm=ls prev_pid=219057 prev_prio=120 prev_state=Z ==> next_comm=swapper/0 next_pid=0 next_prio=120
1123 // clang-format on
1124 
1125 static ExamplePage g_sched_page{
1126     "synthetic_alt",
1127     R"(
1128     00000000: 67ce f4b8 027e 0200 5801 0000 0000 0000  g....~..X.......
1129     00000010: 1e00 0000 0000 0000 1000 0000 3d01 0102  ............=...
1130     00000020: 0000 0000 7377 6170 7065 722f 3000 0000  ....swapper/0...
1131     00000030: 0000 0000 0000 0000 7800 0000 0000 0000  ........x.......
1132     00000040: 0000 0000 6261 7368 0000 0000 0000 0000  ....bash........
1133     00000050: 0000 0000 b157 0300 7800 0000 a9d2 1507  .....W..x.......
1134     00000060: 4001 0103 b157 0300 6b77 6f72 6b65 722f  @....W..kworker/
1135     00000070: 7531 363a 3137 0000 bf1c 0300 7800 0000  u16:17......x...
1136     00000080: 0600 0000 c953 3300 4001 0103 b157 0300  .....S3.@....W..
1137     00000090: 6b77 6f72 6b65 722f 7531 363a 3137 0000  kworker/u16:17..
1138     000000a0: bf1c 0300 7800 0000 0600 0000 0981 2900  ....x.........).
1139     000000b0: 4001 0103 b157 0300 6b77 6f72 6b65 722f  @....W..kworker/
1140     000000c0: 7531 363a 3500 0000 f422 0300 7800 0000  u16:5...."..x...
1141     000000d0: 0400 0000 89e0 0100 4001 0103 b157 0300  ........@....W..
1142     000000e0: 6b77 6f72 6b65 722f 7531 363a 3137 0000  kworker/u16:17..
1143     000000f0: bf1c 0300 7800 0000 0600 0000 e92c bc00  ....x........,..
1144     00000100: 4001 2505 b157 0300 6261 7368 0000 0000  @.%..W..bash....
1145     00000110: 0000 0000 0000 0000 6653 0300 7800 0000  ........fS..x...
1146     00000120: 0600 0000 10f8 0700 3d01 0102 b157 0300  ........=....W..
1147     00000130: 6c73 0000 0000 0000 0000 0000 0000 0000  ls..............
1148     00000140: b157 0300 7800 0000 2000 0000 0000 0000  .W..x... .......
1149     00000150: 7377 6170 7065 722f 3000 0000 0000 0000  swapper/0.......
1150     00000160: 0000 0000 7800 0000 0000 0000 0000 0000  ....x...........
1151     )",
1152 };
1153 
TEST_F(CpuReaderParsePagePayloadTest,ParseCompactSchedSwitchAndWaking)1154 TEST_F(CpuReaderParsePagePayloadTest, ParseCompactSchedSwitchAndWaking) {
1155   const ExamplePage* test_case = &g_sched_page;
1156 
1157   ProtoTranslationTable* table = GetTable(test_case->name);
1158   auto page = PageFromXxd(test_case->data);
1159 
1160   FtraceDataSourceConfig ds_config{EventFilter{},
1161                                    EventFilter{},
1162                                    EnabledCompactSchedConfigForTesting(),
1163                                    std::nullopt,
1164                                    {},
1165                                    {},
1166                                    false /* symbolize_ksyms*/,
1167                                    false /*preserve_ftrace_buffer*/,
1168                                    {}};
1169   ds_config.event_filter.AddEnabledEvent(
1170       table->EventToFtraceId(GroupAndName("sched", "sched_switch")));
1171   ds_config.event_filter.AddEnabledEvent(
1172       table->EventToFtraceId(GroupAndName("sched", "sched_waking")));
1173 
1174   const uint8_t* parse_pos = page.get();
1175   std::optional<CpuReader::PageHeader> page_header =
1176       CpuReader::ParsePageHeader(&parse_pos, table->page_header_size_len());
1177 
1178   const uint8_t* page_end = page.get() + base::GetSysPageSize();
1179   ASSERT_TRUE(page_header.has_value());
1180   EXPECT_FALSE(page_header->lost_events);
1181   EXPECT_LE(parse_pos + page_header->size, page_end);
1182 
1183   FtraceParseStatus status = CpuReader::ParsePagePayload(
1184       parse_pos, &page_header.value(), table, &ds_config,
1185       CreateBundler(ds_config), &metadata_, &last_read_event_ts_);
1186 
1187   EXPECT_EQ(status, FtraceParseStatus::FTRACE_STATUS_OK);
1188 
1189   // sched fields were buffered:
1190   EXPECT_LT(0u, bundler_->compact_sched_buf()->sched_switch().size());
1191   EXPECT_LT(0u, bundler_->compact_sched_buf()->sched_waking().size());
1192   EXPECT_LT(0u,
1193             bundler_->compact_sched_buf()->interner().interned_comms_size());
1194 
1195   // Write the buffer out & check the serialized format:
1196   auto bundle = GetBundle();
1197   const auto& compact_sched = bundle.compact_sched();
1198   // 2 sched_switch events:
1199   EXPECT_EQ(2u, compact_sched.switch_timestamp().size());
1200   EXPECT_EQ(2u, compact_sched.switch_prev_state().size());
1201   EXPECT_EQ(2u, compact_sched.switch_next_pid().size());
1202   EXPECT_EQ(2u, compact_sched.switch_next_prio().size());
1203   EXPECT_EQ(2u, compact_sched.switch_next_comm_index().size());
1204   // 5 sched_waking events:
1205   EXPECT_EQ(5u, compact_sched.waking_timestamp().size());
1206   EXPECT_EQ(5u, compact_sched.waking_pid().size());
1207   EXPECT_EQ(5u, compact_sched.waking_target_cpu().size());
1208   EXPECT_EQ(5u, compact_sched.waking_prio().size());
1209   EXPECT_EQ(5u, compact_sched.waking_comm_index().size());
1210   EXPECT_EQ(5u, compact_sched.waking_common_flags().size());
1211   // 4 unique interned comm strings:
1212   EXPECT_EQ(4u, compact_sched.intern_table().size());
1213 
1214   // First sched waking as expected:
1215   EXPECT_EQ(compact_sched.waking_timestamp()[0], 701500115221756ull);
1216   EXPECT_EQ(compact_sched.waking_pid()[0], 203967);
1217   EXPECT_EQ(compact_sched.waking_target_cpu()[0], 6);
1218   EXPECT_EQ(compact_sched.waking_prio()[0], 120);
1219   EXPECT_EQ(compact_sched.waking_common_flags()[0], 1u);
1220   auto comm_intern_idx = compact_sched.waking_comm_index()[0];
1221   std::string comm = compact_sched.intern_table()[comm_intern_idx];
1222   EXPECT_EQ("kworker/u16:17", comm);
1223 }
1224 
1225 TEST_F(CpuReaderTableTest, ParseAllFields) {
1226   using FakeEventProvider =
1227       ProtoProvider<pbzero::FakeFtraceEvent, gen::FakeFtraceEvent>;
1228 
1229   uint16_t ftrace_event_id = 102;
1230 
1231   std::vector<Field> common_fields;
1232   {
1233     common_fields.emplace_back(Field{});
1234     Field* field = &common_fields.back();
1235     field->ftrace_offset = 4;
1236     field->ftrace_size = 4;
1237     field->ftrace_type = kFtraceCommonPid32;
1238     field->proto_field_id = 2;
1239     field->proto_field_type = ProtoSchemaType::kInt32;
1240     SetTranslationStrategy(field->ftrace_type, field->proto_field_type,
1241                            &field->strategy);
1242   }
1243 
1244   std::vector<Event> events;
1245   events.emplace_back(Event{});
1246   {
1247     Event* event = &events.back();
1248     event->name = "";
1249     event->group = "";
1250     event->proto_field_id = 42;
1251     event->ftrace_event_id = ftrace_event_id;
1252 
1253     {
1254       // uint32 -> uint32
1255       event->fields.emplace_back(Field{});
1256       Field* field = &event->fields.back();
1257       field->ftrace_offset = 8;
1258       field->ftrace_size = 4;
1259       field->ftrace_type = kFtraceUint32;
1260       field->proto_field_id = 1;
1261       field->proto_field_type = ProtoSchemaType::kUint32;
1262     }
1263 
1264     {
1265       // pid32 -> uint32
1266       event->fields.emplace_back(Field{});
1267       Field* field = &event->fields.back();
1268       field->ftrace_offset = 12;
1269       field->ftrace_size = 4;
1270       field->ftrace_type = kFtracePid32;
1271       field->proto_field_id = 2;
1272       field->proto_field_type = ProtoSchemaType::kInt32;
1273     }
1274 
1275     {
1276       // dev32 -> uint64
1277       event->fields.emplace_back(Field{});
1278       Field* field = &event->fields.back();
1279       field->ftrace_offset = 16;
1280       field->ftrace_size = 4;
1281       field->ftrace_type = kFtraceDevId32;
1282       field->proto_field_id = 3;
1283       field->proto_field_type = ProtoSchemaType::kUint64;
1284     }
1285 
1286     {
1287       // ino_t (32bit) -> uint64
1288       event->fields.emplace_back(Field{});
1289       Field* field = &event->fields.back();
1290       field->ftrace_offset = 20;
1291       field->ftrace_size = 4;
1292       field->ftrace_type = kFtraceInode32;
1293       field->proto_field_id = 4;
1294       field->proto_field_type = ProtoSchemaType::kUint64;
1295     }
1296 
1297     {
1298       // dev64 -> uint64
1299       event->fields.emplace_back(Field{});
1300       Field* field = &event->fields.back();
1301       field->ftrace_offset = 24;
1302       field->ftrace_size = 8;
1303       field->ftrace_type = kFtraceDevId64;
1304       field->proto_field_id = 5;
1305       field->proto_field_type = ProtoSchemaType::kUint64;
1306     }
1307 
1308     {
1309       // ino_t (64bit) -> uint64
1310       event->fields.emplace_back(Field{});
1311       Field* field = &event->fields.back();
1312       field->ftrace_offset = 32;
1313       field->ftrace_size = 8;
1314       field->ftrace_type = kFtraceInode64;
1315       field->proto_field_id = 6;
1316       field->proto_field_type = ProtoSchemaType::kUint64;
1317     }
1318 
1319     {
1320       // char[16] -> string
1321       event->fields.emplace_back(Field{});
1322       Field* field = &event->fields.back();
1323       field->ftrace_offset = 40;
1324       field->ftrace_size = 16;
1325       field->ftrace_type = kFtraceFixedCString;
1326       field->proto_field_id = 500;
1327       field->proto_field_type = ProtoSchemaType::kString;
1328     }
1329 
1330     {
1331       // char* -> string
1332       event->fields.emplace_back(Field{});
1333       Field* field = &event->fields.back();
1334       field->ftrace_offset = 56;
1335       field->ftrace_size = 8;
1336       field->ftrace_type = kFtraceStringPtr;
1337       field->proto_field_id = 503;
1338       field->proto_field_type = ProtoSchemaType::kString;
1339     }
1340 
1341     {
1342       // dataloc -> string
1343       event->fields.emplace_back(Field{});
1344       Field* field = &event->fields.back();
1345       field->ftrace_offset = 65;
1346       field->ftrace_size = 4;
1347       field->ftrace_type = kFtraceDataLoc;
1348       field->proto_field_id = 502;
1349       field->proto_field_type = ProtoSchemaType::kString;
1350     }
1351 
1352     {
1353       // char -> string
1354       event->fields.emplace_back(Field{});
1355       Field* field = &event->fields.back();
1356       field->ftrace_offset = 69;
1357       field->ftrace_size = 0;
1358       field->ftrace_type = kFtraceCString;
1359       field->proto_field_id = 501;
1360       field->proto_field_type = ProtoSchemaType::kString;
1361     }
1362 
1363     for (Field& field : event->fields) {
1364       SetTranslationStrategy(field.ftrace_type, field.proto_field_type,
1365                              &field.strategy);
1366     }
1367   }
1368 
1369   PrintkMap printk_formats;
1370   printk_formats.insert(0xffffff8504f51b23, "my_printk_format_string");
1371   ProtoTranslationTable table(
1372       &ftrace_, events, std::move(common_fields),
1373       ProtoTranslationTable::DefaultPageHeaderSpecForTesting(),
1374       InvalidCompactSchedEventFormatForTesting(), printk_formats);
1375   FtraceDataSourceConfig ds_config = EmptyConfig();
1376 
1377   FakeEventProvider provider(base::GetSysPageSize());
1378 
1379   BinaryWriter writer;
1380 
1381   // Must use the bit masks to translate between kernel and userspace device ids
1382   // to generate the below examples
1383   const uint32_t kKernelBlockDeviceId = 271581216;
1384 
1385   const BlockDeviceID kUserspaceBlockDeviceId =
1386       CpuReader::TranslateBlockDeviceIDToUserspace<BlockDeviceID>(
1387           kKernelBlockDeviceId);
1388   const uint64_t k64BitKernelBlockDeviceId = 4442450946;
1389   const BlockDeviceID k64BitUserspaceBlockDeviceId =
1390       CpuReader::TranslateBlockDeviceIDToUserspace<uint64_t>(
1391           k64BitKernelBlockDeviceId);
1392 
1393   writer.Write<int32_t>(1001);                       // Common field.
1394   writer.Write<int32_t>(9999);                       // Common pid
1395   writer.Write<int32_t>(1003);                       // Uint32 field
1396   writer.Write<int32_t>(97);                         // Pid
1397   writer.Write<int32_t>(kKernelBlockDeviceId);       // Dev id
1398   writer.Write<int32_t>(98);                         // Inode 32
1399   writer.Write<int64_t>(k64BitKernelBlockDeviceId);  // Dev id 64
1400   writer.Write<int64_t>(99u);                        // Inode 64
1401   writer.WriteFixedString(16, "Hello");
1402   writer.Write<uint64_t>(0xffffff8504f51b23ULL);  // char* (printk formats)
1403   writer.Write<uint8_t>(0);                       // Deliberately mis-aligning.
1404   writer.Write<uint32_t>(40 | 6 << 16);
1405   writer.WriteFixedString(300, "Goodbye");
1406 
1407   auto input = writer.GetCopy();
1408   auto length = writer.written();
1409   FtraceMetadata metadata{};
1410 
1411   ASSERT_TRUE(CpuReader::ParseEvent(ftrace_event_id, input.get(),
1412                                     input.get() + length, &table, &ds_config,
1413                                     provider.writer(), &metadata));
1414 
1415   auto event = provider.ParseProto();
1416   ASSERT_TRUE(event);
1417   EXPECT_EQ(event->common_pid(), 9999ul);
1418   EXPECT_TRUE(event->has_all_fields());
1419   EXPECT_EQ(event->all_fields().field_uint32(), 1003u);
1420   EXPECT_EQ(event->all_fields().field_pid(), 97);
1421   EXPECT_EQ(event->all_fields().field_dev_32(),
1422             static_cast<uint32_t>(kUserspaceBlockDeviceId));
1423   EXPECT_EQ(event->all_fields().field_inode_32(), 98u);
1424 // TODO(primiano): for some reason this fails on mac.
1425 #if !PERFETTO_BUILDFLAG(PERFETTO_OS_APPLE)
1426   EXPECT_EQ(event->all_fields().field_dev_64(), k64BitUserspaceBlockDeviceId);
1427 #endif
1428   EXPECT_EQ(event->all_fields().field_inode_64(), 99u);
1429   EXPECT_EQ(event->all_fields().field_char_16(), "Hello");
1430   EXPECT_EQ(event->all_fields().field_char(), "Goodbye");
1431   EXPECT_EQ(event->all_fields().field_data_loc(), "Hello");
1432   EXPECT_EQ(event->all_fields().field_char_star(), "my_printk_format_string");
1433   EXPECT_THAT(metadata.pids, Contains(97));
1434   EXPECT_EQ(metadata.inode_and_device.size(), 2U);
1435   EXPECT_THAT(metadata.inode_and_device,
1436               Contains(Pair(98u, kUserspaceBlockDeviceId)));
1437   EXPECT_THAT(metadata.inode_and_device,
1438               Contains(Pair(99u, k64BitUserspaceBlockDeviceId)));
1439 }
1440 
1441 TEST(CpuReaderTest, SysEnterEvent) {
1442   BinaryWriter writer;
1443   ProtoTranslationTable* table = GetTable("synthetic");
1444   FtraceDataSourceConfig ds_config = EmptyConfig();
1445 
1446   const auto kSysEnterId = static_cast<uint16_t>(
1447       table->EventToFtraceId(GroupAndName("raw_syscalls", "sys_enter")));
1448   ASSERT_GT(kSysEnterId, 0ul);
1449   constexpr uint32_t kPid = 23;
1450   constexpr uint32_t kFd = 7;
1451   constexpr auto kSyscall = SYS_close;
1452 
1453   writer.Write<int32_t>(1001);      // Common field.
1454   writer.Write<int32_t>(kPid);      // Common pid
1455   writer.Write<int64_t>(kSyscall);  // id
1456   for (uint32_t i = 0; i < 6; ++i) {
1457     writer.Write<uint64_t>(kFd + i);  // args
1458   }
1459 
1460   auto input = writer.GetCopy();
1461   auto length = writer.written();
1462 
1463   BundleProvider bundle_provider(base::GetSysPageSize());
1464   FtraceMetadata metadata{};
1465 
1466   ASSERT_TRUE(CpuReader::ParseEvent(
1467       kSysEnterId, input.get(), input.get() + length, table, &ds_config,
1468       bundle_provider.writer()->add_event(), &metadata));
1469 
1470   std::unique_ptr<protos::gen::FtraceEventBundle> a =
1471       bundle_provider.ParseProto();
1472   ASSERT_NE(a, nullptr);
1473   ASSERT_EQ(a->event().size(), 1u);
1474   const auto& event = a->event()[0].sys_enter();
1475   EXPECT_EQ(event.id(), kSyscall);
1476   for (uint32_t i = 0; i < 6; ++i) {
1477     EXPECT_EQ(event.args()[i], kFd + i);
1478   }
1479 }
1480 
1481 // MacOS fails on this ...but MacOS will never use cpu_reader so it's
1482 // not a big problem.
1483 #if PERFETTO_BUILDFLAG(PERFETTO_OS_APPLE)
1484 #define MAYBE_SysExitEvent DISABLED_SysExitEvent
1485 #else
1486 #define MAYBE_SysExitEvent SysExitEvent
1487 #endif
1488 TEST(CpuReaderTest, MAYBE_SysExitEvent) {
1489   BinaryWriter writer;
1490   ProtoTranslationTable* table = GetTable("synthetic");
1491   FtraceDataSourceConfig ds_config = EmptyConfig();
1492   const auto syscalls = SyscallTable::FromCurrentArch();
1493 
1494   const auto kSysExitId = static_cast<uint16_t>(
1495       table->EventToFtraceId(GroupAndName("raw_syscalls", "sys_exit")));
1496   ASSERT_GT(kSysExitId, 0ul);
1497   constexpr pid_t kPid = 23;
1498   constexpr int64_t kFd = 2;
1499 
1500   ds_config.syscalls_returning_fd =
1501       FtraceConfigMuxer::GetSyscallsReturningFds(syscalls);
1502   ASSERT_FALSE(ds_config.syscalls_returning_fd.empty());
1503   const auto syscall_id = *ds_config.syscalls_returning_fd.begin();
1504 
1505   writer.Write<int32_t>(1001);        // Common field.
1506   writer.Write<int32_t>(kPid);        // Common pid
1507   writer.Write<int64_t>(syscall_id);  // id
1508   writer.Write<int64_t>(kFd);         // ret
1509 
1510   auto input = writer.GetCopy();
1511   auto length = writer.written();
1512   BundleProvider bundle_provider(base::GetSysPageSize());
1513   FtraceMetadata metadata{};
1514 
1515   ASSERT_TRUE(CpuReader::ParseEvent(
1516       kSysExitId, input.get(), input.get() + length, table, &ds_config,
1517       bundle_provider.writer()->add_event(), &metadata));
1518 
1519   std::unique_ptr<protos::gen::FtraceEventBundle> a =
1520       bundle_provider.ParseProto();
1521   ASSERT_NE(a, nullptr);
1522   ASSERT_EQ(a->event().size(), 1u);
1523   const auto& event = a->event()[0].sys_exit();
1524   EXPECT_EQ(event.id(), syscall_id);
1525   EXPECT_EQ(event.ret(), kFd);
1526   EXPECT_THAT(metadata.fds, Contains(std::make_pair(kPid, kFd)));
1527 }
1528 
1529 TEST(CpuReaderTest, TaskRenameEvent) {
1530   BundleProvider bundle_provider(base::GetSysPageSize());
1531 
1532   BinaryWriter writer;
1533   ProtoTranslationTable* table = GetTable("android_seed_N2F62_3.10.49");
1534   FtraceDataSourceConfig ds_config = EmptyConfig();
1535 
1536   constexpr uint32_t kTaskRenameId = 19;
1537 
1538   writer.Write<int32_t>(1001);             // Common field.
1539   writer.Write<int32_t>(9999);             // Common pid
1540   writer.Write<int32_t>(9999);             // Pid
1541   writer.WriteFixedString(16, "Hello");    // Old Comm
1542   writer.WriteFixedString(16, "Goodbye");  // New Comm
1543   writer.Write<uint64_t>(10);              // flags
1544   writer.Write<int16_t>(10);               // oom_score_adj
1545 
1546   auto input = writer.GetCopy();
1547   auto length = writer.written();
1548   FtraceMetadata metadata{};
1549 
1550   ASSERT_TRUE(CpuReader::ParseEvent(kTaskRenameId, input.get(),
1551                                     input.get() + length, table, &ds_config,
1552                                     bundle_provider.writer(), &metadata));
1553   EXPECT_THAT(metadata.rename_pids, Contains(9999));
1554   EXPECT_THAT(metadata.pids, Contains(9999));
1555 }
1556 
1557 // Regression test for b/205763418: Kernels without f0a515780393("tracing: Don't
1558 // make assumptions about length of string on task rename") can output non
1559 // zero-terminated strings in some cases. Even though it's a kernel bug, there's
1560 // no point in rejecting that.
1561 TEST(CpuReaderTest, EventNonZeroTerminated) {
1562   BundleProvider bundle_provider(base::GetSysPageSize());
1563 
1564   BinaryWriter writer;
1565   ProtoTranslationTable* table = GetTable("android_seed_N2F62_3.10.49");
1566   FtraceDataSourceConfig ds_config = EmptyConfig();
1567 
1568   constexpr uint32_t kTaskRenameId = 19;
1569 
1570   writer.Write<int32_t>(1001);           // Common field.
1571   writer.Write<int32_t>(9999);           // Common pid
1572   writer.Write<int32_t>(9999);           // Pid
1573   writer.WriteFixedString(16, "Hello");  // Old Comm
1574   std::array<char, 16> newcomm;
1575   memcpy(&newcomm, "0123456789abcdef", sizeof newcomm);
1576   writer.Write(newcomm);       // New Comm - not null terminated
1577   writer.Write<uint64_t>(10);  // flags
1578   writer.Write<int16_t>(10);   // oom_score_adj
1579 
1580   auto input = writer.GetCopy();
1581   auto length = writer.written();
1582   FtraceMetadata metadata{};
1583 
1584   ASSERT_TRUE(CpuReader::ParseEvent(
1585       kTaskRenameId, input.get(), input.get() + length, table, &ds_config,
1586       bundle_provider.writer()->add_event(), &metadata));
1587   std::unique_ptr<protos::gen::FtraceEventBundle> a =
1588       bundle_provider.ParseProto();
1589   ASSERT_NE(a, nullptr);
1590   ASSERT_EQ(a->event().size(), 1u);
1591   ASSERT_EQ(a->event()[0].task_rename().newcomm(), "0123456789abcdef");
1592 }
1593 
1594 // Page with a single sched_switch, no data loss.
1595 static char g_switch_page[] =
1596     R"(
1597     00000000: 2b16 c3be 90b6 0300 4c00 0000 0000 0000  ................
1598     00000010: 1e00 0000 0000 0000 1000 0000 2f00 0103  ................
1599     00000020: 0300 0000 6b73 6f66 7469 7271 642f 3000  ................
1600     00000030: 0000 0000 0300 0000 7800 0000 0100 0000  ................
1601     00000040: 0000 0000 736c 6565 7000 722f 3000 0000  ................
1602     00000050: 0000 0000 950e 0000 7800 0000 0000 0000  ................
1603     )";
1604 
1605 // Page with a single sched_switch, header has data loss flag set.
1606 static char g_switch_page_lost_events[] =
1607     R"(
1608     00000000: 2b16 c3be 90b6 0300 4c00 0080 ffff ffff  ................
1609     00000010: 1e00 0000 0000 0000 1000 0000 2f00 0103  ................
1610     00000020: 0300 0000 6b73 6f66 7469 7271 642f 3000  ................
1611     00000030: 0000 0000 0300 0000 7800 0000 0100 0000  ................
1612     00000040: 0000 0000 736c 6565 7000 722f 3000 0000  ................
1613     00000050: 0000 0000 950e 0000 7800 0000 0000 0000  ................
1614     )";
1615 
1616 // Page with invalid data.
1617 static char g_invalid_page[] =
1618     R"(
1619     00000000: 2b16 c3be 90b6 0300 4b00 0000 0000 0000  ................
1620     00000010: 1e00 0000 0000 0000 1000 0000 2f00 0103  ................
1621     00000020: 0300 0000 6b73 6f66 7469 7271 642f 3000  ................
1622     00000030: 0000 0000 0300 0000 7800 0000 0100 0000  ................
1623     00000040: 0000 0000 736c 6565 7000 722f 3000 0000  ................
1624     00000050: 0000 0000 950e 0000 7800 0000 0000 0000  ................
1625     )";
1626 
1627 TEST(CpuReaderTest, NewPacketOnLostEvents) {
1628   auto page_ok = PageFromXxd(g_switch_page);
1629   auto page_loss = PageFromXxd(g_switch_page_lost_events);
1630 
1631   std::vector<const void*> test_page_order = {
1632       page_ok.get(),   page_ok.get(), page_ok.get(), page_loss.get(),
1633       page_loss.get(), page_ok.get(), page_ok.get(), page_ok.get()};
1634 
1635   // Prepare a buffer with 8 contiguous pages, with the above contents.
1636   static constexpr size_t kTestPages = 8;
1637 
1638   std::unique_ptr<uint8_t[]> buf(
1639       new uint8_t[base::GetSysPageSize() * kTestPages]());
1640   for (size_t i = 0; i < kTestPages; i++) {
1641     void* dest = buf.get() + (i * base::GetSysPageSize());
1642     memcpy(dest, static_cast<const void*>(test_page_order[i]),
1643            base::GetSysPageSize());
1644   }
1645 
1646   ProtoTranslationTable* table = GetTable("synthetic");
1647   FtraceMetadata metadata{};
1648   FtraceDataSourceConfig ds_config = EmptyConfig();
1649   ds_config.event_filter.AddEnabledEvent(
1650       table->EventToFtraceId(GroupAndName("sched", "sched_switch")));
1651 
1652   TraceWriterForTesting trace_writer;
1653   auto compact_sched_buf = std::make_unique<CompactSchedBuffer>();
1654   base::FlatSet<protos::pbzero::FtraceParseStatus> parse_errors;
1655   uint64_t last_read_event_ts = 0;
1656   bool success = CpuReader::ProcessPagesForDataSource(
1657       &trace_writer, &metadata, /*cpu=*/1, &ds_config, &parse_errors,
1658       &last_read_event_ts, buf.get(), kTestPages, compact_sched_buf.get(),
1659       table, /*symbolizer=*/nullptr,
1660       /*ftrace_clock_snapshot=*/nullptr,
1661       protos::pbzero::FTRACE_CLOCK_UNSPECIFIED);
1662 
1663   EXPECT_TRUE(success);
1664 
1665   // Each packet should contain the parsed contents of a contiguous run of pages
1666   // without data loss.
1667   // So we should get three packets (each page has 1 event):
1668   //   [3 events] [1 event] [4 events].
1669   auto packets = trace_writer.GetAllTracePackets();
1670 
1671   ASSERT_EQ(3u, packets.size());
1672   EXPECT_FALSE(packets[0].ftrace_events().lost_events());
1673   EXPECT_EQ(3u, packets[0].ftrace_events().event().size());
1674 
1675   EXPECT_TRUE(packets[1].ftrace_events().lost_events());
1676   EXPECT_EQ(1u, packets[1].ftrace_events().event().size());
1677 
1678   EXPECT_TRUE(packets[2].ftrace_events().lost_events());
1679   EXPECT_EQ(4u, packets[2].ftrace_events().event().size());
1680 }
1681 
1682 TEST(CpuReaderTest, ProcessPagesForDataSourceError) {
1683   auto page_ok = PageFromXxd(g_switch_page);
1684   auto page_err = PageFromXxd(g_invalid_page);
1685 
1686   std::vector<const void*> test_page_order = {
1687       page_ok.get(), page_ok.get(), page_ok.get(),  page_err.get(),
1688       page_ok.get(), page_ok.get(), page_err.get(), page_ok.get()};
1689 
1690   // Prepare a buffer with 8 contiguous pages, with the above contents.
1691   static constexpr size_t kTestPages = 8;
1692 
1693   std::unique_ptr<uint8_t[]> buf(
1694       new uint8_t[base::GetSysPageSize() * kTestPages]());
1695   for (size_t i = 0; i < kTestPages; i++) {
1696     void* dest = buf.get() + (i * base::GetSysPageSize());
1697     memcpy(dest, static_cast<const void*>(test_page_order[i]),
1698            base::GetSysPageSize());
1699   }
1700 
1701   ProtoTranslationTable* table = GetTable("synthetic");
1702   FtraceMetadata metadata{};
1703   FtraceDataSourceConfig ds_config = EmptyConfig();
1704   ds_config.event_filter.AddEnabledEvent(
1705       table->EventToFtraceId(GroupAndName("sched", "sched_switch")));
1706 
1707   TraceWriterForTesting trace_writer;
1708   auto compact_sched_buf = std::make_unique<CompactSchedBuffer>();
1709   base::FlatSet<protos::pbzero::FtraceParseStatus> parse_errors;
1710   uint64_t last_read_event_ts = 0;
1711   bool success = CpuReader::ProcessPagesForDataSource(
1712       &trace_writer, &metadata, /*cpu=*/1, &ds_config, &parse_errors,
1713       &last_read_event_ts, buf.get(), kTestPages, compact_sched_buf.get(),
1714       table, /*symbolizer=*/nullptr,
1715       /*ftrace_clock_snapshot=*/nullptr,
1716       protos::pbzero::FTRACE_CLOCK_UNSPECIFIED);
1717 
1718   EXPECT_FALSE(success);
1719 
1720   EXPECT_EQ(
1721       parse_errors.count(FtraceParseStatus::FTRACE_STATUS_ABI_END_OVERFLOW),
1722       1u);
1723 
1724   // 2 invalid pages -> 2 serialised parsing errors
1725   std::vector<protos::gen::TracePacket> packets =
1726       trace_writer.GetAllTracePackets();
1727   ASSERT_EQ(packets.size(), 1u);
1728   protos::gen::FtraceEventBundle bundle = packets[0].ftrace_events();
1729   using Bundle = protos::gen::FtraceEventBundle;
1730   using Error = Bundle::FtraceError;
1731   using protos::gen::FtraceParseStatus::FTRACE_STATUS_ABI_END_OVERFLOW;
1732   EXPECT_THAT(
1733       bundle,
1734       Property(&Bundle::error,
1735                ElementsAre(
1736                    Property(&Error::status, FTRACE_STATUS_ABI_END_OVERFLOW),
1737                    Property(&Error::status, FTRACE_STATUS_ABI_END_OVERFLOW))));
1738 }
1739 
1740 // Page containing an absolute timestamp (RINGBUF_TYPE_TIME_STAMP).
1741 static char g_abs_timestamp[] =
1742     R"(
1743 00000000: 8949 fbfb 38e4 0400 6407 0000 0000 0000  .I..8...d.......
1744 00000010: 5032 0a2d 3b01 0100 0000 0000 7377 6170  P2.-;.......swap
1745 00000020: 7065 722f 3000 0000 0000 0000 0000 0000  per/0...........
1746 00000030: 7800 0000 0000 0000 0000 0000 6776 6673  x...........gvfs
1747 00000040: 2d61 6663 2d76 6f6c 756d 6500 6483 0000  -afc-volume.d...
1748 00000050: 7800 0000 f0de 1700 3b01 0100 6483 0000  x.......;...d...
1749 00000060: 6776 6673 2d61 6663 2d76 6f6c 756d 6500  gvfs-afc-volume.
1750 00000070: 6483 0000 7800 0000 0100 0000 0000 0000  d...x...........
1751 00000080: 7377 6170 7065 722f 3000 0000 0000 0000  swapper/0.......
1752 00000090: 0000 0000 7800 0000 aaa1 5c08 0401 1100  ....x.....\.....
1753 000000a0: 0000 0000 88fc 31eb 029f ffff 609e d3c0  ......1.....`...
1754 000000b0: ffff ffff 0076 b4a1 029f ffff 0020 0000  .....v....... ..
1755 000000c0: ffff ffff e477 1700 0301 1100 0000 0000  .....w..........
1756 000000d0: 88fc 31eb 029f ffff aa26 0100 3e01 1100  ..1......&..>...
1757 000000e0: 0000 0000 6b77 6f72 6b65 722f 7538 3a35  ....kworker/u8:5
1758 000000f0: 0000 0000 24c0 0c00 7800 0000 0100 0000  ....$...x.......
1759 00000100: 0300 0000 90e6 e700 3b01 0100 0000 0000  ........;.......
1760 00000110: 7377 6170 7065 722f 3000 0000 0000 0000  swapper/0.......
1761 00000120: 0000 0000 7800 0000 0000 0000 0000 0000  ....x...........
1762 00000130: 6b77 6f72 6b65 722f 7538 3a35 0000 0000  kworker/u8:5....
1763 00000140: 24c0 0c00 7800 0000 aa56 0300 3e01 0100  $...x....V..>...
1764 00000150: 24c0 0c00 6b77 6f72 6b65 722f 7538 3a31  $...kworker/u8:1
1765 00000160: 0000 0000 8eb5 0c00 7800 0000 0100 0000  ........x.......
1766 00000170: 0300 0000 06eb 0300 0201 0000 24c0 0c00  ............$...
1767 00000180: 6026 f22a 049f ffff f0e4 4cc0 ffff ffff  `&.*......L.....
1768 00000190: ca45 0f00 3e01 0100 24c0 0c00 646d 6372  .E..>...$...dmcr
1769 000001a0: 7970 745f 7772 6974 652f 3200 2601 0000  ypt_write/2.&...
1770 000001b0: 7800 0000 0100 0000 0100 0000 c617 0200  x...............
1771 000001c0: 0101 0000 24c0 0c00 6026 f22a 049f ffff  ....$...`&.*....
1772 000001d0: f0e4 4cc0 ffff ffff a47c 0000 0301 0100  ..L......|......
1773 000001e0: 24c0 0c00 6015 f22a 049f ffff 0685 0000  $...`..*........
1774 000001f0: 0201 0000 24c0 0c00 a05d f22a 049f ffff  ....$....].*....
1775 00000200: f0e4 4cc0 ffff ffff c6dd 0800 0101 0000  ..L.............
1776 00000210: 24c0 0c00 a05d f22a 049f ffff f0e4 4cc0  $....].*......L.
1777 00000220: ffff ffff 8444 0000 0301 0100 24c0 0c00  .....D......$...
1778 00000230: 6059 f22a 049f ffff e672 0000 0201 0000  `Y.*.....r......
1779 00000240: 24c0 0c00 e050 f22a 049f ffff f0e4 4cc0  $....P.*......L.
1780 00000250: ffff ffff 4673 0a00 0101 0000 24c0 0c00  ....Fs......$...
1781 00000260: e050 f22a 049f ffff f0e4 4cc0 ffff ffff  .P.*......L.....
1782 00000270: 04ca 0000 0301 0100 24c0 0c00 2000 f22a  ........$... ..*
1783 00000280: 049f ffff 86b1 0000 0201 0000 24c0 0c00  ............$...
1784 00000290: 6015 f22a 049f ffff f0e4 4cc0 ffff ffff  `..*......L.....
1785 000002a0: e640 0c00 0101 0000 24c0 0c00 6015 f22a  .@......$...`..*
1786 000002b0: 049f ffff f0e4 4cc0 ffff ffff 64b4 0000  ......L.....d...
1787 000002c0: 0301 0100 24c0 0c00 2011 f22a 049f ffff  ....$... ..*....
1788 000002d0: 66b9 0000 0201 0000 24c0 0c00 a06e f22a  f.......$....n.*
1789 000002e0: 049f ffff f0e4 4cc0 ffff ffff 6ae1 4200  ......L.....j.B.
1790 000002f0: 3e01 1100 24c0 0c00 6a62 6432 2f64 6d2d  >...$...jbd2/dm-
1791 00000300: 312d 3800 0000 0000 6a01 0000 7800 0000  1-8.....j...x...
1792 00000310: 0100 0000 0300 0000 269b 0400 0101 0000  ........&.......
1793 00000320: 24c0 0c00 a06e f22a 049f ffff f0e4 4cc0  $....n.*......L.
1794 00000330: ffff ffff ff9d 6fb6 1f87 9c00 1000 0000  ......o.........
1795 00000340: 3b01 0100 24c0 0c00 6b77 6f72 6b65 722f  ;...$...kworker/
1796 00000350: 7538 3a35 0000 0000 24c0 0c00 7800 0000  u8:5....$...x...
1797 00000360: 8000 0000 0000 0000 7377 6170 7065 722f  ........swapper/
1798 00000370: 3000 0000 0000 0000 0000 0000 7800 0000  0...........x...
1799 00000380: 6ad2 3802 0401 1100 0000 0000 c800 384b  j.8...........8K
1800 00000390: 029f ffff 7018 75c0 ffff ffff 00ac edce  ....p.u.........
1801 000003a0: 039f ffff 0020 0000 0000 0000 c4de 0000  ..... ..........
1802 000003b0: 0301 1100 0000 0000 c800 384b 029f ffff  ..........8K....
1803 000003c0: 8a27 0100 3e01 1100 0000 0000 6b77 6f72  .'..>.......kwor
1804 000003d0: 6b65 722f 303a 3200 0000 0000 48b4 0c00  ker/0:2.....H...
1805 000003e0: 7800 0000 0100 0000 0000 0000 706d 0800  x...........pm..
1806 000003f0: 3b01 0100 0000 0000 7377 6170 7065 722f  ;.......swapper/
1807 00000400: 3000 0000 0000 0000 0000 0000 7800 0000  0...........x...
1808 00000410: 0000 0000 0000 0000 6b77 6f72 6b65 722f  ........kworker/
1809 00000420: 303a 3200 0000 0000 48b4 0c00 7800 0000  0:2.....H...x...
1810 00000430: 4636 0200 0201 0000 48b4 0c00 c800 384b  F6......H.....8K
1811 00000440: 029f ffff 7018 75c0 ffff ffff ca56 0500  ....p.u......V..
1812 00000450: 0401 0100 48b4 0c00 606a ad55 029f ffff  ....H...`j.U....
1813 00000460: f0e4 4cc0 ffff ffff 002c 04d0 039f ffff  ..L......,......
1814 00000470: 0020 0000 ffff ffff e435 0000 0301 0100  . .......5......
1815 00000480: 48b4 0c00 606a ad55 029f ffff ca67 0000  H...`j.U.....g..
1816 00000490: 3e01 0100 48b4 0c00 6b77 6f72 6b65 722f  >...H...kworker/
1817 000004a0: 7538 3a35 0000 0000 24c0 0c00 7800 0000  u8:5....$...x...
1818 000004b0: 0100 0000 0000 0000 e6fc 0200 0101 0000  ................
1819 000004c0: 48b4 0c00 c800 384b 029f ffff 7018 75c0  H.....8K....p.u.
1820 000004d0: ffff ffff 708f 0200 3b01 0100 48b4 0c00  ....p...;...H...
1821 000004e0: 6b77 6f72 6b65 722f 303a 3200 0000 0000  kworker/0:2.....
1822 000004f0: 48b4 0c00 7800 0000 8000 0000 0000 0000  H...x...........
1823 00000500: 6b77 6f72 6b65 722f 7538 3a35 0000 0000  kworker/u8:5....
1824 00000510: 24c0 0c00 7800 0000 0614 0100 0201 0000  $...x...........
1825 00000520: 24c0 0c00 606a ad55 029f ffff f0e4 4cc0  $...`j.U......L.
1826 00000530: ffff ffff ea7e 0c00 3e01 0100 24c0 0c00  .....~..>...$...
1827 00000540: 646d 6372 7970 745f 7772 6974 652f 3200  dmcrypt_write/2.
1828 00000550: 2601 0000 7800 0000 0100 0000 0100 0000  &...x...........
1829 00000560: 4645 0200 0101 0000 24c0 0c00 606a ad55  FE......$...`j.U
1830 00000570: 029f ffff f0e4 4cc0 ffff ffff b043 0900  ......L......C..
1831 00000580: 3b01 0100 24c0 0c00 6b77 6f72 6b65 722f  ;...$...kworker/
1832 00000590: 7538 3a35 0000 0000 24c0 0c00 7800 0000  u8:5....$...x...
1833 000005a0: 8000 0000 0000 0000 7377 6170 7065 722f  ........swapper/
1834 000005b0: 3000 0000 0000 0000 0000 0000 7800 0000  0...........x...
1835 000005c0: ca7a 3900 0401 1100 0000 0000 48bc d5a1  .z9.........H...
1836 000005d0: 029f ffff 10e2 62bb ffff ffff 00e0 40d0  ......b.......@.
1837 000005e0: 039f ffff 0020 0000 0000 0000 c4bb 0000  ..... ..........
1838 000005f0: 0301 1100 0000 0000 48bc d5a1 029f ffff  ........H.......
1839 00000600: 2aea 0000 3e01 1100 0000 0000 6b77 6f72  *...>.......kwor
1840 00000610: 6b65 722f 303a 3148 0000 0000 cfc1 0c00  ker/0:1H........
1841 00000620: 6400 0000 0100 0000 0000 0000 90bb 0600  d...............
1842 00000630: 3b01 0100 0000 0000 7377 6170 7065 722f  ;.......swapper/
1843 00000640: 3000 0000 0000 0000 0000 0000 7800 0000  0...........x...
1844 00000650: 0000 0000 0000 0000 6b77 6f72 6b65 722f  ........kworker/
1845 00000660: 303a 3148 0000 0000 cfc1 0c00 6400 0000  0:1H........d...
1846 00000670: 8617 0200 0201 0000 cfc1 0c00 48bc d5a1  ............H...
1847 00000680: 029f ffff 10e2 62bb ffff ffff c68f 0400  ......b.........
1848 00000690: 0101 0000 cfc1 0c00 48bc d5a1 029f ffff  ........H.......
1849 000006a0: 10e2 62bb ffff ffff b063 0300 3b01 0100  ..b......c..;...
1850 000006b0: cfc1 0c00 6b77 6f72 6b65 722f 303a 3148  ....kworker/0:1H
1851 000006c0: 0000 0000 cfc1 0c00 6400 0000 8000 0000  ........d.......
1852 000006d0: 0000 0000 7377 6170 7065 722f 3000 0000  ....swapper/0...
1853 000006e0: 0000 0000 0000 0000 7800 0000 4a10 ad01  ........x...J...
1854 000006f0: 3e01 1100 0000 0000 6a62 6432 2f64 6d2d  >.......jbd2/dm-
1855 00000700: 312d 3800 0000 0000 6a01 0000 7800 0000  1-8.....j...x...
1856 00000710: 0100 0000 0300 0000 ea27 b900 3e01 1100  .........'..>...
1857 00000720: 0000 0000 7263 755f 7363 6865 6400 0000  ....rcu_sched...
1858 00000730: 0000 0000 0d00 0000 7800 0000 0100 0000  ........x.......
1859 00000740: 0200 0000 3d00 0000 2c00 0000 0000 0000  ....=...,.......
1860 00000750: 0000 0000 0000 0000 0000 0000 0000 0000  ................
1861 00000760: 0000 0000 0000 0000 0000 0000 0000 0000  ................
1862 00000770: 0000 0000 0000 0000 0000 0000 0000 0000  ................
1863   )";
1864 
TEST_F(CpuReaderParsePagePayloadTest,ParseAbsoluteTimestamp)1865 TEST_F(CpuReaderParsePagePayloadTest, ParseAbsoluteTimestamp) {
1866   auto page = PageFromXxd(g_abs_timestamp);
1867 
1868   // Hand-build a translation table that handles sched_switch for this test
1869   // page. We cannot reuse the test data format file, since the ftrace id for
1870   // sched_switch in this page is different.
1871   std::vector<Field> common_fields;
1872   {  // common_pid
1873     common_fields.emplace_back(Field{});
1874     Field* field = &common_fields.back();
1875     field->ftrace_offset = 4;
1876     field->ftrace_size = 4;
1877     field->ftrace_type = kFtraceCommonPid32;
1878     field->proto_field_id = 2;
1879     field->proto_field_type = ProtoSchemaType::kInt32;
1880     SetTranslationStrategy(field->ftrace_type, field->proto_field_type,
1881                            &field->strategy);
1882   }
1883   using Switch = protos::gen::SchedSwitchFtraceEvent;
1884   Event sched_switch_event{
1885       "sched_switch",
1886       "sched",
1887       {
1888           {8, 16, FtraceFieldType::kFtraceFixedCString, "prev_comm",
1889            Switch::kPrevCommFieldNumber, ProtoSchemaType::kString,
1890            TranslationStrategy::kInvalidTranslationStrategy},
1891           {24, 4, FtraceFieldType::kFtracePid32, "prev_pid",
1892            Switch::kPrevPidFieldNumber, ProtoSchemaType::kInt32,
1893            TranslationStrategy::kInvalidTranslationStrategy},
1894           {28, 4, FtraceFieldType::kFtraceInt32, "prev_prio",
1895            Switch::kPrevPrioFieldNumber, ProtoSchemaType::kInt32,
1896            TranslationStrategy::kInvalidTranslationStrategy},
1897           {32, 8, FtraceFieldType::kFtraceInt64, "prev_state",
1898            Switch::kPrevStateFieldNumber, ProtoSchemaType::kInt64,
1899            TranslationStrategy::kInvalidTranslationStrategy},
1900           {40, 16, FtraceFieldType::kFtraceFixedCString, "next_comm",
1901            Switch::kNextCommFieldNumber, ProtoSchemaType::kString,
1902            TranslationStrategy::kInvalidTranslationStrategy},
1903           {56, 4, FtraceFieldType::kFtracePid32, "next_pid",
1904            Switch::kNextPidFieldNumber, ProtoSchemaType::kInt32,
1905            TranslationStrategy::kInvalidTranslationStrategy},
1906           {60, 4, FtraceFieldType::kFtraceInt32, "next_prio",
1907            Switch::kNextPrioFieldNumber, ProtoSchemaType::kInt32,
1908            TranslationStrategy::kInvalidTranslationStrategy},
1909       },
1910       /*ftrace_event_id=*/315,
1911       /*proto_field_id=*/4,
1912       /*size=*/64};
1913   for (Field& field : sched_switch_event.fields) {
1914     SetTranslationStrategy(field.ftrace_type, field.proto_field_type,
1915                            &field.strategy);
1916   }
1917   std::vector<Event> events;
1918   events.emplace_back(std::move(sched_switch_event));
1919 
1920   NiceMock<MockFtraceProcfs> mock_ftrace;
1921   PrintkMap printk_formats;
1922   ProtoTranslationTable translation_table(
1923       &mock_ftrace, events, std::move(common_fields),
1924       ProtoTranslationTable::DefaultPageHeaderSpecForTesting(),
1925       InvalidCompactSchedEventFormatForTesting(), printk_formats);
1926   ProtoTranslationTable* table = &translation_table;
1927 
1928   FtraceDataSourceConfig ds_config = EmptyConfig();
1929   ds_config.event_filter.AddEnabledEvent(
1930       table->EventToFtraceId(GroupAndName("sched", "sched_switch")));
1931 
1932   const uint8_t* parse_pos = page.get();
1933   std::optional<CpuReader::PageHeader> page_header =
1934       CpuReader::ParsePageHeader(&parse_pos, table->page_header_size_len());
1935 
1936   const uint8_t* page_end = page.get() + base::GetSysPageSize();
1937   ASSERT_TRUE(page_header.has_value());
1938   EXPECT_FALSE(page_header->lost_events);
1939   EXPECT_LE(parse_pos + page_header->size, page_end);
1940 
1941   FtraceParseStatus status = CpuReader::ParsePagePayload(
1942       parse_pos, &page_header.value(), table, &ds_config,
1943       CreateBundler(ds_config), &metadata_, &last_read_event_ts_);
1944 
1945   EXPECT_EQ(status, FtraceParseStatus::FTRACE_STATUS_OK);
1946 
1947   auto bundle = GetBundle();
1948 
1949   // There should be 9 sched_switch events within the above page.
1950   // We assert that all of their timestamps are exactly as expected.
1951   //
1952   // The key record that we're testing is an absolute timestamp
1953   // (RINGBUF_TYPE_TIME_STAMP) between the 3rd and 4th sched_switch events.
1954   //
1955   // This timestamp record starts at 0x334 bytes into the page.
1956   // The event header (first 4 bytes): 0xb66f9dff
1957   // -> type (bottom 5 bits): 31 (RINGBUF_TYPE_TIME_STAMP)
1958   // -> bottom 27 bits of ts: 0x5b37cef
1959   // Next 4 bytes have the top bits (28..59) of ts.
1960   // -> post-shift: 0x4e438f8000000
1961   // Adding the two parts of the timestamp, we get: 1376833332542703.
1962   //
1963   // The next event (sched_switch at 0x33c) after this timestamp has a
1964   // delta-timestamp of 0 in its event header, so we expect the 4th
1965   // sched_switch to have a timestamp of exactly 1376833332542703.
1966   EXPECT_EQ(bundle.event().size(), 9u);
1967 
1968   std::vector<uint64_t> switch_timestamps;
1969   for (const auto& e : bundle.event())
1970     switch_timestamps.push_back(e.timestamp());
1971 
1972   uint64_t expected_timestamps[] = {
1973       1376833327307547ull, 1376833327356434ull, 1376833332265799ull,
1974       1376833332542703ull, 1376833333729055ull, 1376833333757142ull,
1975       1376833333808564ull, 1376833333943445ull, 1376833333964012ull};
1976 
1977   ASSERT_THAT(switch_timestamps,
1978               testing::ElementsAreArray(expected_timestamps));
1979 }
1980 
TEST(CpuReaderTest,TranslateBlockDeviceIDToUserspace)1981 TEST(CpuReaderTest, TranslateBlockDeviceIDToUserspace) {
1982   const uint32_t kKernelBlockDeviceId = 271581216;
1983   const BlockDeviceID kUserspaceBlockDeviceId = 66336;
1984   const uint64_t k64BitKernelBlockDeviceId = 4442450946;
1985   const BlockDeviceID k64BitUserspaceBlockDeviceId =
1986       static_cast<BlockDeviceID>(17594983681026ULL);
1987 
1988   EXPECT_EQ(CpuReader::TranslateBlockDeviceIDToUserspace<uint32_t>(
1989                 kKernelBlockDeviceId),
1990             kUserspaceBlockDeviceId);
1991   EXPECT_EQ(CpuReader::TranslateBlockDeviceIDToUserspace<uint64_t>(
1992                 k64BitKernelBlockDeviceId),
1993             k64BitUserspaceBlockDeviceId);
1994 }
1995 
1996 // clang-format off
1997 // # tracer: nop
1998 // #
1999 // # entries-in-buffer/entries-written: 1041/238740   #P:8
2000 // #
2001 // #                              _-----=> irqs-off
2002 // #                             / _----=> need-resched
2003 // #                            | / _---=> hardirq/softirq
2004 // #                            || / _--=> preempt-depth
2005 // #                            ||| /     delay
2006 // #           TASK-PID   CPU#  ||||    TIMESTAMP  FUNCTION
2007 // #              | |       |   ||||       |         |
2008 //       android.bg-1668  [000] ...1 174991.234105: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2009 //       android.bg-1668  [000] ...1 174991.234108: ext4_mark_inode_dirty: dev 259,32 ino 2883605 caller ext4_dirty_inode+0x48/0x68
2010 //       android.bg-1668  [000] ...1 174991.234118: ext4_da_write_begin: dev 259,32 ino 2883605 pos 20480 len 4096 flags 0
2011 //       android.bg-1668  [000] ...1 174991.234126: ext4_journal_start: dev 259,32 blocks, 1 rsv_blocks, 0 caller ext4_da_write_begin+0x3d4/0x518
2012 //       android.bg-1668  [000] ...1 174991.234133: ext4_es_lookup_extent_enter: dev 259,32 ino 2883605 lblk 5
2013 //       android.bg-1668  [000] ...1 174991.234135: ext4_es_lookup_extent_exit: dev 259,32 ino 2883605 found 1 [5/4294967290) 576460752303423487 H0x10
2014 //       android.bg-1668  [000] ...2 174991.234140: ext4_da_reserve_space: dev 259,32 ino 2883605 mode 0100600 i_blocks 8 reserved_data_blocks 6 reserved_meta_blocks 0
2015 //       android.bg-1668  [000] ...1 174991.234142: ext4_es_insert_extent: dev 259,32 ino 2883605 es [5/1) mapped 576460752303423487 status D
2016 //       android.bg-1668  [000] ...1 174991.234153: ext4_da_write_end: dev 259,32 ino 2883605 pos 20480 len 4096 copied 4096
2017 //       android.bg-1668  [000] ...1 174991.234158: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2018 //       android.bg-1668  [000] ...1 174991.234160: ext4_mark_inode_dirty: dev 259,32 ino 2883605 caller ext4_dirty_inode+0x48/0x68
2019 //       android.bg-1668  [000] ...1 174991.234170: ext4_da_write_begin: dev 259,32 ino 2883605 pos 24576 len 2968 flags 0
2020 //       android.bg-1668  [000] ...1 174991.234178: ext4_journal_start: dev 259,32 blocks, 1 rsv_blocks, 0 caller ext4_da_write_begin+0x3d4/0x518
2021 //       android.bg-1668  [000] ...1 174991.234184: ext4_es_lookup_extent_enter: dev 259,32 ino 2883605 lblk 6
2022 //       android.bg-1668  [000] ...1 174991.234187: ext4_es_lookup_extent_exit: dev 259,32 ino 2883605 found 1 [6/4294967289) 576460752303423487 H0x10
2023 //       android.bg-1668  [000] ...2 174991.234191: ext4_da_reserve_space: dev 259,32 ino 2883605 mode 0100600 i_blocks 8 reserved_data_blocks 7 reserved_meta_blocks 0
2024 //       android.bg-1668  [000] ...1 174991.234193: ext4_es_insert_extent: dev 259,32 ino 2883605 es [6/1) mapped 576460752303423487 status D
2025 //       android.bg-1668  [000] ...1 174991.234203: ext4_da_write_end: dev 259,32 ino 2883605 pos 24576 len 2968 copied 2968
2026 //       android.bg-1668  [000] ...1 174991.234209: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2027 //       android.bg-1668  [000] ...1 174991.234211: ext4_mark_inode_dirty: dev 259,32 ino 2883605 caller ext4_dirty_inode+0x48/0x68
2028 //       android.bg-1668  [000] ...1 174991.234262: ext4_sync_file_enter: dev 259,32 ino 2883605 parent 2883592 datasync 0
2029 //       android.bg-1668  [000] ...1 174991.234270: ext4_writepages: dev 259,32 ino 2883605 nr_to_write 9223372036854775807 pages_skipped 0 range_start 0 range_end 9223372036854775807 sync_mode 1 for_kupdate 0 range_cyclic 0 writeback_index 0
2030 //       android.bg-1668  [000] ...1 174991.234287: ext4_journal_start: dev 259,32 blocks, 10 rsv_blocks, 0 caller ext4_writepages+0x6a4/0x119c
2031 //       android.bg-1668  [000] ...1 174991.234294: ext4_da_write_pages: dev 259,32 ino 2883605 first_page 0 nr_to_write 9223372036854775807 sync_mode 1
2032 //       android.bg-1668  [000] ...1 174991.234319: ext4_da_write_pages_extent: dev 259,32 ino 2883605 lblk 0 len 7 flags 0x200
2033 //       android.bg-1668  [000] ...1 174991.234322: ext4_es_lookup_extent_enter: dev 259,32 ino 2883605 lblk 0
2034 //       android.bg-1668  [000] ...1 174991.234324: ext4_es_lookup_extent_exit: dev 259,32 ino 2883605 found 1 [0/7) 576460752303423487 D0x10
2035 //       android.bg-1668  [000] ...1 174991.234328: ext4_ext_map_blocks_enter: dev 259,32 ino 2883605 lblk 0 len 7 flags CREATE|DELALLOC|METADATA_NOFAIL
2036 //       android.bg-1668  [000] ...1 174991.234341: ext4_request_blocks: dev 259,32 ino 2883605 flags HINT_DATA|DELALLOC_RESV|USE_RESV len 7 lblk 0 goal 11567104 lleft 0 lright 0 pleft 0 pright 0
2037 //       android.bg-1668  [000] ...1 174991.234394: ext4_mballoc_prealloc: dev 259,32 inode 2883605 orig 353/0/7@0 result 65/25551/7@0
2038 //       android.bg-1668  [000] ...1 174991.234400: ext4_allocate_blocks: dev 259,32 ino 2883605 flags HINT_DATA|DELALLOC_RESV|USE_RESV len 7 block 2155471 lblk 0 goal 11567104 lleft 0 lright 0 pleft 0 pright 0
2039 //       android.bg-1668  [000] ...1 174991.234409: ext4_mark_inode_dirty: dev 259,32 ino 2883605 caller __ext4_ext_dirty+0x104/0x170
2040 //       android.bg-1668  [000] ...1 174991.234420: ext4_get_reserved_cluster_alloc: dev 259,32 ino 2883605 lblk 0 len 7
2041 //       android.bg-1668  [000] ...2 174991.234426: ext4_da_update_reserve_space: dev 259,32 ino 2883605 mode 0100600 i_blocks 8 used_blocks 7 reserved_data_blocks 7 reserved_meta_blocks 0 allocated_meta_blocks 0 quota_claim 1
2042 //       android.bg-1668  [000] ...1 174991.234434: ext4_journal_start: dev 259,32 blocks, 1 rsv_blocks, 0 caller ext4_mark_dquot_dirty+0x80/0xd4
2043 //       android.bg-1668  [000] ...1 174991.234441: ext4_es_lookup_extent_enter: dev 259,32 ino 3 lblk 1
2044 //       android.bg-1668  [000] ...1 174991.234445: ext4_es_lookup_extent_exit: dev 259,32 ino 3 found 1 [0/2) 9255 W0x10
2045 //       android.bg-1668  [000] ...1 174991.234456: ext4_journal_start: dev 259,32 blocks, 1 rsv_blocks, 0 caller ext4_mark_dquot_dirty+0x80/0xd4
2046 //       android.bg-1668  [000] ...1 174991.234460: ext4_es_lookup_extent_enter: dev 259,32 ino 4 lblk 1
2047 //       android.bg-1668  [000] ...1 174991.234463: ext4_es_lookup_extent_exit: dev 259,32 ino 4 found 1 [0/2) 9257 W0x10
2048 //       android.bg-1668  [000] ...1 174991.234471: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2049 //       android.bg-1668  [000] ...1 174991.234474: ext4_mark_inode_dirty: dev 259,32 ino 2883605 caller ext4_dirty_inode+0x48/0x68
2050 //       android.bg-1668  [000] ...1 174991.234481: ext4_ext_map_blocks_exit: dev 259,32 ino 2883605 flags CREATE|DELALLOC|METADATA_NOFAIL lblk 0 pblk 2155471 len 7 mflags NM ret 7
2051 //       android.bg-1668  [000] ...1 174991.234484: ext4_es_insert_extent: dev 259,32 ino 2883605 es [0/7) mapped 2155471 status W
2052 //       android.bg-1668  [000] ...1 174991.234547: ext4_mark_inode_dirty: dev 259,32 ino 2883605 caller ext4_writepages+0xdc0/0x119c
2053 //       android.bg-1668  [000] ...1 174991.234604: ext4_journal_start: dev 259,32 blocks, 10 rsv_blocks, 0 caller ext4_writepages+0x6a4/0x119c
2054 //       android.bg-1668  [000] ...1 174991.234609: ext4_da_write_pages: dev 259,32 ino 2883605 first_page 7 nr_to_write 9223372036854775800 sync_mode 1
2055 //       android.bg-1668  [000] ...1 174991.234876: ext4_writepages_result: dev 259,32 ino 2883605 ret 0 pages_written 7 pages_skipped 0 sync_mode 1 writeback_index 7
2056 //    Profile Saver-5504  [000] ...1 175002.711928: ext4_discard_preallocations: dev 259,32 ino 1311176
2057 //    Profile Saver-5504  [000] ...1 175002.714165: ext4_begin_ordered_truncate: dev 259,32 ino 1311176 new_size 0
2058 //    Profile Saver-5504  [000] ...1 175002.714172: ext4_journal_start: dev 259,32 blocks, 3 rsv_blocks, 0 caller ext4_setattr+0x5b4/0x788
2059 //    Profile Saver-5504  [000] ...1 175002.714218: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_setattr+0x65c/0x788
2060 //    Profile Saver-5504  [000] ...1 175002.714277: ext4_invalidatepage: dev 259,32 ino 1311176 page_index 0 offset 0 length 4096
2061 //    Profile Saver-5504  [000] ...1 175002.714281: ext4_releasepage: dev 259,32 ino 1311176 page_index 0
2062 //    Profile Saver-5504  [000] ...1 175002.714295: ext4_invalidatepage: dev 259,32 ino 1311176 page_index 1 offset 0 length 4096
2063 //    Profile Saver-5504  [000] ...1 175002.714296: ext4_releasepage: dev 259,32 ino 1311176 page_index 1
2064 //    Profile Saver-5504  [000] ...1 175002.714315: ext4_truncate_enter: dev 259,32 ino 1311176 blocks 24
2065 //    Profile Saver-5504  [000] ...1 175002.714318: ext4_journal_start: dev 259,32 blocks, 10 rsv_blocks, 0 caller ext4_truncate+0x258/0x4b8
2066 //    Profile Saver-5504  [000] ...1 175002.714322: ext4_discard_preallocations: dev 259,32 ino 1311176
2067 //    Profile Saver-5504  [000] ...1 175002.714324: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_ext_truncate+0x24/0xc8
2068 //    Profile Saver-5504  [000] ...1 175002.714328: ext4_es_remove_extent: dev 259,32 ino 1311176 es [0/4294967295)
2069 //    Profile Saver-5504  [000] ...1 175002.714335: ext4_journal_start: dev 259,32 blocks, 1 rsv_blocks, 0 caller ext4_ext_remove_space+0x60/0x1180
2070 //    Profile Saver-5504  [000] ...1 175002.714338: ext4_ext_remove_space: dev 259,32 ino 1311176 since 0 end 4294967294 depth 0
2071 //    Profile Saver-5504  [000] ...1 175002.714347: ext4_ext_rm_leaf: dev 259,32 ino 1311176 start_lblk 0 last_extent [0(5276994), 2]partial_cluster 0
2072 //    Profile Saver-5504  [000] ...1 175002.714351: ext4_remove_blocks: dev 259,32 ino 1311176 extent [0(5276994), 2]from 0 to 1 partial_cluster 0
2073 //    Profile Saver-5504  [000] ...1 175002.714354: ext4_free_blocks: dev 259,32 ino 1311176 mode 0100600 block 5276994 count 2 flags 1ST_CLUSTER
2074 //    Profile Saver-5504  [000] ...1 175002.714365: ext4_mballoc_free: dev 259,32 inode 1311176 extent 161/1346/2
2075 //    Profile Saver-5504  [000] ...1 175002.714382: ext4_journal_start: dev 259,32 blocks, 1 rsv_blocks, 0 caller ext4_mark_dquot_dirty+0x80/0xd4
2076 //    Profile Saver-5504  [000] ...1 175002.714391: ext4_es_lookup_extent_enter: dev 259,32 ino 3 lblk 4
2077 //    Profile Saver-5504  [000] ...1 175002.714394: ext4_es_lookup_extent_exit: dev 259,32 ino 3 found 1 [4/1) 557094 W0x10
2078 //    Profile Saver-5504  [000] ...1 175002.714402: ext4_journal_start: dev 259,32 blocks, 1 rsv_blocks, 0 caller ext4_mark_dquot_dirty+0x80/0xd4
2079 //    Profile Saver-5504  [000] ...1 175002.714404: ext4_es_lookup_extent_enter: dev 259,32 ino 4 lblk 8
2080 //    Profile Saver-5504  [000] ...1 175002.714406: ext4_es_lookup_extent_exit: dev 259,32 ino 4 found 1 [8/3) 7376914 W0x10
2081 //    Profile Saver-5504  [000] ...1 175002.714413: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2082 //    Profile Saver-5504  [000] ...1 175002.714414: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_dirty_inode+0x48/0x68
2083 //    Profile Saver-5504  [000] ...1 175002.714420: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller __ext4_ext_dirty+0x104/0x170
2084 //    Profile Saver-5504  [000] ...1 175002.714423: ext4_ext_remove_space_done: dev 259,32 ino 1311176 since 0 end 4294967294 depth 0 partial 0 remaining_entries 0
2085 //    Profile Saver-5504  [000] ...1 175002.714425: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller __ext4_ext_dirty+0x104/0x170
2086 //    Profile Saver-5504  [000] ...1 175002.714433: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_truncate+0x3c4/0x4b8
2087 //    Profile Saver-5504  [000] ...1 175002.714436: ext4_truncate_exit: dev 259,32 ino 1311176 blocks 8
2088 //    Profile Saver-5504  [000] ...1 175002.714437: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2089 //    Profile Saver-5504  [000] ...1 175002.714438: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_dirty_inode+0x48/0x68
2090 //    Profile Saver-5504  [000] ...1 175002.714462: ext4_da_write_begin: dev 259,32 ino 1311176 pos 0 len 4 flags 0
2091 //    Profile Saver-5504  [000] ...1 175002.714472: ext4_journal_start: dev 259,32 blocks, 1 rsv_blocks, 0 caller ext4_da_write_begin+0x3d4/0x518
2092 //    Profile Saver-5504  [000] ...1 175002.714477: ext4_es_lookup_extent_enter: dev 259,32 ino 1311176 lblk 0
2093 //    Profile Saver-5504  [000] ...1 175002.714477: ext4_es_lookup_extent_exit: dev 259,32 ino 1311176 found 0 [0/0) 0
2094 //    Profile Saver-5504  [000] ...1 175002.714480: ext4_ext_map_blocks_enter: dev 259,32 ino 1311176 lblk 0 len 1 flags
2095 //    Profile Saver-5504  [000] ...1 175002.714485: ext4_es_find_delayed_extent_range_enter: dev 259,32 ino 1311176 lblk 0
2096 //    Profile Saver-5504  [000] ...1 175002.714488: ext4_es_find_delayed_extent_range_exit: dev 259,32 ino 1311176 es [0/0) mapped 0 status
2097 //    Profile Saver-5504  [000] ...1 175002.714490: ext4_es_insert_extent: dev 259,32 ino 1311176 es [0/4294967295) mapped 576460752303423487 status H
2098 //    Profile Saver-5504  [000] ...1 175002.714495: ext4_ext_map_blocks_exit: dev 259,32 ino 1311176 flags  lblk 0 pblk 4294967296 len 1 mflags  ret 0
2099 //    Profile Saver-5504  [000] ...2 175002.714501: ext4_da_reserve_space: dev 259,32 ino 1311176 mode 0100600 i_blocks 8 reserved_data_blocks 1 reserved_meta_blocks 0
2100 //    Profile Saver-5504  [000] ...1 175002.714505: ext4_es_insert_extent: dev 259,32 ino 1311176 es [0/1) mapped 576460752303423487 status D
2101 //    Profile Saver-5504  [000] ...1 175002.714513: ext4_da_write_end: dev 259,32 ino 1311176 pos 0 len 4 copied 4
2102 //    Profile Saver-5504  [000] ...1 175002.714519: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2103 //    Profile Saver-5504  [000] ...1 175002.714520: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_dirty_inode+0x48/0x68
2104 //    Profile Saver-5504  [000] ...1 175002.714527: ext4_da_write_begin: dev 259,32 ino 1311176 pos 4 len 4 flags 0
2105 //    Profile Saver-5504  [000] ...1 175002.714529: ext4_journal_start: dev 259,32 blocks, 1 rsv_blocks, 0 caller ext4_da_write_begin+0x3d4/0x518
2106 //    Profile Saver-5504  [000] ...1 175002.714531: ext4_da_write_end: dev 259,32 ino 1311176 pos 4 len 4 copied 4
2107 //    Profile Saver-5504  [000] ...1 175002.714532: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2108 //    Profile Saver-5504  [000] ...1 175002.714532: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_dirty_inode+0x48/0x68
2109 //    Profile Saver-5504  [000] ...1 175002.715313: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2110 //    Profile Saver-5504  [000] ...1 175002.715322: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_dirty_inode+0x48/0x68
2111 //    Profile Saver-5504  [000] ...1 175002.723849: ext4_da_write_begin: dev 259,32 ino 1311176 pos 8 len 5 flags 0
2112 //    Profile Saver-5504  [000] ...1 175002.723862: ext4_journal_start: dev 259,32 blocks, 1 rsv_blocks, 0 caller ext4_da_write_begin+0x3d4/0x518
2113 //    Profile Saver-5504  [000] ...1 175002.723873: ext4_da_write_end: dev 259,32 ino 1311176 pos 8 len 5 copied 5
2114 //    Profile Saver-5504  [000] ...1 175002.723877: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2115 //    Profile Saver-5504  [000] ...1 175002.723879: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_dirty_inode+0x48/0x68
2116 //    Profile Saver-5504  [000] ...1 175002.726857: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2117 //    Profile Saver-5504  [000] ...1 175002.726867: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_dirty_inode+0x48/0x68
2118 //    Profile Saver-5504  [000] ...1 175002.726881: ext4_da_write_begin: dev 259,32 ino 1311176 pos 13 len 4 flags 0
2119 //    Profile Saver-5504  [000] ...1 175002.726883: ext4_journal_start: dev 259,32 blocks, 1 rsv_blocks, 0 caller ext4_da_write_begin+0x3d4/0x518
2120 //    Profile Saver-5504  [000] ...1 175002.726890: ext4_da_write_end: dev 259,32 ino 1311176 pos 13 len 4 copied 4
2121 //    Profile Saver-5504  [000] ...1 175002.726892: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2122 //    Profile Saver-5504  [000] ...1 175002.726892: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_dirty_inode+0x48/0x68
2123 //    Profile Saver-5504  [000] ...1 175002.726900: ext4_da_write_begin: dev 259,32 ino 1311176 pos 17 len 4079 flags 0
2124 //    Profile Saver-5504  [000] ...1 175002.726901: ext4_journal_start: dev 259,32 blocks, 1 rsv_blocks, 0 caller ext4_da_write_begin+0x3d4/0x518
2125 //    Profile Saver-5504  [000] ...1 175002.726904: ext4_da_write_end: dev 259,32 ino 1311176 pos 17 len 4079 copied 4079
2126 //    Profile Saver-5504  [000] ...1 175002.726905: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2127 //    Profile Saver-5504  [000] ...1 175002.726906: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_dirty_inode+0x48/0x68
2128 //    Profile Saver-5504  [000] ...1 175002.726908: ext4_da_write_begin: dev 259,32 ino 1311176 pos 4096 len 2780 flags 0
2129 //    Profile Saver-5504  [000] ...1 175002.726916: ext4_journal_start: dev 259,32 blocks, 1 rsv_blocks, 0 caller ext4_da_write_begin+0x3d4/0x518
2130 //    Profile Saver-5504  [000] ...1 175002.726921: ext4_es_lookup_extent_enter: dev 259,32 ino 1311176 lblk 1
2131 //    Profile Saver-5504  [000] ...1 175002.726924: ext4_es_lookup_extent_exit: dev 259,32 ino 1311176 found 1 [1/4294967294) 576460752303423487 H0x10
2132 //    Profile Saver-5504  [000] ...2 175002.726931: ext4_da_reserve_space: dev 259,32 ino 1311176 mode 0100600 i_blocks 8 reserved_data_blocks 2 reserved_meta_blocks 0
2133 //    Profile Saver-5504  [000] ...1 175002.726933: ext4_es_insert_extent: dev 259,32 ino 1311176 es [1/1) mapped 576460752303423487 status D
2134 //    Profile Saver-5504  [000] ...1 175002.726940: ext4_da_write_end: dev 259,32 ino 1311176 pos 4096 len 2780 copied 2780
2135 //    Profile Saver-5504  [000] ...1 175002.726941: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2136 //    Profile Saver-5504  [000] ...1 175002.726942: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_dirty_inode+0x48/0x68
2137 //   d.process.acor-27885 [000] ...1 175018.227675: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2138 //   d.process.acor-27885 [000] ...1 175018.227699: ext4_mark_inode_dirty: dev 259,32 ino 3278189 caller ext4_dirty_inode+0x48/0x68
2139 //   d.process.acor-27885 [000] ...1 175018.227839: ext4_sync_file_enter: dev 259,32 ino 3278183 parent 3277001 datasync 1
2140 //   d.process.acor-27885 [000] ...1 175018.227847: ext4_writepages: dev 259,32 ino 3278183 nr_to_write 9223372036854775807 pages_skipped 0 range_start 0 range_end 9223372036854775807 sync_mode 1 for_kupdate 0 range_cyclic 0 writeback_index 2
2141 //   d.process.acor-27885 [000] ...1 175018.227852: ext4_writepages_result: dev 259,32 ino 3278183 ret 0 pages_written 0 pages_skipped 0 sync_mode 1 writeback_index 2
2142 // clang-format on
2143 
2144 static ExamplePage g_full_page_sched_switch{
2145     "synthetic",
2146     R"(
2147 00000000: 31f2 7622 1a00 0000 b40f 0000 0000 0000  1.v"............
2148 00000010: 1e00 0000 0000 0000 1000 0000 2f00 0103  ............/...
2149 00000020: 140d 0000 4a69 7420 7468 7265 6164 2070  ....Jit thread p
2150 00000030: 6f6f 6c00 140d 0000 8100 0000 0008 0000  ool.............
2151 00000040: 0000 0000 4576 656e 7454 6872 6561 6400  ....EventThread.
2152 00000050: 6572 0000 7002 0000 6100 0000 f057 0e00  er..p...a....W..
2153 00000060: 2f00 0103 7002 0000 4576 656e 7454 6872  /...p...EventThr
2154 00000070: 6561 6400 6572 0000 7002 0000 6100 0000  ead.er..p...a...
2155 00000080: 0100 0000 0000 0000 4a69 7420 7468 7265  ........Jit thre
2156 00000090: 6164 2070 6f6f 6c00 140d 0000 8100 0000  ad pool.........
2157 000000a0: 50c2 0910 2f00 0103 140d 0000 4a69 7420  P.../.......Jit
2158 000000b0: 7468 7265 6164 2070 6f6f 6c00 140d 0000  thread pool.....
2159 000000c0: 8100 0000 0100 0000 0000 0000 7377 6170  ............swap
2160 000000d0: 7065 722f 3000 0000 0000 0000 0000 0000  per/0...........
2161 000000e0: 7800 0000 901a c80e 2f00 0103 0000 0000  x......./.......
2162 000000f0: 7377 6170 7065 722f 3000 0000 0000 0000  swapper/0.......
2163 00000100: 0000 0000 7800 0000 0000 0000 0000 0000  ....x...........
2164 00000110: 4469 7370 5379 6e63 0069 6e67 6572 0000  DispSync.inger..
2165 00000120: 6f02 0000 6100 0000 1064 1e00 2f00 0103  o...a....d../...
2166 00000130: 6f02 0000 4469 7370 5379 6e63 0069 6e67  o...DispSync.ing
2167 00000140: 6572 0000 6f02 0000 6100 0000 0100 0000  er..o...a.......
2168 00000150: 0000 0000 7377 6170 7065 722f 3000 0000  ....swapper/0...
2169 00000160: 0000 0000 0000 0000 7800 0000 9074 8600  ........x....t..
2170 00000170: 2f00 0103 0000 0000 7377 6170 7065 722f  /.......swapper/
2171 00000180: 3000 0000 0000 0000 0000 0000 7800 0000  0...........x...
2172 00000190: 0000 0000 0000 0000 4576 656e 7454 6872  ........EventThr
2173 000001a0: 6561 6400 6572 0000 7002 0000 6100 0000  ead.er..p...a...
2174 000001b0: d071 0b00 2f00 0103 7002 0000 4576 656e  .q../...p...Even
2175 000001c0: 7454 6872 6561 6400 6572 0000 7002 0000  tThread.er..p...
2176 000001d0: 6100 0000 0100 0000 0000 0000 7377 6170  a...........swap
2177 000001e0: 7065 722f 3000 0000 0000 0000 0000 0000  per/0...........
2178 000001f0: 7800 0000 10cd 4504 2f00 0103 0000 0000  x.....E./.......
2179 00000200: 7377 6170 7065 722f 3000 0000 0000 0000  swapper/0.......
2180 00000210: 0000 0000 7800 0000 0000 0000 0000 0000  ....x...........
2181 00000220: 7375 676f 763a 3000 0000 0000 0000 0000  sugov:0.........
2182 00000230: 3802 0000 3100 0000 30d6 1300 2f00 0103  8...1...0.../...
2183 00000240: 3802 0000 7375 676f 763a 3000 0000 0000  8...sugov:0.....
2184 00000250: 0000 0000 3802 0000 3100 0000 0100 0000  ....8...1.......
2185 00000260: 0000 0000 7377 6170 7065 722f 3000 0000  ....swapper/0...
2186 00000270: 0000 0000 0000 0000 7800 0000 3049 a202  ........x...0I..
2187 00000280: 2f00 0103 0000 0000 7377 6170 7065 722f  /.......swapper/
2188 00000290: 3000 0000 0000 0000 0000 0000 7800 0000  0...........x...
2189 000002a0: 0000 0000 0000 0000 4469 7370 5379 6e63  ........DispSync
2190 000002b0: 0069 6e67 6572 0000 6f02 0000 6100 0000  .inger..o...a...
2191 000002c0: d07a 1000 2f00 0103 6f02 0000 4469 7370  .z../...o...Disp
2192 000002d0: 5379 6e63 0069 6e67 6572 0000 6f02 0000  Sync.inger..o...
2193 000002e0: 6100 0000 0100 0000 0000 0000 7377 6170  a...........swap
2194 000002f0: 7065 722f 3000 0000 0000 0000 0000 0000  per/0...........
2195 00000300: 7800 0000 d085 1100 2f00 0103 0000 0000  x......./.......
2196 00000310: 7377 6170 7065 722f 3000 0000 0000 0000  swapper/0.......
2197 00000320: 0000 0000 7800 0000 0000 0000 0000 0000  ....x...........
2198 00000330: 7375 7266 6163 6566 6c69 6e67 6572 0000  surfaceflinger..
2199 00000340: 4b02 0000 6200 0000 907a f000 2f00 0103  K...b....z../...
2200 00000350: 4b02 0000 7375 7266 6163 6566 6c69 6e67  K...surfacefling
2201 00000360: 6572 0000 4b02 0000 6200 0000 0100 0000  er..K...b.......
2202 00000370: 0000 0000 7377 6170 7065 722f 3000 0000  ....swapper/0...
2203 00000380: 0000 0000 0000 0000 7800 0000 305a 6400  ........x...0Zd.
2204 00000390: 2f00 0103 0000 0000 7377 6170 7065 722f  /.......swapper/
2205 000003a0: 3000 0000 0000 0000 0000 0000 7800 0000  0...........x...
2206 000003b0: 0000 0000 0000 0000 6d64 7373 5f66 6230  ........mdss_fb0
2207 000003c0: 0000 0000 0000 0000 5714 0000 5300 0000  ........W...S...
2208 000003d0: 10b1 9e03 2f00 0103 5714 0000 6d64 7373  ..../...W...mdss
2209 000003e0: 5f66 6230 0000 0000 0000 0000 5714 0000  _fb0........W...
2210 000003f0: 5300 0000 0200 0000 0000 0000 6b73 6f66  S...........ksof
2211 00000400: 7469 7271 642f 3000 0000 0000 0300 0000  tirqd/0.........
2212 00000410: 7800 0000 90bb 9900 2f00 0103 0300 0000  x......./.......
2213 00000420: 6b73 6f66 7469 7271 642f 3000 0000 0000  ksoftirqd/0.....
2214 00000430: 0300 0000 7800 0000 0100 0000 0000 0000  ....x...........
2215 00000440: 7377 6170 7065 722f 3000 0000 0000 0000  swapper/0.......
2216 00000450: 0000 0000 7800 0000 701e 5305 2f00 0103  ....x...p.S./...
2217 00000460: 0000 0000 7377 6170 7065 722f 3000 0000  ....swapper/0...
2218 00000470: 0000 0000 0000 0000 7800 0000 0000 0000  ........x.......
2219 00000480: 0000 0000 6b77 6f72 6b65 722f 7531 363a  ....kworker/u16:
2220 00000490: 3600 0000 6401 0000 7800 0000 90a1 2900  6...d...x.....).
2221 000004a0: 2f00 0103 6401 0000 6b77 6f72 6b65 722f  /...d...kworker/
2222 000004b0: 7531 363a 3600 0000 6401 0000 7800 0000  u16:6...d...x...
2223 000004c0: 0200 0000 0000 0000 7377 6170 7065 722f  ........swapper/
2224 000004d0: 3000 0000 0000 0000 0000 0000 7800 0000  0...........x...
2225 000004e0: b0e5 4f04 2f00 0103 0000 0000 7377 6170  ..O./.......swap
2226 000004f0: 7065 722f 3000 0000 0000 0000 0000 0000  per/0...........
2227 00000500: 7800 0000 0000 0000 0000 0000 4269 6e64  x...........Bind
2228 00000510: 6572 3a32 3136 385f 3135 0000 e614 0000  er:2168_15......
2229 00000520: 7800 0000 b0bd 7c00 2f00 0103 e614 0000  x.....|./.......
2230 00000530: 4269 6e64 6572 3a32 3136 385f 3135 0000  Binder:2168_15..
2231 00000540: e614 0000 7800 0000 0100 0000 0000 0000  ....x...........
2232 00000550: 7377 6170 7065 722f 3000 0000 0000 0000  swapper/0.......
2233 00000560: 0000 0000 7800 0000 d0bd 7e01 2f00 0103  ....x.....~./...
2234 00000570: 0000 0000 7377 6170 7065 722f 3000 0000  ....swapper/0...
2235 00000580: 0000 0000 0000 0000 7800 0000 0000 0000  ........x.......
2236 00000590: 0000 0000 6b77 6f72 6b65 722f 7531 363a  ....kworker/u16:
2237 000005a0: 3900 0000 e204 0000 7800 0000 7016 0800  9.......x...p...
2238 000005b0: 2f00 0103 e204 0000 6b77 6f72 6b65 722f  /.......kworker/
2239 000005c0: 7531 363a 3900 0000 e204 0000 7800 0000  u16:9.......x...
2240 000005d0: 0100 0000 0000 0000 7377 6170 7065 722f  ........swapper/
2241 000005e0: 3000 0000 0000 0000 0000 0000 7800 0000  0...........x...
2242 000005f0: 1004 5200 2f00 0103 0000 0000 7377 6170  ..R./.......swap
2243 00000600: 7065 722f 3000 0000 0000 0000 0000 0000  per/0...........
2244 00000610: 7800 0000 0000 0000 0000 0000 6b77 6f72  x...........kwor
2245 00000620: 6b65 722f 7531 363a 3900 0000 e204 0000  ker/u16:9.......
2246 00000630: 7800 0000 d0db 0700 2f00 0103 e204 0000  x......./.......
2247 00000640: 6b77 6f72 6b65 722f 7531 363a 3900 0000  kworker/u16:9...
2248 00000650: e204 0000 7800 0000 0100 0000 0000 0000  ....x...........
2249 00000660: 7377 6170 7065 722f 3000 0000 0000 0000  swapper/0.......
2250 00000670: 0000 0000 7800 0000 b0a2 8c00 2f00 0103  ....x......./...
2251 00000680: 0000 0000 7377 6170 7065 722f 3000 0000  ....swapper/0...
2252 00000690: 0000 0000 0000 0000 7800 0000 0000 0000  ........x.......
2253 000006a0: 0000 0000 6b77 6f72 6b65 722f 7531 363a  ....kworker/u16:
2254 000006b0: 3900 0000 e204 0000 7800 0000 d02b 0400  9.......x....+..
2255 000006c0: 2f00 0103 e204 0000 6b77 6f72 6b65 722f  /.......kworker/
2256 000006d0: 7531 363a 3900 0000 e204 0000 7800 0000  u16:9.......x...
2257 000006e0: 0100 0000 0000 0000 7377 6170 7065 722f  ........swapper/
2258 000006f0: 3000 0000 0000 0000 0000 0000 7800 0000  0...........x...
2259 00000700: d064 ef05 2f00 0103 0000 0000 7377 6170  .d../.......swap
2260 00000710: 7065 722f 3000 0000 0000 0000 0000 0000  per/0...........
2261 00000720: 7800 0000 0000 0000 0000 0000 4469 7370  x...........Disp
2262 00000730: 5379 6e63 0069 6e67 6572 0000 6f02 0000  Sync.inger..o...
2263 00000740: 6100 0000 f07d 1b00 2f00 0103 6f02 0000  a....}../...o...
2264 00000750: 4469 7370 5379 6e63 0069 6e67 6572 0000  DispSync.inger..
2265 00000760: 6f02 0000 6100 0000 0100 0000 0000 0000  o...a...........
2266 00000770: 6b73 6f66 7469 7271 642f 3000 0000 0000  ksoftirqd/0.....
2267 00000780: 0300 0000 7800 0000 304c 2000 2f00 0103  ....x...0L ./...
2268 00000790: 0300 0000 6b73 6f66 7469 7271 642f 3000  ....ksoftirqd/0.
2269 000007a0: 0000 0000 0300 0000 7800 0000 0100 0000  ........x.......
2270 000007b0: 0000 0000 6465 7832 6f61 7400 3935 5f33  ....dex2oat.95_3
2271 000007c0: 0000 0000 341f 0000 8200 0000 700b 0700  ....4.......p...
2272 000007d0: 2f00 0103 341f 0000 6465 7832 6f61 7400  /...4...dex2oat.
2273 000007e0: 3935 5f33 0000 0000 341f 0000 8200 0000  95_3....4.......
2274 000007f0: 0000 0000 0000 0000 7375 676f 763a 3000  ........sugov:0.
2275 00000800: 0000 0000 0000 0000 3802 0000 3100 0000  ........8...1...
2276 00000810: 50b0 0600 2f00 0103 3802 0000 7375 676f  P.../...8...sugo
2277 00000820: 763a 3000 0000 0000 0000 0000 3802 0000  v:0.........8...
2278 00000830: 3100 0000 0008 0000 0000 0000 6d69 6772  1...........migr
2279 00000840: 6174 696f 6e2f 3000 0000 0000 0d00 0000  ation/0.........
2280 00000850: 0000 0000 d09c 0600 2f00 0103 0d00 0000  ......../.......
2281 00000860: 6d69 6772 6174 696f 6e2f 3000 0000 0000  migration/0.....
2282 00000870: 0d00 0000 0000 0000 0100 0000 0000 0000  ................
2283 00000880: 7375 676f 763a 3000 0000 0000 0000 0000  sugov:0.........
2284 00000890: 3802 0000 3100 0000 7061 1900 2f00 0103  8...1...pa../...
2285 000008a0: 3802 0000 7375 676f 763a 3000 0000 0000  8...sugov:0.....
2286 000008b0: 0000 0000 3802 0000 3100 0000 0100 0000  ....8...1.......
2287 000008c0: 0000 0000 6465 7832 6f61 7400 3935 5f33  ....dex2oat.95_3
2288 000008d0: 0000 0000 341f 0000 8200 0000 f03c 5600  ....4........<V.
2289 000008e0: 2f00 0103 341f 0000 6465 7832 6f61 7400  /...4...dex2oat.
2290 000008f0: 3935 5f33 0000 0000 341f 0000 8200 0000  95_3....4.......
2291 00000900: 0200 0000 0000 0000 7377 6170 7065 722f  ........swapper/
2292 00000910: 3000 0000 0000 0000 0000 0000 7800 0000  0...........x...
2293 00000920: 5013 c400 2f00 0103 0000 0000 7377 6170  P.../.......swap
2294 00000930: 7065 722f 3000 0000 0000 0000 0000 0000  per/0...........
2295 00000940: 7800 0000 0000 0000 0000 0000 616e 6472  x...........andr
2296 00000950: 6f69 642e 6861 7264 7761 7200 d20a 0000  oid.hardwar.....
2297 00000960: 7800 0000 30c9 1300 2f00 0103 d20a 0000  x...0.../.......
2298 00000970: 616e 6472 6f69 642e 6861 7264 7761 7200  android.hardwar.
2299 00000980: d20a 0000 7800 0000 0100 0000 0000 0000  ....x...........
2300 00000990: 7377 6170 7065 722f 3000 0000 0000 0000  swapper/0.......
2301 000009a0: 0000 0000 7800 0000 7097 c000 2f00 0103  ....x...p.../...
2302 000009b0: 0000 0000 7377 6170 7065 722f 3000 0000  ....swapper/0...
2303 000009c0: 0000 0000 0000 0000 7800 0000 0000 0000  ........x.......
2304 000009d0: 0000 0000 616e 6472 6f69 642e 6861 7264  ....android.hard
2305 000009e0: 7761 7200 d20a 0000 7800 0000 305c 0c00  war.....x...0\..
2306 000009f0: 2f00 0103 d20a 0000 616e 6472 6f69 642e  /.......android.
2307 00000a00: 6861 7264 7761 7200 d20a 0000 7800 0000  hardwar.....x...
2308 00000a10: 0100 0000 0000 0000 7377 6170 7065 722f  ........swapper/
2309 00000a20: 3000 0000 0000 0000 0000 0000 7800 0000  0...........x...
2310 00000a30: d0aa 1401 2f00 0103 0000 0000 7377 6170  ..../.......swap
2311 00000a40: 7065 722f 3000 0000 0000 0000 0000 0000  per/0...........
2312 00000a50: 7800 0000 0000 0000 0000 0000 616e 6472  x...........andr
2313 00000a60: 6f69 642e 6861 7264 7761 7200 d20a 0000  oid.hardwar.....
2314 00000a70: 7800 0000 903b 0c00 2f00 0103 d20a 0000  x....;../.......
2315 00000a80: 616e 6472 6f69 642e 6861 7264 7761 7200  android.hardwar.
2316 00000a90: d20a 0000 7800 0000 0100 0000 0000 0000  ....x...........
2317 00000aa0: 7377 6170 7065 722f 3000 0000 0000 0000  swapper/0.......
2318 00000ab0: 0000 0000 7800 0000 f024 5401 2f00 0103  ....x....$T./...
2319 00000ac0: 0000 0000 7377 6170 7065 722f 3000 0000  ....swapper/0...
2320 00000ad0: 0000 0000 0000 0000 7800 0000 0000 0000  ........x.......
2321 00000ae0: 0000 0000 616e 6472 6f69 642e 6861 7264  ....android.hard
2322 00000af0: 7761 7200 d20a 0000 7800 0000 f0f3 0b00  war.....x.......
2323 00000b00: 2f00 0103 d20a 0000 616e 6472 6f69 642e  /.......android.
2324 00000b10: 6861 7264 7761 7200 d20a 0000 7800 0000  hardwar.....x...
2325 00000b20: 0100 0000 0000 0000 7377 6170 7065 722f  ........swapper/
2326 00000b30: 3000 0000 0000 0000 0000 0000 7800 0000  0...........x...
2327 00000b40: d0b5 bf02 2f00 0103 0000 0000 7377 6170  ..../.......swap
2328 00000b50: 7065 722f 3000 0000 0000 0000 0000 0000  per/0...........
2329 00000b60: 7800 0000 0000 0000 0000 0000 4469 7370  x...........Disp
2330 00000b70: 5379 6e63 0069 6e67 6572 0000 6f02 0000  Sync.inger..o...
2331 00000b80: 6100 0000 90cd 1400 2f00 0103 6f02 0000  a......./...o...
2332 00000b90: 4469 7370 5379 6e63 0069 6e67 6572 0000  DispSync.inger..
2333 00000ba0: 6f02 0000 6100 0000 0100 0000 0000 0000  o...a...........
2334 00000bb0: 7377 6170 7065 722f 3000 0000 0000 0000  swapper/0.......
2335 00000bc0: 0000 0000 7800 0000 50a6 1100 2f00 0103  ....x...P.../...
2336 00000bd0: 0000 0000 7377 6170 7065 722f 3000 0000  ....swapper/0...
2337 00000be0: 0000 0000 0000 0000 7800 0000 0000 0000  ........x.......
2338 00000bf0: 0000 0000 7375 7266 6163 6566 6c69 6e67  ....surfacefling
2339 00000c00: 6572 0000 4b02 0000 6200 0000 b04c 4200  er..K...b....LB.
2340 00000c10: 2f00 0103 4b02 0000 7375 7266 6163 6566  /...K...surfacef
2341 00000c20: 6c69 6e67 6572 0000 4b02 0000 6200 0000  linger..K...b...
2342 00000c30: 0100 0000 0000 0000 7377 6170 7065 722f  ........swapper/
2343 00000c40: 3000 0000 0000 0000 0000 0000 7800 0000  0...........x...
2344 00000c50: b025 060a 2f00 0103 0000 0000 7377 6170  .%../.......swap
2345 00000c60: 7065 722f 3000 0000 0000 0000 0000 0000  per/0...........
2346 00000c70: 7800 0000 0000 0000 0000 0000 6b77 6f72  x...........kwor
2347 00000c80: 6b65 722f 7531 363a 3600 0000 6401 0000  ker/u16:6...d...
2348 00000c90: 7800 0000 d0b6 0600 2f00 0103 6401 0000  x......./...d...
2349 00000ca0: 6b77 6f72 6b65 722f 7531 363a 3600 0000  kworker/u16:6...
2350 00000cb0: 6401 0000 7800 0000 0100 0000 0000 0000  d...x...........
2351 00000cc0: 7377 6170 7065 722f 3000 0000 0000 0000  swapper/0.......
2352 00000cd0: 0000 0000 7800 0000 f0a0 5800 2f00 0103  ....x.....X./...
2353 00000ce0: 0000 0000 7377 6170 7065 722f 3000 0000  ....swapper/0...
2354 00000cf0: 0000 0000 0000 0000 7800 0000 0000 0000  ........x.......
2355 00000d00: 0000 0000 6b77 6f72 6b65 722f 7531 363a  ....kworker/u16:
2356 00000d10: 3600 0000 6401 0000 7800 0000 f07a 1300  6...d...x....z..
2357 00000d20: 2f00 0103 6401 0000 6b77 6f72 6b65 722f  /...d...kworker/
2358 00000d30: 7531 363a 3600 0000 6401 0000 7800 0000  u16:6...d...x...
2359 00000d40: 0100 0000 0000 0000 7377 6170 7065 722f  ........swapper/
2360 00000d50: 3000 0000 0000 0000 0000 0000 7800 0000  0...........x...
2361 00000d60: b080 b101 2f00 0103 0000 0000 7377 6170  ..../.......swap
2362 00000d70: 7065 722f 3000 0000 0000 0000 0000 0000  per/0...........
2363 00000d80: 7800 0000 0000 0000 0000 0000 6b77 6f72  x...........kwor
2364 00000d90: 6b65 722f 7531 363a 3600 0000 6401 0000  ker/u16:6...d...
2365 00000da0: 7800 0000 103c 1200 2f00 0103 6401 0000  x....<../...d...
2366 00000db0: 6b77 6f72 6b65 722f 7531 363a 3600 0000  kworker/u16:6...
2367 00000dc0: 6401 0000 7800 0000 0100 0000 0000 0000  d...x...........
2368 00000dd0: 7377 6170 7065 722f 3000 0000 0000 0000  swapper/0.......
2369 00000de0: 0000 0000 7800 0000 50ea 3800 2f00 0103  ....x...P.8./...
2370 00000df0: 0000 0000 7377 6170 7065 722f 3000 0000  ....swapper/0...
2371 00000e00: 0000 0000 0000 0000 7800 0000 0000 0000  ........x.......
2372 00000e10: 0000 0000 6b77 6f72 6b65 722f 7531 363a  ....kworker/u16:
2373 00000e20: 3600 0000 6401 0000 7800 0000 5032 0400  6...d...x...P2..
2374 00000e30: 2f00 0103 6401 0000 6b77 6f72 6b65 722f  /...d...kworker/
2375 00000e40: 7531 363a 3600 0000 6401 0000 7800 0000  u16:6...d...x...
2376 00000e50: 0100 0000 0000 0000 7377 6170 7065 722f  ........swapper/
2377 00000e60: 3000 0000 0000 0000 0000 0000 7800 0000  0...........x...
2378 00000e70: 70f5 9000 2f00 0103 0000 0000 7377 6170  p.../.......swap
2379 00000e80: 7065 722f 3000 0000 0000 0000 0000 0000  per/0...........
2380 00000e90: 7800 0000 0000 0000 0000 0000 6b77 6f72  x...........kwor
2381 00000ea0: 6b65 722f 7531 363a 3600 0000 6401 0000  ker/u16:6...d...
2382 00000eb0: 7800 0000 10d7 0300 2f00 0103 6401 0000  x......./...d...
2383 00000ec0: 6b77 6f72 6b65 722f 7531 363a 3600 0000  kworker/u16:6...
2384 00000ed0: 6401 0000 7800 0000 0100 0000 0000 0000  d...x...........
2385 00000ee0: 7377 6170 7065 722f 3000 0000 0000 0000  swapper/0.......
2386 00000ef0: 0000 0000 7800 0000 907c 0900 2f00 0103  ....x....|../...
2387 00000f00: 0000 0000 7377 6170 7065 722f 3000 0000  ....swapper/0...
2388 00000f10: 0000 0000 0000 0000 7800 0000 0000 0000  ........x.......
2389 00000f20: 0000 0000 6b77 6f72 6b65 722f 7531 363a  ....kworker/u16:
2390 00000f30: 3600 0000 6401 0000 7800 0000 7082 0300  6...d...x...p...
2391 00000f40: 2f00 0103 6401 0000 6b77 6f72 6b65 722f  /...d...kworker/
2392 00000f50: 7531 363a 3600 0000 6401 0000 7800 0000  u16:6...d...x...
2393 00000f60: 0100 0000 0000 0000 7377 6170 7065 722f  ........swapper/
2394 00000f70: 3000 0000 0000 0000 0000 0000 7800 0000  0...........x...
2395 00000f80: f0ec 2100 2f00 0103 0000 0000 7377 6170  ..!./.......swap
2396 00000f90: 7065 722f 3000 0000 0000 0000 0000 0000  per/0...........
2397 00000fa0: 7800 0000 0000 0000 0000 0000 6b77 6f72  x...........kwor
2398 00000fb0: 6b65 722f 7531 363a 3600 0000 6401 0000  ker/u16:6...d...
2399 00000fc0: 7800 0000 0000 0000 0000 0000 0000 0000  x...............
2400 00000fd0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
2401 00000fe0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
2402 00000ff0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
2403     )",
2404 };
2405 
TEST_F(CpuReaderParsePagePayloadTest,ParseFullPageSchedSwitch)2406 TEST_F(CpuReaderParsePagePayloadTest, ParseFullPageSchedSwitch) {
2407   const ExamplePage* test_case = &g_full_page_sched_switch;
2408 
2409   ProtoTranslationTable* table = GetTable(test_case->name);
2410   auto page = PageFromXxd(test_case->data);
2411 
2412   FtraceDataSourceConfig ds_config = EmptyConfig();
2413   ds_config.event_filter.AddEnabledEvent(
2414       table->EventToFtraceId(GroupAndName("sched", "sched_switch")));
2415 
2416   const uint8_t* parse_pos = page.get();
2417   std::optional<CpuReader::PageHeader> page_header =
2418       CpuReader::ParsePageHeader(&parse_pos, table->page_header_size_len());
2419 
2420   const uint8_t* page_end = page.get() + base::GetSysPageSize();
2421   ASSERT_TRUE(page_header.has_value());
2422   EXPECT_FALSE(page_header->lost_events);
2423   EXPECT_LE(parse_pos + page_header->size, page_end);
2424 
2425   FtraceParseStatus status = CpuReader::ParsePagePayload(
2426       parse_pos, &page_header.value(), table, &ds_config,
2427       CreateBundler(ds_config), &metadata_, &last_read_event_ts_);
2428 
2429   EXPECT_EQ(status, FtraceParseStatus::FTRACE_STATUS_OK);
2430 
2431   auto bundle = GetBundle();
2432   EXPECT_EQ(bundle.event().size(), 59u);
2433 }
2434 
2435 // clang-format off
2436 // # tracer: nop
2437 // #
2438 // # entries-in-buffer/entries-written: 18/18   #P:8
2439 // #
2440 // #                              _-----=> irqs-off
2441 // #                             / _----=> need-resched
2442 // #                            | / _---=> hardirq/softirq
2443 // #                            || / _--=> preempt-depth
2444 // #                            ||| /     delay
2445 // #           TASK-PID   CPU#  ||||    TIMESTAMP  FUNCTION
2446 // #              | |       |   ||||       |         |
2447 //            <...>-9290  [000] ....  1352.654573: suspend_resume: sync_filesystems[0] end
2448 //            <...>-9290  [000] ....  1352.665366: suspend_resume: freeze_processes[0] begin
2449 //            <...>-9290  [000] ....  1352.699711: suspend_resume: freeze_processes[0] end
2450 //            <...>-9290  [000] ....  1352.699718: suspend_resume: suspend_enter[1] end
2451 //            <...>-9290  [000] ....  1352.699723: suspend_resume: dpm_prepare[2] begin
2452 //            <...>-9290  [000] ....  1352.703470: suspend_resume: dpm_prepare[2] end
2453 //            <...>-9290  [000] ....  1352.703477: suspend_resume: dpm_suspend[2] begin
2454 //            <...>-9290  [000] ....  1352.720107: suspend_resume: dpm_resume[16] end
2455 //            <...>-9290  [000] ....  1352.720113: suspend_resume: dpm_complete[16] begin
2456 //            <...>-9290  [000] .n..  1352.724540: suspend_resume: dpm_complete[16] end
2457 //            <...>-9290  [000] ....  1352.724567: suspend_resume: resume_console[1] begin
2458 //            <...>-9290  [000] ....  1352.724570: suspend_resume: resume_console[1] end
2459 //            <...>-9290  [000] ....  1352.724574: suspend_resume: thaw_processes[0] begin
2460 // clang-format on
2461 
2462 static ExamplePage g_suspend_resume{
2463     "synthetic",
2464     R"(00000000: edba 155a 3201 0000 7401 0000 0000 0000  ...Z2...t.......
2465 00000010: 7e58 22cd 1201 0000 0600 0000 ac00 0000  ~X".............
2466 00000020: 4a24 0000 5a7a f504 85ff ffff 0000 0000  J$..Zz..........
2467 00000030: 0017 0000 c621 9614 ac00 0000 4a24 0000  .....!......J$..
2468 00000040: 1c7a f504 85ff ffff 0000 0000 0100 0000  .z..............
2469 00000050: e6f1 8141 ac00 0000 4a24 0000 1c7a f504  ...A....J$...z..
2470 00000060: 85ff ffff 0000 0000 0000 0000 8682 0300  ................
2471 00000070: ac00 0000 4a24 0000 4c7a f504 85ff ffff  ....J$..Lz......
2472 00000080: 0100 0000 0063 755f 0657 0200 ac00 0000  .....cu_.W......
2473 00000090: 4a24 0000 8ad5 0105 85ff ffff 0200 0000  J$..............
2474 000000a0: 0100 0000 06b5 2507 ac00 0000 4a24 0000  ......%.....J$..
2475 000000b0: 8ad5 0105 85ff ffff 0200 0000 0000 0000  ................
2476 000000c0: 460d 0300 ac00 0000 4a24 0000 51d5 0105  F.......J$..Q...
2477 000000d0: 85ff ffff 0200 0000 0117 0000 c63e b81f  .............>..
2478 000000e0: ac00 0000 4a24 0000 7fd5 0105 85ff ffff  ....J$..........
2479 000000f0: 1000 0000 0010 0b00 a6f9 0200 ac00 0000  ................
2480 00000100: 4a24 0000 96d5 0105 85ff ffff 1000 0000  J$..............
2481 00000110: 01c0 1f00 a6dd 7108 ac00 0400 4a24 0000  ......q.....J$..
2482 00000120: 96d5 0105 85ff ffff 1000 0000 0000 0000  ................
2483 00000130: c6f1 0c00 ac00 0000 4a24 0000 3d7a f504  ........J$..=z..
2484 00000140: 85ff ffff 0100 0000 01ea 24d5 a66c 0100  ..........$..l..
2485 00000150: ac00 0000 4a24 0000 3d7a f504 85ff ffff  ....J$..=z......
2486 00000160: 0100 0000 0000 0001 6636 0200 ac00 0000  ........f6......
2487 00000170: 4a24 0000 d178 f504 85ff ffff 0000 0000  J$...x..........
2488 00000180: 0100 0000 0000 0000 0000 0000 0000 0000  ................
2489 00000190: 0000 0000 0000 0000 0000 0000 0000 0000  ................
2490 )"};
2491 
TEST_F(CpuReaderParsePagePayloadTest,ParseSuspendResume)2492 TEST_F(CpuReaderParsePagePayloadTest, ParseSuspendResume) {
2493   const ExamplePage* test_case = &g_suspend_resume;
2494 
2495   ProtoTranslationTable* table = GetTable(test_case->name);
2496   auto page = PageFromXxd(test_case->data);
2497 
2498   FtraceDataSourceConfig ds_config = EmptyConfig();
2499   ds_config.event_filter.AddEnabledEvent(
2500       table->EventToFtraceId(GroupAndName("power", "suspend_resume")));
2501 
2502   const uint8_t* parse_pos = page.get();
2503   std::optional<CpuReader::PageHeader> page_header =
2504       CpuReader::ParsePageHeader(&parse_pos, table->page_header_size_len());
2505   ASSERT_TRUE(page_header.has_value());
2506 
2507   CpuReader::ParsePagePayload(parse_pos, &page_header.value(), table,
2508                               &ds_config, CreateBundler(ds_config), &metadata_,
2509                               &last_read_event_ts_);
2510   auto bundle = GetBundle();
2511   ASSERT_EQ(bundle.event().size(), 13u);
2512   EXPECT_EQ(bundle.event()[0].suspend_resume().action(), "sync_filesystems");
2513   EXPECT_EQ(bundle.event()[1].suspend_resume().action(), "freeze_processes");
2514   EXPECT_EQ(bundle.event()[2].suspend_resume().action(), "freeze_processes");
2515   EXPECT_EQ(bundle.event()[3].suspend_resume().action(), "suspend_enter");
2516   // dpm_prepare deliberately missing from:
2517   // src/traced/probes/ftrace/test/data/synthetic/printk_formats to ensure we
2518   // handle that case correctly.
2519   EXPECT_EQ(bundle.event()[4].suspend_resume().action(), "");
2520 }
2521 
2522 // clang-format off
2523 // # tracer: nop
2524 // #
2525 // # entries-in-buffer/entries-written: 1041/238740   #P:8
2526 // #
2527 // #                              _-----=> irqs-off
2528 // #                             / _----=> need-resched
2529 // #                            | / _---=> hardirq/softirq
2530 // #                            || / _--=> preempt-depth
2531 // #                            ||| /     delay
2532 // #           TASK-PID   CPU#  ||||    TIMESTAMP  FUNCTION
2533 // #              | |       |   ||||       |         |
2534 //       android.bg-1668  [000] ...1 174991.234105: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2535 //       android.bg-1668  [000] ...1 174991.234108: ext4_mark_inode_dirty: dev 259,32 ino 2883605 caller ext4_dirty_inode+0x48/0x68
2536 //       android.bg-1668  [000] ...1 174991.234118: ext4_da_write_begin: dev 259,32 ino 2883605 pos 20480 len 4096 flags 0
2537 //       android.bg-1668  [000] ...1 174991.234126: ext4_journal_start: dev 259,32 blocks, 1 rsv_blocks, 0 caller ext4_da_write_begin+0x3d4/0x518
2538 //       android.bg-1668  [000] ...1 174991.234133: ext4_es_lookup_extent_enter: dev 259,32 ino 2883605 lblk 5
2539 //       android.bg-1668  [000] ...1 174991.234135: ext4_es_lookup_extent_exit: dev 259,32 ino 2883605 found 1 [5/4294967290) 576460752303423487 H0x10
2540 //       android.bg-1668  [000] ...2 174991.234140: ext4_da_reserve_space: dev 259,32 ino 2883605 mode 0100600 i_blocks 8 reserved_data_blocks 6 reserved_meta_blocks 0
2541 //       android.bg-1668  [000] ...1 174991.234142: ext4_es_insert_extent: dev 259,32 ino 2883605 es [5/1) mapped 576460752303423487 status D
2542 //       android.bg-1668  [000] ...1 174991.234153: ext4_da_write_end: dev 259,32 ino 2883605 pos 20480 len 4096 copied 4096
2543 //       android.bg-1668  [000] ...1 174991.234158: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2544 //       android.bg-1668  [000] ...1 174991.234160: ext4_mark_inode_dirty: dev 259,32 ino 2883605 caller ext4_dirty_inode+0x48/0x68
2545 //       android.bg-1668  [000] ...1 174991.234170: ext4_da_write_begin: dev 259,32 ino 2883605 pos 24576 len 2968 flags 0
2546 //       android.bg-1668  [000] ...1 174991.234178: ext4_journal_start: dev 259,32 blocks, 1 rsv_blocks, 0 caller ext4_da_write_begin+0x3d4/0x518
2547 //       android.bg-1668  [000] ...1 174991.234184: ext4_es_lookup_extent_enter: dev 259,32 ino 2883605 lblk 6
2548 //       android.bg-1668  [000] ...1 174991.234187: ext4_es_lookup_extent_exit: dev 259,32 ino 2883605 found 1 [6/4294967289) 576460752303423487 H0x10
2549 //       android.bg-1668  [000] ...2 174991.234191: ext4_da_reserve_space: dev 259,32 ino 2883605 mode 0100600 i_blocks 8 reserved_data_blocks 7 reserved_meta_blocks 0
2550 //       android.bg-1668  [000] ...1 174991.234193: ext4_es_insert_extent: dev 259,32 ino 2883605 es [6/1) mapped 576460752303423487 status D
2551 //       android.bg-1668  [000] ...1 174991.234203: ext4_da_write_end: dev 259,32 ino 2883605 pos 24576 len 2968 copied 2968
2552 //       android.bg-1668  [000] ...1 174991.234209: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2553 //       android.bg-1668  [000] ...1 174991.234211: ext4_mark_inode_dirty: dev 259,32 ino 2883605 caller ext4_dirty_inode+0x48/0x68
2554 //       android.bg-1668  [000] ...1 174991.234262: ext4_sync_file_enter: dev 259,32 ino 2883605 parent 2883592 datasync 0
2555 //       android.bg-1668  [000] ...1 174991.234270: ext4_writepages: dev 259,32 ino 2883605 nr_to_write 9223372036854775807 pages_skipped 0 range_start 0 range_end 9223372036854775807 sync_mode 1 for_kupdate 0 range_cyclic 0 writeback_index 0
2556 //       android.bg-1668  [000] ...1 174991.234287: ext4_journal_start: dev 259,32 blocks, 10 rsv_blocks, 0 caller ext4_writepages+0x6a4/0x119c
2557 //       android.bg-1668  [000] ...1 174991.234294: ext4_da_write_pages: dev 259,32 ino 2883605 first_page 0 nr_to_write 9223372036854775807 sync_mode 1
2558 //       android.bg-1668  [000] ...1 174991.234319: ext4_da_write_pages_extent: dev 259,32 ino 2883605 lblk 0 len 7 flags 0x200
2559 //       android.bg-1668  [000] ...1 174991.234322: ext4_es_lookup_extent_enter: dev 259,32 ino 2883605 lblk 0
2560 //       android.bg-1668  [000] ...1 174991.234324: ext4_es_lookup_extent_exit: dev 259,32 ino 2883605 found 1 [0/7) 576460752303423487 D0x10
2561 //       android.bg-1668  [000] ...1 174991.234328: ext4_ext_map_blocks_enter: dev 259,32 ino 2883605 lblk 0 len 7 flags CREATE|DELALLOC|METADATA_NOFAIL
2562 //       android.bg-1668  [000] ...1 174991.234341: ext4_request_blocks: dev 259,32 ino 2883605 flags HINT_DATA|DELALLOC_RESV|USE_RESV len 7 lblk 0 goal 11567104 lleft 0 lright 0 pleft 0 pright 0
2563 //       android.bg-1668  [000] ...1 174991.234394: ext4_mballoc_prealloc: dev 259,32 inode 2883605 orig 353/0/7@0 result 65/25551/7@0
2564 //       android.bg-1668  [000] ...1 174991.234400: ext4_allocate_blocks: dev 259,32 ino 2883605 flags HINT_DATA|DELALLOC_RESV|USE_RESV len 7 block 2155471 lblk 0 goal 11567104 lleft 0 lright 0 pleft 0 pright 0
2565 //       android.bg-1668  [000] ...1 174991.234409: ext4_mark_inode_dirty: dev 259,32 ino 2883605 caller __ext4_ext_dirty+0x104/0x170
2566 //       android.bg-1668  [000] ...1 174991.234420: ext4_get_reserved_cluster_alloc: dev 259,32 ino 2883605 lblk 0 len 7
2567 //       android.bg-1668  [000] ...2 174991.234426: ext4_da_update_reserve_space: dev 259,32 ino 2883605 mode 0100600 i_blocks 8 used_blocks 7 reserved_data_blocks 7 reserved_meta_blocks 0 allocated_meta_blocks 0 quota_claim 1
2568 //       android.bg-1668  [000] ...1 174991.234434: ext4_journal_start: dev 259,32 blocks, 1 rsv_blocks, 0 caller ext4_mark_dquot_dirty+0x80/0xd4
2569 //       android.bg-1668  [000] ...1 174991.234441: ext4_es_lookup_extent_enter: dev 259,32 ino 3 lblk 1
2570 //       android.bg-1668  [000] ...1 174991.234445: ext4_es_lookup_extent_exit: dev 259,32 ino 3 found 1 [0/2) 9255 W0x10
2571 //       android.bg-1668  [000] ...1 174991.234456: ext4_journal_start: dev 259,32 blocks, 1 rsv_blocks, 0 caller ext4_mark_dquot_dirty+0x80/0xd4
2572 //       android.bg-1668  [000] ...1 174991.234460: ext4_es_lookup_extent_enter: dev 259,32 ino 4 lblk 1
2573 //       android.bg-1668  [000] ...1 174991.234463: ext4_es_lookup_extent_exit: dev 259,32 ino 4 found 1 [0/2) 9257 W0x10
2574 //       android.bg-1668  [000] ...1 174991.234471: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2575 //       android.bg-1668  [000] ...1 174991.234474: ext4_mark_inode_dirty: dev 259,32 ino 2883605 caller ext4_dirty_inode+0x48/0x68
2576 //       android.bg-1668  [000] ...1 174991.234481: ext4_ext_map_blocks_exit: dev 259,32 ino 2883605 flags CREATE|DELALLOC|METADATA_NOFAIL lblk 0 pblk 2155471 len 7 mflags NM ret 7
2577 //       android.bg-1668  [000] ...1 174991.234484: ext4_es_insert_extent: dev 259,32 ino 2883605 es [0/7) mapped 2155471 status W
2578 //       android.bg-1668  [000] ...1 174991.234547: ext4_mark_inode_dirty: dev 259,32 ino 2883605 caller ext4_writepages+0xdc0/0x119c
2579 //       android.bg-1668  [000] ...1 174991.234604: ext4_journal_start: dev 259,32 blocks, 10 rsv_blocks, 0 caller ext4_writepages+0x6a4/0x119c
2580 //       android.bg-1668  [000] ...1 174991.234609: ext4_da_write_pages: dev 259,32 ino 2883605 first_page 7 nr_to_write 9223372036854775800 sync_mode 1
2581 //       android.bg-1668  [000] ...1 174991.234876: ext4_writepages_result: dev 259,32 ino 2883605 ret 0 pages_written 7 pages_skipped 0 sync_mode 1 writeback_index 7
2582 //    Profile Saver-5504  [000] ...1 175002.711928: ext4_discard_preallocations: dev 259,32 ino 1311176
2583 //    Profile Saver-5504  [000] ...1 175002.714165: ext4_begin_ordered_truncate: dev 259,32 ino 1311176 new_size 0
2584 //    Profile Saver-5504  [000] ...1 175002.714172: ext4_journal_start: dev 259,32 blocks, 3 rsv_blocks, 0 caller ext4_setattr+0x5b4/0x788
2585 //    Profile Saver-5504  [000] ...1 175002.714218: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_setattr+0x65c/0x788
2586 //    Profile Saver-5504  [000] ...1 175002.714277: ext4_invalidatepage: dev 259,32 ino 1311176 page_index 0 offset 0 length 4096
2587 //    Profile Saver-5504  [000] ...1 175002.714281: ext4_releasepage: dev 259,32 ino 1311176 page_index 0
2588 //    Profile Saver-5504  [000] ...1 175002.714295: ext4_invalidatepage: dev 259,32 ino 1311176 page_index 1 offset 0 length 4096
2589 //    Profile Saver-5504  [000] ...1 175002.714296: ext4_releasepage: dev 259,32 ino 1311176 page_index 1
2590 //    Profile Saver-5504  [000] ...1 175002.714315: ext4_truncate_enter: dev 259,32 ino 1311176 blocks 24
2591 //    Profile Saver-5504  [000] ...1 175002.714318: ext4_journal_start: dev 259,32 blocks, 10 rsv_blocks, 0 caller ext4_truncate+0x258/0x4b8
2592 //    Profile Saver-5504  [000] ...1 175002.714322: ext4_discard_preallocations: dev 259,32 ino 1311176
2593 //    Profile Saver-5504  [000] ...1 175002.714324: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_ext_truncate+0x24/0xc8
2594 //    Profile Saver-5504  [000] ...1 175002.714328: ext4_es_remove_extent: dev 259,32 ino 1311176 es [0/4294967295)
2595 //    Profile Saver-5504  [000] ...1 175002.714335: ext4_journal_start: dev 259,32 blocks, 1 rsv_blocks, 0 caller ext4_ext_remove_space+0x60/0x1180
2596 //    Profile Saver-5504  [000] ...1 175002.714338: ext4_ext_remove_space: dev 259,32 ino 1311176 since 0 end 4294967294 depth 0
2597 //    Profile Saver-5504  [000] ...1 175002.714347: ext4_ext_rm_leaf: dev 259,32 ino 1311176 start_lblk 0 last_extent [0(5276994), 2]partial_cluster 0
2598 //    Profile Saver-5504  [000] ...1 175002.714351: ext4_remove_blocks: dev 259,32 ino 1311176 extent [0(5276994), 2]from 0 to 1 partial_cluster 0
2599 //    Profile Saver-5504  [000] ...1 175002.714354: ext4_free_blocks: dev 259,32 ino 1311176 mode 0100600 block 5276994 count 2 flags 1ST_CLUSTER
2600 //    Profile Saver-5504  [000] ...1 175002.714365: ext4_mballoc_free: dev 259,32 inode 1311176 extent 161/1346/2
2601 //    Profile Saver-5504  [000] ...1 175002.714382: ext4_journal_start: dev 259,32 blocks, 1 rsv_blocks, 0 caller ext4_mark_dquot_dirty+0x80/0xd4
2602 //    Profile Saver-5504  [000] ...1 175002.714391: ext4_es_lookup_extent_enter: dev 259,32 ino 3 lblk 4
2603 //    Profile Saver-5504  [000] ...1 175002.714394: ext4_es_lookup_extent_exit: dev 259,32 ino 3 found 1 [4/1) 557094 W0x10
2604 //    Profile Saver-5504  [000] ...1 175002.714402: ext4_journal_start: dev 259,32 blocks, 1 rsv_blocks, 0 caller ext4_mark_dquot_dirty+0x80/0xd4
2605 //    Profile Saver-5504  [000] ...1 175002.714404: ext4_es_lookup_extent_enter: dev 259,32 ino 4 lblk 8
2606 //    Profile Saver-5504  [000] ...1 175002.714406: ext4_es_lookup_extent_exit: dev 259,32 ino 4 found 1 [8/3) 7376914 W0x10
2607 //    Profile Saver-5504  [000] ...1 175002.714413: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2608 //    Profile Saver-5504  [000] ...1 175002.714414: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_dirty_inode+0x48/0x68
2609 //    Profile Saver-5504  [000] ...1 175002.714420: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller __ext4_ext_dirty+0x104/0x170
2610 //    Profile Saver-5504  [000] ...1 175002.714423: ext4_ext_remove_space_done: dev 259,32 ino 1311176 since 0 end 4294967294 depth 0 partial 0 remaining_entries 0
2611 //    Profile Saver-5504  [000] ...1 175002.714425: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller __ext4_ext_dirty+0x104/0x170
2612 //    Profile Saver-5504  [000] ...1 175002.714433: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_truncate+0x3c4/0x4b8
2613 //    Profile Saver-5504  [000] ...1 175002.714436: ext4_truncate_exit: dev 259,32 ino 1311176 blocks 8
2614 //    Profile Saver-5504  [000] ...1 175002.714437: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2615 //    Profile Saver-5504  [000] ...1 175002.714438: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_dirty_inode+0x48/0x68
2616 //    Profile Saver-5504  [000] ...1 175002.714462: ext4_da_write_begin: dev 259,32 ino 1311176 pos 0 len 4 flags 0
2617 //    Profile Saver-5504  [000] ...1 175002.714472: ext4_journal_start: dev 259,32 blocks, 1 rsv_blocks, 0 caller ext4_da_write_begin+0x3d4/0x518
2618 //    Profile Saver-5504  [000] ...1 175002.714477: ext4_es_lookup_extent_enter: dev 259,32 ino 1311176 lblk 0
2619 //    Profile Saver-5504  [000] ...1 175002.714477: ext4_es_lookup_extent_exit: dev 259,32 ino 1311176 found 0 [0/0) 0
2620 //    Profile Saver-5504  [000] ...1 175002.714480: ext4_ext_map_blocks_enter: dev 259,32 ino 1311176 lblk 0 len 1 flags
2621 //    Profile Saver-5504  [000] ...1 175002.714485: ext4_es_find_delayed_extent_range_enter: dev 259,32 ino 1311176 lblk 0
2622 //    Profile Saver-5504  [000] ...1 175002.714488: ext4_es_find_delayed_extent_range_exit: dev 259,32 ino 1311176 es [0/0) mapped 0 status
2623 //    Profile Saver-5504  [000] ...1 175002.714490: ext4_es_insert_extent: dev 259,32 ino 1311176 es [0/4294967295) mapped 576460752303423487 status H
2624 //    Profile Saver-5504  [000] ...1 175002.714495: ext4_ext_map_blocks_exit: dev 259,32 ino 1311176 flags  lblk 0 pblk 4294967296 len 1 mflags  ret 0
2625 //    Profile Saver-5504  [000] ...2 175002.714501: ext4_da_reserve_space: dev 259,32 ino 1311176 mode 0100600 i_blocks 8 reserved_data_blocks 1 reserved_meta_blocks 0
2626 //    Profile Saver-5504  [000] ...1 175002.714505: ext4_es_insert_extent: dev 259,32 ino 1311176 es [0/1) mapped 576460752303423487 status D
2627 //    Profile Saver-5504  [000] ...1 175002.714513: ext4_da_write_end: dev 259,32 ino 1311176 pos 0 len 4 copied 4
2628 //    Profile Saver-5504  [000] ...1 175002.714519: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2629 //    Profile Saver-5504  [000] ...1 175002.714520: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_dirty_inode+0x48/0x68
2630 //    Profile Saver-5504  [000] ...1 175002.714527: ext4_da_write_begin: dev 259,32 ino 1311176 pos 4 len 4 flags 0
2631 //    Profile Saver-5504  [000] ...1 175002.714529: ext4_journal_start: dev 259,32 blocks, 1 rsv_blocks, 0 caller ext4_da_write_begin+0x3d4/0x518
2632 //    Profile Saver-5504  [000] ...1 175002.714531: ext4_da_write_end: dev 259,32 ino 1311176 pos 4 len 4 copied 4
2633 //    Profile Saver-5504  [000] ...1 175002.714532: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2634 //    Profile Saver-5504  [000] ...1 175002.714532: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_dirty_inode+0x48/0x68
2635 //    Profile Saver-5504  [000] ...1 175002.715313: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2636 //    Profile Saver-5504  [000] ...1 175002.715322: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_dirty_inode+0x48/0x68
2637 //    Profile Saver-5504  [000] ...1 175002.723849: ext4_da_write_begin: dev 259,32 ino 1311176 pos 8 len 5 flags 0
2638 //    Profile Saver-5504  [000] ...1 175002.723862: ext4_journal_start: dev 259,32 blocks, 1 rsv_blocks, 0 caller ext4_da_write_begin+0x3d4/0x518
2639 //    Profile Saver-5504  [000] ...1 175002.723873: ext4_da_write_end: dev 259,32 ino 1311176 pos 8 len 5 copied 5
2640 //    Profile Saver-5504  [000] ...1 175002.723877: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2641 //    Profile Saver-5504  [000] ...1 175002.723879: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_dirty_inode+0x48/0x68
2642 //    Profile Saver-5504  [000] ...1 175002.726857: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2643 //    Profile Saver-5504  [000] ...1 175002.726867: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_dirty_inode+0x48/0x68
2644 //    Profile Saver-5504  [000] ...1 175002.726881: ext4_da_write_begin: dev 259,32 ino 1311176 pos 13 len 4 flags 0
2645 //    Profile Saver-5504  [000] ...1 175002.726883: ext4_journal_start: dev 259,32 blocks, 1 rsv_blocks, 0 caller ext4_da_write_begin+0x3d4/0x518
2646 //    Profile Saver-5504  [000] ...1 175002.726890: ext4_da_write_end: dev 259,32 ino 1311176 pos 13 len 4 copied 4
2647 //    Profile Saver-5504  [000] ...1 175002.726892: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2648 //    Profile Saver-5504  [000] ...1 175002.726892: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_dirty_inode+0x48/0x68
2649 //    Profile Saver-5504  [000] ...1 175002.726900: ext4_da_write_begin: dev 259,32 ino 1311176 pos 17 len 4079 flags 0
2650 //    Profile Saver-5504  [000] ...1 175002.726901: ext4_journal_start: dev 259,32 blocks, 1 rsv_blocks, 0 caller ext4_da_write_begin+0x3d4/0x518
2651 //    Profile Saver-5504  [000] ...1 175002.726904: ext4_da_write_end: dev 259,32 ino 1311176 pos 17 len 4079 copied 4079
2652 //    Profile Saver-5504  [000] ...1 175002.726905: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2653 //    Profile Saver-5504  [000] ...1 175002.726906: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_dirty_inode+0x48/0x68
2654 //    Profile Saver-5504  [000] ...1 175002.726908: ext4_da_write_begin: dev 259,32 ino 1311176 pos 4096 len 2780 flags 0
2655 //    Profile Saver-5504  [000] ...1 175002.726916: ext4_journal_start: dev 259,32 blocks, 1 rsv_blocks, 0 caller ext4_da_write_begin+0x3d4/0x518
2656 //    Profile Saver-5504  [000] ...1 175002.726921: ext4_es_lookup_extent_enter: dev 259,32 ino 1311176 lblk 1
2657 //    Profile Saver-5504  [000] ...1 175002.726924: ext4_es_lookup_extent_exit: dev 259,32 ino 1311176 found 1 [1/4294967294) 576460752303423487 H0x10
2658 //    Profile Saver-5504  [000] ...2 175002.726931: ext4_da_reserve_space: dev 259,32 ino 1311176 mode 0100600 i_blocks 8 reserved_data_blocks 2 reserved_meta_blocks 0
2659 //    Profile Saver-5504  [000] ...1 175002.726933: ext4_es_insert_extent: dev 259,32 ino 1311176 es [1/1) mapped 576460752303423487 status D
2660 //    Profile Saver-5504  [000] ...1 175002.726940: ext4_da_write_end: dev 259,32 ino 1311176 pos 4096 len 2780 copied 2780
2661 //    Profile Saver-5504  [000] ...1 175002.726941: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2662 //    Profile Saver-5504  [000] ...1 175002.726942: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_dirty_inode+0x48/0x68
2663 //   d.process.acor-27885 [000] ...1 175018.227675: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2664 //   d.process.acor-27885 [000] ...1 175018.227699: ext4_mark_inode_dirty: dev 259,32 ino 3278189 caller ext4_dirty_inode+0x48/0x68
2665 //   d.process.acor-27885 [000] ...1 175018.227839: ext4_sync_file_enter: dev 259,32 ino 3278183 parent 3277001 datasync 1
2666 //   d.process.acor-27885 [000] ...1 175018.227847: ext4_writepages: dev 259,32 ino 3278183 nr_to_write 9223372036854775807 pages_skipped 0 range_start 0 range_end 9223372036854775807 sync_mode 1 for_kupdate 0 range_cyclic 0 writeback_index 2
2667 //   d.process.acor-27885 [000] ...1 175018.227852: ext4_writepages_result: dev 259,32 ino 3278183 ret 0 pages_written 0 pages_skipped 0 sync_mode 1 writeback_index 2
2668 // clang-format on
2669 
2670 static ExamplePage g_full_page_ext4{
2671     "synthetic",
2672     R"(
2673 00000000: 50fe 5852 279f 0000 c80f 00c0 ffff ffff  P.XR'...........
2674 00000010: 0800 0000 5701 0001 8406 0000 2000 3010  ....W....... .0.
2675 00000020: 566b 0000 8829 e86a 91ff ffff 0200 0000  Vk...).j........
2676 00000030: 0000 0000 2873 0100 1b01 0001 8406 0000  ....(s..........
2677 00000040: 2000 3010 9200 0000 1500 2c00 0000 0000   .0.......,.....
2678 00000050: a029 e86a 91ff ffff 0ac8 0400 1e01 0001  .).j............
2679 00000060: 8406 0000 2000 3010 2866 0100 1500 2c00  .... .0.(f....,.
2680 00000070: 0000 0000 0050 0000 0000 0000 0010 0000  .....P..........
2681 00000080: 0000 0000 a804 0400 5701 0001 8406 0000  ........W.......
2682 00000090: 2000 3010 91ff ffff 586f e86a 91ff ffff   .0.....Xo.j....
2683 000000a0: 0100 0000 0000 0000 c83a 0300 6c01 0001  .........:..l...
2684 000000b0: 8406 0000 2000 3010 0000 0000 1500 2c00  .... .0.......,.
2685 000000c0: 0000 0000 0500 0000 5701 0001 ac6c 0100  ........W....l..
2686 000000d0: 6d01 0001 8406 0000 2000 3010 91ff ffff  m....... .0.....
2687 000000e0: 1500 2c00 0000 0000 0500 0000 faff ffff  ..,.............
2688 000000f0: ffff ffff ffff ff07 184e 0000 0100 0000  .........N......
2689 00000100: ec08 0200 3f01 0002 8406 0000 2000 3010  ....?....... .0.
2690 00000110: 0000 0000 1500 2c00 0000 0000 0800 0000  ......,.........
2691 00000120: 0000 0000 0600 0000 0000 0000 8081 0000  ................
2692 00000130: 0000 0000 ec24 0100 6701 0001 8406 0000  .....$..g.......
2693 00000140: 2000 3010 0000 0000 1500 2c00 0000 0000   .0.......,.....
2694 00000150: 0500 0000 0100 0000 ffff ffff ffff ff07  ................
2695 00000160: 0400 0000 7b04 3200 2a30 0500 2101 0001  ....{.2.*0..!...
2696 00000170: 8406 0000 2000 3010 0000 0000 1500 2c00  .... .0.......,.
2697 00000180: 0000 0000 0050 0000 0000 0000 0010 0000  .....P..........
2698 00000190: 0010 0000 288b 0200 5701 0001 8406 0000  ....(...W.......
2699 000001a0: 2000 3010 0000 0000 8829 e86a 91ff ffff   .0......).j....
2700 000001b0: 0200 0000 0000 0000 0832 0100 1b01 0001  .........2......
2701 000001c0: 8406 0000 2000 3010 566b 0000 1500 2c00  .... .0.Vk....,.
2702 000001d0: 0000 0000 a029 e86a 91ff ffff eaa0 0400  .....).j........
2703 000001e0: 1e01 0001 8406 0000 2000 3010 280b 0400  ........ .0.(...
2704 000001f0: 1500 2c00 0000 0000 0060 0000 0000 0000  ..,......`......
2705 00000200: 980b 0000 0000 0000 88d0 0300 5701 0001  ............W...
2706 00000210: 8406 0000 2000 3010 566b 0000 586f e86a  .... .0.Vk..Xo.j
2707 00000220: 91ff ffff 0100 0000 0000 0000 c813 0300  ................
2708 00000230: 6c01 0001 8406 0000 2000 3010 566b 0000  l....... .0.Vk..
2709 00000240: 1500 2c00 0000 0000 0600 0000 0000 0000  ..,.............
2710 00000250: ac5f 0100 6d01 0001 8406 0000 2000 3010  ._..m....... .0.
2711 00000260: 1100 3010 1500 2c00 0000 0000 0600 0000  ..0...,.........
2712 00000270: f9ff ffff ffff ffff ffff ff07 185a ea6a  .............Z.j
2713 00000280: 0100 0000 4c02 0200 3f01 0002 8406 0000  ....L...?.......
2714 00000290: 2000 3010 566b 0000 1500 2c00 0000 0000   .0.Vk....,.....
2715 000002a0: 0800 0000 0000 0000 0700 0000 0000 0000  ................
2716 000002b0: 8081 0000 6d01 0001 0c0b 0100 6701 0001  ....m.......g...
2717 000002c0: 8406 0000 2000 3010 0000 0000 1500 2c00  .... .0.......,.
2718 000002d0: 0000 0000 0600 0000 0100 0000 ffff ffff  ................
2719 000002e0: ffff ff07 049a 0100 5701 0001 aa1c 0500  ........W.......
2720 000002f0: 2101 0001 8406 0000 2000 3010 91ff ffff  !....... .0.....
2721 00000300: 1500 2c00 0000 0000 0060 0000 0000 0000  ..,......`......
2722 00000310: 980b 0000 980b 0000 889e 0200 5701 0001  ............W...
2723 00000320: 8406 0000 2000 3010 91ff ffff 8829 e86a  .... .0......).j
2724 00000330: 91ff ffff 0200 0000 0000 0000 8838 0100  .............8..
2725 00000340: 1b01 0001 8406 0000 2000 3010 91ff ffff  ........ .0.....
2726 00000350: 1500 2c00 0000 0000 a029 e86a 91ff ffff  ..,......).j....
2727 00000360: 2ab8 1800 3501 0001 8406 0000 2000 3010  *...5....... .0.
2728 00000370: feff ffff 1500 2c00 0000 0000 0800 2c00  ......,.......,.
2729 00000380: 0000 0000 0000 0000 2000 3010 32fe 0300  ........ .0.2...
2730 00000390: 2201 0001 8406 0000 2000 3010 0000 0000  "....... .0.....
2731 000003a0: 1500 2c00 0000 0000 ffff ffff ffff ff7f  ..,.............
2732 000003b0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
2733 000003c0: ffff ffff ffff ff7f 0000 0000 0000 0000  ................
2734 000003d0: 0100 0000 0000 0000 887e 0800 5701 0001  .........~..W...
2735 000003e0: 8406 0000 2000 3010 7b04 3200 7c3f e86a  .... .0.{.2.|?.j
2736 000003f0: 91ff ffff 0a00 0000 0000 0000 ec2d 0300  .............-..
2737 00000400: 2301 0001 8406 0000 2000 3010 7b04 3200  #....... .0.{.2.
2738 00000410: 1500 2c00 0000 0000 0000 0000 0000 0000  ..,.............
2739 00000420: ffff ffff ffff ff7f 0100 0000 3c01 0001  ............<...
2740 00000430: 0a42 0c00 2401 0001 8406 0000 2000 3010  .B..$....... .0.
2741 00000440: 0800 0000 1500 2c00 0000 0000 0000 0000  ......,.........
2742 00000450: 0000 0000 0700 0000 0002 0000 885f 0100  ............._..
2743 00000460: 6c01 0001 8406 0000 2000 3010 0100 0000  l....... .0.....
2744 00000470: 1500 2c00 0000 0000 0000 0000 566b 0000  ..,.........Vk..
2745 00000480: 0c25 0100 6d01 0001 8406 0000 2000 3010  .%..m....... .0.
2746 00000490: 0400 0000 1500 2c00 0000 0000 0000 0000  ......,.........
2747 000004a0: 0700 0000 ffff ffff ffff ff07 1400 0000  ................
2748 000004b0: 0100 0000 caee 0100 5101 0001 8406 0000  ........Q.......
2749 000004c0: 2000 3010 1100 0000 1500 2c00 0000 0000   .0.......,.....
2750 000004d0: 0000 0000 0700 0000 2500 0000 2000 3010  ........%... .0.
2751 000004e0: 323b 0600 3201 0001 8406 0000 2000 3010  2;..2....... .0.
2752 000004f0: c86e 0000 1500 2c00 0000 0000 0700 0000  .n....,.........
2753 00000500: 0000 0000 0000 0000 0000 0000 0080 b000  ................
2754 00000510: 0000 0000 0000 0000 0000 0000 0000 0000  ................
2755 00000520: 0000 0000 2024 0000 0400 0000 ae0a 1a00  .... $..........
2756 00000530: 3a01 0001 8406 0000 2000 3010 0000 0000  :....... .0.....
2757 00000540: 1500 2c00 0000 0000 0000 0000 0000 0000  ..,.............
2758 00000550: 6101 0000 0700 0000 0000 0000 cf63 0000  a............c..
2759 00000560: 4100 0000 0700 0000 b4c5 0200 3301 0001  A...........3...
2760 00000570: 8406 0000 2000 3010 2000 3010 1500 2c00  .... .0. .0...,.
2761 00000580: 0000 0000 cfe3 2000 0000 0000 0700 0000  ...... .........
2762 00000590: 0000 0000 0000 0000 0000 0000 0080 b000  ................
2763 000005a0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
2764 000005b0: 0000 0000 2024 0000 6c01 0001 4859 0400  .... $..l...HY..
2765 000005c0: 1b01 0001 8406 0000 2000 3010 0000 0000  ........ .0.....
2766 000005d0: 1500 2c00 0000 0000 9c99 ea6a 91ff ffff  ..,........j....
2767 000005e0: c850 0500 6001 0001 8406 0000 2000 3010  .P..`....... .0.
2768 000005f0: 0000 0000 1500 2c00 0000 0000 0000 0000  ......,.........
2769 00000600: 0700 0000 2ee6 0200 3e01 0002 8406 0000  ........>.......
2770 00000610: 2000 3010 566b 0000 1500 2c00 0000 0000   .0.Vk....,.....
2771 00000620: 0800 0000 0000 0000 0700 0000 0700 0000  ................
2772 00000630: 0000 0000 0000 0000 0100 0000 8081 3010  ..............0.
2773 00000640: a804 0400 5701 0001 8406 0000 2000 3010  ....W....... .0.
2774 00000650: cb07 3200 885a ea6a 91ff ffff 0100 0000  ..2..Z.j........
2775 00000660: 0000 0000 8875 0300 6c01 0001 8406 0000  .....u..l.......
2776 00000670: 2000 3010 0300 0000 0300 0000 0000 0000   .0.............
2777 00000680: 0100 0000 0100 0000 ccd4 0100 6d01 0001  ............m...
2778 00000690: 8406 0000 2000 3010 cb07 3200 0300 0000  .... .0...2.....
2779 000006a0: 0000 0000 0000 0000 0200 0000 2724 0000  ............'$..
2780 000006b0: 0000 0000 1100 3010 0100 0000 a850 0500  ......0......P..
2781 000006c0: 5701 0001 8406 0000 2000 3010 0000 0000  W....... .0.....
2782 000006d0: 885a ea6a 91ff ffff 0100 0000 0000 0000  .Z.j............
2783 000006e0: 680f 0200 6c01 0001 8406 0000 2000 3010  h...l....... .0.
2784 000006f0: 0000 0000 0400 0000 0000 0000 0100 0000  ................
2785 00000700: 6d01 0001 ac79 0100 6d01 0001 8406 0000  m....y..m.......
2786 00000710: 2000 3010 0000 0000 0400 0000 0000 0000   .0.............
2787 00000720: 0000 0000 0200 0000 2924 0000 0000 0000  ........)$......
2788 00000730: 1143 0200 0100 0000 2818 0400 5701 0001  .C......(...W...
2789 00000740: 8406 0000 2000 3010 0000 0000 8829 e86a  .... .0......).j
2790 00000750: 91ff ffff 0200 0000 0000 0000 8838 0100  .............8..
2791 00000760: 1b01 0001 8406 0000 2000 3010 0400 0000  ........ .0.....
2792 00000770: 1500 2c00 0000 0000 a029 e86a 91ff ffff  ..,......).j....
2793 00000780: 0e89 0300 5301 0001 8406 0000 2000 3010  ....S....... .0.
2794 00000790: e128 0000 1500 2c00 0000 0000 2500 0000  .(....,.....%...
2795 000007a0: 0000 0000 cfe3 2000 0000 0000 0000 0000  ...... .........
2796 000007b0: 0700 0000 6000 0000 0700 0000 aca0 0100  ....`...........
2797 000007c0: 6701 0001 8406 0000 2000 3010 e128 0000  g....... .0..(..
2798 000007d0: 1500 2c00 0000 0000 0000 0000 0700 0000  ..,.............
2799 000007e0: cfe3 2000 0000 0000 01a2 0800 0000 0000  .. .............
2800 000007f0: 28b2 1e00 1b01 0001 8406 0000 2000 3010  (........... .0.
2801 00000800: e128 0000 1500 2c00 0000 0000 9846 e86a  .(....,......F.j
2802 00000810: 91ff ffff 68d2 1b00 5701 0001 8406 0000  ....h...W.......
2803 00000820: 2000 3010 e128 0000 7c3f e86a 91ff ffff   .0..(..|?.j....
2804 00000830: 0a00 0000 0000 0000 0c57 0200 2301 0001  .........W..#...
2805 00000840: 8406 0000 2000 3010 006c 0000 1500 2c00  .... .0..l....,.
2806 00000850: 0000 0000 0700 0000 0000 0000 f8ff ffff  ................
2807 00000860: ffff ff7f 0100 0000 0000 0000 6e69 8200  ............ni..
2808 00000870: 2501 0001 8406 0000 2000 3010 ca6e 0000  %....... .0..n..
2809 00000880: 1500 2c00 0000 0000 0000 0000 0700 0000  ..,.............
2810 00000890: 0000 0000 0000 0000 0700 0000 0000 0000  ................
2811 000008a0: 0100 0000 0200 3010 3e13 bd82 5500 0000  ......0.>...U...
2812 000008b0: 0600 0000 3001 0001 8015 0000 2000 3010  ....0....... .0.
2813 000008c0: 0000 0000 c801 1400 0000 0000 8860 4404  .............`D.
2814 000008d0: 1c01 0001 8015 0000 2000 3010 2000 0000  ........ .0. ...
2815 000008e0: c801 1400 0000 0000 0000 0000 0000 0000  ................
2816 000008f0: 88a9 0300 5701 0001 8015 0000 2000 3010  ....W....... .0.
2817 00000900: 0400 0000 1c1e e86a 91ff ffff 0300 0000  .......j........
2818 00000910: 0000 0000 a85a 1600 1b01 0001 8015 0000  .....Z..........
2819 00000920: 2000 3010 2000 3010 c801 1400 0000 0000   .0. .0.........
2820 00000930: c41e e86a 91ff ffff ca95 1c00 2901 0001  ...j........)...
2821 00000940: 8015 0000 2000 3010 2000 3010 c801 1400  .... .0. .0.....
2822 00000950: 0000 0000 0000 0000 0000 0000 0000 0000  ................
2823 00000960: 0010 0000 c8fb 0100 2801 0001 8015 0000  ........(.......
2824 00000970: 2000 3010 5101 0001 c801 1400 0000 0000   .0.Q...........
2825 00000980: 0000 0000 0000 0000 6af1 0600 2901 0001  ........j...)...
2826 00000990: 8015 0000 2000 3010 0000 0000 c801 1400  .... .0.........
2827 000009a0: 0000 0000 0100 0000 0000 0000 0000 0000  ................
2828 000009b0: 0010 0000 488f 0000 2801 0001 8015 0000  ....H...(.......
2829 000009c0: 2000 3010 0200 ffff c801 1400 0000 0000   .0.............
2830 000009d0: 0100 0000 0000 0000 483b 0900 4d01 0001  ........H;..M...
2831 000009e0: 8015 0000 2000 3010 0000 0000 c801 1400  .... .0.........
2832 000009f0: 0000 0000 1800 0000 0000 0000 8852 0100  .............R..
2833 00000a00: 5701 0001 8015 0000 2000 3010 e128 0000  W....... .0..(..
2834 00000a10: 9ce9 e76a 91ff ffff 0a00 0000 0000 0000  ...j............
2835 00000a20: e615 0200 3001 0001 8015 0000 2000 3010  ....0....... .0.
2836 00000a30: 0155 0000 c801 1400 0000 0000 68d0 0000  .U..........h...
2837 00000a40: 1b01 0001 8015 0000 2000 3010 6606 3200  ........ .0.f.2.
2838 00000a50: c801 1400 0000 0000 acfa ea6a 91ff ffff  ...........j....
2839 00000a60: 6a0f 0200 6901 0001 8015 0000 2000 3010  j...i....... .0.
2840 00000a70: 7106 3200 c801 1400 0000 0000 0000 0000  q.2.............
2841 00000a80: 0000 0000 ffff ffff 0000 0000 e895 0300  ................
2842 00000a90: 5701 0001 8015 0000 2000 3010 0300 0000  W....... .0.....
2843 00000aa0: acbe ea6a 91ff ffff 0100 0000 0000 0000  ...j............
2844 00000ab0: 8a38 0100 6501 0001 8015 0000 2000 3010  .8..e....... .0.
2845 00000ac0: c41e e86a c801 1400 0000 0000 0000 0000  ...j............
2846 00000ad0: feff ffff 0000 0000 0000 0000 ee86 0400  ................
2847 00000ae0: 6301 0001 8015 0000 2000 3010 0000 0000  c....... .0.....
2848 00000af0: c801 1400 0000 0000 0000 0000 0000 0000  ................
2849 00000b00: 0000 0000 0000 0000 4285 5000 0000 0000  ........B.P.....
2850 00000b10: 0200 0000 0000 0000 8e36 0200 6201 0001  .........6..b...
2851 00000b20: 8015 0000 2000 3010 7d55 0000 c801 1400  .... .0.}U......
2852 00000b30: 0000 0000 0000 0000 0100 0000 0000 0000  ................
2853 00000b40: 0000 0000 4285 5000 0000 0000 0000 0000  ....B.P.........
2854 00000b50: 0200 3010 8c5f 0100 3401 0001 8015 0000  ..0.._..4.......
2855 00000b60: 2000 3010 0000 0000 c801 1400 0000 0000   .0.............
2856 00000b70: 4285 5000 0000 0000 0200 0000 0000 0000  B.P.............
2857 00000b80: 1000 0000 8081 0000 aa43 0500 3c01 0001  .........C..<...
2858 00000b90: 8015 0000 2000 3010 2801 0001 c801 1400  .... .0.(.......
2859 00000ba0: 0000 0000 4205 0000 a100 0000 0200 0000  ....B...........
2860 00000bb0: 0200 0000 8871 0800 5701 0001 8015 0000  .....q..W.......
2861 00000bc0: 2000 3010 2000 3010 885a ea6a 91ff ffff   .0. .0..Z.j....
2862 00000bd0: 0100 0000 0000 0000 4825 0400 6c01 0001  ........H%..l...
2863 00000be0: 8015 0000 2000 3010 2801 0001 0300 0000  .... .0.(.......
2864 00000bf0: 0000 0000 0400 0000 7106 3200 0c73 0100  ........q.2..s..
2865 00000c00: 6d01 0001 8015 0000 2000 3010 2901 0001  m....... .0.)...
2866 00000c10: 0300 0000 0000 0000 0400 0000 0100 0000  ................
2867 00000c20: 2680 0800 0000 0000 1100 0000 0100 0000  &...............
2868 00000c30: c845 0400 5701 0001 8015 0000 2000 3010  .E..W....... .0.
2869 00000c40: 2000 3010 885a ea6a 91ff ffff 0100 0000   .0..Z.j........
2870 00000c50: 0000 0000 e8c9 0000 6c01 0001 8015 0000  ........l.......
2871 00000c60: 2000 3010 2000 3010 0400 0000 0000 0000   .0. .0.........
2872 00000c70: 0800 0000 0500 0000 6cdd 0000 6d01 0001  ........l...m...
2873 00000c80: 8015 0000 2000 3010 2801 0001 0400 0000  .... .0.(.......
2874 00000c90: 0000 0000 0800 0000 0300 0000 1290 7000  ..............p.
2875 00000ca0: 0000 0000 1100 0000 0100 0000 6875 0300  ............hu..
2876 00000cb0: 5701 0001 8015 0000 2000 3010 7106 3200  W....... .0.q.2.
2877 00000cc0: 8829 e86a 91ff ffff 0200 0000 0000 0000  .).j............
2878 00000cd0: a847 0000 1b01 0001 8015 0000 2000 3010  .G.......... .0.
2879 00000ce0: 9ce9 e76a c801 1400 0000 0000 a029 e86a  ...j.........).j
2880 00000cf0: 91ff ffff e83a 0300 1b01 0001 8015 0000  .....:..........
2881 00000d00: 2000 3010 7106 3200 c801 1400 0000 0000   .0.q.2.........
2882 00000d10: 9c99 ea6a 91ff ffff ae93 0100 6601 0001  ...j........f...
2883 00000d20: 8015 0000 2000 3010 acfa ea6a c801 1400  .... .0....j....
2884 00000d30: 0000 0000 0000 0000 feff ffff 0000 0000  ................
2885 00000d40: 2000 3010 0000 0000 0000 0000 0000 0000   .0.............
2886 00000d50: 0000 0000 48b6 0000 1b01 0001 8015 0000  ....H...........
2887 00000d60: 2000 3010 e128 0000 c801 1400 0000 0000   .0..(..........
2888 00000d70: 9c99 ea6a 91ff ffff a8ea 0300 1b01 0001  ...j............
2889 00000d80: 8015 0000 2000 3010 e128 0000 c801 1400  .... .0..(......
2890 00000d90: 0000 0000 08eb e76a 91ff ffff 885f 0100  .......j....._..
2891 00000da0: 4e01 0001 8015 0000 2000 3010 2efe 0300  N....... .0.....
2892 00000db0: c801 1400 0000 0000 0800 0000 0000 0000  ................
2893 00000dc0: e8bc 0000 5701 0001 8015 0000 2000 3010  ....W....... .0.
2894 00000dd0: 0000 0000 8829 e86a 91ff ffff 0200 0000  .....).j........
2895 00000de0: 0000 0000 c895 0000 1b01 0001 8015 0000  ................
2896 00000df0: 2000 3010 2000 3010 c801 1400 0000 0000   .0. .0.........
2897 00000e00: a029 e86a 91ff ffff cab2 0b00 1e01 0001  .).j............
2898 00000e10: 8015 0000 2000 3010 0000 0000 c801 1400  .... .0.........
2899 00000e20: 0000 0000 0000 0000 0000 0000 0400 0000  ................
2900 00000e30: 0000 0000 689a 0400 5701 0001 8015 0000  ....h...W.......
2901 00000e40: 2000 3010 0000 0000 586f e86a 91ff ffff   .0.....Xo.j....
2902 00000e50: 0100 0000 0000 0000 8884 0200 6c01 0001  ............l...
2903 00000e60: 8015 0000 2000 3010 8829 e86a c801 1400  .... .0..).j....
2904 00000e70: 0000 0000 0000 0000 4100 0000 ac47 0000  ........A....G..
2905 00000e80: 6d01 0001 8015 0000 2000 3010 e128 0000  m....... .0..(..
2906 00000e90: c801 1400 0000 0000 0000 0000 0000 0000  ................
2907 00000ea0: 0000 0000 0000 0000 001c 0200 0000 0000  ................
2908 00000eb0: 2a66 0100 5101 0001 8015 0000 2000 3010  *f..Q....... .0.
2909 00000ec0: 0000 0000 c801 1400 0000 0000 0000 0000  ................
2910 00000ed0: 0100 0000 0000 0000 2000 3010 087e 0200  ........ .0..~..
2911 00000ee0: 6a01 0001 8015 0000 2000 3010 0100 0000  j....... .0.....
2912 00000ef0: c801 1400 0000 0000 0000 0000 0100 0000  ................
2913 00000f00: 8c11 0100 6b01 0001 8015 0000 2000 3010  ....k....... .0.
2914 00000f10: 1b01 0001 c801 1400 0000 0000 0000 0000  ................
2915 00000f20: 0000 0000 0000 0000 0000 0000 0028 0000  .............(..
2916 00000f30: 2000 3010 8c5f 0100 6701 0001 8015 0000   .0.._..g.......
2917 00000f40: 2000 3010 0000 0000 c801 1400 0000 0000   .0.............
2918 00000f50: 0000 0000 ffff ffff ffff ffff ffff ff07  ................
2919 00000f60: 0800 0000 0700 0000 6e02 0200 5301 0001  ........n...S...
2920 00000f70: 8015 0000 2000 3010 0100 0000 c801 1400  .... .0.........
2921 00000f80: 0000 0000 0000 0000 2000 3010 0000 0000  ........ .0.....
2922 00000f90: 0100 0000 0000 0000 0100 0000 0000 0000  ................
2923 00000fa0: 0000 0000 cc3a 0300 3f01 0002 8015 0000  .....:..?.......
2924 00000fb0: 2000 3010 7106 3200 c801 1400 0000 0000   .0.q.2.........
2925 00000fc0: 0800 0000 0000 0000 0100 0000 0000 0000  ................
2926 00000fd0: 8081 3010 3d00 0000 f542 0000 0000 0000  ..0.=....B......
2927 00000fe0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
2928 00000ff0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
2929     )",
2930 };
2931 
TEST_F(CpuReaderParsePagePayloadTest,ParseExt4WithOverwrite)2932 TEST_F(CpuReaderParsePagePayloadTest, ParseExt4WithOverwrite) {
2933   const ExamplePage* test_case = &g_full_page_ext4;
2934 
2935   ProtoTranslationTable* table = GetTable(test_case->name);
2936   auto page = PageFromXxd(test_case->data);
2937 
2938   FtraceDataSourceConfig ds_config = EmptyConfig();
2939   ds_config.event_filter.AddEnabledEvent(
2940       table->EventToFtraceId(GroupAndName("sched", "sched_switch")));
2941 
2942   const uint8_t* parse_pos = page.get();
2943   std::optional<CpuReader::PageHeader> page_header =
2944       CpuReader::ParsePageHeader(&parse_pos, table->page_header_size_len());
2945 
2946   const uint8_t* page_end = page.get() + base::GetSysPageSize();
2947   ASSERT_TRUE(page_header.has_value());
2948   EXPECT_TRUE(page_header->lost_events);  // data loss
2949   EXPECT_LE(parse_pos + page_header->size, page_end);
2950 
2951   FtraceParseStatus status = CpuReader::ParsePagePayload(
2952       parse_pos, &page_header.value(), table, &ds_config,
2953       CreateBundler(ds_config), &metadata_, &last_read_event_ts_);
2954 
2955   EXPECT_EQ(status, FtraceParseStatus::FTRACE_STATUS_OK);
2956 
2957   EXPECT_THAT(AllTracePackets(), IsEmpty());
2958 }
2959 
2960 // Page with a single event containing a __data_loc entry with value 0x0000
2961 //
2962 //            [timestamp            ] [32 byte payload next ]
2963 //  00000000: D7 B3 0A 57 CF 02 00 00 20 00 00 00 00 00 00 00   ...W.... .......
2964 //            [evt hdr  ] [id ]
2965 //  00000010: 67 A6 13 00 0F 06 00 00 3D 01 00 00 45 00 00 00   g.......=...E...
2966 //  00000020: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................
2967 //
2968 // name: tracing_mark_write
2969 // ID: 1551
2970 // format:
2971 //     field:unsigned short common_type;    offset:0;    size:2;    signed:0;
2972 //     field:unsigned char common_flags;    offset:2;    size:1;    signed:0;
2973 //     field:unsigned char common_preempt_count;    offset:3;    size:1;
2974 //     signed:0; field:int common_pid;    offset:4;    size:4;    signed:1;
2975 //
2976 //     field:char type;    offset:8;    size:1;    signed:0;
2977 //     field:int pid;    offset:12;    size:4;    signed:1;
2978 //     field:__data_loc char[] name;    offset:16;    size:4;    signed:0;
2979 //     field:int value;    offset:20;    size:4;    signed:1;
2980 //
2981 static char g_zero_data_loc[] =
2982     R"(
2983 00000000: D7B3 0A57 CF02 0000 2000 0000 0000 0000   ...W.... .......
2984 00000010: 67A6 1300 0F06 0000 3D01 0000 4500 0000   g.......=...E...
2985 00000020: 0000 0000 0000 0000 0000 0000 0000 0000   ................
2986 00000030: 0000 0000 0000 0000 0000 0000 0000 0000   ................
2987   )";
2988 
2989 TEST_F(CpuReaderParsePagePayloadTest, ZeroLengthDataLoc) {
2990   auto page = PageFromXxd(g_zero_data_loc);
2991 
2992   // Hand-build a translation table that handles dpu/tracing_mark_write for this
2993   // test page.
2994   // TODO(rsavitski): look into making these tests less verbose by feeding a
2995   // format string through proto_translation_table to get the format.
2996   std::vector<Field> common_fields;
2997   {  // common_pid
2998     common_fields.emplace_back(Field{});
2999     Field* field = &common_fields.back();
3000     field->ftrace_offset = 4;
3001     field->ftrace_size = 4;
3002     field->ftrace_type = kFtraceCommonPid32;
3003     field->proto_field_id = 2;
3004     field->proto_field_type = ProtoSchemaType::kInt32;
3005     SetTranslationStrategy(field->ftrace_type, field->proto_field_type,
3006                            &field->strategy);
3007   }
3008   using Dpu = protos::gen::DpuTracingMarkWriteFtraceEvent;
3009   Event evt{"tracing_mark_write",
3010             "dpu",
3011             {
3012                 {8, 1, FtraceFieldType::kFtraceUint8, "type",
3013                  Dpu::kTypeFieldNumber, ProtoSchemaType::kUint32,
3014                  TranslationStrategy::kInvalidTranslationStrategy},
3015                 {12, 4, FtraceFieldType::kFtraceInt32, "pid",
3016                  Dpu::kPidFieldNumber, ProtoSchemaType::kInt32,
3017                  TranslationStrategy::kInvalidTranslationStrategy},
3018                 {16, 4, FtraceFieldType::kFtraceDataLoc, "name",
3019                  Dpu::kNameFieldNumber, ProtoSchemaType::kString,
3020                  TranslationStrategy::kInvalidTranslationStrategy},
3021                 {20, 4, FtraceFieldType::kFtraceInt32, "value",
3022                  Dpu::kValueFieldNumber, ProtoSchemaType::kInt32,
3023                  TranslationStrategy::kInvalidTranslationStrategy},
3024             },
3025             /*ftrace_event_id=*/1551,
3026             /*proto_field_id=*/348,
3027             /*size=*/24};
3028   for (Field& field : evt.fields) {
3029     SetTranslationStrategy(field.ftrace_type, field.proto_field_type,
3030                            &field.strategy);
3031   }
3032   std::vector<Event> events;
3033   events.emplace_back(std::move(evt));
3034 
3035   NiceMock<MockFtraceProcfs> mock_ftrace;
3036   PrintkMap printk_formats;
3037   ProtoTranslationTable translation_table(
3038       &mock_ftrace, events, std::move(common_fields),
3039       ProtoTranslationTable::DefaultPageHeaderSpecForTesting(),
3040       InvalidCompactSchedEventFormatForTesting(), printk_formats);
3041   ProtoTranslationTable* table = &translation_table;
3042 
3043   FtraceDataSourceConfig ds_config = EmptyConfig();
3044   ds_config.event_filter.AddEnabledEvent(
3045       table->EventToFtraceId(GroupAndName("dpu", "tracing_mark_write")));
3046 
3047   FtraceMetadata metadata{};
3048   const uint8_t* parse_pos = page.get();
3049   std::optional<CpuReader::PageHeader> page_header =
3050       CpuReader::ParsePageHeader(&parse_pos, table->page_header_size_len());
3051 
3052   const uint8_t* page_end = page.get() + base::GetSysPageSize();
3053   ASSERT_TRUE(page_header.has_value());
3054   EXPECT_FALSE(page_header->lost_events);
3055   EXPECT_LE(parse_pos + page_header->size, page_end);
3056 
3057   FtraceParseStatus status = CpuReader::ParsePagePayload(
3058       parse_pos, &page_header.value(), table, &ds_config,
3059       CreateBundler(ds_config), &metadata_, &last_read_event_ts_);
3060 
3061   // successfully parsed the whole 32 byte event
3062   ASSERT_EQ(32u, page_header->size);
3063   EXPECT_EQ(status, FtraceParseStatus::FTRACE_STATUS_OK);
3064 
3065   auto bundle = GetBundle();
3066   EXPECT_EQ(bundle.event().size(), 1u);
3067   const protos::gen::FtraceEvent& event = bundle.event()[0];
3068   EXPECT_EQ(event.pid(), 317u);
3069   EXPECT_EQ(event.dpu_tracing_mark_write().type(), 69u);
3070   EXPECT_EQ(event.dpu_tracing_mark_write().pid(), 0);
3071   EXPECT_EQ(event.dpu_tracing_mark_write().value(), 0);
3072   EXPECT_EQ(event.dpu_tracing_mark_write().name(), "");
3073 }
3074 
3075 static ExamplePage g_zero_padded{
3076     "synthetic",
3077     R"(
3078     00000000: DBF4 87FE F901 0000 F00F 0000 0000 0000   ................
3079     00000010: 0700 0000 0500 0000 EE02 0000 50AA 4C00   ............P.L.
3080     00000020: AEFF FFFF 457C 3633 390A 0000 0000 0000   ....E|639.......
3081     00000030: E939 1300 0500 0000 EE02 0000 50AA 4C00   .9..........P.L.
3082     00000040: AEFF FFFF 427C 3633 397C 6361 6E63 656C   ....B|639|cancel
3083     00000050: 2074 696D 6572 0A00 4753 0A00 0500 0000    timer..GS......
3084     00000060: EE02 0000 50AA 4C00 AEFF FFFF 457C 3633   ....P.L.....E|63
3085     00000070: 390A 0000 0000 0000 C929 0800 0500 0000   9........)......
3086     00000080: EE02 0000 50AA 4C00 AEFF FFFF 437C 3633   ....P.L.....C|63
3087     00000090: 397C 5653 594E 432D 6170 707C 310A 0000   9|VSYNC-app|1...
3088     000000A0: 2A48 0600 6500 0101 EE02 0000 6170 7000   *H..e.......app.
3089     000000B0: 6163 6566 6C69 6E67 6572 0000 EF02 0000   aceflinger......
3090     000000C0: 6100 0000 0100 0000 0200 0000 E94D 1900   a............M..
3091     000000D0: 0500 0000 EE02 0000 50AA 4C00 AEFF FFFF   ........P.L.....
3092     000000E0: 437C 3633 397C 5653 502D 6D6F 6465 7C30   C|639|VSP-mode|0
3093     000000F0: 0A00 0000 0DD5 0400 0500 0000 EE02 0000   ................
3094     00000100: 50AA 4C00 AEFF FFFF 437C 3633 397C 5653   P.L.....C|639|VS
3095     00000110: 502D 7469 6D65 506F 696E 747C 3231 3733   P-timePoint|2173
3096     00000120: 3235 3939 3337 3132 360A 0000 2DF1 0300   259937126...-...
3097     00000130: 0500 0000 EE02 0000 50AA 4C00 AEFF FFFF   ........P.L.....
3098     00000140: 437C 3633 397C 5653 502D 7072 6564 6963   C|639|VSP-predic
3099     00000150: 7469 6F6E 7C32 3137 3332 3736 3230 3036   tion|21732762006
3100     00000160: 3538 0A00 30B0 0600 0500 0000 EE02 0000   58..0...........
3101     00000170: 50AA 4C00 AEFF FFFF 427C 3633 397C 6170   P.L.....B|639|ap
3102     00000180: 7020 616C 6172 6D20 696E 2031 3632 3633   p alarm in 16263
3103     00000190: 7573 3B20 5653 594E 4320 696E 2034 3732   us; VSYNC in 472
3104     000001A0: 3633 7573 0A00 0000 878F 0300 0500 0000   63us............
3105     000001B0: EE02 0000 50AA 4C00 AEFF FFFF 457C 3633   ....P.L.....E|63
3106     000001C0: 390A 0000 0000 0000 3029 1B00 5B00 0102   9.......0)..[...
3107     000001D0: EE02 0000 5469 6D65 7244 6973 7061 7463   ....TimerDispatc
3108     000001E0: 6800 0000 EE02 0000 6100 0000 0100 0000   h.......a.......
3109     000001F0: 0000 0000 7377 6170 7065 722F 3500 0000   ....swapper/5...
3110     00000200: 0000 0000 0000 0000 7800 0000 10DC 4302   ........x.....C.
3111     00000210: 5B00 0102 0000 0000 7377 6170 7065 722F   [.......swapper/
3112     00000220: 3500 0000 0000 0000 0000 0000 7800 0000   5...........x...
3113     00000230: 0000 0000 0000 0000 7263 756F 702F 3200   ........rcuop/2.
3114     00000240: 0000 0000 0000 0000 2000 0000 7800 0000   ........ ...x...
3115     00000250: CA71 0B00 6500 0102 2000 0000 7263 755F   .q..e... ...rcu_
3116     00000260: 7072 6565 6D70 7400 0000 0000 0B00 0000   preempt.........
3117     00000270: 7800 0000 0100 0000 0300 0000 0859 0100   x............Y..
3118     00000280: 3700 0102 2000 0000 0B00 0000 0000 0000   7... ...........
3119     00000290: 6899 4200 AEFF FFFF 0000 0000 0000 0000   h.B.............
3120     000002A0: 300F 1B00 5B00 0102 2000 0000 7263 756F   0...[... ...rcuo
3121     000002B0: 702F 3200 0000 0000 0000 0000 2000 0000   p/2......... ...
3122     000002C0: 7800 0000 0100 0000 0000 0000 6E64 726F   x...........ndro
3123     000002D0: 6964 2E73 7973 7465 6D75 6900 A009 0000   id.systemui.....
3124     000002E0: 7800 0000 17EC 3100 0500 0000 A009 0000   x.....1.........
3125     000002F0: 50AA 4C00 AEFF FFFF 427C 3234 3634 7C61   P.L.....B|2464|a
3126     00000300: 6E64 726F 6964 2E76 6965 772E 4163 6365   ndroid.view.Acce
3127     00000310: 7373 6962 696C 6974 7949 6E74 6572 6163   ssibilityInterac
3128     00000320: 7469 6F6E 436F 6E74 726F 6C6C 6572 2450   tionController$P
3129     00000330: 7269 7661 7465 4861 6E64 6C65 723A 2023   rivateHandler: #
3130     00000340: 320A 0000 8998 EB00 EA02 0000 A009 0000   2...............
3131     00000350: 4AD7 0C00 2697 0500 CE22 0000 0000 0000   J...&...."......
3132     00000360: 0000 0000 0100 0000 1100 0000 CA45 0400   .............E..
3133     00000370: EB02 0000 A009 0000 4AD7 0C00 0000 0000   ........J.......
3134     00000380: A402 0000 0000 0000 0000 0000 0000 0000   ................
3135     00000390: 0000 0000 0000 0000 CA6C 0400 6500 0104   .........l..e...
3136     000003A0: A009 0000 6269 6E64 6572 3A38 3931 305F   ....binder:8910_
3137     000003B0: 3400 6F00 3C2C 0000 7800 0000 0100 0000   4.o.<,..x.......
3138     000003C0: 0400 0000 673C 3400 0500 0000 A009 0000   ....g<4.........
3139     000003D0: 50AA 4C00 AEFF FFFF 457C 3234 3634 0A00   P.L.....E|2464..
3140     000003E0: 0000 0000 10EF 2000 5B00 0102 A009 0000   ...... .[.......
3141     000003F0: 6E64 726F 6964 2E73 7973 7465 6D75 6900   ndroid.systemui.
3142     00000400: A009 0000 7800 0000 0100 0000 0000 0000   ....x...........
3143     00000410: 7377 6170 7065 722F 3500 0000 0000 0000   swapper/5.......
3144     00000420: 0000 0000 7800 0000 D098 ED01 5B00 0102   ....x.......[...
3145     00000430: 0000 0000 7377 6170 7065 722F 3500 0000   ....swapper/5...
3146     00000440: 0000 0000 0000 0000 7800 0000 0000 0000   ........x.......
3147     00000450: 0000 0000 6E64 726F 6964 2E73 7973 7465   ....ndroid.syste
3148     00000460: 6D75 6900 A009 0000 7800 0000 F761 1F00   mui.....x....a..
3149     00000470: 0500 0000 A009 0000 50AA 4C00 AEFF FFFF   ........P.L.....
3150     00000480: 427C 3234 3634 7C61 6E64 726F 6964 2E76   B|2464|android.v
3151     00000490: 6965 772E 4163 6365 7373 6962 696C 6974   iew.Accessibilit
3152     000004A0: 7949 6E74 6572 6163 7469 6F6E 436F 6E74   yInteractionCont
3153     000004B0: 726F 6C6C 6572 2450 7269 7661 7465 4861   roller$PrivateHa
3154     000004C0: 6E64 6C65 723A 2023 320A 0000 E9F6 A500   ndler: #2.......
3155     000004D0: EA02 0000 A009 0000 4ED7 0C00 2697 0500   ........N...&...
3156     000004E0: CE22 0000 0000 0000 0000 0000 0100 0000   ."..............
3157     000004F0: 1100 0000 4A3F 0400 EB02 0000 A009 0000   ....J?..........
3158     00000500: 4ED7 0C00 0000 0000 2802 0000 0000 0000   N.......(.......
3159     00000510: 0000 0000 0000 0000 0000 0000 0000 0000   ................
3160     00000520: EA93 0400 6500 0104 A009 0000 6269 6E64   ....e.......bind
3161     00000530: 6572 3A38 3931 305F 3400 6F00 3C2C 0000   er:8910_4.o.<,..
3162     00000540: 7800 0000 0100 0000 0000 0000 0AD7 3A01   x.............:.
3163     00000550: 3100 1101 A009 0000 B028 39B1 CCFF FFFF   1........(9.....
3164     00000560: A837 F9A8 ADFF FFFF 0010 39B1 CCFF FFFF   .7........9.....
3165     00000570: 2000 0000 FFFF FFFF 44F5 0100 2E00 1101    .......D.......
3166     00000580: A009 0000 B028 39B1 CCFF FFFF AA79 0100   .....(9......y..
3167     00000590: 6500 1102 A009 0000 6B77 6F72 6B65 722F   e.......kworker/
3168     000005A0: 7531 363A 3130 0000 6001 0000 7800 0000   u16:10..`...x...
3169     000005B0: 0100 0000 0000 0000 8845 0100 3700 1102   .........E..7...
3170     000005C0: A009 0000 6001 0000 0000 0000 7C51 3800   ....`.......|Q8.
3171     000005D0: AEFF FFFF 0000 0000 0000 0000 89DD 7300   ..............s.
3172     000005E0: EA02 0000 A009 0000 50D7 0C00 2697 0500   ........P...&...
3173     000005F0: CE22 0000 0000 0000 0000 0000 0300 0000   ."..............
3174     00000600: 1100 0000 0AD5 0400 EB02 0000 A009 0000   ................
3175     00000610: 50D7 0C00 0000 0000 A404 0000 0000 0000   P...............
3176     00000620: 0000 0000 0000 0000 0000 0000 0000 0000   ................
3177     00000630: 4A7E 0500 6500 0104 A009 0000 6269 6E64   J~..e.......bind
3178     00000640: 6572 3A38 3931 305F 3400 6F00 3C2C 0000   er:8910_4.o.<,..
3179     00000650: 7800 0000 0100 0000 0000 0000 A790 2E00   x...............
3180     00000660: 0500 0000 A009 0000 50AA 4C00 AEFF FFFF   ........P.L.....
3181     00000670: 457C 3234 3634 0A00 0000 0000 9048 2800   E|2464.......H(.
3182     00000680: 5B00 0102 A009 0000 6E64 726F 6964 2E73   [.......ndroid.s
3183     00000690: 7973 7465 6D75 6900 A009 0000 7800 0000   ystemui.....x...
3184     000006A0: 0100 0000 0000 0000 7377 6170 7065 722F   ........swapper/
3185     000006B0: 3500 0000 0000 0000 0000 0000 7800 0000   5...........x...
3186     000006C0: B043 2100 5B00 0102 0000 0000 7377 6170   .C!.[.......swap
3187     000006D0: 7065 722F 3500 0000 0000 0000 0000 0000   per/5...........
3188     000006E0: 7800 0000 0000 0000 0000 0000 6269 6E64   x...........bind
3189     000006F0: 6572 3A32 3436 345F 3800 6900 EF0C 0000   er:2464_8.i.....
3190     00000700: 7800 0000 834C 0700 F002 0000 EF0C 0000   x....L..........
3191     00000710: 51D7 0C00 AA4E 5D00 6500 0103 EF0C 0000   Q....N].e.......
3192     00000720: 6E64 726F 6964 2E73 7973 7465 6D75 6900   ndroid.systemui.
3193     00000730: A009 0000 7800 0000 0100 0000 0500 0000   ....x...........
3194     00000740: D05E 6800 5B00 0102 EF0C 0000 6269 6E64   .^h.[.......bind
3195     00000750: 6572 3A32 3436 345F 3800 6900 EF0C 0000   er:2464_8.i.....
3196     00000760: 7800 0000 0100 0000 0000 0000 6269 6E64   x...........bind
3197     00000770: 6572 3A31 3936 375F 4200 0000 A20B 0000   er:1967_B.......
3198     00000780: 7000 0000 67CA 0600 E902 0000 A20B 0000   p...g...........
3199     00000790: AF07 0000 A20B 0000 7000 0000 7800 0000   ........p...x...
3200     000007A0: 7800 0000 B006 3B00 5B00 0102 A20B 0000   x.....;.[.......
3201     000007B0: 6269 6E64 6572 3A31 3936 375F 4200 0000   binder:1967_B...
3202     000007C0: A20B 0000 7800 0000 0100 0000 0000 0000   ....x...........
3203     000007D0: 7377 6170 7065 722F 3500 0000 0000 0000   swapper/5.......
3204     000007E0: 0000 0000 7800 0000 108B 5603 5B00 0102   ....x.....V.[...
3205     000007F0: 0000 0000 7377 6170 7065 722F 3500 0000   ....swapper/5...
3206     00000800: 0000 0000 0000 0000 7800 0000 0000 0000   ........x.......
3207     00000810: 0000 0000 6269 6E64 6572 3A32 3436 345F   ....binder:2464_
3208     00000820: 3800 6900 EF0C 0000 7800 0000 831A 0600   8.i.....x.......
3209     00000830: F002 0000 EF0C 0000 56D7 0C00 AAD2 5600   ........V.....V.
3210     00000840: 6500 0103 EF0C 0000 6E64 726F 6964 2E73   e.......ndroid.s
3211     00000850: 7973 7465 6D75 6900 A009 0000 7800 0000   ystemui.....x...
3212     00000860: 0100 0000 0000 0000 B027 4100 5B00 0102   .........'A.[...
3213     00000870: EF0C 0000 6269 6E64 6572 3A32 3436 345F   ....binder:2464_
3214     00000880: 3800 6900 EF0C 0000 7800 0000 0100 0000   8.i.....x.......
3215     00000890: 0000 0000 7377 6170 7065 722F 3500 0000   ....swapper/5...
3216     000008A0: 0000 0000 0000 0000 7800 0000 50F4 2A03   ........x...P.*.
3217     000008B0: 5B00 0102 0000 0000 7377 6170 7065 722F   [.......swapper/
3218     000008C0: 3500 0000 0000 0000 0000 0000 7800 0000   5...........x...
3219     000008D0: 0000 0000 0000 0000 6269 6E64 6572 3A32   ........binder:2
3220     000008E0: 3436 345F 3800 6900 EF0C 0000 7800 0000   464_8.i.....x...
3221     000008F0: 831A 0600 F002 0000 EF0C 0000 5BD7 0C00   ............[...
3222     00000900: 8A08 5300 6500 0103 EF0C 0000 6E64 726F   ..S.e.......ndro
3223     00000910: 6964 2E73 7973 7465 6D75 6900 A009 0000   id.systemui.....
3224     00000920: 7800 0000 0100 0000 0000 0000 B0BE 5000   x.............P.
3225     00000930: 5B00 0102 EF0C 0000 6269 6E64 6572 3A32   [.......binder:2
3226     00000940: 3436 345F 3800 6900 EF0C 0000 7800 0000   464_8.i.....x...
3227     00000950: 0100 0000 0000 0000 7377 6170 7065 722F   ........swapper/
3228     00000960: 3500 0000 0000 0000 0000 0000 7800 0000   5...........x...
3229     00000970: 50A1 5A0A 5B00 0102 0000 0000 7377 6170   P.Z.[.......swap
3230     00000980: 7065 722F 3500 0000 0000 0000 0000 0000   per/5...........
3231     00000990: 7800 0000 0000 0000 0000 0000 7263 756F   x...........rcuo
3232     000009A0: 702F 3200 0000 0000 0000 0000 2000 0000   p/2......... ...
3233     000009B0: 7800 0000 EA2B 0700 6500 0102 2000 0000   x....+..e... ...
3234     000009C0: 7263 756F 702F 3300 0000 0000 0000 0000   rcuop/3.........
3235     000009D0: 2800 0000 7800 0000 0100 0000 0000 0000   (...x...........
3236     000009E0: 90F9 1B00 5B00 0102 2000 0000 7263 756F   ....[... ...rcuo
3237     000009F0: 702F 3200 0000 0000 0000 0000 2000 0000   p/2......... ...
3238     00000A00: 7800 0000 0100 0000 0000 0000 7377 6170   x...........swap
3239     00000A10: 7065 722F 3500 0000 0000 0000 0000 0000   per/5...........
3240     00000A20: 7800 0000 303E D509 5B00 0102 0000 0000   x...0>..[.......
3241     00000A30: 7377 6170 7065 722F 3500 0000 0000 0000   swapper/5.......
3242     00000A40: 0000 0000 7800 0000 0000 0000 0000 0000   ....x...........
3243     00000A50: 6269 6E64 6572 3A32 3436 345F 3800 6900   binder:2464_8.i.
3244     00000A60: EF0C 0000 7800 0000 03AA 0900 F002 0000   ....x...........
3245     00000A70: EF0C 0000 66D7 0C00 EAFE 7F00 6500 0103   ....f.......e...
3246     00000A80: EF0C 0000 5363 7265 656E 4465 636F 7261   ....ScreenDecora
3247     00000A90: 7469 6F00 840B 0000 7800 0000 0100 0000   tio.....x.......
3248     00000AA0: 0200 0000 7028 4A00 5B00 0102 EF0C 0000   ....p(J.[.......
3249     00000AB0: 6269 6E64 6572 3A32 3436 345F 3800 6900   binder:2464_8.i.
3250     00000AC0: EF0C 0000 7800 0000 0100 0000 0000 0000   ....x...........
3251     00000AD0: 7377 6170 7065 722F 3500 0000 0000 0000   swapper/5.......
3252     00000AE0: 0000 0000 7800 0000 908D 0406 5B00 0102   ....x.......[...
3253     00000AF0: 0000 0000 7377 6170 7065 722F 3500 0000   ....swapper/5...
3254     00000B00: 0000 0000 0000 0000 7800 0000 0000 0000   ........x.......
3255     00000B10: 0000 0000 6C6F 6764 2E72 6561 6465 722E   ....logd.reader.
3256     00000B20: 7065 7200 5B06 0000 8200 0000 AAE6 2400   per.[.........$.
3257     00000B30: 6500 0102 5B06 0000 6C6F 6763 6174 0000   e...[...logcat..
3258     00000B40: 3000 0000 0000 0000 C105 0000 8200 0000   0...............
3259     00000B50: 0100 0000 0500 0000 90DB 2000 5B00 0102   .......... .[...
3260     00000B60: 5B06 0000 6C6F 6764 2E72 6561 6465 722E   [...logd.reader.
3261     00000B70: 7065 7200 5B06 0000 8200 0000 0100 0000   per.[...........
3262     00000B80: 0000 0000 6C6F 6763 6174 0000 3000 0000   ....logcat..0...
3263     00000B90: 0000 0000 C105 0000 8200 0000 7060 6100   ............p`a.
3264     00000BA0: 5B00 0102 C105 0000 6C6F 6763 6174 0000   [.......logcat..
3265     00000BB0: 3000 0000 0000 0000 C105 0000 8200 0000   0...............
3266     00000BC0: 0100 0000 0000 0000 7377 6170 7065 722F   ........swapper/
3267     00000BD0: 3500 0000 0000 0000 0000 0000 7800 0000   5...........x...
3268     00000BE0: D086 0202 5B00 0102 0000 0000 7377 6170   ....[.......swap
3269     00000BF0: 7065 722F 3500 0000 0000 0000 0000 0000   per/5...........
3270     00000C00: 7800 0000 0000 0000 0000 0000 6170 7000   x...........app.
3271     00000C10: 6163 6566 6C69 6E67 6572 0000 EF02 0000   aceflinger......
3272     00000C20: 6100 0000 2937 2700 0500 0000 EF02 0000   a...)7'.........
3273     00000C30: 50AA 4C00 AEFF FFFF 437C 3633 397C 5653   P.L.....C|639|VS
3274     00000C40: 502D 6D6F 6465 7C30 0A00 0000 8DC3 0300   P-mode|0........
3275     00000C50: 0500 0000 EF02 0000 50AA 4C00 AEFF FFFF   ........P.L.....
3276     00000C60: 437C 3633 397C 5653 502D 7469 6D65 506F   C|639|VSP-timePo
3277     00000C70: 696E 747C 3231 3733 3238 3530 3139 3236   int|217328501926
3278     00000C80: 340A 0000 6D43 0200 0500 0000 EF02 0000   4...mC..........
3279     00000C90: 50AA 4C00 AEFF FFFF 437C 3633 397C 5653   P.L.....C|639|VS
3280     00000CA0: 502D 7072 6564 6963 7469 6F6E 7C32 3137   P-prediction|217
3281     00000CB0: 3332 3932 3839 3739 3138 0A00 70FE 0600   3292897918..p...
3282     00000CC0: 0500 0000 EF02 0000 50AA 4C00 AEFF FFFF   ........P.L.....
3283     00000CD0: 427C 3633 397C 6170 7020 616C 6172 6D20   B|639|app alarm
3284     00000CE0: 696E 2037 3837 3875 733B 2056 5359 4E43   in 7878us; VSYNC
3285     00000CF0: 2069 6E20 3338 3837 3875 730A 0000 0000    in 38878us.....
3286     00000D00: C7AD 0100 0500 0000 EF02 0000 50AA 4C00   ............P.L.
3287     00000D10: AEFF FFFF 457C 3633 390A 0000 0000 0000   ....E|639.......
3288     00000D20: 3028 2B00 5B00 0102 EF02 0000 6170 7000   0(+.[.......app.
3289     00000D30: 6163 6566 6C69 6E67 6572 0000 EF02 0000   aceflinger......
3290     00000D40: 6100 0000 0100 0000 0000 0000 6C6F 6764   a...........logd
3291     00000D50: 2E72 6561 6465 722E 7065 7200 C611 0000   .reader.per.....
3292     00000D60: 8200 0000 8AE1 1A00 6500 0102 C611 0000   ........e.......
3293     00000D70: 6C6F 6763 6174 002F 3000 0000 0000 0000   logcat./0.......
3294     00000D80: BE11 0000 7800 0000 0100 0000 0400 0000   ....x...........
3295     00000D90: 3074 0D00 5B00 0102 C611 0000 6C6F 6764   0t..[.......logd
3296     00000DA0: 2E72 6561 6465 722E 7065 7200 C611 0000   .reader.per.....
3297     00000DB0: 8200 0000 0001 0000 0000 0000 6C6F 6763   ............logc
3298     00000DC0: 6174 002F 3000 0000 0000 0000 BE11 0000   at./0...........
3299     00000DD0: 7800 0000 4A34 3B00 3100 0101 BE11 0000   x...J4;.1.......
3300     00000DE0: 08D4 FF74 CCFF FFFF 40C9 2900 AEFF FFFF   ...t....@.).....
3301     00000DF0: 0044 8940 CBFF FFFF 2000 0000 FFFF FFFF   .D.@.... .......
3302     00000E00: A486 0100 2E00 0101 BE11 0000 08D4 FF74   ...............t
3303     00000E10: CCFF FFFF EA17 0100 6500 0102 BE11 0000   ........e.......
3304     00000E20: 6B77 6F72 6B65 722F 7531 363A 3130 0000   kworker/u16:10..
3305     00000E30: 6001 0000 7800 0000 0100 0000 0200 0000   `...x...........
3306     00000E40: E8BC 0000 3700 0102 BE11 0000 6001 0000   ....7.......`...
3307     00000E50: 0000 0000 7C51 3800 AEFF FFFF 0000 0000   ....|Q8.........
3308     00000E60: 0000 0000 B074 1600 5B00 0102 BE11 0000   .....t..[.......
3309     00000E70: 6C6F 6763 6174 002F 3000 0000 0000 0000   logcat./0.......
3310     00000E80: BE11 0000 7800 0000 0100 0000 0000 0000   ....x...........
3311     00000E90: 6C6F 6764 2E72 6561 6465 722E 7065 7200   logd.reader.per.
3312     00000EA0: C611 0000 8200 0000 6AFA 0B00 6500 0102   ........j...e...
3313     00000EB0: C611 0000 6C6F 6763 6174 002F 3000 0000   ....logcat./0...
3314     00000EC0: 0000 0000 BE11 0000 7800 0000 0100 0000   ........x.......
3315     00000ED0: 0500 0000 7023 0800 5B00 0102 C611 0000   ....p#..[.......
3316     00000EE0: 6C6F 6764 2E72 6561 6465 722E 7065 7200   logd.reader.per.
3317     00000EF0: C611 0000 8200 0000 0001 0000 0000 0000   ................
3318     00000F00: 6C6F 6763 6174 002F 3000 0000 0000 0000   logcat./0.......
3319     00000F10: BE11 0000 7800 0000 AA6B 1100 3100 0101   ....x....k..1...
3320     00000F20: BE11 0000 08D4 FF74 CCFF FFFF 40C9 2900   .......t....@.).
3321     00000F30: AEFF FFFF 0044 8940 CBFF FFFF 2000 0000   .....D.@.... ...
3322     00000F40: FFFF FFFF 64EA 0000 2E00 0101 BE11 0000   ....d...........
3323     00000F50: 08D4 FF74 CCFF FFFF EABC 0000 6500 0102   ...t........e...
3324     00000F60: BE11 0000 6B77 6F72 6B65 722F 7531 363A   ....kworker/u16:
3325     00000F70: 3130 0000 6001 0000 7800 0000 0100 0000   10..`...x.......
3326     00000F80: 0200 0000 48C3 0000 3700 0102 BE11 0000   ....H...7.......
3327     00000F90: 6001 0000 0000 0000 7C51 3800 AEFF FFFF   `.......|Q8.....
3328     00000FA0: 0000 0000 0000 0000 90C4 0F00 5B00 0102   ............[...
3329     00000FB0: BE11 0000 6C6F 6763 6174 002F 3000 0000   ....logcat./0...
3330     00000FC0: 0000 0000 BE11 0000 7800 0000 0100 0000   ........x.......
3331     00000FD0: 0000 0000 6C6F 6764 2E72 6561 6465 722E   ....logd.reader.
3332     00000FE0: 7065 7200 C611 0000 8200 0000 0000 0000   per.............
3333     00000FF0: 0000 0000 0000 0000 0000 0000 0000 0000   ................
3334     )",
3335 };
3336 
3337 // b/204564312: some (mostly 4.19) kernels rarely emit an invalid page, where
3338 // the header says there's valid data, but the contents are a run of zeros
3339 // (which doesn't decode to valid events per the ring buffer ABI). Confirm that
3340 // the error is reported in the ftrace event bundle.
3341 TEST_F(CpuReaderParsePagePayloadTest, InvalidZeroPaddedPage) {
3342   const ExamplePage* test_case = &g_zero_padded;
3343   ProtoTranslationTable* table = GetTable(test_case->name);
3344   auto page = PageFromXxd(test_case->data);
3345 
3346   // Don't need enabled events, as the test checks that we can walk the event
3347   // headers down to the end of the page.
3348   FtraceDataSourceConfig ds_config = EmptyConfig();
3349 
3350   const uint8_t* parse_pos = page.get();
3351   std::optional<CpuReader::PageHeader> page_header =
3352       CpuReader::ParsePageHeader(&parse_pos, table->page_header_size_len());
3353 
3354   const uint8_t* page_end = page.get() + base::GetSysPageSize();
3355   ASSERT_TRUE(page_header.has_value());
3356   EXPECT_FALSE(page_header->lost_events);
3357   EXPECT_LE(parse_pos + page_header->size, page_end);
3358 
3359   FtraceParseStatus status = CpuReader::ParsePagePayload(
3360       parse_pos, &page_header.value(), table, &ds_config,
3361       CreateBundler(ds_config), &metadata_, &last_read_event_ts_);
3362 
3363   EXPECT_EQ(0xff0u, page_header->size);
3364   EXPECT_EQ(status, FtraceParseStatus::FTRACE_STATUS_ABI_ZERO_DATA_LENGTH);
3365 
3366   EXPECT_THAT(AllTracePackets(), IsEmpty());
3367 }
3368 
3369 static ExamplePage g_four_byte_commit{
3370     "synthetic",
3371     R"(
3372 00000000: 105B DA5D C100 0000 0400 0000 0000 0000  .[.]............
3373 00000010: 0000 0000 0000 0000 0000 0000 0000 0000  ................
3374     )",
3375 };
3376 
TEST_F(CpuReaderParsePagePayloadTest,InvalidHeaderLength)3377 TEST_F(CpuReaderParsePagePayloadTest, InvalidHeaderLength) {
3378   const ExamplePage* test_case = &g_four_byte_commit;
3379   ProtoTranslationTable* table = GetTable(test_case->name);
3380   auto page = PageFromXxd(test_case->data);
3381 
3382   FtraceDataSourceConfig ds_config = EmptyConfig();
3383 
3384   const uint8_t* parse_pos = page.get();
3385   std::optional<CpuReader::PageHeader> page_header =
3386       CpuReader::ParsePageHeader(&parse_pos, table->page_header_size_len());
3387 
3388   const uint8_t* page_end = page.get() + base::GetSysPageSize();
3389   ASSERT_TRUE(page_header.has_value());
3390   EXPECT_FALSE(page_header->lost_events);
3391   EXPECT_LE(parse_pos + page_header->size, page_end);
3392 
3393   FtraceParseStatus status = CpuReader::ParsePagePayload(
3394       parse_pos, &page_header.value(), table, &ds_config,
3395       CreateBundler(ds_config), &metadata_, &last_read_event_ts_);
3396 
3397   EXPECT_EQ(4u, page_header->size);
3398   EXPECT_EQ(status, FtraceParseStatus::FTRACE_STATUS_ABI_SHORT_DATA_LENGTH);
3399 
3400   EXPECT_THAT(AllTracePackets(), IsEmpty());
3401 }
3402 
3403 // Kernel code:
3404 // trace_f2fs_truncate_partial_nodes(... nid = {1,2,3}, depth = 4, err = 0)
3405 //
3406 // After kernel commit 0b04d4c0542e("f2fs: Fix
3407 // f2fs_truncate_partial_nodes ftrace event")
3408 static ExamplePage g_f2fs_truncate_partial_nodes_new{
3409     "b281660544_new",
3410     R"(
3411 00000000: 1555 c3e4 cb07 0000 3c00 0000 0000 0000  .U......<.......
3412 00000010: 3e33 0b87 2700 0000 0c00 0000 7d02 0000  >3..'.......}...
3413 00000020: c638 0000 3900 e00f 0000 0000 b165 0000  .8..9........e..
3414 00000030: 0000 0000 0100 0000 0200 0000 0300 0000  ................
3415 00000040: 0400 0000 0000 0000 0000 0000 0000 0000  ................
3416     )",
3417 };
3418 
TEST_F(CpuReaderParsePagePayloadTest,F2fsTruncatePartialNodesNew)3419 TEST_F(CpuReaderParsePagePayloadTest, F2fsTruncatePartialNodesNew) {
3420   const ExamplePage* test_case = &g_f2fs_truncate_partial_nodes_new;
3421 
3422   ProtoTranslationTable* table = GetTable(test_case->name);
3423   auto page = PageFromXxd(test_case->data);
3424 
3425   FtraceDataSourceConfig ds_config = EmptyConfig();
3426   ds_config.event_filter.AddEnabledEvent(table->EventToFtraceId(
3427       GroupAndName("f2fs", "f2fs_truncate_partial_nodes")));
3428 
3429   const uint8_t* parse_pos = page.get();
3430   std::optional<CpuReader::PageHeader> page_header =
3431       CpuReader::ParsePageHeader(&parse_pos, table->page_header_size_len());
3432 
3433   const uint8_t* page_end = page.get() + base::GetSysPageSize();
3434   ASSERT_TRUE(page_header.has_value());
3435   EXPECT_FALSE(page_header->lost_events);
3436   EXPECT_LE(parse_pos + page_header->size, page_end);
3437 
3438   FtraceParseStatus status = CpuReader::ParsePagePayload(
3439       parse_pos, &page_header.value(), table, &ds_config,
3440       CreateBundler(ds_config), &metadata_, &last_read_event_ts_);
3441 
3442   EXPECT_EQ(status, FtraceParseStatus::FTRACE_STATUS_OK);
3443 
3444   auto bundle = GetBundle();
3445   ASSERT_THAT(bundle.event(), SizeIs(1));
3446   auto& event = bundle.event()[0];
3447   EXPECT_EQ(event.f2fs_truncate_partial_nodes().dev(), 65081u);
3448   EXPECT_EQ(event.f2fs_truncate_partial_nodes().ino(), 26033u);
3449   // This field is disabled in ftrace_proto_gen.cc
3450   EXPECT_FALSE(event.f2fs_truncate_partial_nodes().has_nid());
3451   EXPECT_EQ(event.f2fs_truncate_partial_nodes().depth(), 4);
3452   EXPECT_EQ(event.f2fs_truncate_partial_nodes().err(), 0);
3453 }
3454 
3455 // Kernel code:
3456 // trace_f2fs_truncate_partial_nodes(... nid = {1,2,3}, depth = 4, err = 0)
3457 //
3458 // Before kernel commit 0b04d4c0542e("f2fs: Fix
3459 // f2fs_truncate_partial_nodes ftrace event")
3460 static ExamplePage g_f2fs_truncate_partial_nodes_old{
3461     "b281660544_old",
3462     R"(
3463 00000000: 8f90 aa0d 9e00 0000 3c00 0000 0000 0000  ........<.......
3464 00000010: 3e97 0295 0e01 0000 0c00 0000 7d02 0000  >...........}...
3465 00000020: 8021 0000 3900 e00f 0000 0000 0d66 0000  .!..9........f..
3466 00000030: 0000 0000 0100 0000 0200 0000 0300 0000  ................
3467 00000040: 0400 0000 0000 0000 0000 0000 0000 0000  ................
3468     )",
3469 };
3470 
TEST_F(CpuReaderParsePagePayloadTest,F2fsTruncatePartialNodesOld)3471 TEST_F(CpuReaderParsePagePayloadTest, F2fsTruncatePartialNodesOld) {
3472   const ExamplePage* test_case = &g_f2fs_truncate_partial_nodes_old;
3473 
3474   ProtoTranslationTable* table = GetTable(test_case->name);
3475   auto page = PageFromXxd(test_case->data);
3476 
3477   FtraceDataSourceConfig ds_config = EmptyConfig();
3478   auto id = table->EventToFtraceId(
3479       GroupAndName("f2fs", "f2fs_truncate_partial_nodes"));
3480   PERFETTO_LOG("Enabling: %zu", id);
3481   ds_config.event_filter.AddEnabledEvent(id);
3482 
3483   const uint8_t* parse_pos = page.get();
3484   std::optional<CpuReader::PageHeader> page_header =
3485       CpuReader::ParsePageHeader(&parse_pos, table->page_header_size_len());
3486 
3487   const uint8_t* page_end = page.get() + base::GetSysPageSize();
3488   ASSERT_TRUE(page_header.has_value());
3489   EXPECT_FALSE(page_header->lost_events);
3490   EXPECT_LE(parse_pos + page_header->size, page_end);
3491 
3492   FtraceParseStatus status = CpuReader::ParsePagePayload(
3493       parse_pos, &page_header.value(), table, &ds_config,
3494       CreateBundler(ds_config), &metadata_, &last_read_event_ts_);
3495 
3496   EXPECT_EQ(status, FtraceParseStatus::FTRACE_STATUS_OK);
3497 
3498   auto bundle = GetBundle();
3499   ASSERT_THAT(bundle.event(), SizeIs(1));
3500   auto& event = bundle.event()[0];
3501   EXPECT_EQ(event.f2fs_truncate_partial_nodes().dev(), 65081u);
3502   EXPECT_EQ(event.f2fs_truncate_partial_nodes().ino(), 26125u);
3503   // This field is disabled in ftrace_proto_gen.cc
3504   EXPECT_FALSE(event.f2fs_truncate_partial_nodes().has_nid());
3505   // Due to a kernel bug, nid[1] is parsed as depth.
3506   EXPECT_EQ(event.f2fs_truncate_partial_nodes().depth(), 2);
3507   // Due to a kernel bug, nid[2] is parsed as err.
3508   EXPECT_EQ(event.f2fs_truncate_partial_nodes().err(), 3);
3509 }
3510 
3511 // one print
3512 char g_last_ts_test_page_0[] = R"(
3513     00000000: cd79 fb3a 2fa4 0400 2c00 0000 0000 0000  .y.:/...,.......
3514     00000010: 7eb6 e5eb 8f11 0000 0800 0000 0500 0000  ~...............
3515     00000020: 1e83 1400 42ab e0af ffff ffff 6669 7273  ....B.......firs
3516     00000030: 745f 7072 696e 740a 0000 0000 0000 0000  t_print.........
3517   )";
3518 
3519 // one print
3520 char g_last_ts_test_page_1[] = R"(
3521     00000000: 3c11 d579 99a5 0400 2c00 0000 0000 0000  <..y....,.......
3522     00000010: 3ed1 6315 3701 0000 0800 0000 0500 0000  >.c.7...........
3523     00000020: 9e8c 1400 42ab e0af ffff ffff 7365 636f  ....B.......seco
3524     00000030: 6e64 5f70 7269 6e74 0a00 0000 0000 0000  nd_print........
3525   )";
3526 
3527 // data loss marker ("since last read") + multiple sched_switch + one print
3528 char g_last_ts_test_page_2[] = R"(
3529     00000000: 8ac6 cb70 a8a5 0400 4c02 0080 ffff ffff  ...p....L.......
3530     00000010: 1000 0000 4701 0102 01b1 0f00 636f 6465  ....G.......code
3531     00000020: 0000 0000 0000 0000 0000 0000 01b1 0f00  ................
3532     00000030: 7800 0000 0100 0000 0000 0000 7377 6170  x...........swap
3533     00000040: 7065 722f 3000 0000 0000 0000 0000 0000  per/0...........
3534     00000050: 7800 0000 b0e3 f602 4701 0102 0000 0000  x.......G.......
3535     00000060: 7377 6170 7065 722f 3000 0000 0000 0000  swapper/0.......
3536     00000070: 0000 0000 7800 0000 0000 0000 0000 0000  ....x...........
3537     00000080: 6b77 6f72 6b65 722f 303a 3500 0000 0000  kworker/0:5.....
3538     00000090: ac85 1400 7800 0000 1002 0300 4701 0102  ....x.......G...
3539     000000a0: ac85 1400 6b77 6f72 6b65 722f 303a 3500  ....kworker/0:5.
3540     000000b0: 0000 0000 ac85 1400 7800 0000 8000 0000  ........x.......
3541     000000c0: 0000 0000 7377 6170 7065 722f 3000 0000  ....swapper/0...
3542     000000d0: 0000 0000 0000 0000 7800 0000 f086 7106  ........x.....q.
3543     000000e0: 4701 0102 0000 0000 7377 6170 7065 722f  G.......swapper/
3544     000000f0: 3000 0000 0000 0000 0000 0000 7800 0000  0...........x...
3545     00000100: 0000 0000 0000 0000 6f62 6e6f 2d64 6573  ........obno-des
3546     00000110: 6b74 6f70 2d6e 6f00 d513 0000 7800 0000  ktop-no.....x...
3547     00000120: 3013 1000 4701 0102 d513 0000 6f62 6e6f  0...G.......obno
3548     00000130: 2d64 6573 6b74 6f70 2d6e 6f00 d513 0000  -desktop-no.....
3549     00000140: 7800 0000 0100 0000 0000 0000 7377 6170  x...........swap
3550     00000150: 7065 722f 3000 0000 0000 0000 0000 0000  per/0...........
3551     00000160: 7800 0000 10b0 2703 4701 0102 0000 0000  x.....'.G.......
3552     00000170: 7377 6170 7065 722f 3000 0000 0000 0000  swapper/0.......
3553     00000180: 0000 0000 7800 0000 0000 0000 0000 0000  ....x...........
3554     00000190: 6b77 6f72 6b65 722f 303a 3500 0000 0000  kworker/0:5.....
3555     000001a0: ac85 1400 7800 0000 70e7 0200 4701 0102  ....x...p...G...
3556     000001b0: ac85 1400 6b77 6f72 6b65 722f 303a 3500  ....kworker/0:5.
3557     000001c0: 0000 0000 ac85 1400 7800 0000 8000 0000  ........x.......
3558     000001d0: 0000 0000 6b73 6f66 7469 7271 642f 3000  ....ksoftirqd/0.
3559     000001e0: 0000 0000 0f00 0000 7800 0000 10a4 0200  ........x.......
3560     000001f0: 4701 0102 0f00 0000 6b73 6f66 7469 7271  G.......ksoftirq
3561     00000200: 642f 3000 0000 0000 0f00 0000 7800 0000  d/0.........x...
3562     00000210: 0100 0000 0000 0000 7377 6170 7065 722f  ........swapper/
3563     00000220: 3000 0000 0000 0000 0000 0000 7800 0000  0...........x...
3564     00000230: fef2 0a4d 7500 0000 0800 0000 0500 0000  ...Mu...........
3565     00000240: 1a8d 1400 42ab e0af ffff ffff 7468 6972  ....B.......thir
3566     00000250: 645f 7072 696e 740a 0000 0000 0000 0000  d_print.........
3567   )";
3568 
3569 // Tests that |last_read_event_timestamp| is correctly updated in cases where a
3570 // single ProcessPagesForDataSource call produces multiple ftrace bundle packets
3571 // (due to splitting on data loss markers).
TEST(CpuReaderTest,LastReadEventTimestampWithSplitBundles)3572 TEST(CpuReaderTest, LastReadEventTimestampWithSplitBundles) {
3573   // build test buffer with 3 pages
3574   ProtoTranslationTable* table = GetTable("synthetic");
3575   std::vector<std::unique_ptr<uint8_t[]>> test_pages;
3576   test_pages.emplace_back(PageFromXxd(g_last_ts_test_page_0));
3577   test_pages.emplace_back(PageFromXxd(g_last_ts_test_page_1));
3578   test_pages.emplace_back(PageFromXxd(g_last_ts_test_page_2));
3579   size_t num_pages = test_pages.size();
3580   size_t page_sz = base::GetSysPageSize();
3581   auto buf = std::make_unique<uint8_t[]>(page_sz * num_pages);
3582   for (size_t i = 0; i < num_pages; i++) {
3583     void* dest = buf.get() + (i * page_sz);
3584     memcpy(dest, static_cast<const void*>(test_pages[i].get()), page_sz);
3585   }
3586 
3587   // build cfg requesting ftrace/print
3588   auto compact_sched_buf = std::make_unique<CompactSchedBuffer>();
3589   FtraceMetadata metadata{};
3590   FtraceDataSourceConfig ftrace_cfg = EmptyConfig();
3591   ftrace_cfg.event_filter.AddEnabledEvent(
3592       table->EventToFtraceId(GroupAndName("ftrace", "print")));
3593 
3594   // invoke ProcessPagesForDataSource
3595   TraceWriterForTesting trace_writer;
3596   base::FlatSet<protos::pbzero::FtraceParseStatus> parse_errors;
3597   uint64_t last_read_event_ts = 0;
3598   bool success = CpuReader::ProcessPagesForDataSource(
3599       &trace_writer, &metadata, /*cpu=*/0, &ftrace_cfg, &parse_errors,
3600       &last_read_event_ts, buf.get(), num_pages, compact_sched_buf.get(), table,
3601       /*symbolizer=*/nullptr,
3602       /*ftrace_clock_snapshot=*/nullptr,
3603       protos::pbzero::FTRACE_CLOCK_UNSPECIFIED);
3604 
3605   EXPECT_TRUE(success);
3606 
3607   // We've read three pages, one print event on each. There is a data loss
3608   // marker on the third page, indicating that the kernel overwrote events
3609   // between 2nd and 3rd page (imagine our daemon getting cpu starved between
3610   // those reads).
3611   //
3612   // Therefore we expect two bundles, as we start a new one whenever we
3613   // encounter data loss (to set the |lost_events| field in the bundle proto).
3614   //
3615   // In terms of |last_read_event_timestamp|, the first bundle will emit zero
3616   // since that's our initial input. The second bundle needs to emit the
3617   // timestamp of the last event in the first bundle.
3618   auto packets = trace_writer.GetAllTracePackets();
3619   ASSERT_EQ(2u, packets.size());
3620 
3621   // 2 prints
3622   auto const& first_bundle = packets[0].ftrace_events();
3623   EXPECT_FALSE(first_bundle.lost_events());
3624   ASSERT_EQ(2u, first_bundle.event().size());
3625   EXPECT_TRUE(first_bundle.has_last_read_event_timestamp());
3626   EXPECT_EQ(0u, first_bundle.last_read_event_timestamp());
3627 
3628   const uint64_t kSecondPrintTs = 1308020252356549ULL;
3629   EXPECT_EQ(kSecondPrintTs, first_bundle.event()[1].timestamp());
3630   EXPECT_EQ(0u, first_bundle.last_read_event_timestamp());
3631 
3632   // 1 print + lost_events + updated last_read_event_timestamp
3633   auto const& second_bundle = packets[1].ftrace_events();
3634   EXPECT_TRUE(second_bundle.lost_events());
3635   EXPECT_EQ(1u, second_bundle.event().size());
3636   EXPECT_EQ(kSecondPrintTs, second_bundle.last_read_event_timestamp());
3637 }
3638 
3639 }  // namespace
3640 }  // namespace perfetto
3641