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 
22 #include "perfetto/base/build_config.h"
23 #include "perfetto/ext/base/utils.h"
24 #include "perfetto/protozero/proto_utils.h"
25 #include "perfetto/protozero/scattered_heap_buffer.h"
26 #include "perfetto/protozero/scattered_stream_writer.h"
27 #include "src/traced/probes/ftrace/event_info.h"
28 #include "src/traced/probes/ftrace/ftrace_config_muxer.h"
29 #include "src/traced/probes/ftrace/ftrace_procfs.h"
30 #include "src/traced/probes/ftrace/proto_translation_table.h"
31 #include "src/traced/probes/ftrace/test/cpu_reader_support.h"
32 #include "src/tracing/core/trace_writer_for_testing.h"
33 #include "test/gtest_and_gmock.h"
34 
35 #include "protos/perfetto/trace/ftrace/ftrace.gen.h"
36 #include "protos/perfetto/trace/ftrace/ftrace_event.gen.h"
37 #include "protos/perfetto/trace/ftrace/ftrace_event.pbzero.h"
38 #include "protos/perfetto/trace/ftrace/ftrace_event_bundle.gen.h"
39 #include "protos/perfetto/trace/ftrace/ftrace_event_bundle.pbzero.h"
40 #include "protos/perfetto/trace/ftrace/power.gen.h"
41 #include "protos/perfetto/trace/ftrace/sched.gen.h"
42 #include "protos/perfetto/trace/trace_packet.gen.h"
43 #include "src/traced/probes/ftrace/test/test_messages.gen.h"
44 #include "src/traced/probes/ftrace/test/test_messages.pbzero.h"
45 
46 using protozero::proto_utils::ProtoSchemaType;
47 using testing::_;
48 using testing::AnyNumber;
49 using testing::Contains;
50 using testing::Each;
51 using testing::ElementsAre;
52 using testing::ElementsAreArray;
53 using testing::EndsWith;
54 using testing::Eq;
55 using testing::NiceMock;
56 using testing::Pair;
57 using testing::Return;
58 using testing::StartsWith;
59 
60 namespace perfetto {
61 
62 namespace {
63 
EmptyConfig()64 FtraceDataSourceConfig EmptyConfig() {
65   return FtraceDataSourceConfig{EventFilter{},
66                                 DisabledCompactSchedConfigForTesting(),
67                                 {},
68                                 {},
69                                 false /*symbolize_ksyms*/};
70 }
71 
72 constexpr uint64_t kNanoInSecond = 1000 * 1000 * 1000;
73 constexpr uint64_t kNanoInMicro = 1000;
74 
WithinOneMicrosecond(uint64_t actual_ns,uint64_t expected_s,uint64_t expected_us)75 ::testing::AssertionResult WithinOneMicrosecond(uint64_t actual_ns,
76                                                 uint64_t expected_s,
77                                                 uint64_t expected_us) {
78   // Round to closest us.
79   uint64_t actual_us = (actual_ns + kNanoInMicro / 2) / kNanoInMicro;
80   uint64_t total_expected_us = expected_s * 1000 * 1000 + expected_us;
81   if (actual_us == total_expected_us)
82     return ::testing::AssertionSuccess();
83 
84   return ::testing::AssertionFailure()
85          << actual_ns / kNanoInSecond << "."
86          << (actual_ns % kNanoInSecond) / kNanoInMicro << " vs. " << expected_s
87          << "." << expected_us;
88 }
89 
90 class MockFtraceProcfs : public FtraceProcfs {
91  public:
MockFtraceProcfs()92   MockFtraceProcfs() : FtraceProcfs("/root/") {
93     ON_CALL(*this, NumberOfCpus()).WillByDefault(Return(1));
94     ON_CALL(*this, WriteToFile(_, _)).WillByDefault(Return(true));
95     ON_CALL(*this, ClearFile(_)).WillByDefault(Return(true));
96     EXPECT_CALL(*this, NumberOfCpus()).Times(AnyNumber());
97   }
98 
99   MOCK_METHOD2(WriteToFile,
100                bool(const std::string& path, const std::string& str));
101   MOCK_METHOD1(ReadOneCharFromFile, char(const std::string& path));
102   MOCK_METHOD1(ClearFile, bool(const std::string& path));
103   MOCK_CONST_METHOD1(ReadFileIntoString, std::string(const std::string& path));
104   MOCK_CONST_METHOD0(NumberOfCpus, size_t());
105 };
106 
107 class CpuReaderTableTest : public ::testing::Test {
108  protected:
109   NiceMock<MockFtraceProcfs> ftrace_;
110 };
111 
112 // Single class to manage the whole protozero -> scattered stream -> chunks ->
113 // single buffer -> real proto dance. Has a method: writer() to get an
114 // protozero ftrace bundle writer and a method ParseProto() to attempt to
115 // parse whatever has been written so far into a proto message.
116 template <class ZeroT, class ProtoT>
117 class ProtoProvider {
118  public:
ProtoProvider(size_t chunk_size)119   explicit ProtoProvider(size_t chunk_size) : chunk_size_(chunk_size) {}
120   ~ProtoProvider() = default;
121 
writer()122   ZeroT* writer() { return writer_.get(); }
ResetWriter()123   void ResetWriter() { writer_.Reset(); }
124 
125   // Stitch together the scattered chunks into a single buffer then attempt
126   // to parse the buffer as a FtraceEventBundle. Returns the FtraceEventBundle
127   // on success and nullptr on failure.
ParseProto()128   std::unique_ptr<ProtoT> ParseProto() {
129     auto bundle = std::unique_ptr<ProtoT>(new ProtoT());
130     std::vector<uint8_t> buffer = writer_.SerializeAsArray();
131     if (!bundle->ParseFromArray(buffer.data(), buffer.size()))
132       return nullptr;
133     return bundle;
134   }
135 
136  private:
137   ProtoProvider(const ProtoProvider&) = delete;
138   ProtoProvider& operator=(const ProtoProvider&) = delete;
139 
140   size_t chunk_size_;
141   protozero::HeapBuffered<ZeroT> writer_;
142 };
143 
144 using BundleProvider = ProtoProvider<protos::pbzero::FtraceEventBundle,
145                                      protos::gen::FtraceEventBundle>;
146 
147 class BinaryWriter {
148  public:
BinaryWriter()149   BinaryWriter()
150       : size_(base::kPageSize), page_(new uint8_t[size_]), ptr_(page_.get()) {}
151 
152   template <typename T>
Write(T t)153   void Write(T t) {
154     memcpy(ptr_, &t, sizeof(T));
155     ptr_ += sizeof(T);
156     PERFETTO_CHECK(ptr_ < ptr_ + size_);
157   }
158 
WriteFixedString(size_t n,const char * s)159   void WriteFixedString(size_t n, const char* s) {
160     size_t length = strlen(s);
161     PERFETTO_CHECK(length < n);
162     char c;
163     while ((c = *s++)) {
164       Write<char>(c);
165     }
166     Write<char>('\0');
167     for (size_t i = 0; i < n - length - 1; i++) {
168       Write<char>('\xff');
169     }
170   }
171 
GetCopy()172   std::unique_ptr<uint8_t[]> GetCopy() {
173     std::unique_ptr<uint8_t[]> buffer(new uint8_t[written()]);
174     memcpy(buffer.get(), page_.get(), written());
175     return buffer;
176   }
177 
written()178   size_t written() { return static_cast<size_t>(ptr_ - page_.get()); }
179 
180  private:
181   size_t size_;
182   std::unique_ptr<uint8_t[]> page_;
183   uint8_t* ptr_;
184 };
185 
186 }  // namespace
187 
TEST(PageFromXxdTest,OneLine)188 TEST(PageFromXxdTest, OneLine) {
189   std::string text = R"(
190     00000000: 0000 0000 0000 0000 0000 0000 0000 0000  ................
191     00000000: 0000 0000 5600 0000 0000 0000 0000 0000  ................
192   )";
193   auto page = PageFromXxd(text);
194   EXPECT_EQ(page.get()[0x14], 0x56);
195 }
196 
TEST(PageFromXxdTest,ManyLines)197 TEST(PageFromXxdTest, ManyLines) {
198   std::string text = R"(
199     00000000: 1234 0000 0000 0000 0000 0000 0000 0056  ................
200     00000010: 7800 0000 0000 0000 0000 0000 0000 009a  ................
201     00000020: 0000 0000 bc00 0000 00de 0000 0000 009a  ................
202   )";
203   auto page = PageFromXxd(text);
204   EXPECT_EQ(page.get()[0x00], 0x12);
205   EXPECT_EQ(page.get()[0x01], 0x34);
206   EXPECT_EQ(page.get()[0x0f], 0x56);
207   EXPECT_EQ(page.get()[0x10], 0x78);
208   EXPECT_EQ(page.get()[0x1f], 0x9a);
209   EXPECT_EQ(page.get()[0x24], 0xbc);
210   EXPECT_EQ(page.get()[0x29], 0xde);
211 }
212 
TEST(CpuReaderTest,BinaryWriter)213 TEST(CpuReaderTest, BinaryWriter) {
214   BinaryWriter writer;
215   writer.Write<uint64_t>(1);
216   writer.Write<uint32_t>(2);
217   writer.Write<uint16_t>(3);
218   writer.Write<uint8_t>(4);
219   auto buffer = writer.GetCopy();
220   EXPECT_EQ(buffer.get()[0], 1);
221   EXPECT_EQ(buffer.get()[1], 0);
222   EXPECT_EQ(buffer.get()[2], 0);
223   EXPECT_EQ(buffer.get()[3], 0);
224   EXPECT_EQ(buffer.get()[4], 0);
225   EXPECT_EQ(buffer.get()[5], 0);
226   EXPECT_EQ(buffer.get()[6], 0);
227   EXPECT_EQ(buffer.get()[7], 0);
228   EXPECT_EQ(buffer.get()[8], 2);
229 }
230 
TEST(ReadAndAdvanceTest,Number)231 TEST(ReadAndAdvanceTest, Number) {
232   uint64_t expected = 42;
233   uint64_t actual = 0;
234   uint8_t buffer[8] = {};
235   const uint8_t* start = buffer;
236   const uint8_t* ptr = buffer;
237   memcpy(&buffer, &expected, 8);
238   EXPECT_TRUE(CpuReader::ReadAndAdvance<uint64_t>(&ptr, ptr + 8, &actual));
239   EXPECT_EQ(ptr, start + 8);
240   EXPECT_EQ(actual, expected);
241 }
242 
TEST(ReadAndAdvanceTest,PlainStruct)243 TEST(ReadAndAdvanceTest, PlainStruct) {
244   struct PlainStruct {
245     uint64_t timestamp;
246     uint64_t length;
247   };
248 
249   uint64_t expected[2] = {42, 999};
250   PlainStruct actual;
251   uint8_t buffer[16] = {};
252   const uint8_t* start = buffer;
253   const uint8_t* ptr = buffer;
254   memcpy(&buffer, &expected, 16);
255   EXPECT_TRUE(CpuReader::ReadAndAdvance<PlainStruct>(&ptr, ptr + 16, &actual));
256   EXPECT_EQ(ptr, start + 16);
257   EXPECT_EQ(actual.timestamp, 42ul);
258   EXPECT_EQ(actual.length, 999ul);
259 }
260 
TEST(ReadAndAdvanceTest,ComplexStruct)261 TEST(ReadAndAdvanceTest, ComplexStruct) {
262   struct ComplexStruct {
263     uint64_t timestamp;
264     uint32_t length;
265     uint32_t : 24;
266     uint32_t overwrite : 8;
267   };
268 
269   uint64_t expected[2] = {42, 0xcdffffffabababab};
270   ComplexStruct actual = {};
271   uint8_t buffer[16] = {};
272   const uint8_t* start = buffer;
273   const uint8_t* ptr = buffer;
274   memcpy(&buffer, &expected, 16);
275   EXPECT_TRUE(
276       CpuReader::ReadAndAdvance<ComplexStruct>(&ptr, ptr + 16, &actual));
277   EXPECT_EQ(ptr, start + 16);
278   EXPECT_EQ(actual.timestamp, 42ul);
279   EXPECT_EQ(actual.length, 0xabababab);
280   EXPECT_EQ(actual.overwrite, 0xCDu);
281 }
282 
TEST(ReadAndAdvanceTest,Overruns)283 TEST(ReadAndAdvanceTest, Overruns) {
284   uint64_t result = 42;
285   uint8_t buffer[7] = {};
286   const uint8_t* start = buffer;
287   const uint8_t* ptr = buffer;
288   EXPECT_FALSE(CpuReader::ReadAndAdvance<uint64_t>(&ptr, ptr + 7, &result));
289   EXPECT_EQ(ptr, start);
290   EXPECT_EQ(result, 42ul);
291 }
292 
TEST(ReadAndAdvanceTest,AtEnd)293 TEST(ReadAndAdvanceTest, AtEnd) {
294   uint8_t result = 42;
295   uint8_t buffer[8] = {};
296   const uint8_t* start = buffer;
297   const uint8_t* ptr = buffer;
298   EXPECT_FALSE(CpuReader::ReadAndAdvance<uint8_t>(&ptr, ptr, &result));
299   EXPECT_EQ(ptr, start);
300   EXPECT_EQ(result, 42);
301 }
302 
TEST(ReadAndAdvanceTest,Underruns)303 TEST(ReadAndAdvanceTest, Underruns) {
304   uint64_t expected = 42;
305   uint64_t actual = 0;
306   uint8_t buffer[9] = {};
307   const uint8_t* start = buffer;
308   const uint8_t* ptr = buffer;
309   memcpy(&buffer, &expected, 8);
310   EXPECT_TRUE(CpuReader::ReadAndAdvance<uint64_t>(&ptr, ptr + 8, &actual));
311   EXPECT_EQ(ptr, start + 8);
312   EXPECT_EQ(actual, expected);
313 }
314 
TEST(ParsePageHeaderTest,WithOverrun)315 TEST(ParsePageHeaderTest, WithOverrun) {
316   std::string text = R"(
317     00000000: 3ef3 db77 67a2 0100 f00f 0080 ffff ffff
318     )";
319   auto page = PageFromXxd(text);
320 
321   // parse as if we're on a 32 bit kernel (4 byte "commit" field)
322   {
323     const uint8_t* ptr = page.get();
324     auto ret = CpuReader::ParsePageHeader(&ptr, 4u);
325     ASSERT_TRUE(ret.has_value());
326     CpuReader::PageHeader parsed = ret.value();
327 
328     ASSERT_EQ(parsed.timestamp, 0x0001A26777DBF33Eull);  // first 8 bytes
329     ASSERT_EQ(parsed.size, 0x0ff0u);                     // 4080
330     ASSERT_TRUE(parsed.lost_events);
331 
332     // pointer advanced past the header (8+4 bytes)
333     ASSERT_EQ(ptr, page.get() + 12);
334   }
335 
336   // parse as if we're on a 64 bit kernel (8 byte "commit" field)
337   {
338     const uint8_t* ptr = page.get();
339     auto ret = CpuReader::ParsePageHeader(&ptr, 8u);
340     ASSERT_TRUE(ret.has_value());
341     CpuReader::PageHeader parsed = ret.value();
342 
343     ASSERT_EQ(parsed.timestamp, 0x0001A26777DBF33Eull);  // first 8 bytes
344     ASSERT_EQ(parsed.size, 0x0ff0u);                     // 4080
345     ASSERT_TRUE(parsed.lost_events);
346 
347     // pointer advanced past the header (8+8 bytes)
348     ASSERT_EQ(ptr, page.get() + 16);
349   }
350 }
351 
352 // clang-format off
353 // # tracer: nop
354 // #
355 // # entries-in-buffer/entries-written: 1/1   #P:8
356 // #
357 // #                              _-----=> irqs-off
358 // #                             / _----=> need-resched
359 // #                            | / _---=> hardirq/softirq
360 // #                            || / _--=> preempt-depth
361 // #                            ||| /     delay
362 // #           TASK-PID   CPU#  ||||    TIMESTAMP  FUNCTION
363 // #              | |       |   ||||       |         |
364 //               sh-28712 [000] ...1 608934.535199: tracing_mark_write: Hello, world!
365 // clang-format on
366 
367 static ExamplePage g_single_print{
368     "synthetic",
369     R"(
370     00000000: ba12 6a33 c628 0200 2c00 0000 0000 0000  ..j3.(..,.......
371     00000010: def0 ec67 8d21 0000 0800 0000 0500 0001  ...g.!..........
372     00000020: 2870 0000 ac5d 1661 86ff ffff 4865 6c6c  (p...].a....Hell
373     00000030: 6f2c 2077 6f72 6c64 210a 00ff 0000 0000  o, world!.......
374   )",
375 };
376 
TEST(CpuReaderTest,ParseSinglePrint)377 TEST(CpuReaderTest, ParseSinglePrint) {
378   const ExamplePage* test_case = &g_single_print;
379 
380   BundleProvider bundle_provider(base::kPageSize);
381   ProtoTranslationTable* table = GetTable(test_case->name);
382   auto page = PageFromXxd(test_case->data);
383 
384   FtraceDataSourceConfig ds_config = EmptyConfig();
385   ds_config.event_filter.AddEnabledEvent(
386       table->EventToFtraceId(GroupAndName("ftrace", "print")));
387 
388   FtraceMetadata metadata{};
389   CompactSchedBuffer compact_buffer;
390   const uint8_t* parse_pos = page.get();
391   base::Optional<CpuReader::PageHeader> page_header =
392       CpuReader::ParsePageHeader(&parse_pos, table->page_header_size_len());
393 
394   const uint8_t* page_end = page.get() + base::kPageSize;
395   ASSERT_TRUE(page_header.has_value());
396   EXPECT_EQ(44ul, page_header->size);
397   EXPECT_FALSE(page_header->lost_events);
398   EXPECT_TRUE(parse_pos < page_end);
399   EXPECT_TRUE(parse_pos + page_header->size < page_end);
400 
401   size_t evt_bytes = CpuReader::ParsePagePayload(
402       parse_pos, &page_header.value(), table, &ds_config, &compact_buffer,
403       bundle_provider.writer(), &metadata);
404 
405   EXPECT_EQ(evt_bytes, 44ul);
406 
407   auto bundle = bundle_provider.ParseProto();
408   ASSERT_TRUE(bundle);
409   ASSERT_EQ(bundle->event().size(), 1u);
410   const protos::gen::FtraceEvent& event = bundle->event()[0];
411   EXPECT_EQ(event.pid(), 28712ul);
412   EXPECT_TRUE(WithinOneMicrosecond(event.timestamp(), 608934, 535199));
413   EXPECT_EQ(event.print().buf(), "Hello, world!\n");
414 }
415 
416 // clang-format off
417 // # tracer: nop
418 // #
419 // # entries-in-buffer/entries-written: 2/2   #P:8
420 // #
421 // #                                      _-----=> irqs-off
422 // #                                     / _----=> need-resched
423 // #                                    | / _---=> hardirq/softirq
424 // #                                    || / _--=> preempt-depth
425 // #                                    ||| /     delay
426 // #           TASK-PID    TGID   CPU#  ||||    TIMESTAMP  FUNCTION
427 // #              | |        |      |   ||||       |         |
428 //             echo-6908  ( 6908) [000] ...1 282762.884473: tracing_mark_write: qwertyuiopqwrtyuiopqwertyuiopqwertyuiopqwer[...]
429 //             echo-6908  ( 6908) [000] ...1 282762.884492: tracing_mark_write:
430 // clang-format on
431 
432 static ExamplePage g_really_long_event{
433     "synthetic",
434     R"(
435       00000000: 6be0 48dd 2b01 0100 e403 0000 0000 0000  k.H.+...........
436       00000010: 1e00 0000 0000 0000 0000 0000 c003 0000  ................
437       00000020: 0500 0001 fc1a 0000 4096 3615 9cff ffff  ........@.6.....
438       00000030: 7177 6572 7479 7569 6f70 7177 7274 7975  qwertyuiopqwrtyu
439       00000040: 696f 7071 7765 7274 7975 696f 7071 7765  iopqwertyuiopqwe
440       00000050: 7274 7975 696f 7071 7765 7274 7975 696f  rtyuiopqwertyuio
441       00000060: 7071 7772 7479 7569 6f70 7177 6572 7479  pqwrtyuiopqwerty
442       00000070: 7569 6f70 7177 6572 7479 7569 6f71 7765  uiopqwertyuioqwe
443       00000080: 7274 7975 696f 7071 7772 7479 7569 6f70  rtyuiopqwrtyuiop
444       00000090: 7177 6572 7479 7569 6f70 7177 6572 7479  qwertyuiopqwerty
445       000000a0: 7569 6f71 7765 7274 7975 696f 7071 7772  uioqwertyuiopqwr
446       000000b0: 7479 7569 6f70 7177 6572 7479 7569 6f70  tyuiopqwertyuiop
447       000000c0: 7177 6572 7479 7569 6f70 7070 7177 6572  qwertyuiopppqwer
448       000000d0: 7479 7569 6f70 7177 7274 7975 696f 7071  tyuiopqwrtyuiopq
449       000000e0: 7765 7274 7975 696f 7071 7765 7274 7975  wertyuiopqwertyu
450       000000f0: 696f 7071 7765 7274 7975 696f 7071 7772  iopqwertyuiopqwr
451       00000100: 7479 7569 6f70 7177 6572 7479 7569 6f70  tyuiopqwertyuiop
452       00000110: 7177 6572 7479 7569 6f71 7765 7274 7975  qwertyuioqwertyu
453       00000120: 696f 7071 7772 7479 7569 6f70 7177 6572  iopqwrtyuiopqwer
454       00000130: 7479 7569 6f70 7177 6572 7479 7569 6f71  tyuiopqwertyuioq
455       00000140: 7765 7274 7975 696f 7071 7772 7479 7569  wertyuiopqwrtyui
456       00000150: 6f70 7177 6572 7479 7569 6f70 7177 6572  opqwertyuiopqwer
457       00000160: 7479 7569 6f70 7070 7177 6572 7479 7569  tyuiopppqwertyui
458       00000170: 6f70 7177 7274 7975 696f 7071 7765 7274  opqwrtyuiopqwert
459       00000180: 7975 696f 7071 7765 7274 7975 696f 7071  yuiopqwertyuiopq
460       00000190: 7765 7274 7975 696f 7071 7772 7479 7569  wertyuiopqwrtyui
461       000001a0: 6f70 7177 6572 7479 7569 6f70 7177 6572  opqwertyuiopqwer
462       000001b0: 7479 7569 6f71 7765 7274 7975 696f 7071  tyuioqwertyuiopq
463       000001c0: 7772 7479 7569 6f70 7177 6572 7479 7569  wrtyuiopqwertyui
464       000001d0: 6f70 7177 6572 7479 7569 6f71 7765 7274  opqwertyuioqwert
465       000001e0: 7975 696f 7071 7772 7479 7569 6f70 7177  yuiopqwrtyuiopqw
466       000001f0: 6572 7479 7569 6f70 7177 6572 7479 7569  ertyuiopqwertyui
467       00000200: 6f70 7070 7177 6572 7479 7569 6f70 7177  opppqwertyuiopqw
468       00000210: 7274 7975 696f 7071 7765 7274 7975 696f  rtyuiopqwertyuio
469       00000220: 7071 7765 7274 7975 696f 7071 7765 7274  pqwertyuiopqwert
470       00000230: 7975 696f 7071 7772 7479 7569 6f70 7177  yuiopqwrtyuiopqw
471       00000240: 6572 7479 7569 6f70 7177 6572 7479 7569  ertyuiopqwertyui
472       00000250: 6f71 7765 7274 7975 696f 7071 7772 7479  oqwertyuiopqwrty
473       00000260: 7569 6f70 7177 6572 7479 7569 6f70 7177  uiopqwertyuiopqw
474       00000270: 6572 7479 7569 6f71 7765 7274 7975 696f  ertyuioqwertyuio
475       00000280: 7071 7772 7479 7569 6f70 7177 6572 7479  pqwrtyuiopqwerty
476       00000290: 7569 6f70 7177 6572 7479 7569 6f70 7070  uiopqwertyuioppp
477       000002a0: 7177 6572 7479 7569 6f70 7177 7274 7975  qwertyuiopqwrtyu
478       000002b0: 696f 7071 7765 7274 7975 696f 7071 7765  iopqwertyuiopqwe
479       000002c0: 7274 7975 696f 7071 7765 7274 7975 696f  rtyuiopqwertyuio
480       000002d0: 7071 7772 7479 7569 6f70 7177 6572 7479  pqwrtyuiopqwerty
481       000002e0: 7569 6f70 7177 6572 7479 7569 6f71 7765  uiopqwertyuioqwe
482       000002f0: 7274 7975 696f 7071 7772 7479 7569 6f70  rtyuiopqwrtyuiop
483       00000300: 7177 6572 7479 7569 6f70 7177 6572 7479  qwertyuiopqwerty
484       00000310: 7569 6f71 7765 7274 7975 696f 7071 7772  uioqwertyuiopqwr
485       00000320: 7479 7569 6f70 7177 6572 7479 7569 6f70  tyuiopqwertyuiop
486       00000330: 7177 6572 7479 7569 6f70 7070 7177 6572  qwertyuiopppqwer
487       00000340: 7479 7569 6f70 7177 7274 7975 696f 7071  tyuiopqwrtyuiopq
488       00000350: 7765 7274 7975 696f 7071 7765 7274 7975  wertyuiopqwertyu
489       00000360: 696f 7071 7765 7274 7975 696f 7071 7772  iopqwertyuiopqwr
490       00000370: 7479 7569 6f70 7177 6572 7479 7569 6f70  tyuiopqwertyuiop
491       00000380: 7177 6572 7479 7569 6f71 7765 7274 7975  qwertyuioqwertyu
492       00000390: 696f 7071 7772 7479 7569 6f70 7177 6572  iopqwrtyuiopqwer
493       000003a0: 7479 7569 6f70 7177 6572 7479 7569 6f71  tyuiopqwertyuioq
494       000003b0: 7765 7274 7975 696f 7071 7772 7479 7569  wertyuiopqwrtyui
495       000003c0: 6f70 7177 6572 7479 7569 6f70 7177 6572  opqwertyuiopqwer
496       000003d0: 7479 7569 6f70 7070 0a00 5115 6562 0900  tyuioppp..Q.eb..
497       000003e0: 0500 0001 fc1a 0000 4096 3615 9cff ffff  ........@.6.....
498       000003f0: 0a00 0000 0000 0000 0000 0000 0000 0000  ................
499       00000400: 0000 0000 0000 0000 0000 0000 0000 0000  ................
500       00000410: 0000 0000 0000 0000 0000 0000 0000 0000  ................
501       00000420: 0000 0000 0000 0000 0000 0000 0000 0000  ................
502   )",
503 };
504 
TEST(CpuReaderTest,ReallyLongEvent)505 TEST(CpuReaderTest, ReallyLongEvent) {
506   const ExamplePage* test_case = &g_really_long_event;
507 
508   BundleProvider bundle_provider(base::kPageSize);
509   ProtoTranslationTable* table = GetTable(test_case->name);
510   auto page = PageFromXxd(test_case->data);
511 
512   FtraceDataSourceConfig ds_config = EmptyConfig();
513   ds_config.event_filter.AddEnabledEvent(
514       table->EventToFtraceId(GroupAndName("ftrace", "print")));
515 
516   FtraceMetadata metadata{};
517   CompactSchedBuffer compact_buffer;
518   const uint8_t* parse_pos = page.get();
519   base::Optional<CpuReader::PageHeader> page_header =
520       CpuReader::ParsePageHeader(&parse_pos, table->page_header_size_len());
521 
522   const uint8_t* page_end = page.get() + base::kPageSize;
523   ASSERT_TRUE(page_header.has_value());
524   EXPECT_FALSE(page_header->lost_events);
525   EXPECT_TRUE(parse_pos < page_end);
526   EXPECT_TRUE(parse_pos + page_header->size < page_end);
527 
528   CpuReader::ParsePagePayload(parse_pos, &page_header.value(), table,
529                               &ds_config, &compact_buffer,
530                               bundle_provider.writer(), &metadata);
531 
532   auto bundle = bundle_provider.ParseProto();
533   ASSERT_TRUE(bundle);
534   const protos::gen::FtraceEvent& long_print = bundle->event()[0];
535   EXPECT_THAT(long_print.print().buf(), StartsWith("qwerty"));
536   EXPECT_THAT(long_print.print().buf(), EndsWith("ppp\n"));
537   const protos::gen::FtraceEvent& newline = bundle->event()[1];
538   EXPECT_EQ(newline.print().buf(), "\n");
539 }
540 
541 // This event is as the event for ParseSinglePrint above except the string
542 // is extended to overflow the page size written in the header.
543 static ExamplePage g_single_print_malformed{
544     "synthetic",
545     R"(
546     00000000: ba12 6a33 c628 0200 2c00 0000 0000 0000  ................
547     00000010: def0 ec67 8d21 0000 0800 0000 0500 0001  ................
548     00000020: 2870 0000 ac5d 1661 86ff ffff 4865 6c6c  ................
549     00000030: 6f2c 2077 6f72 6c64 2120 776f 726c 6421  ................
550     00000040: 0a00 ff00 0000 0000 0000 0000 0000 0000  ................
551   )",
552 };
553 
TEST(CpuReaderTest,ParseSinglePrintMalformed)554 TEST(CpuReaderTest, ParseSinglePrintMalformed) {
555   const ExamplePage* test_case = &g_single_print_malformed;
556 
557   BundleProvider bundle_provider(base::kPageSize);
558   ProtoTranslationTable* table = GetTable(test_case->name);
559   auto page = PageFromXxd(test_case->data);
560 
561   FtraceDataSourceConfig ds_config = EmptyConfig();
562   ds_config.event_filter.AddEnabledEvent(
563       table->EventToFtraceId(GroupAndName("ftrace", "print")));
564 
565   FtraceMetadata metadata{};
566   CompactSchedBuffer compact_buffer;
567   const uint8_t* parse_pos = page.get();
568   base::Optional<CpuReader::PageHeader> page_header =
569       CpuReader::ParsePageHeader(&parse_pos, table->page_header_size_len());
570 
571   const uint8_t* page_end = page.get() + base::kPageSize;
572   ASSERT_TRUE(page_header.has_value());
573   EXPECT_FALSE(page_header->lost_events);
574   EXPECT_TRUE(parse_pos < page_end);
575   EXPECT_TRUE(parse_pos + page_header->size < page_end);
576 
577   size_t evt_bytes = CpuReader::ParsePagePayload(
578       parse_pos, &page_header.value(), table, &ds_config, &compact_buffer,
579       bundle_provider.writer(), &metadata);
580 
581   ASSERT_EQ(0u, evt_bytes);
582 
583   auto bundle = bundle_provider.ParseProto();
584   ASSERT_TRUE(bundle);
585   ASSERT_EQ(bundle->event().size(), 1u);
586   // Although one field is malformed we still see data for the rest
587   // since we write the fields as we parse them for speed.
588   const protos::gen::FtraceEvent& event = bundle->event()[0];
589   EXPECT_EQ(event.pid(), 28712ul);
590   EXPECT_TRUE(WithinOneMicrosecond(event.timestamp(), 608934, 535199));
591   EXPECT_EQ(event.print().buf(), "");
592 }
593 
TEST(CpuReaderTest,FilterByEvent)594 TEST(CpuReaderTest, FilterByEvent) {
595   const ExamplePage* test_case = &g_single_print;
596 
597   BundleProvider bundle_provider(base::kPageSize);
598   ProtoTranslationTable* table = GetTable(test_case->name);
599   auto page = PageFromXxd(test_case->data);
600 
601   FtraceDataSourceConfig ds_config = EmptyConfig();
602 
603   FtraceMetadata metadata{};
604   CompactSchedBuffer compact_buffer;
605   const uint8_t* parse_pos = page.get();
606   base::Optional<CpuReader::PageHeader> page_header =
607       CpuReader::ParsePageHeader(&parse_pos, table->page_header_size_len());
608 
609   ASSERT_TRUE(page_header.has_value());
610   EXPECT_FALSE(page_header->lost_events);
611 
612   size_t evt_bytes = CpuReader::ParsePagePayload(
613       parse_pos, &page_header.value(), table, &ds_config, &compact_buffer,
614       bundle_provider.writer(), &metadata);
615 
616   EXPECT_LT(0u, evt_bytes);
617 
618   auto bundle = bundle_provider.ParseProto();
619   ASSERT_TRUE(bundle);
620   ASSERT_EQ(bundle->event().size(), 0u);
621 }
622 
623 // clang-format off
624 // # tracer: nop
625 // #
626 // # entries-in-buffer/entries-written: 3/3   #P:8
627 // #
628 // #                              _-----=> irqs-off
629 // #                             / _----=> need-resched
630 // #                            | / _---=> hardirq/softirq
631 // #                            || / _--=> preempt-depth
632 // #                            ||| /     delay
633 // #           TASK-PID   CPU#  ||||    TIMESTAMP  FUNCTION
634 // #              | |       |   ||||       |         |
635 //               sh-30693 [000] ...1 615436.216806: tracing_mark_write: Hello, world!
636 //               sh-30693 [000] ...1 615486.377232: tracing_mark_write: Good afternoon, world!
637 //               sh-30693 [000] ...1 615495.632679: tracing_mark_write: Goodbye, world!
638 // clang-format on
639 
640 static ExamplePage g_three_prints{
641     "synthetic",
642     R"(
643     00000000: a3ab 1569 bc2f 0200 9400 0000 0000 0000  ...i./..........
644     00000010: 1e00 0000 0000 0000 0800 0000 0500 0001  ................
645     00000020: e577 0000 ac5d 1661 86ff ffff 4865 6c6c  .w...].a....Hell
646     00000030: 6f2c 2077 6f72 6c64 210a 0000 5e32 6bb9  o, world!...^2k.
647     00000040: 7501 0000 0b00 0000 0500 0001 e577 0000  u............w..
648     00000050: ac5d 1661 86ff ffff 476f 6f64 2061 6674  .].a....Good aft
649     00000060: 6572 6e6f 6f6e 2c20 776f 726c 6421 0a00  ernoon, world!..
650     00000070: 0000 0000 9e6a 5df5 4400 0000 0900 0000  .....j].D.......
651     00000080: 0500 0001 e577 0000 ac5d 1661 86ff ffff  .....w...].a....
652     00000090: 476f 6f64 6279 652c 2077 6f72 6c64 210a  Goodbye, world!.
653     000000a0: 0051 0000 0000 0000 0000 0000 0000 0000  .Q..............
654   )",
655 };
656 
TEST(CpuReaderTest,ParseThreePrint)657 TEST(CpuReaderTest, ParseThreePrint) {
658   const ExamplePage* test_case = &g_three_prints;
659 
660   BundleProvider bundle_provider(base::kPageSize);
661   ProtoTranslationTable* table = GetTable(test_case->name);
662   auto page = PageFromXxd(test_case->data);
663 
664   FtraceDataSourceConfig ds_config = EmptyConfig();
665   ds_config.event_filter.AddEnabledEvent(
666       table->EventToFtraceId(GroupAndName("ftrace", "print")));
667 
668   FtraceMetadata metadata{};
669   CompactSchedBuffer compact_buffer;
670   const uint8_t* parse_pos = page.get();
671   base::Optional<CpuReader::PageHeader> page_header =
672       CpuReader::ParsePageHeader(&parse_pos, table->page_header_size_len());
673 
674   const uint8_t* page_end = page.get() + base::kPageSize;
675   ASSERT_TRUE(page_header.has_value());
676   EXPECT_FALSE(page_header->lost_events);
677   EXPECT_TRUE(parse_pos < page_end);
678   EXPECT_TRUE(parse_pos + page_header->size < page_end);
679 
680   size_t evt_bytes = CpuReader::ParsePagePayload(
681       parse_pos, &page_header.value(), table, &ds_config, &compact_buffer,
682       bundle_provider.writer(), &metadata);
683 
684   EXPECT_LT(0u, evt_bytes);
685 
686   auto bundle = bundle_provider.ParseProto();
687   ASSERT_TRUE(bundle);
688   ASSERT_EQ(bundle->event().size(), 3u);
689 
690   {
691     const protos::gen::FtraceEvent& event = bundle->event()[0];
692     EXPECT_EQ(event.pid(), 30693ul);
693     EXPECT_TRUE(WithinOneMicrosecond(event.timestamp(), 615436, 216806));
694     EXPECT_EQ(event.print().buf(), "Hello, world!\n");
695   }
696 
697   {
698     const protos::gen::FtraceEvent& event = bundle->event()[1];
699     EXPECT_EQ(event.pid(), 30693ul);
700     EXPECT_TRUE(WithinOneMicrosecond(event.timestamp(), 615486, 377232));
701     EXPECT_EQ(event.print().buf(), "Good afternoon, world!\n");
702   }
703 
704   {
705     const protos::gen::FtraceEvent& event = bundle->event()[2];
706     EXPECT_EQ(event.pid(), 30693ul);
707     EXPECT_TRUE(WithinOneMicrosecond(event.timestamp(), 615495, 632679));
708     EXPECT_EQ(event.print().buf(), "Goodbye, world!\n");
709   }
710 }
711 
712 // clang-format off
713 // # tracer: nop
714 // #
715 // # entries-in-buffer/entries-written: 6/6   #P:8
716 // #
717 // #                              _-----=> irqs-off
718 // #                             / _----=> need-resched
719 // #                            | / _---=> hardirq/softirq
720 // #                            || / _--=> preempt-depth
721 // #                            ||| /     delay
722 // #           TASK-PID   CPU#  ||||    TIMESTAMP  FUNCTION
723 // #              | |       |   ||||       |         |
724 //      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
725 //            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
726 //      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
727 //            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
728 //               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
729 //            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
730 // clang-format on
731 
732 static ExamplePage g_six_sched_switch{
733     "synthetic",
734     R"(
735     00000000: 2b16 c3be 90b6 0300 a001 0000 0000 0000  +...............
736     00000010: 1e00 0000 0000 0000 1000 0000 2f00 0103  ............/...
737     00000020: 0300 0000 6b73 6f66 7469 7271 642f 3000  ....ksoftirqd/0.
738     00000030: 0000 0000 0300 0000 7800 0000 0100 0000  ........x.......
739     00000040: 0000 0000 736c 6565 7000 722f 3000 0000  ....sleep.r/0...
740     00000050: 0000 0000 950e 0000 7800 0000 b072 8805  ........x....r..
741     00000060: 2f00 0103 950e 0000 736c 6565 7000 722f  /.......sleep.r/
742     00000070: 3000 0000 0000 0000 950e 0000 7800 0000  0...........x...
743     00000080: 0008 0000 0000 0000 7263 756f 702f 3000  ........rcuop/0.
744     00000090: 0000 0000 0000 0000 0a00 0000 7800 0000  ............x...
745     000000a0: f0b0 4700 2f00 0103 0700 0000 7263 755f  ..G./.......rcu_
746     000000b0: 7072 6565 6d70 7400 0000 0000 0700 0000  preempt.........
747     000000c0: 7800 0000 0100 0000 0000 0000 736c 6565  x...........slee
748     000000d0: 7000 722f 3000 0000 0000 0000 950e 0000  p.r/0...........
749     000000e0: 7800 0000 1001 ef00 2f00 0103 950e 0000  x......./.......
750     000000f0: 736c 6565 7000 722f 3000 0000 0000 0000  sleep.r/0.......
751     00000100: 950e 0000 7800 0000 0008 0000 0000 0000  ....x...........
752     00000110: 7368 0064 0065 722f 3000 0000 0000 0000  sh.d.er/0.......
753     00000120: b90d 0000 7800 0000 f0c7 e601 2f00 0103  ....x......./...
754     00000130: b90d 0000 7368 0064 0065 722f 3000 0000  ....sh.d.er/0...
755     00000140: 0000 0000 b90d 0000 7800 0000 0100 0000  ........x.......
756     00000150: 0000 0000 736c 6565 7000 722f 3000 0000  ....sleep.r/0...
757     00000160: 0000 0000 950e 0000 7800 0000 d030 0e00  ........x....0..
758     00000170: 2f00 0103 950e 0000 736c 6565 7000 722f  /.......sleep.r/
759     00000180: 3000 0000 0000 0000 950e 0000 7800 0000  0...........x...
760     00000190: 4000 0000 0000 0000 6b77 6f72 6b65 722f  @.......kworker/
761     000001a0: 7531 363a 3300 0000 610e 0000 7800 0000  u16:3...a...x...
762     000001b0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
763     )",
764 };
765 
TEST(CpuReaderTest,ParseSixSchedSwitch)766 TEST(CpuReaderTest, ParseSixSchedSwitch) {
767   const ExamplePage* test_case = &g_six_sched_switch;
768 
769   BundleProvider bundle_provider(base::kPageSize);
770   ProtoTranslationTable* table = GetTable(test_case->name);
771   auto page = PageFromXxd(test_case->data);
772 
773   FtraceDataSourceConfig ds_config = EmptyConfig();
774   ds_config.event_filter.AddEnabledEvent(
775       table->EventToFtraceId(GroupAndName("sched", "sched_switch")));
776 
777   FtraceMetadata metadata{};
778   CompactSchedBuffer compact_buffer;
779   const uint8_t* parse_pos = page.get();
780   base::Optional<CpuReader::PageHeader> page_header =
781       CpuReader::ParsePageHeader(&parse_pos, table->page_header_size_len());
782 
783   const uint8_t* page_end = page.get() + base::kPageSize;
784   ASSERT_TRUE(page_header.has_value());
785   EXPECT_FALSE(page_header->lost_events);
786   EXPECT_TRUE(parse_pos < page_end);
787   EXPECT_TRUE(parse_pos + page_header->size < page_end);
788 
789   size_t evt_bytes = CpuReader::ParsePagePayload(
790       parse_pos, &page_header.value(), table, &ds_config, &compact_buffer,
791       bundle_provider.writer(), &metadata);
792 
793   EXPECT_LT(0u, evt_bytes);
794 
795   auto bundle = bundle_provider.ParseProto();
796   ASSERT_TRUE(bundle);
797   ASSERT_EQ(bundle->event().size(), 6u);
798 
799   {
800     const protos::gen::FtraceEvent& event = bundle->event()[1];
801     EXPECT_EQ(event.pid(), 3733ul);
802     EXPECT_TRUE(WithinOneMicrosecond(event.timestamp(), 1045157, 725035));
803     EXPECT_EQ(event.sched_switch().prev_comm(), "sleep");
804     EXPECT_EQ(event.sched_switch().prev_pid(), 3733);
805     EXPECT_EQ(event.sched_switch().prev_prio(), 120);
806     EXPECT_EQ(event.sched_switch().next_comm(), "rcuop/0");
807     EXPECT_EQ(event.sched_switch().next_pid(), 10);
808     EXPECT_EQ(event.sched_switch().next_prio(), 120);
809   }
810 }
811 
TEST(CpuReaderTest,ParseSixSchedSwitchCompactFormat)812 TEST(CpuReaderTest, ParseSixSchedSwitchCompactFormat) {
813   const ExamplePage* test_case = &g_six_sched_switch;
814 
815   BundleProvider bundle_provider(base::kPageSize);
816   ProtoTranslationTable* table = GetTable(test_case->name);
817   auto page = PageFromXxd(test_case->data);
818 
819   FtraceDataSourceConfig ds_config{EventFilter{},
820                                    EnabledCompactSchedConfigForTesting(),
821                                    {},
822                                    {},
823                                    false /* symbolize_ksyms*/};
824   ds_config.event_filter.AddEnabledEvent(
825       table->EventToFtraceId(GroupAndName("sched", "sched_switch")));
826 
827   FtraceMetadata metadata{};
828   CompactSchedBuffer compact_buffer;
829   const uint8_t* parse_pos = page.get();
830   base::Optional<CpuReader::PageHeader> page_header =
831       CpuReader::ParsePageHeader(&parse_pos, table->page_header_size_len());
832 
833   const uint8_t* page_end = page.get() + base::kPageSize;
834   ASSERT_TRUE(page_header.has_value());
835   EXPECT_FALSE(page_header->lost_events);
836   EXPECT_TRUE(parse_pos < page_end);
837   EXPECT_TRUE(parse_pos + page_header->size < page_end);
838 
839   size_t evt_bytes = CpuReader::ParsePagePayload(
840       parse_pos, &page_header.value(), table, &ds_config, &compact_buffer,
841       bundle_provider.writer(), &metadata);
842 
843   EXPECT_LT(0u, evt_bytes);
844 
845   // Nothing written into the proto yet:
846   auto bundle = bundle_provider.ParseProto();
847   ASSERT_TRUE(bundle);
848   EXPECT_EQ(0u, bundle->event().size());
849   EXPECT_FALSE(bundle->has_compact_sched());
850   bundle_provider.ResetWriter();
851 
852   // Instead, sched switch fields were buffered:
853   EXPECT_LT(0u, compact_buffer.sched_switch().size());
854   EXPECT_LT(0u, compact_buffer.interner().interned_comms_size());
855 
856   // Write the buffer out & check the serialized format:
857   compact_buffer.WriteAndReset(bundle_provider.writer());
858   bundle_provider.writer()->Finalize();
859   bundle = bundle_provider.ParseProto();
860   ASSERT_TRUE(bundle);
861 
862   const auto& compact_sched = bundle->compact_sched();
863 
864   EXPECT_EQ(6u, compact_sched.switch_timestamp().size());
865   EXPECT_EQ(6u, compact_sched.switch_prev_state().size());
866   EXPECT_EQ(6u, compact_sched.switch_next_pid().size());
867   EXPECT_EQ(6u, compact_sched.switch_next_prio().size());
868   // 4 unique interned next_comm strings:
869   EXPECT_EQ(4u, compact_sched.intern_table().size());
870   EXPECT_EQ(6u, compact_sched.switch_next_comm_index().size());
871 
872   // First event exactly as expected (absolute timestamp):
873   EXPECT_TRUE(WithinOneMicrosecond(compact_sched.switch_timestamp()[0], 1045157,
874                                    722134));
875   EXPECT_EQ(1, compact_sched.switch_prev_state()[0]);
876   EXPECT_EQ(3733, compact_sched.switch_next_pid()[0]);
877   EXPECT_EQ(120, compact_sched.switch_next_prio()[0]);
878   auto comm_intern_idx = compact_sched.switch_next_comm_index()[0];
879   std::string next_comm = compact_sched.intern_table()[comm_intern_idx];
880   EXPECT_EQ("sleep", next_comm);
881 }
882 
TEST_F(CpuReaderTableTest,ParseAllFields)883 TEST_F(CpuReaderTableTest, ParseAllFields) {
884   using FakeEventProvider =
885       ProtoProvider<pbzero::FakeFtraceEvent, gen::FakeFtraceEvent>;
886 
887   uint16_t ftrace_event_id = 102;
888 
889   std::vector<Field> common_fields;
890   {
891     common_fields.emplace_back(Field{});
892     Field* field = &common_fields.back();
893     field->ftrace_offset = 0;
894     field->ftrace_size = 4;
895     field->ftrace_type = kFtraceUint32;
896     field->proto_field_id = 1;
897     field->proto_field_type = ProtoSchemaType::kUint32;
898     SetTranslationStrategy(field->ftrace_type, field->proto_field_type,
899                            &field->strategy);
900   }
901 
902   {
903     common_fields.emplace_back(Field{});
904     Field* field = &common_fields.back();
905     field->ftrace_offset = 4;
906     field->ftrace_size = 4;
907     field->ftrace_type = kFtraceCommonPid32;
908     field->proto_field_id = 2;
909     field->proto_field_type = ProtoSchemaType::kInt32;
910     SetTranslationStrategy(field->ftrace_type, field->proto_field_type,
911                            &field->strategy);
912   }
913 
914   std::vector<Event> events;
915   events.emplace_back(Event{});
916   {
917     Event* event = &events.back();
918     event->name = "";
919     event->group = "";
920     event->proto_field_id = 42;
921     event->ftrace_event_id = ftrace_event_id;
922 
923     {
924       // uint32 -> uint32
925       event->fields.emplace_back(Field{});
926       Field* field = &event->fields.back();
927       field->ftrace_offset = 8;
928       field->ftrace_size = 4;
929       field->ftrace_type = kFtraceUint32;
930       field->proto_field_id = 1;
931       field->proto_field_type = ProtoSchemaType::kUint32;
932     }
933 
934     {
935       // pid32 -> uint32
936       event->fields.emplace_back(Field{});
937       Field* field = &event->fields.back();
938       field->ftrace_offset = 12;
939       field->ftrace_size = 4;
940       field->ftrace_type = kFtracePid32;
941       field->proto_field_id = 2;
942       field->proto_field_type = ProtoSchemaType::kInt32;
943     }
944 
945     {
946       // dev32 -> uint64
947       event->fields.emplace_back(Field{});
948       Field* field = &event->fields.back();
949       field->ftrace_offset = 16;
950       field->ftrace_size = 4;
951       field->ftrace_type = kFtraceDevId32;
952       field->proto_field_id = 3;
953       field->proto_field_type = ProtoSchemaType::kUint64;
954     }
955 
956     {
957       // ino_t (32bit) -> uint64
958       event->fields.emplace_back(Field{});
959       Field* field = &event->fields.back();
960       field->ftrace_offset = 20;
961       field->ftrace_size = 4;
962       field->ftrace_type = kFtraceInode32;
963       field->proto_field_id = 4;
964       field->proto_field_type = ProtoSchemaType::kUint64;
965     }
966 
967     {
968       // dev64 -> uint64
969       event->fields.emplace_back(Field{});
970       Field* field = &event->fields.back();
971       field->ftrace_offset = 24;
972       field->ftrace_size = 8;
973       field->ftrace_type = kFtraceDevId64;
974       field->proto_field_id = 5;
975       field->proto_field_type = ProtoSchemaType::kUint64;
976     }
977 
978     {
979       // ino_t (64bit) -> uint64
980       event->fields.emplace_back(Field{});
981       Field* field = &event->fields.back();
982       field->ftrace_offset = 32;
983       field->ftrace_size = 8;
984       field->ftrace_type = kFtraceInode64;
985       field->proto_field_id = 6;
986       field->proto_field_type = ProtoSchemaType::kUint64;
987     }
988 
989     {
990       // char[16] -> string
991       event->fields.emplace_back(Field{});
992       Field* field = &event->fields.back();
993       field->ftrace_offset = 40;
994       field->ftrace_size = 16;
995       field->ftrace_type = kFtraceFixedCString;
996       field->proto_field_id = 500;
997       field->proto_field_type = ProtoSchemaType::kString;
998     }
999 
1000     {
1001       // char* -> string
1002       event->fields.emplace_back(Field{});
1003       Field* field = &event->fields.back();
1004       field->ftrace_offset = 56;
1005       field->ftrace_size = 8;
1006       field->ftrace_type = kFtraceStringPtr;
1007       field->proto_field_id = 503;
1008       field->proto_field_type = ProtoSchemaType::kString;
1009     }
1010 
1011     {
1012       // dataloc -> string
1013       event->fields.emplace_back(Field{});
1014       Field* field = &event->fields.back();
1015       field->ftrace_offset = 65;
1016       field->ftrace_size = 4;
1017       field->ftrace_type = kFtraceDataLoc;
1018       field->proto_field_id = 502;
1019       field->proto_field_type = ProtoSchemaType::kString;
1020     }
1021 
1022     {
1023       // char -> string
1024       event->fields.emplace_back(Field{});
1025       Field* field = &event->fields.back();
1026       field->ftrace_offset = 69;
1027       field->ftrace_size = 0;
1028       field->ftrace_type = kFtraceCString;
1029       field->proto_field_id = 501;
1030       field->proto_field_type = ProtoSchemaType::kString;
1031     }
1032 
1033     for (Field& field : event->fields) {
1034       SetTranslationStrategy(field.ftrace_type, field.proto_field_type,
1035                              &field.strategy);
1036     }
1037   }
1038 
1039   PrintkMap printk_formats;
1040   printk_formats.insert(0xffffff8504f51b23, "my_printk_format_string");
1041   ProtoTranslationTable table(
1042       &ftrace_, events, std::move(common_fields),
1043       ProtoTranslationTable::DefaultPageHeaderSpecForTesting(),
1044       InvalidCompactSchedEventFormatForTesting(), printk_formats);
1045 
1046   FakeEventProvider provider(base::kPageSize);
1047 
1048   BinaryWriter writer;
1049 
1050   // Must use the bit masks to translate between kernel and userspace device ids
1051   // to generate the below examples
1052   const uint32_t kKernelBlockDeviceId = 271581216;
1053 
1054   const BlockDeviceID kUserspaceBlockDeviceId =
1055       CpuReader::TranslateBlockDeviceIDToUserspace<BlockDeviceID>(
1056           kKernelBlockDeviceId);
1057   const uint64_t k64BitKernelBlockDeviceId = 4442450946;
1058   const BlockDeviceID k64BitUserspaceBlockDeviceId =
1059       CpuReader::TranslateBlockDeviceIDToUserspace<uint64_t>(
1060           k64BitKernelBlockDeviceId);
1061 
1062   writer.Write<int32_t>(1001);                       // Common field.
1063   writer.Write<int32_t>(9999);                       // Common pid
1064   writer.Write<int32_t>(1003);                       // Uint32 field
1065   writer.Write<int32_t>(97);                         // Pid
1066   writer.Write<int32_t>(kKernelBlockDeviceId);       // Dev id
1067   writer.Write<int32_t>(98);                         // Inode 32
1068   writer.Write<int64_t>(k64BitKernelBlockDeviceId);  // Dev id 64
1069   writer.Write<int64_t>(99u);                        // Inode 64
1070   writer.WriteFixedString(16, "Hello");
1071   writer.Write<uint64_t>(0xffffff8504f51b23ULL);  // char* (printk formats)
1072   writer.Write<uint8_t>(0);  // Deliberately mis-aligning.
1073   writer.Write<uint32_t>(40 | 6 << 16);
1074   writer.WriteFixedString(300, "Goodbye");
1075 
1076   auto input = writer.GetCopy();
1077   auto length = writer.written();
1078   FtraceMetadata metadata{};
1079 
1080   ASSERT_TRUE(CpuReader::ParseEvent(ftrace_event_id, input.get(),
1081                                     input.get() + length, &table,
1082                                     provider.writer(), &metadata));
1083 
1084   auto event = provider.ParseProto();
1085   ASSERT_TRUE(event);
1086   EXPECT_EQ(event->common_field(), 1001ul);
1087   EXPECT_EQ(event->common_pid(), 9999ul);
1088   EXPECT_TRUE(event->has_all_fields());
1089   EXPECT_EQ(event->all_fields().field_uint32(), 1003u);
1090   EXPECT_EQ(event->all_fields().field_pid(), 97);
1091   EXPECT_EQ(event->all_fields().field_dev_32(),
1092             static_cast<uint32_t>(kUserspaceBlockDeviceId));
1093   EXPECT_EQ(event->all_fields().field_inode_32(), 98u);
1094 // TODO(primiano): for some reason this fails on mac.
1095 #if !PERFETTO_BUILDFLAG(PERFETTO_OS_APPLE)
1096   EXPECT_EQ(event->all_fields().field_dev_64(), k64BitUserspaceBlockDeviceId);
1097 #endif
1098   EXPECT_EQ(event->all_fields().field_inode_64(), 99u);
1099   EXPECT_EQ(event->all_fields().field_char_16(), "Hello");
1100   EXPECT_EQ(event->all_fields().field_char(), "Goodbye");
1101   EXPECT_EQ(event->all_fields().field_data_loc(), "Hello");
1102   EXPECT_EQ(event->all_fields().field_char_star(), "my_printk_format_string");
1103   EXPECT_THAT(metadata.pids, Contains(97));
1104   EXPECT_EQ(metadata.inode_and_device.size(), 2U);
1105   EXPECT_THAT(metadata.inode_and_device,
1106               Contains(Pair(98u, kUserspaceBlockDeviceId)));
1107   EXPECT_THAT(metadata.inode_and_device,
1108               Contains(Pair(99u, k64BitUserspaceBlockDeviceId)));
1109 }
1110 
TEST(CpuReaderTest,TaskRenameEvent)1111 TEST(CpuReaderTest, TaskRenameEvent) {
1112   BundleProvider bundle_provider(base::kPageSize);
1113 
1114   BinaryWriter writer;
1115   ProtoTranslationTable* table = GetTable("android_seed_N2F62_3.10.49");
1116 
1117   constexpr uint32_t kTaskRenameId = 19;
1118 
1119   writer.Write<int32_t>(1001);             // Common field.
1120   writer.Write<int32_t>(9999);             // Common pid
1121   writer.Write<int32_t>(9999);             // Pid
1122   writer.WriteFixedString(16, "Hello");    // Old Comm
1123   writer.WriteFixedString(16, "Goodbye");  // New Comm
1124   writer.Write<uint64_t>(10);              // flags
1125   writer.Write<int16_t>(10);               // oom_score_adj
1126 
1127   auto input = writer.GetCopy();
1128   auto length = writer.written();
1129   FtraceMetadata metadata{};
1130 
1131   ASSERT_TRUE(CpuReader::ParseEvent(kTaskRenameId, input.get(),
1132                                     input.get() + length, table,
1133                                     bundle_provider.writer(), &metadata));
1134   EXPECT_THAT(metadata.rename_pids, Contains(9999));
1135   EXPECT_THAT(metadata.pids, Contains(9999));
1136 }
1137 
1138 // Page with a single sched_switch, no data loss.
1139 static char g_switch_page[] =
1140     R"(
1141     00000000: 2b16 c3be 90b6 0300 4c00 0000 0000 0000  ................
1142     00000010: 1e00 0000 0000 0000 1000 0000 2f00 0103  ................
1143     00000020: 0300 0000 6b73 6f66 7469 7271 642f 3000  ................
1144     00000030: 0000 0000 0300 0000 7800 0000 0100 0000  ................
1145     00000040: 0000 0000 736c 6565 7000 722f 3000 0000  ................
1146     00000050: 0000 0000 950e 0000 7800 0000 0000 0000  ................
1147     )";
1148 
1149 // Page with a single sched_switch, header has data loss flag set.
1150 static char g_switch_page_lost_events[] =
1151     R"(
1152     00000000: 2b16 c3be 90b6 0300 4c00 0080 ffff ffff  ................
1153     00000010: 1e00 0000 0000 0000 1000 0000 2f00 0103  ................
1154     00000020: 0300 0000 6b73 6f66 7469 7271 642f 3000  ................
1155     00000030: 0000 0000 0300 0000 7800 0000 0100 0000  ................
1156     00000040: 0000 0000 736c 6565 7000 722f 3000 0000  ................
1157     00000050: 0000 0000 950e 0000 7800 0000 0000 0000  ................
1158     )";
1159 
TEST(CpuReaderTest,NewPacketOnLostEvents)1160 TEST(CpuReaderTest, NewPacketOnLostEvents) {
1161   auto page_ok = PageFromXxd(g_switch_page);
1162   auto page_loss = PageFromXxd(g_switch_page_lost_events);
1163 
1164   std::vector<const void*> test_page_order = {
1165       page_ok.get(),   page_ok.get(), page_ok.get(), page_loss.get(),
1166       page_loss.get(), page_ok.get(), page_ok.get(), page_ok.get()};
1167 
1168   // Prepare a buffer with 8 contiguous pages, with the above contents.
1169   static constexpr size_t kTestPages = 8;
1170   uint8_t buf[base::kPageSize * kTestPages] = {};
1171   for (size_t i = 0; i < kTestPages; i++) {
1172     void* dest = buf + (i * base::kPageSize);
1173     memcpy(dest, static_cast<const void*>(test_page_order[i]), base::kPageSize);
1174   }
1175 
1176   BundleProvider bundle_provider(base::kPageSize);
1177   ProtoTranslationTable* table = GetTable("synthetic");
1178   FtraceMetadata metadata{};
1179   FtraceDataSourceConfig ds_config = EmptyConfig();
1180   ds_config.event_filter.AddEnabledEvent(
1181       table->EventToFtraceId(GroupAndName("sched", "sched_switch")));
1182 
1183   TraceWriterForTesting trace_writer;
1184   CpuReader::ProcessPagesForDataSource(&trace_writer, &metadata, /*cpu=*/1,
1185                                        &ds_config, buf, kTestPages, table,
1186                                        /*symbolizer=*/nullptr);
1187 
1188   // Each packet should contain the parsed contents of a contiguous run of pages
1189   // without data loss.
1190   // So we should get three packets (each page has 1 event):
1191   //   [3 events] [1 event] [4 events].
1192   auto packets = trace_writer.GetAllTracePackets();
1193 
1194   ASSERT_EQ(3u, packets.size());
1195   EXPECT_FALSE(packets[0].ftrace_events().lost_events());
1196   EXPECT_EQ(3u, packets[0].ftrace_events().event().size());
1197 
1198   EXPECT_TRUE(packets[1].ftrace_events().lost_events());
1199   EXPECT_EQ(1u, packets[1].ftrace_events().event().size());
1200 
1201   EXPECT_TRUE(packets[2].ftrace_events().lost_events());
1202   EXPECT_EQ(4u, packets[2].ftrace_events().event().size());
1203 }
1204 
1205 // Page containing an absolute timestamp (RINGBUF_TYPE_TIME_STAMP).
1206 static char g_abs_timestamp[] =
1207     R"(
1208 00000000: 8949 fbfb 38e4 0400 6407 0000 0000 0000  .I..8...d.......
1209 00000010: 5032 0a2d 3b01 0100 0000 0000 7377 6170  P2.-;.......swap
1210 00000020: 7065 722f 3000 0000 0000 0000 0000 0000  per/0...........
1211 00000030: 7800 0000 0000 0000 0000 0000 6776 6673  x...........gvfs
1212 00000040: 2d61 6663 2d76 6f6c 756d 6500 6483 0000  -afc-volume.d...
1213 00000050: 7800 0000 f0de 1700 3b01 0100 6483 0000  x.......;...d...
1214 00000060: 6776 6673 2d61 6663 2d76 6f6c 756d 6500  gvfs-afc-volume.
1215 00000070: 6483 0000 7800 0000 0100 0000 0000 0000  d...x...........
1216 00000080: 7377 6170 7065 722f 3000 0000 0000 0000  swapper/0.......
1217 00000090: 0000 0000 7800 0000 aaa1 5c08 0401 1100  ....x.....\.....
1218 000000a0: 0000 0000 88fc 31eb 029f ffff 609e d3c0  ......1.....`...
1219 000000b0: ffff ffff 0076 b4a1 029f ffff 0020 0000  .....v....... ..
1220 000000c0: ffff ffff e477 1700 0301 1100 0000 0000  .....w..........
1221 000000d0: 88fc 31eb 029f ffff aa26 0100 3e01 1100  ..1......&..>...
1222 000000e0: 0000 0000 6b77 6f72 6b65 722f 7538 3a35  ....kworker/u8:5
1223 000000f0: 0000 0000 24c0 0c00 7800 0000 0100 0000  ....$...x.......
1224 00000100: 0300 0000 90e6 e700 3b01 0100 0000 0000  ........;.......
1225 00000110: 7377 6170 7065 722f 3000 0000 0000 0000  swapper/0.......
1226 00000120: 0000 0000 7800 0000 0000 0000 0000 0000  ....x...........
1227 00000130: 6b77 6f72 6b65 722f 7538 3a35 0000 0000  kworker/u8:5....
1228 00000140: 24c0 0c00 7800 0000 aa56 0300 3e01 0100  $...x....V..>...
1229 00000150: 24c0 0c00 6b77 6f72 6b65 722f 7538 3a31  $...kworker/u8:1
1230 00000160: 0000 0000 8eb5 0c00 7800 0000 0100 0000  ........x.......
1231 00000170: 0300 0000 06eb 0300 0201 0000 24c0 0c00  ............$...
1232 00000180: 6026 f22a 049f ffff f0e4 4cc0 ffff ffff  `&.*......L.....
1233 00000190: ca45 0f00 3e01 0100 24c0 0c00 646d 6372  .E..>...$...dmcr
1234 000001a0: 7970 745f 7772 6974 652f 3200 2601 0000  ypt_write/2.&...
1235 000001b0: 7800 0000 0100 0000 0100 0000 c617 0200  x...............
1236 000001c0: 0101 0000 24c0 0c00 6026 f22a 049f ffff  ....$...`&.*....
1237 000001d0: f0e4 4cc0 ffff ffff a47c 0000 0301 0100  ..L......|......
1238 000001e0: 24c0 0c00 6015 f22a 049f ffff 0685 0000  $...`..*........
1239 000001f0: 0201 0000 24c0 0c00 a05d f22a 049f ffff  ....$....].*....
1240 00000200: f0e4 4cc0 ffff ffff c6dd 0800 0101 0000  ..L.............
1241 00000210: 24c0 0c00 a05d f22a 049f ffff f0e4 4cc0  $....].*......L.
1242 00000220: ffff ffff 8444 0000 0301 0100 24c0 0c00  .....D......$...
1243 00000230: 6059 f22a 049f ffff e672 0000 0201 0000  `Y.*.....r......
1244 00000240: 24c0 0c00 e050 f22a 049f ffff f0e4 4cc0  $....P.*......L.
1245 00000250: ffff ffff 4673 0a00 0101 0000 24c0 0c00  ....Fs......$...
1246 00000260: e050 f22a 049f ffff f0e4 4cc0 ffff ffff  .P.*......L.....
1247 00000270: 04ca 0000 0301 0100 24c0 0c00 2000 f22a  ........$... ..*
1248 00000280: 049f ffff 86b1 0000 0201 0000 24c0 0c00  ............$...
1249 00000290: 6015 f22a 049f ffff f0e4 4cc0 ffff ffff  `..*......L.....
1250 000002a0: e640 0c00 0101 0000 24c0 0c00 6015 f22a  .@......$...`..*
1251 000002b0: 049f ffff f0e4 4cc0 ffff ffff 64b4 0000  ......L.....d...
1252 000002c0: 0301 0100 24c0 0c00 2011 f22a 049f ffff  ....$... ..*....
1253 000002d0: 66b9 0000 0201 0000 24c0 0c00 a06e f22a  f.......$....n.*
1254 000002e0: 049f ffff f0e4 4cc0 ffff ffff 6ae1 4200  ......L.....j.B.
1255 000002f0: 3e01 1100 24c0 0c00 6a62 6432 2f64 6d2d  >...$...jbd2/dm-
1256 00000300: 312d 3800 0000 0000 6a01 0000 7800 0000  1-8.....j...x...
1257 00000310: 0100 0000 0300 0000 269b 0400 0101 0000  ........&.......
1258 00000320: 24c0 0c00 a06e f22a 049f ffff f0e4 4cc0  $....n.*......L.
1259 00000330: ffff ffff ff9d 6fb6 1f87 9c00 1000 0000  ......o.........
1260 00000340: 3b01 0100 24c0 0c00 6b77 6f72 6b65 722f  ;...$...kworker/
1261 00000350: 7538 3a35 0000 0000 24c0 0c00 7800 0000  u8:5....$...x...
1262 00000360: 8000 0000 0000 0000 7377 6170 7065 722f  ........swapper/
1263 00000370: 3000 0000 0000 0000 0000 0000 7800 0000  0...........x...
1264 00000380: 6ad2 3802 0401 1100 0000 0000 c800 384b  j.8...........8K
1265 00000390: 029f ffff 7018 75c0 ffff ffff 00ac edce  ....p.u.........
1266 000003a0: 039f ffff 0020 0000 0000 0000 c4de 0000  ..... ..........
1267 000003b0: 0301 1100 0000 0000 c800 384b 029f ffff  ..........8K....
1268 000003c0: 8a27 0100 3e01 1100 0000 0000 6b77 6f72  .'..>.......kwor
1269 000003d0: 6b65 722f 303a 3200 0000 0000 48b4 0c00  ker/0:2.....H...
1270 000003e0: 7800 0000 0100 0000 0000 0000 706d 0800  x...........pm..
1271 000003f0: 3b01 0100 0000 0000 7377 6170 7065 722f  ;.......swapper/
1272 00000400: 3000 0000 0000 0000 0000 0000 7800 0000  0...........x...
1273 00000410: 0000 0000 0000 0000 6b77 6f72 6b65 722f  ........kworker/
1274 00000420: 303a 3200 0000 0000 48b4 0c00 7800 0000  0:2.....H...x...
1275 00000430: 4636 0200 0201 0000 48b4 0c00 c800 384b  F6......H.....8K
1276 00000440: 029f ffff 7018 75c0 ffff ffff ca56 0500  ....p.u......V..
1277 00000450: 0401 0100 48b4 0c00 606a ad55 029f ffff  ....H...`j.U....
1278 00000460: f0e4 4cc0 ffff ffff 002c 04d0 039f ffff  ..L......,......
1279 00000470: 0020 0000 ffff ffff e435 0000 0301 0100  . .......5......
1280 00000480: 48b4 0c00 606a ad55 029f ffff ca67 0000  H...`j.U.....g..
1281 00000490: 3e01 0100 48b4 0c00 6b77 6f72 6b65 722f  >...H...kworker/
1282 000004a0: 7538 3a35 0000 0000 24c0 0c00 7800 0000  u8:5....$...x...
1283 000004b0: 0100 0000 0000 0000 e6fc 0200 0101 0000  ................
1284 000004c0: 48b4 0c00 c800 384b 029f ffff 7018 75c0  H.....8K....p.u.
1285 000004d0: ffff ffff 708f 0200 3b01 0100 48b4 0c00  ....p...;...H...
1286 000004e0: 6b77 6f72 6b65 722f 303a 3200 0000 0000  kworker/0:2.....
1287 000004f0: 48b4 0c00 7800 0000 8000 0000 0000 0000  H...x...........
1288 00000500: 6b77 6f72 6b65 722f 7538 3a35 0000 0000  kworker/u8:5....
1289 00000510: 24c0 0c00 7800 0000 0614 0100 0201 0000  $...x...........
1290 00000520: 24c0 0c00 606a ad55 029f ffff f0e4 4cc0  $...`j.U......L.
1291 00000530: ffff ffff ea7e 0c00 3e01 0100 24c0 0c00  .....~..>...$...
1292 00000540: 646d 6372 7970 745f 7772 6974 652f 3200  dmcrypt_write/2.
1293 00000550: 2601 0000 7800 0000 0100 0000 0100 0000  &...x...........
1294 00000560: 4645 0200 0101 0000 24c0 0c00 606a ad55  FE......$...`j.U
1295 00000570: 029f ffff f0e4 4cc0 ffff ffff b043 0900  ......L......C..
1296 00000580: 3b01 0100 24c0 0c00 6b77 6f72 6b65 722f  ;...$...kworker/
1297 00000590: 7538 3a35 0000 0000 24c0 0c00 7800 0000  u8:5....$...x...
1298 000005a0: 8000 0000 0000 0000 7377 6170 7065 722f  ........swapper/
1299 000005b0: 3000 0000 0000 0000 0000 0000 7800 0000  0...........x...
1300 000005c0: ca7a 3900 0401 1100 0000 0000 48bc d5a1  .z9.........H...
1301 000005d0: 029f ffff 10e2 62bb ffff ffff 00e0 40d0  ......b.......@.
1302 000005e0: 039f ffff 0020 0000 0000 0000 c4bb 0000  ..... ..........
1303 000005f0: 0301 1100 0000 0000 48bc d5a1 029f ffff  ........H.......
1304 00000600: 2aea 0000 3e01 1100 0000 0000 6b77 6f72  *...>.......kwor
1305 00000610: 6b65 722f 303a 3148 0000 0000 cfc1 0c00  ker/0:1H........
1306 00000620: 6400 0000 0100 0000 0000 0000 90bb 0600  d...............
1307 00000630: 3b01 0100 0000 0000 7377 6170 7065 722f  ;.......swapper/
1308 00000640: 3000 0000 0000 0000 0000 0000 7800 0000  0...........x...
1309 00000650: 0000 0000 0000 0000 6b77 6f72 6b65 722f  ........kworker/
1310 00000660: 303a 3148 0000 0000 cfc1 0c00 6400 0000  0:1H........d...
1311 00000670: 8617 0200 0201 0000 cfc1 0c00 48bc d5a1  ............H...
1312 00000680: 029f ffff 10e2 62bb ffff ffff c68f 0400  ......b.........
1313 00000690: 0101 0000 cfc1 0c00 48bc d5a1 029f ffff  ........H.......
1314 000006a0: 10e2 62bb ffff ffff b063 0300 3b01 0100  ..b......c..;...
1315 000006b0: cfc1 0c00 6b77 6f72 6b65 722f 303a 3148  ....kworker/0:1H
1316 000006c0: 0000 0000 cfc1 0c00 6400 0000 8000 0000  ........d.......
1317 000006d0: 0000 0000 7377 6170 7065 722f 3000 0000  ....swapper/0...
1318 000006e0: 0000 0000 0000 0000 7800 0000 4a10 ad01  ........x...J...
1319 000006f0: 3e01 1100 0000 0000 6a62 6432 2f64 6d2d  >.......jbd2/dm-
1320 00000700: 312d 3800 0000 0000 6a01 0000 7800 0000  1-8.....j...x...
1321 00000710: 0100 0000 0300 0000 ea27 b900 3e01 1100  .........'..>...
1322 00000720: 0000 0000 7263 755f 7363 6865 6400 0000  ....rcu_sched...
1323 00000730: 0000 0000 0d00 0000 7800 0000 0100 0000  ........x.......
1324 00000740: 0200 0000 3d00 0000 2c00 0000 0000 0000  ....=...,.......
1325 00000750: 0000 0000 0000 0000 0000 0000 0000 0000  ................
1326 00000760: 0000 0000 0000 0000 0000 0000 0000 0000  ................
1327 00000770: 0000 0000 0000 0000 0000 0000 0000 0000  ................
1328   )";
1329 
TEST(CpuReaderTest,ParseAbsoluteTimestamp)1330 TEST(CpuReaderTest, ParseAbsoluteTimestamp) {
1331   BundleProvider bundle_provider(base::kPageSize);
1332   auto page = PageFromXxd(g_abs_timestamp);
1333 
1334   // Hand-build a translation table that handles sched_switch for this test
1335   // page. We cannot reuse the test data format file, since the ftrace id for
1336   // sched_switch in this page is different.
1337   std::vector<Field> common_fields;
1338   {  // common_pid
1339     common_fields.emplace_back(Field{});
1340     Field* field = &common_fields.back();
1341     field->ftrace_offset = 4;
1342     field->ftrace_size = 4;
1343     field->ftrace_type = kFtraceCommonPid32;
1344     field->proto_field_id = 2;
1345     field->proto_field_type = ProtoSchemaType::kInt32;
1346     SetTranslationStrategy(field->ftrace_type, field->proto_field_type,
1347                            &field->strategy);
1348   }
1349   Event sched_switch_event{
1350       "sched_switch",
1351       "sched",
1352       {
1353           {8, 16, FtraceFieldType::kFtraceFixedCString, "prev_comm", 1,
1354            ProtoSchemaType::kString,
1355            TranslationStrategy::kInvalidTranslationStrategy},
1356           {24, 4, FtraceFieldType::kFtracePid32, "prev_pid", 2,
1357            ProtoSchemaType::kInt32,
1358            TranslationStrategy::kInvalidTranslationStrategy},
1359           {28, 4, FtraceFieldType::kFtraceInt32, "prev_prio", 3,
1360            ProtoSchemaType::kInt32,
1361            TranslationStrategy::kInvalidTranslationStrategy},
1362           {32, 8, FtraceFieldType::kFtraceInt64, "prev_state", 4,
1363            ProtoSchemaType::kInt64,
1364            TranslationStrategy::kInvalidTranslationStrategy},
1365           {40, 16, FtraceFieldType::kFtraceFixedCString, "next_comm", 5,
1366            ProtoSchemaType::kString,
1367            TranslationStrategy::kInvalidTranslationStrategy},
1368           {56, 4, FtraceFieldType::kFtracePid32, "next_pid", 6,
1369            ProtoSchemaType::kInt32,
1370            TranslationStrategy::kInvalidTranslationStrategy},
1371           {60, 4, FtraceFieldType::kFtraceInt32, "next_prio", 7,
1372            ProtoSchemaType::kInt32,
1373            TranslationStrategy::kInvalidTranslationStrategy},
1374       },
1375       /*ftrace_event_id=*/315,
1376       /*proto_field_id=*/4,
1377       /*size=*/64};
1378   for (Field& field : sched_switch_event.fields) {
1379     SetTranslationStrategy(field.ftrace_type, field.proto_field_type,
1380                            &field.strategy);
1381   }
1382   std::vector<Event> events;
1383   events.emplace_back(std::move(sched_switch_event));
1384 
1385   NiceMock<MockFtraceProcfs> mock_ftrace;
1386   PrintkMap printk_formats;
1387   ProtoTranslationTable translation_table(
1388       &mock_ftrace, events, std::move(common_fields),
1389       ProtoTranslationTable::DefaultPageHeaderSpecForTesting(),
1390       InvalidCompactSchedEventFormatForTesting(), printk_formats);
1391   ProtoTranslationTable* table = &translation_table;
1392 
1393   FtraceDataSourceConfig ds_config = EmptyConfig();
1394   ds_config.event_filter.AddEnabledEvent(
1395       table->EventToFtraceId(GroupAndName("sched", "sched_switch")));
1396 
1397   FtraceMetadata metadata{};
1398   CompactSchedBuffer compact_buffer;
1399   const uint8_t* parse_pos = page.get();
1400   base::Optional<CpuReader::PageHeader> page_header =
1401       CpuReader::ParsePageHeader(&parse_pos, table->page_header_size_len());
1402 
1403   const uint8_t* page_end = page.get() + base::kPageSize;
1404   ASSERT_TRUE(page_header.has_value());
1405   EXPECT_FALSE(page_header->lost_events);
1406   EXPECT_TRUE(parse_pos < page_end);
1407   EXPECT_TRUE(parse_pos + page_header->size < page_end);
1408 
1409   size_t evt_bytes = CpuReader::ParsePagePayload(
1410       parse_pos, &page_header.value(), table, &ds_config, &compact_buffer,
1411       bundle_provider.writer(), &metadata);
1412 
1413   ASSERT_LT(0u, evt_bytes);
1414 
1415   auto bundle = bundle_provider.ParseProto();
1416   ASSERT_TRUE(bundle);
1417 
1418   // There should be 9 sched_switch events within the above page.
1419   // We assert that all of their timestamps are exactly as expected.
1420   //
1421   // The key record that we're testing is an absolute timestamp
1422   // (RINGBUF_TYPE_TIME_STAMP) between the 3rd and 4th sched_switch events.
1423   //
1424   // This timestamp record starts at 0x334 bytes into the page.
1425   // The event header (first 4 bytes): 0xb66f9dff
1426   // -> type (bottom 5 bits): 31 (RINGBUF_TYPE_TIME_STAMP)
1427   // -> bottom 27 bits of ts: 0x5b37cef
1428   // Next 4 bytes have the top bits (28..59) of ts.
1429   // -> post-shift: 0x4e438f8000000
1430   // Adding the two parts of the timestamp, we get: 1376833332542703.
1431   //
1432   // The next event (sched_switch at 0x33c) after this timestamp has a
1433   // delta-timestamp of 0 in its event header, so we expect the 4th
1434   // sched_switch to have a timestamp of exactly 1376833332542703.
1435   EXPECT_EQ(bundle->event().size(), 9u);
1436 
1437   std::vector<uint64_t> switch_timestamps;
1438   for (const auto& e : bundle->event())
1439     switch_timestamps.push_back(e.timestamp());
1440 
1441   uint64_t expected_timestamps[] = {
1442       1376833327307547ull, 1376833327356434ull, 1376833332265799ull,
1443       1376833332542703ull, 1376833333729055ull, 1376833333757142ull,
1444       1376833333808564ull, 1376833333943445ull, 1376833333964012ull};
1445 
1446   ASSERT_THAT(switch_timestamps,
1447               testing::ElementsAreArray(expected_timestamps));
1448 }
1449 
TEST(CpuReaderTest,TranslateBlockDeviceIDToUserspace)1450 TEST(CpuReaderTest, TranslateBlockDeviceIDToUserspace) {
1451   const uint32_t kKernelBlockDeviceId = 271581216;
1452   const BlockDeviceID kUserspaceBlockDeviceId = 66336;
1453   const uint64_t k64BitKernelBlockDeviceId = 4442450946;
1454   const BlockDeviceID k64BitUserspaceBlockDeviceId =
1455       static_cast<BlockDeviceID>(17594983681026ULL);
1456 
1457   EXPECT_EQ(CpuReader::TranslateBlockDeviceIDToUserspace<uint32_t>(
1458                 kKernelBlockDeviceId),
1459             kUserspaceBlockDeviceId);
1460   EXPECT_EQ(CpuReader::TranslateBlockDeviceIDToUserspace<uint64_t>(
1461                 k64BitKernelBlockDeviceId),
1462             k64BitUserspaceBlockDeviceId);
1463 }
1464 
1465 // clang-format off
1466 // # tracer: nop
1467 // #
1468 // # entries-in-buffer/entries-written: 1041/238740   #P:8
1469 // #
1470 // #                              _-----=> irqs-off
1471 // #                             / _----=> need-resched
1472 // #                            | / _---=> hardirq/softirq
1473 // #                            || / _--=> preempt-depth
1474 // #                            ||| /     delay
1475 // #           TASK-PID   CPU#  ||||    TIMESTAMP  FUNCTION
1476 // #              | |       |   ||||       |         |
1477 //       android.bg-1668  [000] ...1 174991.234105: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
1478 //       android.bg-1668  [000] ...1 174991.234108: ext4_mark_inode_dirty: dev 259,32 ino 2883605 caller ext4_dirty_inode+0x48/0x68
1479 //       android.bg-1668  [000] ...1 174991.234118: ext4_da_write_begin: dev 259,32 ino 2883605 pos 20480 len 4096 flags 0
1480 //       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
1481 //       android.bg-1668  [000] ...1 174991.234133: ext4_es_lookup_extent_enter: dev 259,32 ino 2883605 lblk 5
1482 //       android.bg-1668  [000] ...1 174991.234135: ext4_es_lookup_extent_exit: dev 259,32 ino 2883605 found 1 [5/4294967290) 576460752303423487 H0x10
1483 //       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
1484 //       android.bg-1668  [000] ...1 174991.234142: ext4_es_insert_extent: dev 259,32 ino 2883605 es [5/1) mapped 576460752303423487 status D
1485 //       android.bg-1668  [000] ...1 174991.234153: ext4_da_write_end: dev 259,32 ino 2883605 pos 20480 len 4096 copied 4096
1486 //       android.bg-1668  [000] ...1 174991.234158: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
1487 //       android.bg-1668  [000] ...1 174991.234160: ext4_mark_inode_dirty: dev 259,32 ino 2883605 caller ext4_dirty_inode+0x48/0x68
1488 //       android.bg-1668  [000] ...1 174991.234170: ext4_da_write_begin: dev 259,32 ino 2883605 pos 24576 len 2968 flags 0
1489 //       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
1490 //       android.bg-1668  [000] ...1 174991.234184: ext4_es_lookup_extent_enter: dev 259,32 ino 2883605 lblk 6
1491 //       android.bg-1668  [000] ...1 174991.234187: ext4_es_lookup_extent_exit: dev 259,32 ino 2883605 found 1 [6/4294967289) 576460752303423487 H0x10
1492 //       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
1493 //       android.bg-1668  [000] ...1 174991.234193: ext4_es_insert_extent: dev 259,32 ino 2883605 es [6/1) mapped 576460752303423487 status D
1494 //       android.bg-1668  [000] ...1 174991.234203: ext4_da_write_end: dev 259,32 ino 2883605 pos 24576 len 2968 copied 2968
1495 //       android.bg-1668  [000] ...1 174991.234209: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
1496 //       android.bg-1668  [000] ...1 174991.234211: ext4_mark_inode_dirty: dev 259,32 ino 2883605 caller ext4_dirty_inode+0x48/0x68
1497 //       android.bg-1668  [000] ...1 174991.234262: ext4_sync_file_enter: dev 259,32 ino 2883605 parent 2883592 datasync 0
1498 //       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
1499 //       android.bg-1668  [000] ...1 174991.234287: ext4_journal_start: dev 259,32 blocks, 10 rsv_blocks, 0 caller ext4_writepages+0x6a4/0x119c
1500 //       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
1501 //       android.bg-1668  [000] ...1 174991.234319: ext4_da_write_pages_extent: dev 259,32 ino 2883605 lblk 0 len 7 flags 0x200
1502 //       android.bg-1668  [000] ...1 174991.234322: ext4_es_lookup_extent_enter: dev 259,32 ino 2883605 lblk 0
1503 //       android.bg-1668  [000] ...1 174991.234324: ext4_es_lookup_extent_exit: dev 259,32 ino 2883605 found 1 [0/7) 576460752303423487 D0x10
1504 //       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
1505 //       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
1506 //       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
1507 //       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
1508 //       android.bg-1668  [000] ...1 174991.234409: ext4_mark_inode_dirty: dev 259,32 ino 2883605 caller __ext4_ext_dirty+0x104/0x170
1509 //       android.bg-1668  [000] ...1 174991.234420: ext4_get_reserved_cluster_alloc: dev 259,32 ino 2883605 lblk 0 len 7
1510 //       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
1511 //       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
1512 //       android.bg-1668  [000] ...1 174991.234441: ext4_es_lookup_extent_enter: dev 259,32 ino 3 lblk 1
1513 //       android.bg-1668  [000] ...1 174991.234445: ext4_es_lookup_extent_exit: dev 259,32 ino 3 found 1 [0/2) 9255 W0x10
1514 //       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
1515 //       android.bg-1668  [000] ...1 174991.234460: ext4_es_lookup_extent_enter: dev 259,32 ino 4 lblk 1
1516 //       android.bg-1668  [000] ...1 174991.234463: ext4_es_lookup_extent_exit: dev 259,32 ino 4 found 1 [0/2) 9257 W0x10
1517 //       android.bg-1668  [000] ...1 174991.234471: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
1518 //       android.bg-1668  [000] ...1 174991.234474: ext4_mark_inode_dirty: dev 259,32 ino 2883605 caller ext4_dirty_inode+0x48/0x68
1519 //       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
1520 //       android.bg-1668  [000] ...1 174991.234484: ext4_es_insert_extent: dev 259,32 ino 2883605 es [0/7) mapped 2155471 status W
1521 //       android.bg-1668  [000] ...1 174991.234547: ext4_mark_inode_dirty: dev 259,32 ino 2883605 caller ext4_writepages+0xdc0/0x119c
1522 //       android.bg-1668  [000] ...1 174991.234604: ext4_journal_start: dev 259,32 blocks, 10 rsv_blocks, 0 caller ext4_writepages+0x6a4/0x119c
1523 //       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
1524 //       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
1525 //    Profile Saver-5504  [000] ...1 175002.711928: ext4_discard_preallocations: dev 259,32 ino 1311176
1526 //    Profile Saver-5504  [000] ...1 175002.714165: ext4_begin_ordered_truncate: dev 259,32 ino 1311176 new_size 0
1527 //    Profile Saver-5504  [000] ...1 175002.714172: ext4_journal_start: dev 259,32 blocks, 3 rsv_blocks, 0 caller ext4_setattr+0x5b4/0x788
1528 //    Profile Saver-5504  [000] ...1 175002.714218: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_setattr+0x65c/0x788
1529 //    Profile Saver-5504  [000] ...1 175002.714277: ext4_invalidatepage: dev 259,32 ino 1311176 page_index 0 offset 0 length 4096
1530 //    Profile Saver-5504  [000] ...1 175002.714281: ext4_releasepage: dev 259,32 ino 1311176 page_index 0
1531 //    Profile Saver-5504  [000] ...1 175002.714295: ext4_invalidatepage: dev 259,32 ino 1311176 page_index 1 offset 0 length 4096
1532 //    Profile Saver-5504  [000] ...1 175002.714296: ext4_releasepage: dev 259,32 ino 1311176 page_index 1
1533 //    Profile Saver-5504  [000] ...1 175002.714315: ext4_truncate_enter: dev 259,32 ino 1311176 blocks 24
1534 //    Profile Saver-5504  [000] ...1 175002.714318: ext4_journal_start: dev 259,32 blocks, 10 rsv_blocks, 0 caller ext4_truncate+0x258/0x4b8
1535 //    Profile Saver-5504  [000] ...1 175002.714322: ext4_discard_preallocations: dev 259,32 ino 1311176
1536 //    Profile Saver-5504  [000] ...1 175002.714324: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_ext_truncate+0x24/0xc8
1537 //    Profile Saver-5504  [000] ...1 175002.714328: ext4_es_remove_extent: dev 259,32 ino 1311176 es [0/4294967295)
1538 //    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
1539 //    Profile Saver-5504  [000] ...1 175002.714338: ext4_ext_remove_space: dev 259,32 ino 1311176 since 0 end 4294967294 depth 0
1540 //    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
1541 //    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
1542 //    Profile Saver-5504  [000] ...1 175002.714354: ext4_free_blocks: dev 259,32 ino 1311176 mode 0100600 block 5276994 count 2 flags 1ST_CLUSTER
1543 //    Profile Saver-5504  [000] ...1 175002.714365: ext4_mballoc_free: dev 259,32 inode 1311176 extent 161/1346/2
1544 //    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
1545 //    Profile Saver-5504  [000] ...1 175002.714391: ext4_es_lookup_extent_enter: dev 259,32 ino 3 lblk 4
1546 //    Profile Saver-5504  [000] ...1 175002.714394: ext4_es_lookup_extent_exit: dev 259,32 ino 3 found 1 [4/1) 557094 W0x10
1547 //    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
1548 //    Profile Saver-5504  [000] ...1 175002.714404: ext4_es_lookup_extent_enter: dev 259,32 ino 4 lblk 8
1549 //    Profile Saver-5504  [000] ...1 175002.714406: ext4_es_lookup_extent_exit: dev 259,32 ino 4 found 1 [8/3) 7376914 W0x10
1550 //    Profile Saver-5504  [000] ...1 175002.714413: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
1551 //    Profile Saver-5504  [000] ...1 175002.714414: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_dirty_inode+0x48/0x68
1552 //    Profile Saver-5504  [000] ...1 175002.714420: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller __ext4_ext_dirty+0x104/0x170
1553 //    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
1554 //    Profile Saver-5504  [000] ...1 175002.714425: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller __ext4_ext_dirty+0x104/0x170
1555 //    Profile Saver-5504  [000] ...1 175002.714433: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_truncate+0x3c4/0x4b8
1556 //    Profile Saver-5504  [000] ...1 175002.714436: ext4_truncate_exit: dev 259,32 ino 1311176 blocks 8
1557 //    Profile Saver-5504  [000] ...1 175002.714437: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
1558 //    Profile Saver-5504  [000] ...1 175002.714438: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_dirty_inode+0x48/0x68
1559 //    Profile Saver-5504  [000] ...1 175002.714462: ext4_da_write_begin: dev 259,32 ino 1311176 pos 0 len 4 flags 0
1560 //    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
1561 //    Profile Saver-5504  [000] ...1 175002.714477: ext4_es_lookup_extent_enter: dev 259,32 ino 1311176 lblk 0
1562 //    Profile Saver-5504  [000] ...1 175002.714477: ext4_es_lookup_extent_exit: dev 259,32 ino 1311176 found 0 [0/0) 0
1563 //    Profile Saver-5504  [000] ...1 175002.714480: ext4_ext_map_blocks_enter: dev 259,32 ino 1311176 lblk 0 len 1 flags
1564 //    Profile Saver-5504  [000] ...1 175002.714485: ext4_es_find_delayed_extent_range_enter: dev 259,32 ino 1311176 lblk 0
1565 //    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
1566 //    Profile Saver-5504  [000] ...1 175002.714490: ext4_es_insert_extent: dev 259,32 ino 1311176 es [0/4294967295) mapped 576460752303423487 status H
1567 //    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
1568 //    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
1569 //    Profile Saver-5504  [000] ...1 175002.714505: ext4_es_insert_extent: dev 259,32 ino 1311176 es [0/1) mapped 576460752303423487 status D
1570 //    Profile Saver-5504  [000] ...1 175002.714513: ext4_da_write_end: dev 259,32 ino 1311176 pos 0 len 4 copied 4
1571 //    Profile Saver-5504  [000] ...1 175002.714519: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
1572 //    Profile Saver-5504  [000] ...1 175002.714520: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_dirty_inode+0x48/0x68
1573 //    Profile Saver-5504  [000] ...1 175002.714527: ext4_da_write_begin: dev 259,32 ino 1311176 pos 4 len 4 flags 0
1574 //    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
1575 //    Profile Saver-5504  [000] ...1 175002.714531: ext4_da_write_end: dev 259,32 ino 1311176 pos 4 len 4 copied 4
1576 //    Profile Saver-5504  [000] ...1 175002.714532: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
1577 //    Profile Saver-5504  [000] ...1 175002.714532: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_dirty_inode+0x48/0x68
1578 //    Profile Saver-5504  [000] ...1 175002.715313: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
1579 //    Profile Saver-5504  [000] ...1 175002.715322: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_dirty_inode+0x48/0x68
1580 //    Profile Saver-5504  [000] ...1 175002.723849: ext4_da_write_begin: dev 259,32 ino 1311176 pos 8 len 5 flags 0
1581 //    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
1582 //    Profile Saver-5504  [000] ...1 175002.723873: ext4_da_write_end: dev 259,32 ino 1311176 pos 8 len 5 copied 5
1583 //    Profile Saver-5504  [000] ...1 175002.723877: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
1584 //    Profile Saver-5504  [000] ...1 175002.723879: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_dirty_inode+0x48/0x68
1585 //    Profile Saver-5504  [000] ...1 175002.726857: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
1586 //    Profile Saver-5504  [000] ...1 175002.726867: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_dirty_inode+0x48/0x68
1587 //    Profile Saver-5504  [000] ...1 175002.726881: ext4_da_write_begin: dev 259,32 ino 1311176 pos 13 len 4 flags 0
1588 //    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
1589 //    Profile Saver-5504  [000] ...1 175002.726890: ext4_da_write_end: dev 259,32 ino 1311176 pos 13 len 4 copied 4
1590 //    Profile Saver-5504  [000] ...1 175002.726892: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
1591 //    Profile Saver-5504  [000] ...1 175002.726892: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_dirty_inode+0x48/0x68
1592 //    Profile Saver-5504  [000] ...1 175002.726900: ext4_da_write_begin: dev 259,32 ino 1311176 pos 17 len 4079 flags 0
1593 //    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
1594 //    Profile Saver-5504  [000] ...1 175002.726904: ext4_da_write_end: dev 259,32 ino 1311176 pos 17 len 4079 copied 4079
1595 //    Profile Saver-5504  [000] ...1 175002.726905: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
1596 //    Profile Saver-5504  [000] ...1 175002.726906: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_dirty_inode+0x48/0x68
1597 //    Profile Saver-5504  [000] ...1 175002.726908: ext4_da_write_begin: dev 259,32 ino 1311176 pos 4096 len 2780 flags 0
1598 //    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
1599 //    Profile Saver-5504  [000] ...1 175002.726921: ext4_es_lookup_extent_enter: dev 259,32 ino 1311176 lblk 1
1600 //    Profile Saver-5504  [000] ...1 175002.726924: ext4_es_lookup_extent_exit: dev 259,32 ino 1311176 found 1 [1/4294967294) 576460752303423487 H0x10
1601 //    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
1602 //    Profile Saver-5504  [000] ...1 175002.726933: ext4_es_insert_extent: dev 259,32 ino 1311176 es [1/1) mapped 576460752303423487 status D
1603 //    Profile Saver-5504  [000] ...1 175002.726940: ext4_da_write_end: dev 259,32 ino 1311176 pos 4096 len 2780 copied 2780
1604 //    Profile Saver-5504  [000] ...1 175002.726941: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
1605 //    Profile Saver-5504  [000] ...1 175002.726942: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_dirty_inode+0x48/0x68
1606 //   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
1607 //   d.process.acor-27885 [000] ...1 175018.227699: ext4_mark_inode_dirty: dev 259,32 ino 3278189 caller ext4_dirty_inode+0x48/0x68
1608 //   d.process.acor-27885 [000] ...1 175018.227839: ext4_sync_file_enter: dev 259,32 ino 3278183 parent 3277001 datasync 1
1609 //   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
1610 //   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
1611 // clang-format on
1612 
1613 static ExamplePage g_full_page_sched_switch{
1614     "synthetic",
1615     R"(
1616 00000000: 31f2 7622 1a00 0000 b40f 0000 0000 0000  1.v"............
1617 00000010: 1e00 0000 0000 0000 1000 0000 2f00 0103  ............/...
1618 00000020: 140d 0000 4a69 7420 7468 7265 6164 2070  ....Jit thread p
1619 00000030: 6f6f 6c00 140d 0000 8100 0000 0008 0000  ool.............
1620 00000040: 0000 0000 4576 656e 7454 6872 6561 6400  ....EventThread.
1621 00000050: 6572 0000 7002 0000 6100 0000 f057 0e00  er..p...a....W..
1622 00000060: 2f00 0103 7002 0000 4576 656e 7454 6872  /...p...EventThr
1623 00000070: 6561 6400 6572 0000 7002 0000 6100 0000  ead.er..p...a...
1624 00000080: 0100 0000 0000 0000 4a69 7420 7468 7265  ........Jit thre
1625 00000090: 6164 2070 6f6f 6c00 140d 0000 8100 0000  ad pool.........
1626 000000a0: 50c2 0910 2f00 0103 140d 0000 4a69 7420  P.../.......Jit
1627 000000b0: 7468 7265 6164 2070 6f6f 6c00 140d 0000  thread pool.....
1628 000000c0: 8100 0000 0100 0000 0000 0000 7377 6170  ............swap
1629 000000d0: 7065 722f 3000 0000 0000 0000 0000 0000  per/0...........
1630 000000e0: 7800 0000 901a c80e 2f00 0103 0000 0000  x......./.......
1631 000000f0: 7377 6170 7065 722f 3000 0000 0000 0000  swapper/0.......
1632 00000100: 0000 0000 7800 0000 0000 0000 0000 0000  ....x...........
1633 00000110: 4469 7370 5379 6e63 0069 6e67 6572 0000  DispSync.inger..
1634 00000120: 6f02 0000 6100 0000 1064 1e00 2f00 0103  o...a....d../...
1635 00000130: 6f02 0000 4469 7370 5379 6e63 0069 6e67  o...DispSync.ing
1636 00000140: 6572 0000 6f02 0000 6100 0000 0100 0000  er..o...a.......
1637 00000150: 0000 0000 7377 6170 7065 722f 3000 0000  ....swapper/0...
1638 00000160: 0000 0000 0000 0000 7800 0000 9074 8600  ........x....t..
1639 00000170: 2f00 0103 0000 0000 7377 6170 7065 722f  /.......swapper/
1640 00000180: 3000 0000 0000 0000 0000 0000 7800 0000  0...........x...
1641 00000190: 0000 0000 0000 0000 4576 656e 7454 6872  ........EventThr
1642 000001a0: 6561 6400 6572 0000 7002 0000 6100 0000  ead.er..p...a...
1643 000001b0: d071 0b00 2f00 0103 7002 0000 4576 656e  .q../...p...Even
1644 000001c0: 7454 6872 6561 6400 6572 0000 7002 0000  tThread.er..p...
1645 000001d0: 6100 0000 0100 0000 0000 0000 7377 6170  a...........swap
1646 000001e0: 7065 722f 3000 0000 0000 0000 0000 0000  per/0...........
1647 000001f0: 7800 0000 10cd 4504 2f00 0103 0000 0000  x.....E./.......
1648 00000200: 7377 6170 7065 722f 3000 0000 0000 0000  swapper/0.......
1649 00000210: 0000 0000 7800 0000 0000 0000 0000 0000  ....x...........
1650 00000220: 7375 676f 763a 3000 0000 0000 0000 0000  sugov:0.........
1651 00000230: 3802 0000 3100 0000 30d6 1300 2f00 0103  8...1...0.../...
1652 00000240: 3802 0000 7375 676f 763a 3000 0000 0000  8...sugov:0.....
1653 00000250: 0000 0000 3802 0000 3100 0000 0100 0000  ....8...1.......
1654 00000260: 0000 0000 7377 6170 7065 722f 3000 0000  ....swapper/0...
1655 00000270: 0000 0000 0000 0000 7800 0000 3049 a202  ........x...0I..
1656 00000280: 2f00 0103 0000 0000 7377 6170 7065 722f  /.......swapper/
1657 00000290: 3000 0000 0000 0000 0000 0000 7800 0000  0...........x...
1658 000002a0: 0000 0000 0000 0000 4469 7370 5379 6e63  ........DispSync
1659 000002b0: 0069 6e67 6572 0000 6f02 0000 6100 0000  .inger..o...a...
1660 000002c0: d07a 1000 2f00 0103 6f02 0000 4469 7370  .z../...o...Disp
1661 000002d0: 5379 6e63 0069 6e67 6572 0000 6f02 0000  Sync.inger..o...
1662 000002e0: 6100 0000 0100 0000 0000 0000 7377 6170  a...........swap
1663 000002f0: 7065 722f 3000 0000 0000 0000 0000 0000  per/0...........
1664 00000300: 7800 0000 d085 1100 2f00 0103 0000 0000  x......./.......
1665 00000310: 7377 6170 7065 722f 3000 0000 0000 0000  swapper/0.......
1666 00000320: 0000 0000 7800 0000 0000 0000 0000 0000  ....x...........
1667 00000330: 7375 7266 6163 6566 6c69 6e67 6572 0000  surfaceflinger..
1668 00000340: 4b02 0000 6200 0000 907a f000 2f00 0103  K...b....z../...
1669 00000350: 4b02 0000 7375 7266 6163 6566 6c69 6e67  K...surfacefling
1670 00000360: 6572 0000 4b02 0000 6200 0000 0100 0000  er..K...b.......
1671 00000370: 0000 0000 7377 6170 7065 722f 3000 0000  ....swapper/0...
1672 00000380: 0000 0000 0000 0000 7800 0000 305a 6400  ........x...0Zd.
1673 00000390: 2f00 0103 0000 0000 7377 6170 7065 722f  /.......swapper/
1674 000003a0: 3000 0000 0000 0000 0000 0000 7800 0000  0...........x...
1675 000003b0: 0000 0000 0000 0000 6d64 7373 5f66 6230  ........mdss_fb0
1676 000003c0: 0000 0000 0000 0000 5714 0000 5300 0000  ........W...S...
1677 000003d0: 10b1 9e03 2f00 0103 5714 0000 6d64 7373  ..../...W...mdss
1678 000003e0: 5f66 6230 0000 0000 0000 0000 5714 0000  _fb0........W...
1679 000003f0: 5300 0000 0200 0000 0000 0000 6b73 6f66  S...........ksof
1680 00000400: 7469 7271 642f 3000 0000 0000 0300 0000  tirqd/0.........
1681 00000410: 7800 0000 90bb 9900 2f00 0103 0300 0000  x......./.......
1682 00000420: 6b73 6f66 7469 7271 642f 3000 0000 0000  ksoftirqd/0.....
1683 00000430: 0300 0000 7800 0000 0100 0000 0000 0000  ....x...........
1684 00000440: 7377 6170 7065 722f 3000 0000 0000 0000  swapper/0.......
1685 00000450: 0000 0000 7800 0000 701e 5305 2f00 0103  ....x...p.S./...
1686 00000460: 0000 0000 7377 6170 7065 722f 3000 0000  ....swapper/0...
1687 00000470: 0000 0000 0000 0000 7800 0000 0000 0000  ........x.......
1688 00000480: 0000 0000 6b77 6f72 6b65 722f 7531 363a  ....kworker/u16:
1689 00000490: 3600 0000 6401 0000 7800 0000 90a1 2900  6...d...x.....).
1690 000004a0: 2f00 0103 6401 0000 6b77 6f72 6b65 722f  /...d...kworker/
1691 000004b0: 7531 363a 3600 0000 6401 0000 7800 0000  u16:6...d...x...
1692 000004c0: 0200 0000 0000 0000 7377 6170 7065 722f  ........swapper/
1693 000004d0: 3000 0000 0000 0000 0000 0000 7800 0000  0...........x...
1694 000004e0: b0e5 4f04 2f00 0103 0000 0000 7377 6170  ..O./.......swap
1695 000004f0: 7065 722f 3000 0000 0000 0000 0000 0000  per/0...........
1696 00000500: 7800 0000 0000 0000 0000 0000 4269 6e64  x...........Bind
1697 00000510: 6572 3a32 3136 385f 3135 0000 e614 0000  er:2168_15......
1698 00000520: 7800 0000 b0bd 7c00 2f00 0103 e614 0000  x.....|./.......
1699 00000530: 4269 6e64 6572 3a32 3136 385f 3135 0000  Binder:2168_15..
1700 00000540: e614 0000 7800 0000 0100 0000 0000 0000  ....x...........
1701 00000550: 7377 6170 7065 722f 3000 0000 0000 0000  swapper/0.......
1702 00000560: 0000 0000 7800 0000 d0bd 7e01 2f00 0103  ....x.....~./...
1703 00000570: 0000 0000 7377 6170 7065 722f 3000 0000  ....swapper/0...
1704 00000580: 0000 0000 0000 0000 7800 0000 0000 0000  ........x.......
1705 00000590: 0000 0000 6b77 6f72 6b65 722f 7531 363a  ....kworker/u16:
1706 000005a0: 3900 0000 e204 0000 7800 0000 7016 0800  9.......x...p...
1707 000005b0: 2f00 0103 e204 0000 6b77 6f72 6b65 722f  /.......kworker/
1708 000005c0: 7531 363a 3900 0000 e204 0000 7800 0000  u16:9.......x...
1709 000005d0: 0100 0000 0000 0000 7377 6170 7065 722f  ........swapper/
1710 000005e0: 3000 0000 0000 0000 0000 0000 7800 0000  0...........x...
1711 000005f0: 1004 5200 2f00 0103 0000 0000 7377 6170  ..R./.......swap
1712 00000600: 7065 722f 3000 0000 0000 0000 0000 0000  per/0...........
1713 00000610: 7800 0000 0000 0000 0000 0000 6b77 6f72  x...........kwor
1714 00000620: 6b65 722f 7531 363a 3900 0000 e204 0000  ker/u16:9.......
1715 00000630: 7800 0000 d0db 0700 2f00 0103 e204 0000  x......./.......
1716 00000640: 6b77 6f72 6b65 722f 7531 363a 3900 0000  kworker/u16:9...
1717 00000650: e204 0000 7800 0000 0100 0000 0000 0000  ....x...........
1718 00000660: 7377 6170 7065 722f 3000 0000 0000 0000  swapper/0.......
1719 00000670: 0000 0000 7800 0000 b0a2 8c00 2f00 0103  ....x......./...
1720 00000680: 0000 0000 7377 6170 7065 722f 3000 0000  ....swapper/0...
1721 00000690: 0000 0000 0000 0000 7800 0000 0000 0000  ........x.......
1722 000006a0: 0000 0000 6b77 6f72 6b65 722f 7531 363a  ....kworker/u16:
1723 000006b0: 3900 0000 e204 0000 7800 0000 d02b 0400  9.......x....+..
1724 000006c0: 2f00 0103 e204 0000 6b77 6f72 6b65 722f  /.......kworker/
1725 000006d0: 7531 363a 3900 0000 e204 0000 7800 0000  u16:9.......x...
1726 000006e0: 0100 0000 0000 0000 7377 6170 7065 722f  ........swapper/
1727 000006f0: 3000 0000 0000 0000 0000 0000 7800 0000  0...........x...
1728 00000700: d064 ef05 2f00 0103 0000 0000 7377 6170  .d../.......swap
1729 00000710: 7065 722f 3000 0000 0000 0000 0000 0000  per/0...........
1730 00000720: 7800 0000 0000 0000 0000 0000 4469 7370  x...........Disp
1731 00000730: 5379 6e63 0069 6e67 6572 0000 6f02 0000  Sync.inger..o...
1732 00000740: 6100 0000 f07d 1b00 2f00 0103 6f02 0000  a....}../...o...
1733 00000750: 4469 7370 5379 6e63 0069 6e67 6572 0000  DispSync.inger..
1734 00000760: 6f02 0000 6100 0000 0100 0000 0000 0000  o...a...........
1735 00000770: 6b73 6f66 7469 7271 642f 3000 0000 0000  ksoftirqd/0.....
1736 00000780: 0300 0000 7800 0000 304c 2000 2f00 0103  ....x...0L ./...
1737 00000790: 0300 0000 6b73 6f66 7469 7271 642f 3000  ....ksoftirqd/0.
1738 000007a0: 0000 0000 0300 0000 7800 0000 0100 0000  ........x.......
1739 000007b0: 0000 0000 6465 7832 6f61 7400 3935 5f33  ....dex2oat.95_3
1740 000007c0: 0000 0000 341f 0000 8200 0000 700b 0700  ....4.......p...
1741 000007d0: 2f00 0103 341f 0000 6465 7832 6f61 7400  /...4...dex2oat.
1742 000007e0: 3935 5f33 0000 0000 341f 0000 8200 0000  95_3....4.......
1743 000007f0: 0000 0000 0000 0000 7375 676f 763a 3000  ........sugov:0.
1744 00000800: 0000 0000 0000 0000 3802 0000 3100 0000  ........8...1...
1745 00000810: 50b0 0600 2f00 0103 3802 0000 7375 676f  P.../...8...sugo
1746 00000820: 763a 3000 0000 0000 0000 0000 3802 0000  v:0.........8...
1747 00000830: 3100 0000 0008 0000 0000 0000 6d69 6772  1...........migr
1748 00000840: 6174 696f 6e2f 3000 0000 0000 0d00 0000  ation/0.........
1749 00000850: 0000 0000 d09c 0600 2f00 0103 0d00 0000  ......../.......
1750 00000860: 6d69 6772 6174 696f 6e2f 3000 0000 0000  migration/0.....
1751 00000870: 0d00 0000 0000 0000 0100 0000 0000 0000  ................
1752 00000880: 7375 676f 763a 3000 0000 0000 0000 0000  sugov:0.........
1753 00000890: 3802 0000 3100 0000 7061 1900 2f00 0103  8...1...pa../...
1754 000008a0: 3802 0000 7375 676f 763a 3000 0000 0000  8...sugov:0.....
1755 000008b0: 0000 0000 3802 0000 3100 0000 0100 0000  ....8...1.......
1756 000008c0: 0000 0000 6465 7832 6f61 7400 3935 5f33  ....dex2oat.95_3
1757 000008d0: 0000 0000 341f 0000 8200 0000 f03c 5600  ....4........<V.
1758 000008e0: 2f00 0103 341f 0000 6465 7832 6f61 7400  /...4...dex2oat.
1759 000008f0: 3935 5f33 0000 0000 341f 0000 8200 0000  95_3....4.......
1760 00000900: 0200 0000 0000 0000 7377 6170 7065 722f  ........swapper/
1761 00000910: 3000 0000 0000 0000 0000 0000 7800 0000  0...........x...
1762 00000920: 5013 c400 2f00 0103 0000 0000 7377 6170  P.../.......swap
1763 00000930: 7065 722f 3000 0000 0000 0000 0000 0000  per/0...........
1764 00000940: 7800 0000 0000 0000 0000 0000 616e 6472  x...........andr
1765 00000950: 6f69 642e 6861 7264 7761 7200 d20a 0000  oid.hardwar.....
1766 00000960: 7800 0000 30c9 1300 2f00 0103 d20a 0000  x...0.../.......
1767 00000970: 616e 6472 6f69 642e 6861 7264 7761 7200  android.hardwar.
1768 00000980: d20a 0000 7800 0000 0100 0000 0000 0000  ....x...........
1769 00000990: 7377 6170 7065 722f 3000 0000 0000 0000  swapper/0.......
1770 000009a0: 0000 0000 7800 0000 7097 c000 2f00 0103  ....x...p.../...
1771 000009b0: 0000 0000 7377 6170 7065 722f 3000 0000  ....swapper/0...
1772 000009c0: 0000 0000 0000 0000 7800 0000 0000 0000  ........x.......
1773 000009d0: 0000 0000 616e 6472 6f69 642e 6861 7264  ....android.hard
1774 000009e0: 7761 7200 d20a 0000 7800 0000 305c 0c00  war.....x...0\..
1775 000009f0: 2f00 0103 d20a 0000 616e 6472 6f69 642e  /.......android.
1776 00000a00: 6861 7264 7761 7200 d20a 0000 7800 0000  hardwar.....x...
1777 00000a10: 0100 0000 0000 0000 7377 6170 7065 722f  ........swapper/
1778 00000a20: 3000 0000 0000 0000 0000 0000 7800 0000  0...........x...
1779 00000a30: d0aa 1401 2f00 0103 0000 0000 7377 6170  ..../.......swap
1780 00000a40: 7065 722f 3000 0000 0000 0000 0000 0000  per/0...........
1781 00000a50: 7800 0000 0000 0000 0000 0000 616e 6472  x...........andr
1782 00000a60: 6f69 642e 6861 7264 7761 7200 d20a 0000  oid.hardwar.....
1783 00000a70: 7800 0000 903b 0c00 2f00 0103 d20a 0000  x....;../.......
1784 00000a80: 616e 6472 6f69 642e 6861 7264 7761 7200  android.hardwar.
1785 00000a90: d20a 0000 7800 0000 0100 0000 0000 0000  ....x...........
1786 00000aa0: 7377 6170 7065 722f 3000 0000 0000 0000  swapper/0.......
1787 00000ab0: 0000 0000 7800 0000 f024 5401 2f00 0103  ....x....$T./...
1788 00000ac0: 0000 0000 7377 6170 7065 722f 3000 0000  ....swapper/0...
1789 00000ad0: 0000 0000 0000 0000 7800 0000 0000 0000  ........x.......
1790 00000ae0: 0000 0000 616e 6472 6f69 642e 6861 7264  ....android.hard
1791 00000af0: 7761 7200 d20a 0000 7800 0000 f0f3 0b00  war.....x.......
1792 00000b00: 2f00 0103 d20a 0000 616e 6472 6f69 642e  /.......android.
1793 00000b10: 6861 7264 7761 7200 d20a 0000 7800 0000  hardwar.....x...
1794 00000b20: 0100 0000 0000 0000 7377 6170 7065 722f  ........swapper/
1795 00000b30: 3000 0000 0000 0000 0000 0000 7800 0000  0...........x...
1796 00000b40: d0b5 bf02 2f00 0103 0000 0000 7377 6170  ..../.......swap
1797 00000b50: 7065 722f 3000 0000 0000 0000 0000 0000  per/0...........
1798 00000b60: 7800 0000 0000 0000 0000 0000 4469 7370  x...........Disp
1799 00000b70: 5379 6e63 0069 6e67 6572 0000 6f02 0000  Sync.inger..o...
1800 00000b80: 6100 0000 90cd 1400 2f00 0103 6f02 0000  a......./...o...
1801 00000b90: 4469 7370 5379 6e63 0069 6e67 6572 0000  DispSync.inger..
1802 00000ba0: 6f02 0000 6100 0000 0100 0000 0000 0000  o...a...........
1803 00000bb0: 7377 6170 7065 722f 3000 0000 0000 0000  swapper/0.......
1804 00000bc0: 0000 0000 7800 0000 50a6 1100 2f00 0103  ....x...P.../...
1805 00000bd0: 0000 0000 7377 6170 7065 722f 3000 0000  ....swapper/0...
1806 00000be0: 0000 0000 0000 0000 7800 0000 0000 0000  ........x.......
1807 00000bf0: 0000 0000 7375 7266 6163 6566 6c69 6e67  ....surfacefling
1808 00000c00: 6572 0000 4b02 0000 6200 0000 b04c 4200  er..K...b....LB.
1809 00000c10: 2f00 0103 4b02 0000 7375 7266 6163 6566  /...K...surfacef
1810 00000c20: 6c69 6e67 6572 0000 4b02 0000 6200 0000  linger..K...b...
1811 00000c30: 0100 0000 0000 0000 7377 6170 7065 722f  ........swapper/
1812 00000c40: 3000 0000 0000 0000 0000 0000 7800 0000  0...........x...
1813 00000c50: b025 060a 2f00 0103 0000 0000 7377 6170  .%../.......swap
1814 00000c60: 7065 722f 3000 0000 0000 0000 0000 0000  per/0...........
1815 00000c70: 7800 0000 0000 0000 0000 0000 6b77 6f72  x...........kwor
1816 00000c80: 6b65 722f 7531 363a 3600 0000 6401 0000  ker/u16:6...d...
1817 00000c90: 7800 0000 d0b6 0600 2f00 0103 6401 0000  x......./...d...
1818 00000ca0: 6b77 6f72 6b65 722f 7531 363a 3600 0000  kworker/u16:6...
1819 00000cb0: 6401 0000 7800 0000 0100 0000 0000 0000  d...x...........
1820 00000cc0: 7377 6170 7065 722f 3000 0000 0000 0000  swapper/0.......
1821 00000cd0: 0000 0000 7800 0000 f0a0 5800 2f00 0103  ....x.....X./...
1822 00000ce0: 0000 0000 7377 6170 7065 722f 3000 0000  ....swapper/0...
1823 00000cf0: 0000 0000 0000 0000 7800 0000 0000 0000  ........x.......
1824 00000d00: 0000 0000 6b77 6f72 6b65 722f 7531 363a  ....kworker/u16:
1825 00000d10: 3600 0000 6401 0000 7800 0000 f07a 1300  6...d...x....z..
1826 00000d20: 2f00 0103 6401 0000 6b77 6f72 6b65 722f  /...d...kworker/
1827 00000d30: 7531 363a 3600 0000 6401 0000 7800 0000  u16:6...d...x...
1828 00000d40: 0100 0000 0000 0000 7377 6170 7065 722f  ........swapper/
1829 00000d50: 3000 0000 0000 0000 0000 0000 7800 0000  0...........x...
1830 00000d60: b080 b101 2f00 0103 0000 0000 7377 6170  ..../.......swap
1831 00000d70: 7065 722f 3000 0000 0000 0000 0000 0000  per/0...........
1832 00000d80: 7800 0000 0000 0000 0000 0000 6b77 6f72  x...........kwor
1833 00000d90: 6b65 722f 7531 363a 3600 0000 6401 0000  ker/u16:6...d...
1834 00000da0: 7800 0000 103c 1200 2f00 0103 6401 0000  x....<../...d...
1835 00000db0: 6b77 6f72 6b65 722f 7531 363a 3600 0000  kworker/u16:6...
1836 00000dc0: 6401 0000 7800 0000 0100 0000 0000 0000  d...x...........
1837 00000dd0: 7377 6170 7065 722f 3000 0000 0000 0000  swapper/0.......
1838 00000de0: 0000 0000 7800 0000 50ea 3800 2f00 0103  ....x...P.8./...
1839 00000df0: 0000 0000 7377 6170 7065 722f 3000 0000  ....swapper/0...
1840 00000e00: 0000 0000 0000 0000 7800 0000 0000 0000  ........x.......
1841 00000e10: 0000 0000 6b77 6f72 6b65 722f 7531 363a  ....kworker/u16:
1842 00000e20: 3600 0000 6401 0000 7800 0000 5032 0400  6...d...x...P2..
1843 00000e30: 2f00 0103 6401 0000 6b77 6f72 6b65 722f  /...d...kworker/
1844 00000e40: 7531 363a 3600 0000 6401 0000 7800 0000  u16:6...d...x...
1845 00000e50: 0100 0000 0000 0000 7377 6170 7065 722f  ........swapper/
1846 00000e60: 3000 0000 0000 0000 0000 0000 7800 0000  0...........x...
1847 00000e70: 70f5 9000 2f00 0103 0000 0000 7377 6170  p.../.......swap
1848 00000e80: 7065 722f 3000 0000 0000 0000 0000 0000  per/0...........
1849 00000e90: 7800 0000 0000 0000 0000 0000 6b77 6f72  x...........kwor
1850 00000ea0: 6b65 722f 7531 363a 3600 0000 6401 0000  ker/u16:6...d...
1851 00000eb0: 7800 0000 10d7 0300 2f00 0103 6401 0000  x......./...d...
1852 00000ec0: 6b77 6f72 6b65 722f 7531 363a 3600 0000  kworker/u16:6...
1853 00000ed0: 6401 0000 7800 0000 0100 0000 0000 0000  d...x...........
1854 00000ee0: 7377 6170 7065 722f 3000 0000 0000 0000  swapper/0.......
1855 00000ef0: 0000 0000 7800 0000 907c 0900 2f00 0103  ....x....|../...
1856 00000f00: 0000 0000 7377 6170 7065 722f 3000 0000  ....swapper/0...
1857 00000f10: 0000 0000 0000 0000 7800 0000 0000 0000  ........x.......
1858 00000f20: 0000 0000 6b77 6f72 6b65 722f 7531 363a  ....kworker/u16:
1859 00000f30: 3600 0000 6401 0000 7800 0000 7082 0300  6...d...x...p...
1860 00000f40: 2f00 0103 6401 0000 6b77 6f72 6b65 722f  /...d...kworker/
1861 00000f50: 7531 363a 3600 0000 6401 0000 7800 0000  u16:6...d...x...
1862 00000f60: 0100 0000 0000 0000 7377 6170 7065 722f  ........swapper/
1863 00000f70: 3000 0000 0000 0000 0000 0000 7800 0000  0...........x...
1864 00000f80: f0ec 2100 2f00 0103 0000 0000 7377 6170  ..!./.......swap
1865 00000f90: 7065 722f 3000 0000 0000 0000 0000 0000  per/0...........
1866 00000fa0: 7800 0000 0000 0000 0000 0000 6b77 6f72  x...........kwor
1867 00000fb0: 6b65 722f 7531 363a 3600 0000 6401 0000  ker/u16:6...d...
1868 00000fc0: 7800 0000 0000 0000 0000 0000 0000 0000  x...............
1869 00000fd0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
1870 00000fe0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
1871 00000ff0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
1872     )",
1873 };
1874 
TEST(CpuReaderTest,ParseFullPageSchedSwitch)1875 TEST(CpuReaderTest, ParseFullPageSchedSwitch) {
1876   const ExamplePage* test_case = &g_full_page_sched_switch;
1877 
1878   BundleProvider bundle_provider(base::kPageSize);
1879   ProtoTranslationTable* table = GetTable(test_case->name);
1880   auto page = PageFromXxd(test_case->data);
1881 
1882   FtraceDataSourceConfig ds_config = EmptyConfig();
1883   ds_config.event_filter.AddEnabledEvent(
1884       table->EventToFtraceId(GroupAndName("sched", "sched_switch")));
1885 
1886   FtraceMetadata metadata{};
1887   CompactSchedBuffer compact_buffer;
1888   const uint8_t* parse_pos = page.get();
1889   base::Optional<CpuReader::PageHeader> page_header =
1890       CpuReader::ParsePageHeader(&parse_pos, table->page_header_size_len());
1891 
1892   const uint8_t* page_end = page.get() + base::kPageSize;
1893   ASSERT_TRUE(page_header.has_value());
1894   EXPECT_FALSE(page_header->lost_events);
1895   EXPECT_TRUE(parse_pos < page_end);
1896   EXPECT_TRUE(parse_pos + page_header->size < page_end);
1897 
1898   size_t evt_bytes = CpuReader::ParsePagePayload(
1899       parse_pos, &page_header.value(), table, &ds_config, &compact_buffer,
1900       bundle_provider.writer(), &metadata);
1901 
1902   EXPECT_LT(0u, evt_bytes);
1903 
1904   auto bundle = bundle_provider.ParseProto();
1905   ASSERT_TRUE(bundle);
1906   EXPECT_EQ(bundle->event().size(), 59u);
1907 }
1908 
1909 // clang-format off
1910 // # tracer: nop
1911 // #
1912 // # entries-in-buffer/entries-written: 18/18   #P:8
1913 // #
1914 // #                              _-----=> irqs-off
1915 // #                             / _----=> need-resched
1916 // #                            | / _---=> hardirq/softirq
1917 // #                            || / _--=> preempt-depth
1918 // #                            ||| /     delay
1919 // #           TASK-PID   CPU#  ||||    TIMESTAMP  FUNCTION
1920 // #              | |       |   ||||       |         |
1921 //            <...>-9290  [000] ....  1352.654573: suspend_resume: sync_filesystems[0] end
1922 //            <...>-9290  [000] ....  1352.665366: suspend_resume: freeze_processes[0] begin
1923 //            <...>-9290  [000] ....  1352.699711: suspend_resume: freeze_processes[0] end
1924 //            <...>-9290  [000] ....  1352.699718: suspend_resume: suspend_enter[1] end
1925 //            <...>-9290  [000] ....  1352.699723: suspend_resume: dpm_prepare[2] begin
1926 //            <...>-9290  [000] ....  1352.703470: suspend_resume: dpm_prepare[2] end
1927 //            <...>-9290  [000] ....  1352.703477: suspend_resume: dpm_suspend[2] begin
1928 //            <...>-9290  [000] ....  1352.720107: suspend_resume: dpm_resume[16] end
1929 //            <...>-9290  [000] ....  1352.720113: suspend_resume: dpm_complete[16] begin
1930 //            <...>-9290  [000] .n..  1352.724540: suspend_resume: dpm_complete[16] end
1931 //            <...>-9290  [000] ....  1352.724567: suspend_resume: resume_console[1] begin
1932 //            <...>-9290  [000] ....  1352.724570: suspend_resume: resume_console[1] end
1933 //            <...>-9290  [000] ....  1352.724574: suspend_resume: thaw_processes[0] begin
1934 static ExamplePage g_suspend_resume {
1935     "synthetic",
1936     R"(00000000: edba 155a 3201 0000 7401 0000 0000 0000  ...Z2...t.......
1937 00000010: 7e58 22cd 1201 0000 0600 0000 ac00 0000  ~X".............
1938 00000020: 4a24 0000 5a7a f504 85ff ffff 0000 0000  J$..Zz..........
1939 00000030: 0017 0000 c621 9614 ac00 0000 4a24 0000  .....!......J$..
1940 00000040: 1c7a f504 85ff ffff 0000 0000 0100 0000  .z..............
1941 00000050: e6f1 8141 ac00 0000 4a24 0000 1c7a f504  ...A....J$...z..
1942 00000060: 85ff ffff 0000 0000 0000 0000 8682 0300  ................
1943 00000070: ac00 0000 4a24 0000 4c7a f504 85ff ffff  ....J$..Lz......
1944 00000080: 0100 0000 0063 755f 0657 0200 ac00 0000  .....cu_.W......
1945 00000090: 4a24 0000 8ad5 0105 85ff ffff 0200 0000  J$..............
1946 000000a0: 0100 0000 06b5 2507 ac00 0000 4a24 0000  ......%.....J$..
1947 000000b0: 8ad5 0105 85ff ffff 0200 0000 0000 0000  ................
1948 000000c0: 460d 0300 ac00 0000 4a24 0000 51d5 0105  F.......J$..Q...
1949 000000d0: 85ff ffff 0200 0000 0117 0000 c63e b81f  .............>..
1950 000000e0: ac00 0000 4a24 0000 7fd5 0105 85ff ffff  ....J$..........
1951 000000f0: 1000 0000 0010 0b00 a6f9 0200 ac00 0000  ................
1952 00000100: 4a24 0000 96d5 0105 85ff ffff 1000 0000  J$..............
1953 00000110: 01c0 1f00 a6dd 7108 ac00 0400 4a24 0000  ......q.....J$..
1954 00000120: 96d5 0105 85ff ffff 1000 0000 0000 0000  ................
1955 00000130: c6f1 0c00 ac00 0000 4a24 0000 3d7a f504  ........J$..=z..
1956 00000140: 85ff ffff 0100 0000 01ea 24d5 a66c 0100  ..........$..l..
1957 00000150: ac00 0000 4a24 0000 3d7a f504 85ff ffff  ....J$..=z......
1958 00000160: 0100 0000 0000 0001 6636 0200 ac00 0000  ........f6......
1959 00000170: 4a24 0000 d178 f504 85ff ffff 0000 0000  J$...x..........
1960 00000180: 0100 0000 0000 0000 0000 0000 0000 0000  ................
1961 00000190: 0000 0000 0000 0000 0000 0000 0000 0000  ................
1962 )"};
1963 
TEST(CpuReaderTest,ParseSuspendResume)1964 TEST(CpuReaderTest, ParseSuspendResume) {
1965   const ExamplePage* test_case = &g_suspend_resume;
1966 
1967   BundleProvider bundle_provider(base::kPageSize);
1968   ProtoTranslationTable* table = GetTable(test_case->name);
1969   auto page = PageFromXxd(test_case->data);
1970 
1971   FtraceDataSourceConfig ds_config = EmptyConfig();
1972   ds_config.event_filter.AddEnabledEvent(
1973       table->EventToFtraceId(GroupAndName("power", "suspend_resume")));
1974 
1975   FtraceMetadata metadata{};
1976   CompactSchedBuffer compact_buffer;
1977   const uint8_t* parse_pos = page.get();
1978   base::Optional<CpuReader::PageHeader> page_header =
1979       CpuReader::ParsePageHeader(&parse_pos, table->page_header_size_len());
1980   ASSERT_TRUE(page_header.has_value());
1981 
1982   CpuReader::ParsePagePayload(
1983       parse_pos, &page_header.value(), table, &ds_config, &compact_buffer,
1984       bundle_provider.writer(), &metadata);
1985   auto bundle = bundle_provider.ParseProto();
1986   ASSERT_TRUE(bundle);
1987   ASSERT_EQ(bundle->event().size(), 13u);
1988   EXPECT_EQ(bundle->event()[0].suspend_resume().action(), "sync_filesystems");
1989   EXPECT_EQ(bundle->event()[1].suspend_resume().action(), "freeze_processes");
1990   EXPECT_EQ(bundle->event()[2].suspend_resume().action(), "freeze_processes");
1991   EXPECT_EQ(bundle->event()[3].suspend_resume().action(), "suspend_enter");
1992   // dpm_prepare deliberately missing from:
1993   // src/traced/probes/ftrace/test/data/synthetic/printk_formats to ensure we
1994   // handle that case correctly.
1995   EXPECT_EQ(bundle->event()[4].suspend_resume().action(), "");
1996 }
1997 
1998 // clang-format off
1999 // # tracer: nop
2000 // #
2001 // # entries-in-buffer/entries-written: 1041/238740   #P:8
2002 // #
2003 // #                              _-----=> irqs-off
2004 // #                             / _----=> need-resched
2005 // #                            | / _---=> hardirq/softirq
2006 // #                            || / _--=> preempt-depth
2007 // #                            ||| /     delay
2008 // #           TASK-PID   CPU#  ||||    TIMESTAMP  FUNCTION
2009 // #              | |       |   ||||       |         |
2010 //       android.bg-1668  [000] ...1 174991.234105: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2011 //       android.bg-1668  [000] ...1 174991.234108: ext4_mark_inode_dirty: dev 259,32 ino 2883605 caller ext4_dirty_inode+0x48/0x68
2012 //       android.bg-1668  [000] ...1 174991.234118: ext4_da_write_begin: dev 259,32 ino 2883605 pos 20480 len 4096 flags 0
2013 //       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
2014 //       android.bg-1668  [000] ...1 174991.234133: ext4_es_lookup_extent_enter: dev 259,32 ino 2883605 lblk 5
2015 //       android.bg-1668  [000] ...1 174991.234135: ext4_es_lookup_extent_exit: dev 259,32 ino 2883605 found 1 [5/4294967290) 576460752303423487 H0x10
2016 //       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
2017 //       android.bg-1668  [000] ...1 174991.234142: ext4_es_insert_extent: dev 259,32 ino 2883605 es [5/1) mapped 576460752303423487 status D
2018 //       android.bg-1668  [000] ...1 174991.234153: ext4_da_write_end: dev 259,32 ino 2883605 pos 20480 len 4096 copied 4096
2019 //       android.bg-1668  [000] ...1 174991.234158: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2020 //       android.bg-1668  [000] ...1 174991.234160: ext4_mark_inode_dirty: dev 259,32 ino 2883605 caller ext4_dirty_inode+0x48/0x68
2021 //       android.bg-1668  [000] ...1 174991.234170: ext4_da_write_begin: dev 259,32 ino 2883605 pos 24576 len 2968 flags 0
2022 //       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
2023 //       android.bg-1668  [000] ...1 174991.234184: ext4_es_lookup_extent_enter: dev 259,32 ino 2883605 lblk 6
2024 //       android.bg-1668  [000] ...1 174991.234187: ext4_es_lookup_extent_exit: dev 259,32 ino 2883605 found 1 [6/4294967289) 576460752303423487 H0x10
2025 //       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
2026 //       android.bg-1668  [000] ...1 174991.234193: ext4_es_insert_extent: dev 259,32 ino 2883605 es [6/1) mapped 576460752303423487 status D
2027 //       android.bg-1668  [000] ...1 174991.234203: ext4_da_write_end: dev 259,32 ino 2883605 pos 24576 len 2968 copied 2968
2028 //       android.bg-1668  [000] ...1 174991.234209: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2029 //       android.bg-1668  [000] ...1 174991.234211: ext4_mark_inode_dirty: dev 259,32 ino 2883605 caller ext4_dirty_inode+0x48/0x68
2030 //       android.bg-1668  [000] ...1 174991.234262: ext4_sync_file_enter: dev 259,32 ino 2883605 parent 2883592 datasync 0
2031 //       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
2032 //       android.bg-1668  [000] ...1 174991.234287: ext4_journal_start: dev 259,32 blocks, 10 rsv_blocks, 0 caller ext4_writepages+0x6a4/0x119c
2033 //       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
2034 //       android.bg-1668  [000] ...1 174991.234319: ext4_da_write_pages_extent: dev 259,32 ino 2883605 lblk 0 len 7 flags 0x200
2035 //       android.bg-1668  [000] ...1 174991.234322: ext4_es_lookup_extent_enter: dev 259,32 ino 2883605 lblk 0
2036 //       android.bg-1668  [000] ...1 174991.234324: ext4_es_lookup_extent_exit: dev 259,32 ino 2883605 found 1 [0/7) 576460752303423487 D0x10
2037 //       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
2038 //       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
2039 //       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
2040 //       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
2041 //       android.bg-1668  [000] ...1 174991.234409: ext4_mark_inode_dirty: dev 259,32 ino 2883605 caller __ext4_ext_dirty+0x104/0x170
2042 //       android.bg-1668  [000] ...1 174991.234420: ext4_get_reserved_cluster_alloc: dev 259,32 ino 2883605 lblk 0 len 7
2043 //       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
2044 //       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
2045 //       android.bg-1668  [000] ...1 174991.234441: ext4_es_lookup_extent_enter: dev 259,32 ino 3 lblk 1
2046 //       android.bg-1668  [000] ...1 174991.234445: ext4_es_lookup_extent_exit: dev 259,32 ino 3 found 1 [0/2) 9255 W0x10
2047 //       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
2048 //       android.bg-1668  [000] ...1 174991.234460: ext4_es_lookup_extent_enter: dev 259,32 ino 4 lblk 1
2049 //       android.bg-1668  [000] ...1 174991.234463: ext4_es_lookup_extent_exit: dev 259,32 ino 4 found 1 [0/2) 9257 W0x10
2050 //       android.bg-1668  [000] ...1 174991.234471: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2051 //       android.bg-1668  [000] ...1 174991.234474: ext4_mark_inode_dirty: dev 259,32 ino 2883605 caller ext4_dirty_inode+0x48/0x68
2052 //       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
2053 //       android.bg-1668  [000] ...1 174991.234484: ext4_es_insert_extent: dev 259,32 ino 2883605 es [0/7) mapped 2155471 status W
2054 //       android.bg-1668  [000] ...1 174991.234547: ext4_mark_inode_dirty: dev 259,32 ino 2883605 caller ext4_writepages+0xdc0/0x119c
2055 //       android.bg-1668  [000] ...1 174991.234604: ext4_journal_start: dev 259,32 blocks, 10 rsv_blocks, 0 caller ext4_writepages+0x6a4/0x119c
2056 //       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
2057 //       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
2058 //    Profile Saver-5504  [000] ...1 175002.711928: ext4_discard_preallocations: dev 259,32 ino 1311176
2059 //    Profile Saver-5504  [000] ...1 175002.714165: ext4_begin_ordered_truncate: dev 259,32 ino 1311176 new_size 0
2060 //    Profile Saver-5504  [000] ...1 175002.714172: ext4_journal_start: dev 259,32 blocks, 3 rsv_blocks, 0 caller ext4_setattr+0x5b4/0x788
2061 //    Profile Saver-5504  [000] ...1 175002.714218: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_setattr+0x65c/0x788
2062 //    Profile Saver-5504  [000] ...1 175002.714277: ext4_invalidatepage: dev 259,32 ino 1311176 page_index 0 offset 0 length 4096
2063 //    Profile Saver-5504  [000] ...1 175002.714281: ext4_releasepage: dev 259,32 ino 1311176 page_index 0
2064 //    Profile Saver-5504  [000] ...1 175002.714295: ext4_invalidatepage: dev 259,32 ino 1311176 page_index 1 offset 0 length 4096
2065 //    Profile Saver-5504  [000] ...1 175002.714296: ext4_releasepage: dev 259,32 ino 1311176 page_index 1
2066 //    Profile Saver-5504  [000] ...1 175002.714315: ext4_truncate_enter: dev 259,32 ino 1311176 blocks 24
2067 //    Profile Saver-5504  [000] ...1 175002.714318: ext4_journal_start: dev 259,32 blocks, 10 rsv_blocks, 0 caller ext4_truncate+0x258/0x4b8
2068 //    Profile Saver-5504  [000] ...1 175002.714322: ext4_discard_preallocations: dev 259,32 ino 1311176
2069 //    Profile Saver-5504  [000] ...1 175002.714324: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_ext_truncate+0x24/0xc8
2070 //    Profile Saver-5504  [000] ...1 175002.714328: ext4_es_remove_extent: dev 259,32 ino 1311176 es [0/4294967295)
2071 //    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
2072 //    Profile Saver-5504  [000] ...1 175002.714338: ext4_ext_remove_space: dev 259,32 ino 1311176 since 0 end 4294967294 depth 0
2073 //    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
2074 //    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
2075 //    Profile Saver-5504  [000] ...1 175002.714354: ext4_free_blocks: dev 259,32 ino 1311176 mode 0100600 block 5276994 count 2 flags 1ST_CLUSTER
2076 //    Profile Saver-5504  [000] ...1 175002.714365: ext4_mballoc_free: dev 259,32 inode 1311176 extent 161/1346/2
2077 //    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
2078 //    Profile Saver-5504  [000] ...1 175002.714391: ext4_es_lookup_extent_enter: dev 259,32 ino 3 lblk 4
2079 //    Profile Saver-5504  [000] ...1 175002.714394: ext4_es_lookup_extent_exit: dev 259,32 ino 3 found 1 [4/1) 557094 W0x10
2080 //    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
2081 //    Profile Saver-5504  [000] ...1 175002.714404: ext4_es_lookup_extent_enter: dev 259,32 ino 4 lblk 8
2082 //    Profile Saver-5504  [000] ...1 175002.714406: ext4_es_lookup_extent_exit: dev 259,32 ino 4 found 1 [8/3) 7376914 W0x10
2083 //    Profile Saver-5504  [000] ...1 175002.714413: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2084 //    Profile Saver-5504  [000] ...1 175002.714414: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_dirty_inode+0x48/0x68
2085 //    Profile Saver-5504  [000] ...1 175002.714420: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller __ext4_ext_dirty+0x104/0x170
2086 //    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
2087 //    Profile Saver-5504  [000] ...1 175002.714425: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller __ext4_ext_dirty+0x104/0x170
2088 //    Profile Saver-5504  [000] ...1 175002.714433: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_truncate+0x3c4/0x4b8
2089 //    Profile Saver-5504  [000] ...1 175002.714436: ext4_truncate_exit: dev 259,32 ino 1311176 blocks 8
2090 //    Profile Saver-5504  [000] ...1 175002.714437: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2091 //    Profile Saver-5504  [000] ...1 175002.714438: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_dirty_inode+0x48/0x68
2092 //    Profile Saver-5504  [000] ...1 175002.714462: ext4_da_write_begin: dev 259,32 ino 1311176 pos 0 len 4 flags 0
2093 //    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
2094 //    Profile Saver-5504  [000] ...1 175002.714477: ext4_es_lookup_extent_enter: dev 259,32 ino 1311176 lblk 0
2095 //    Profile Saver-5504  [000] ...1 175002.714477: ext4_es_lookup_extent_exit: dev 259,32 ino 1311176 found 0 [0/0) 0
2096 //    Profile Saver-5504  [000] ...1 175002.714480: ext4_ext_map_blocks_enter: dev 259,32 ino 1311176 lblk 0 len 1 flags
2097 //    Profile Saver-5504  [000] ...1 175002.714485: ext4_es_find_delayed_extent_range_enter: dev 259,32 ino 1311176 lblk 0
2098 //    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
2099 //    Profile Saver-5504  [000] ...1 175002.714490: ext4_es_insert_extent: dev 259,32 ino 1311176 es [0/4294967295) mapped 576460752303423487 status H
2100 //    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
2101 //    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
2102 //    Profile Saver-5504  [000] ...1 175002.714505: ext4_es_insert_extent: dev 259,32 ino 1311176 es [0/1) mapped 576460752303423487 status D
2103 //    Profile Saver-5504  [000] ...1 175002.714513: ext4_da_write_end: dev 259,32 ino 1311176 pos 0 len 4 copied 4
2104 //    Profile Saver-5504  [000] ...1 175002.714519: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2105 //    Profile Saver-5504  [000] ...1 175002.714520: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_dirty_inode+0x48/0x68
2106 //    Profile Saver-5504  [000] ...1 175002.714527: ext4_da_write_begin: dev 259,32 ino 1311176 pos 4 len 4 flags 0
2107 //    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
2108 //    Profile Saver-5504  [000] ...1 175002.714531: ext4_da_write_end: dev 259,32 ino 1311176 pos 4 len 4 copied 4
2109 //    Profile Saver-5504  [000] ...1 175002.714532: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2110 //    Profile Saver-5504  [000] ...1 175002.714532: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_dirty_inode+0x48/0x68
2111 //    Profile Saver-5504  [000] ...1 175002.715313: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2112 //    Profile Saver-5504  [000] ...1 175002.715322: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_dirty_inode+0x48/0x68
2113 //    Profile Saver-5504  [000] ...1 175002.723849: ext4_da_write_begin: dev 259,32 ino 1311176 pos 8 len 5 flags 0
2114 //    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
2115 //    Profile Saver-5504  [000] ...1 175002.723873: ext4_da_write_end: dev 259,32 ino 1311176 pos 8 len 5 copied 5
2116 //    Profile Saver-5504  [000] ...1 175002.723877: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2117 //    Profile Saver-5504  [000] ...1 175002.723879: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_dirty_inode+0x48/0x68
2118 //    Profile Saver-5504  [000] ...1 175002.726857: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2119 //    Profile Saver-5504  [000] ...1 175002.726867: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_dirty_inode+0x48/0x68
2120 //    Profile Saver-5504  [000] ...1 175002.726881: ext4_da_write_begin: dev 259,32 ino 1311176 pos 13 len 4 flags 0
2121 //    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
2122 //    Profile Saver-5504  [000] ...1 175002.726890: ext4_da_write_end: dev 259,32 ino 1311176 pos 13 len 4 copied 4
2123 //    Profile Saver-5504  [000] ...1 175002.726892: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2124 //    Profile Saver-5504  [000] ...1 175002.726892: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_dirty_inode+0x48/0x68
2125 //    Profile Saver-5504  [000] ...1 175002.726900: ext4_da_write_begin: dev 259,32 ino 1311176 pos 17 len 4079 flags 0
2126 //    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
2127 //    Profile Saver-5504  [000] ...1 175002.726904: ext4_da_write_end: dev 259,32 ino 1311176 pos 17 len 4079 copied 4079
2128 //    Profile Saver-5504  [000] ...1 175002.726905: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2129 //    Profile Saver-5504  [000] ...1 175002.726906: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_dirty_inode+0x48/0x68
2130 //    Profile Saver-5504  [000] ...1 175002.726908: ext4_da_write_begin: dev 259,32 ino 1311176 pos 4096 len 2780 flags 0
2131 //    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
2132 //    Profile Saver-5504  [000] ...1 175002.726921: ext4_es_lookup_extent_enter: dev 259,32 ino 1311176 lblk 1
2133 //    Profile Saver-5504  [000] ...1 175002.726924: ext4_es_lookup_extent_exit: dev 259,32 ino 1311176 found 1 [1/4294967294) 576460752303423487 H0x10
2134 //    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
2135 //    Profile Saver-5504  [000] ...1 175002.726933: ext4_es_insert_extent: dev 259,32 ino 1311176 es [1/1) mapped 576460752303423487 status D
2136 //    Profile Saver-5504  [000] ...1 175002.726940: ext4_da_write_end: dev 259,32 ino 1311176 pos 4096 len 2780 copied 2780
2137 //    Profile Saver-5504  [000] ...1 175002.726941: ext4_journal_start: dev 259,32 blocks, 2 rsv_blocks, 0 caller ext4_dirty_inode+0x30/0x68
2138 //    Profile Saver-5504  [000] ...1 175002.726942: ext4_mark_inode_dirty: dev 259,32 ino 1311176 caller ext4_dirty_inode+0x48/0x68
2139 //   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
2140 //   d.process.acor-27885 [000] ...1 175018.227699: ext4_mark_inode_dirty: dev 259,32 ino 3278189 caller ext4_dirty_inode+0x48/0x68
2141 //   d.process.acor-27885 [000] ...1 175018.227839: ext4_sync_file_enter: dev 259,32 ino 3278183 parent 3277001 datasync 1
2142 //   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
2143 //   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
2144 // clang-format on
2145 
2146 static ExamplePage g_full_page_ext4{
2147     "synthetic",
2148     R"(
2149 00000000: 50fe 5852 279f 0000 c80f 00c0 ffff ffff  P.XR'...........
2150 00000010: 0800 0000 5701 0001 8406 0000 2000 3010  ....W....... .0.
2151 00000020: 566b 0000 8829 e86a 91ff ffff 0200 0000  Vk...).j........
2152 00000030: 0000 0000 2873 0100 1b01 0001 8406 0000  ....(s..........
2153 00000040: 2000 3010 9200 0000 1500 2c00 0000 0000   .0.......,.....
2154 00000050: a029 e86a 91ff ffff 0ac8 0400 1e01 0001  .).j............
2155 00000060: 8406 0000 2000 3010 2866 0100 1500 2c00  .... .0.(f....,.
2156 00000070: 0000 0000 0050 0000 0000 0000 0010 0000  .....P..........
2157 00000080: 0000 0000 a804 0400 5701 0001 8406 0000  ........W.......
2158 00000090: 2000 3010 91ff ffff 586f e86a 91ff ffff   .0.....Xo.j....
2159 000000a0: 0100 0000 0000 0000 c83a 0300 6c01 0001  .........:..l...
2160 000000b0: 8406 0000 2000 3010 0000 0000 1500 2c00  .... .0.......,.
2161 000000c0: 0000 0000 0500 0000 5701 0001 ac6c 0100  ........W....l..
2162 000000d0: 6d01 0001 8406 0000 2000 3010 91ff ffff  m....... .0.....
2163 000000e0: 1500 2c00 0000 0000 0500 0000 faff ffff  ..,.............
2164 000000f0: ffff ffff ffff ff07 184e 0000 0100 0000  .........N......
2165 00000100: ec08 0200 3f01 0002 8406 0000 2000 3010  ....?....... .0.
2166 00000110: 0000 0000 1500 2c00 0000 0000 0800 0000  ......,.........
2167 00000120: 0000 0000 0600 0000 0000 0000 8081 0000  ................
2168 00000130: 0000 0000 ec24 0100 6701 0001 8406 0000  .....$..g.......
2169 00000140: 2000 3010 0000 0000 1500 2c00 0000 0000   .0.......,.....
2170 00000150: 0500 0000 0100 0000 ffff ffff ffff ff07  ................
2171 00000160: 0400 0000 7b04 3200 2a30 0500 2101 0001  ....{.2.*0..!...
2172 00000170: 8406 0000 2000 3010 0000 0000 1500 2c00  .... .0.......,.
2173 00000180: 0000 0000 0050 0000 0000 0000 0010 0000  .....P..........
2174 00000190: 0010 0000 288b 0200 5701 0001 8406 0000  ....(...W.......
2175 000001a0: 2000 3010 0000 0000 8829 e86a 91ff ffff   .0......).j....
2176 000001b0: 0200 0000 0000 0000 0832 0100 1b01 0001  .........2......
2177 000001c0: 8406 0000 2000 3010 566b 0000 1500 2c00  .... .0.Vk....,.
2178 000001d0: 0000 0000 a029 e86a 91ff ffff eaa0 0400  .....).j........
2179 000001e0: 1e01 0001 8406 0000 2000 3010 280b 0400  ........ .0.(...
2180 000001f0: 1500 2c00 0000 0000 0060 0000 0000 0000  ..,......`......
2181 00000200: 980b 0000 0000 0000 88d0 0300 5701 0001  ............W...
2182 00000210: 8406 0000 2000 3010 566b 0000 586f e86a  .... .0.Vk..Xo.j
2183 00000220: 91ff ffff 0100 0000 0000 0000 c813 0300  ................
2184 00000230: 6c01 0001 8406 0000 2000 3010 566b 0000  l....... .0.Vk..
2185 00000240: 1500 2c00 0000 0000 0600 0000 0000 0000  ..,.............
2186 00000250: ac5f 0100 6d01 0001 8406 0000 2000 3010  ._..m....... .0.
2187 00000260: 1100 3010 1500 2c00 0000 0000 0600 0000  ..0...,.........
2188 00000270: f9ff ffff ffff ffff ffff ff07 185a ea6a  .............Z.j
2189 00000280: 0100 0000 4c02 0200 3f01 0002 8406 0000  ....L...?.......
2190 00000290: 2000 3010 566b 0000 1500 2c00 0000 0000   .0.Vk....,.....
2191 000002a0: 0800 0000 0000 0000 0700 0000 0000 0000  ................
2192 000002b0: 8081 0000 6d01 0001 0c0b 0100 6701 0001  ....m.......g...
2193 000002c0: 8406 0000 2000 3010 0000 0000 1500 2c00  .... .0.......,.
2194 000002d0: 0000 0000 0600 0000 0100 0000 ffff ffff  ................
2195 000002e0: ffff ff07 049a 0100 5701 0001 aa1c 0500  ........W.......
2196 000002f0: 2101 0001 8406 0000 2000 3010 91ff ffff  !....... .0.....
2197 00000300: 1500 2c00 0000 0000 0060 0000 0000 0000  ..,......`......
2198 00000310: 980b 0000 980b 0000 889e 0200 5701 0001  ............W...
2199 00000320: 8406 0000 2000 3010 91ff ffff 8829 e86a  .... .0......).j
2200 00000330: 91ff ffff 0200 0000 0000 0000 8838 0100  .............8..
2201 00000340: 1b01 0001 8406 0000 2000 3010 91ff ffff  ........ .0.....
2202 00000350: 1500 2c00 0000 0000 a029 e86a 91ff ffff  ..,......).j....
2203 00000360: 2ab8 1800 3501 0001 8406 0000 2000 3010  *...5....... .0.
2204 00000370: feff ffff 1500 2c00 0000 0000 0800 2c00  ......,.......,.
2205 00000380: 0000 0000 0000 0000 2000 3010 32fe 0300  ........ .0.2...
2206 00000390: 2201 0001 8406 0000 2000 3010 0000 0000  "....... .0.....
2207 000003a0: 1500 2c00 0000 0000 ffff ffff ffff ff7f  ..,.............
2208 000003b0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
2209 000003c0: ffff ffff ffff ff7f 0000 0000 0000 0000  ................
2210 000003d0: 0100 0000 0000 0000 887e 0800 5701 0001  .........~..W...
2211 000003e0: 8406 0000 2000 3010 7b04 3200 7c3f e86a  .... .0.{.2.|?.j
2212 000003f0: 91ff ffff 0a00 0000 0000 0000 ec2d 0300  .............-..
2213 00000400: 2301 0001 8406 0000 2000 3010 7b04 3200  #....... .0.{.2.
2214 00000410: 1500 2c00 0000 0000 0000 0000 0000 0000  ..,.............
2215 00000420: ffff ffff ffff ff7f 0100 0000 3c01 0001  ............<...
2216 00000430: 0a42 0c00 2401 0001 8406 0000 2000 3010  .B..$....... .0.
2217 00000440: 0800 0000 1500 2c00 0000 0000 0000 0000  ......,.........
2218 00000450: 0000 0000 0700 0000 0002 0000 885f 0100  ............._..
2219 00000460: 6c01 0001 8406 0000 2000 3010 0100 0000  l....... .0.....
2220 00000470: 1500 2c00 0000 0000 0000 0000 566b 0000  ..,.........Vk..
2221 00000480: 0c25 0100 6d01 0001 8406 0000 2000 3010  .%..m....... .0.
2222 00000490: 0400 0000 1500 2c00 0000 0000 0000 0000  ......,.........
2223 000004a0: 0700 0000 ffff ffff ffff ff07 1400 0000  ................
2224 000004b0: 0100 0000 caee 0100 5101 0001 8406 0000  ........Q.......
2225 000004c0: 2000 3010 1100 0000 1500 2c00 0000 0000   .0.......,.....
2226 000004d0: 0000 0000 0700 0000 2500 0000 2000 3010  ........%... .0.
2227 000004e0: 323b 0600 3201 0001 8406 0000 2000 3010  2;..2....... .0.
2228 000004f0: c86e 0000 1500 2c00 0000 0000 0700 0000  .n....,.........
2229 00000500: 0000 0000 0000 0000 0000 0000 0080 b000  ................
2230 00000510: 0000 0000 0000 0000 0000 0000 0000 0000  ................
2231 00000520: 0000 0000 2024 0000 0400 0000 ae0a 1a00  .... $..........
2232 00000530: 3a01 0001 8406 0000 2000 3010 0000 0000  :....... .0.....
2233 00000540: 1500 2c00 0000 0000 0000 0000 0000 0000  ..,.............
2234 00000550: 6101 0000 0700 0000 0000 0000 cf63 0000  a............c..
2235 00000560: 4100 0000 0700 0000 b4c5 0200 3301 0001  A...........3...
2236 00000570: 8406 0000 2000 3010 2000 3010 1500 2c00  .... .0. .0...,.
2237 00000580: 0000 0000 cfe3 2000 0000 0000 0700 0000  ...... .........
2238 00000590: 0000 0000 0000 0000 0000 0000 0080 b000  ................
2239 000005a0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
2240 000005b0: 0000 0000 2024 0000 6c01 0001 4859 0400  .... $..l...HY..
2241 000005c0: 1b01 0001 8406 0000 2000 3010 0000 0000  ........ .0.....
2242 000005d0: 1500 2c00 0000 0000 9c99 ea6a 91ff ffff  ..,........j....
2243 000005e0: c850 0500 6001 0001 8406 0000 2000 3010  .P..`....... .0.
2244 000005f0: 0000 0000 1500 2c00 0000 0000 0000 0000  ......,.........
2245 00000600: 0700 0000 2ee6 0200 3e01 0002 8406 0000  ........>.......
2246 00000610: 2000 3010 566b 0000 1500 2c00 0000 0000   .0.Vk....,.....
2247 00000620: 0800 0000 0000 0000 0700 0000 0700 0000  ................
2248 00000630: 0000 0000 0000 0000 0100 0000 8081 3010  ..............0.
2249 00000640: a804 0400 5701 0001 8406 0000 2000 3010  ....W....... .0.
2250 00000650: cb07 3200 885a ea6a 91ff ffff 0100 0000  ..2..Z.j........
2251 00000660: 0000 0000 8875 0300 6c01 0001 8406 0000  .....u..l.......
2252 00000670: 2000 3010 0300 0000 0300 0000 0000 0000   .0.............
2253 00000680: 0100 0000 0100 0000 ccd4 0100 6d01 0001  ............m...
2254 00000690: 8406 0000 2000 3010 cb07 3200 0300 0000  .... .0...2.....
2255 000006a0: 0000 0000 0000 0000 0200 0000 2724 0000  ............'$..
2256 000006b0: 0000 0000 1100 3010 0100 0000 a850 0500  ......0......P..
2257 000006c0: 5701 0001 8406 0000 2000 3010 0000 0000  W....... .0.....
2258 000006d0: 885a ea6a 91ff ffff 0100 0000 0000 0000  .Z.j............
2259 000006e0: 680f 0200 6c01 0001 8406 0000 2000 3010  h...l....... .0.
2260 000006f0: 0000 0000 0400 0000 0000 0000 0100 0000  ................
2261 00000700: 6d01 0001 ac79 0100 6d01 0001 8406 0000  m....y..m.......
2262 00000710: 2000 3010 0000 0000 0400 0000 0000 0000   .0.............
2263 00000720: 0000 0000 0200 0000 2924 0000 0000 0000  ........)$......
2264 00000730: 1143 0200 0100 0000 2818 0400 5701 0001  .C......(...W...
2265 00000740: 8406 0000 2000 3010 0000 0000 8829 e86a  .... .0......).j
2266 00000750: 91ff ffff 0200 0000 0000 0000 8838 0100  .............8..
2267 00000760: 1b01 0001 8406 0000 2000 3010 0400 0000  ........ .0.....
2268 00000770: 1500 2c00 0000 0000 a029 e86a 91ff ffff  ..,......).j....
2269 00000780: 0e89 0300 5301 0001 8406 0000 2000 3010  ....S....... .0.
2270 00000790: e128 0000 1500 2c00 0000 0000 2500 0000  .(....,.....%...
2271 000007a0: 0000 0000 cfe3 2000 0000 0000 0000 0000  ...... .........
2272 000007b0: 0700 0000 6000 0000 0700 0000 aca0 0100  ....`...........
2273 000007c0: 6701 0001 8406 0000 2000 3010 e128 0000  g....... .0..(..
2274 000007d0: 1500 2c00 0000 0000 0000 0000 0700 0000  ..,.............
2275 000007e0: cfe3 2000 0000 0000 01a2 0800 0000 0000  .. .............
2276 000007f0: 28b2 1e00 1b01 0001 8406 0000 2000 3010  (........... .0.
2277 00000800: e128 0000 1500 2c00 0000 0000 9846 e86a  .(....,......F.j
2278 00000810: 91ff ffff 68d2 1b00 5701 0001 8406 0000  ....h...W.......
2279 00000820: 2000 3010 e128 0000 7c3f e86a 91ff ffff   .0..(..|?.j....
2280 00000830: 0a00 0000 0000 0000 0c57 0200 2301 0001  .........W..#...
2281 00000840: 8406 0000 2000 3010 006c 0000 1500 2c00  .... .0..l....,.
2282 00000850: 0000 0000 0700 0000 0000 0000 f8ff ffff  ................
2283 00000860: ffff ff7f 0100 0000 0000 0000 6e69 8200  ............ni..
2284 00000870: 2501 0001 8406 0000 2000 3010 ca6e 0000  %....... .0..n..
2285 00000880: 1500 2c00 0000 0000 0000 0000 0700 0000  ..,.............
2286 00000890: 0000 0000 0000 0000 0700 0000 0000 0000  ................
2287 000008a0: 0100 0000 0200 3010 3e13 bd82 5500 0000  ......0.>...U...
2288 000008b0: 0600 0000 3001 0001 8015 0000 2000 3010  ....0....... .0.
2289 000008c0: 0000 0000 c801 1400 0000 0000 8860 4404  .............`D.
2290 000008d0: 1c01 0001 8015 0000 2000 3010 2000 0000  ........ .0. ...
2291 000008e0: c801 1400 0000 0000 0000 0000 0000 0000  ................
2292 000008f0: 88a9 0300 5701 0001 8015 0000 2000 3010  ....W....... .0.
2293 00000900: 0400 0000 1c1e e86a 91ff ffff 0300 0000  .......j........
2294 00000910: 0000 0000 a85a 1600 1b01 0001 8015 0000  .....Z..........
2295 00000920: 2000 3010 2000 3010 c801 1400 0000 0000   .0. .0.........
2296 00000930: c41e e86a 91ff ffff ca95 1c00 2901 0001  ...j........)...
2297 00000940: 8015 0000 2000 3010 2000 3010 c801 1400  .... .0. .0.....
2298 00000950: 0000 0000 0000 0000 0000 0000 0000 0000  ................
2299 00000960: 0010 0000 c8fb 0100 2801 0001 8015 0000  ........(.......
2300 00000970: 2000 3010 5101 0001 c801 1400 0000 0000   .0.Q...........
2301 00000980: 0000 0000 0000 0000 6af1 0600 2901 0001  ........j...)...
2302 00000990: 8015 0000 2000 3010 0000 0000 c801 1400  .... .0.........
2303 000009a0: 0000 0000 0100 0000 0000 0000 0000 0000  ................
2304 000009b0: 0010 0000 488f 0000 2801 0001 8015 0000  ....H...(.......
2305 000009c0: 2000 3010 0200 ffff c801 1400 0000 0000   .0.............
2306 000009d0: 0100 0000 0000 0000 483b 0900 4d01 0001  ........H;..M...
2307 000009e0: 8015 0000 2000 3010 0000 0000 c801 1400  .... .0.........
2308 000009f0: 0000 0000 1800 0000 0000 0000 8852 0100  .............R..
2309 00000a00: 5701 0001 8015 0000 2000 3010 e128 0000  W....... .0..(..
2310 00000a10: 9ce9 e76a 91ff ffff 0a00 0000 0000 0000  ...j............
2311 00000a20: e615 0200 3001 0001 8015 0000 2000 3010  ....0....... .0.
2312 00000a30: 0155 0000 c801 1400 0000 0000 68d0 0000  .U..........h...
2313 00000a40: 1b01 0001 8015 0000 2000 3010 6606 3200  ........ .0.f.2.
2314 00000a50: c801 1400 0000 0000 acfa ea6a 91ff ffff  ...........j....
2315 00000a60: 6a0f 0200 6901 0001 8015 0000 2000 3010  j...i....... .0.
2316 00000a70: 7106 3200 c801 1400 0000 0000 0000 0000  q.2.............
2317 00000a80: 0000 0000 ffff ffff 0000 0000 e895 0300  ................
2318 00000a90: 5701 0001 8015 0000 2000 3010 0300 0000  W....... .0.....
2319 00000aa0: acbe ea6a 91ff ffff 0100 0000 0000 0000  ...j............
2320 00000ab0: 8a38 0100 6501 0001 8015 0000 2000 3010  .8..e....... .0.
2321 00000ac0: c41e e86a c801 1400 0000 0000 0000 0000  ...j............
2322 00000ad0: feff ffff 0000 0000 0000 0000 ee86 0400  ................
2323 00000ae0: 6301 0001 8015 0000 2000 3010 0000 0000  c....... .0.....
2324 00000af0: c801 1400 0000 0000 0000 0000 0000 0000  ................
2325 00000b00: 0000 0000 0000 0000 4285 5000 0000 0000  ........B.P.....
2326 00000b10: 0200 0000 0000 0000 8e36 0200 6201 0001  .........6..b...
2327 00000b20: 8015 0000 2000 3010 7d55 0000 c801 1400  .... .0.}U......
2328 00000b30: 0000 0000 0000 0000 0100 0000 0000 0000  ................
2329 00000b40: 0000 0000 4285 5000 0000 0000 0000 0000  ....B.P.........
2330 00000b50: 0200 3010 8c5f 0100 3401 0001 8015 0000  ..0.._..4.......
2331 00000b60: 2000 3010 0000 0000 c801 1400 0000 0000   .0.............
2332 00000b70: 4285 5000 0000 0000 0200 0000 0000 0000  B.P.............
2333 00000b80: 1000 0000 8081 0000 aa43 0500 3c01 0001  .........C..<...
2334 00000b90: 8015 0000 2000 3010 2801 0001 c801 1400  .... .0.(.......
2335 00000ba0: 0000 0000 4205 0000 a100 0000 0200 0000  ....B...........
2336 00000bb0: 0200 0000 8871 0800 5701 0001 8015 0000  .....q..W.......
2337 00000bc0: 2000 3010 2000 3010 885a ea6a 91ff ffff   .0. .0..Z.j....
2338 00000bd0: 0100 0000 0000 0000 4825 0400 6c01 0001  ........H%..l...
2339 00000be0: 8015 0000 2000 3010 2801 0001 0300 0000  .... .0.(.......
2340 00000bf0: 0000 0000 0400 0000 7106 3200 0c73 0100  ........q.2..s..
2341 00000c00: 6d01 0001 8015 0000 2000 3010 2901 0001  m....... .0.)...
2342 00000c10: 0300 0000 0000 0000 0400 0000 0100 0000  ................
2343 00000c20: 2680 0800 0000 0000 1100 0000 0100 0000  &...............
2344 00000c30: c845 0400 5701 0001 8015 0000 2000 3010  .E..W....... .0.
2345 00000c40: 2000 3010 885a ea6a 91ff ffff 0100 0000   .0..Z.j........
2346 00000c50: 0000 0000 e8c9 0000 6c01 0001 8015 0000  ........l.......
2347 00000c60: 2000 3010 2000 3010 0400 0000 0000 0000   .0. .0.........
2348 00000c70: 0800 0000 0500 0000 6cdd 0000 6d01 0001  ........l...m...
2349 00000c80: 8015 0000 2000 3010 2801 0001 0400 0000  .... .0.(.......
2350 00000c90: 0000 0000 0800 0000 0300 0000 1290 7000  ..............p.
2351 00000ca0: 0000 0000 1100 0000 0100 0000 6875 0300  ............hu..
2352 00000cb0: 5701 0001 8015 0000 2000 3010 7106 3200  W....... .0.q.2.
2353 00000cc0: 8829 e86a 91ff ffff 0200 0000 0000 0000  .).j............
2354 00000cd0: a847 0000 1b01 0001 8015 0000 2000 3010  .G.......... .0.
2355 00000ce0: 9ce9 e76a c801 1400 0000 0000 a029 e86a  ...j.........).j
2356 00000cf0: 91ff ffff e83a 0300 1b01 0001 8015 0000  .....:..........
2357 00000d00: 2000 3010 7106 3200 c801 1400 0000 0000   .0.q.2.........
2358 00000d10: 9c99 ea6a 91ff ffff ae93 0100 6601 0001  ...j........f...
2359 00000d20: 8015 0000 2000 3010 acfa ea6a c801 1400  .... .0....j....
2360 00000d30: 0000 0000 0000 0000 feff ffff 0000 0000  ................
2361 00000d40: 2000 3010 0000 0000 0000 0000 0000 0000   .0.............
2362 00000d50: 0000 0000 48b6 0000 1b01 0001 8015 0000  ....H...........
2363 00000d60: 2000 3010 e128 0000 c801 1400 0000 0000   .0..(..........
2364 00000d70: 9c99 ea6a 91ff ffff a8ea 0300 1b01 0001  ...j............
2365 00000d80: 8015 0000 2000 3010 e128 0000 c801 1400  .... .0..(......
2366 00000d90: 0000 0000 08eb e76a 91ff ffff 885f 0100  .......j....._..
2367 00000da0: 4e01 0001 8015 0000 2000 3010 2efe 0300  N....... .0.....
2368 00000db0: c801 1400 0000 0000 0800 0000 0000 0000  ................
2369 00000dc0: e8bc 0000 5701 0001 8015 0000 2000 3010  ....W....... .0.
2370 00000dd0: 0000 0000 8829 e86a 91ff ffff 0200 0000  .....).j........
2371 00000de0: 0000 0000 c895 0000 1b01 0001 8015 0000  ................
2372 00000df0: 2000 3010 2000 3010 c801 1400 0000 0000   .0. .0.........
2373 00000e00: a029 e86a 91ff ffff cab2 0b00 1e01 0001  .).j............
2374 00000e10: 8015 0000 2000 3010 0000 0000 c801 1400  .... .0.........
2375 00000e20: 0000 0000 0000 0000 0000 0000 0400 0000  ................
2376 00000e30: 0000 0000 689a 0400 5701 0001 8015 0000  ....h...W.......
2377 00000e40: 2000 3010 0000 0000 586f e86a 91ff ffff   .0.....Xo.j....
2378 00000e50: 0100 0000 0000 0000 8884 0200 6c01 0001  ............l...
2379 00000e60: 8015 0000 2000 3010 8829 e86a c801 1400  .... .0..).j....
2380 00000e70: 0000 0000 0000 0000 4100 0000 ac47 0000  ........A....G..
2381 00000e80: 6d01 0001 8015 0000 2000 3010 e128 0000  m....... .0..(..
2382 00000e90: c801 1400 0000 0000 0000 0000 0000 0000  ................
2383 00000ea0: 0000 0000 0000 0000 001c 0200 0000 0000  ................
2384 00000eb0: 2a66 0100 5101 0001 8015 0000 2000 3010  *f..Q....... .0.
2385 00000ec0: 0000 0000 c801 1400 0000 0000 0000 0000  ................
2386 00000ed0: 0100 0000 0000 0000 2000 3010 087e 0200  ........ .0..~..
2387 00000ee0: 6a01 0001 8015 0000 2000 3010 0100 0000  j....... .0.....
2388 00000ef0: c801 1400 0000 0000 0000 0000 0100 0000  ................
2389 00000f00: 8c11 0100 6b01 0001 8015 0000 2000 3010  ....k....... .0.
2390 00000f10: 1b01 0001 c801 1400 0000 0000 0000 0000  ................
2391 00000f20: 0000 0000 0000 0000 0000 0000 0028 0000  .............(..
2392 00000f30: 2000 3010 8c5f 0100 6701 0001 8015 0000   .0.._..g.......
2393 00000f40: 2000 3010 0000 0000 c801 1400 0000 0000   .0.............
2394 00000f50: 0000 0000 ffff ffff ffff ffff ffff ff07  ................
2395 00000f60: 0800 0000 0700 0000 6e02 0200 5301 0001  ........n...S...
2396 00000f70: 8015 0000 2000 3010 0100 0000 c801 1400  .... .0.........
2397 00000f80: 0000 0000 0000 0000 2000 3010 0000 0000  ........ .0.....
2398 00000f90: 0100 0000 0000 0000 0100 0000 0000 0000  ................
2399 00000fa0: 0000 0000 cc3a 0300 3f01 0002 8015 0000  .....:..?.......
2400 00000fb0: 2000 3010 7106 3200 c801 1400 0000 0000   .0.q.2.........
2401 00000fc0: 0800 0000 0000 0000 0100 0000 0000 0000  ................
2402 00000fd0: 8081 3010 3d00 0000 f542 0000 0000 0000  ..0.=....B......
2403 00000fe0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
2404 00000ff0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
2405     )",
2406 };
2407 
TEST(CpuReaderTest,ParseExt4WithOverwrite)2408 TEST(CpuReaderTest, ParseExt4WithOverwrite) {
2409   const ExamplePage* test_case = &g_full_page_ext4;
2410 
2411   BundleProvider bundle_provider(base::kPageSize);
2412   ProtoTranslationTable* table = GetTable(test_case->name);
2413   auto page = PageFromXxd(test_case->data);
2414 
2415   FtraceDataSourceConfig ds_config = EmptyConfig();
2416   ds_config.event_filter.AddEnabledEvent(
2417       table->EventToFtraceId(GroupAndName("sched", "sched_switch")));
2418 
2419   FtraceMetadata metadata{};
2420   CompactSchedBuffer compact_buffer;
2421   const uint8_t* parse_pos = page.get();
2422   base::Optional<CpuReader::PageHeader> page_header =
2423       CpuReader::ParsePageHeader(&parse_pos, table->page_header_size_len());
2424 
2425   const uint8_t* page_end = page.get() + base::kPageSize;
2426   ASSERT_TRUE(page_header.has_value());
2427   EXPECT_TRUE(page_header->lost_events);  // data loss
2428   EXPECT_TRUE(parse_pos < page_end);
2429   EXPECT_TRUE(parse_pos + page_header->size < page_end);
2430 
2431   size_t evt_bytes = CpuReader::ParsePagePayload(
2432       parse_pos, &page_header.value(), table, &ds_config, &compact_buffer,
2433       bundle_provider.writer(), &metadata);
2434 
2435   EXPECT_LT(0u, evt_bytes);
2436 
2437   auto bundle = bundle_provider.ParseProto();
2438   ASSERT_TRUE(bundle);
2439 }
2440 
2441 }  // namespace perfetto
2442