• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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/perfetto_cmd/packet_writer.h"
18 
19 #include <string.h>
20 
21 #include <random>
22 
23 #include "perfetto/base/build_config.h"
24 #include "perfetto/ext/base/file_utils.h"
25 #include "perfetto/ext/base/scoped_file.h"
26 #include "perfetto/ext/base/temp_file.h"
27 #include "perfetto/ext/tracing/core/trace_packet.h"
28 #include "src/perfetto_cmd/packet_writer.h"
29 #include "test/gtest_and_gmock.h"
30 
31 #include "protos/perfetto/trace/test_event.gen.h"
32 #include "protos/perfetto/trace/trace.gen.h"
33 #include "protos/perfetto/trace/trace_packet.gen.h"
34 
35 #if PERFETTO_BUILDFLAG(PERFETTO_ZLIB)
36 #include <zlib.h>
37 #endif
38 
39 namespace perfetto {
40 namespace {
41 
42 using TracePacketProto = protos::gen::TracePacket;
43 
44 template <typename F>
CreateTracePacket(F fill_function)45 TracePacket CreateTracePacket(F fill_function) {
46   TracePacketProto msg;
47   fill_function(&msg);
48   std::vector<uint8_t> buf = msg.SerializeAsArray();
49   Slice slice = Slice::Allocate(buf.size());
50   memcpy(slice.own_data(), buf.data(), buf.size());
51   perfetto::TracePacket packet;
52   packet.AddSlice(std::move(slice));
53   return packet;
54 }
55 
56 #if PERFETTO_BUILDFLAG(PERFETTO_ZLIB)
RandomString(size_t size)57 std::string RandomString(size_t size) {
58   std::minstd_rand0 rnd(0);
59   std::uniform_int_distribution<> dist(0, 255);
60   std::string s;
61   s.resize(size);
62   for (size_t i = 0; i < s.size(); i++)
63     s[i] = static_cast<char>(dist(rnd));
64   return s;
65 }
66 
Decompress(const std::string & data)67 std::string Decompress(const std::string& data) {
68   uint8_t out[1024];
69 
70   z_stream stream{};
71   stream.next_in = reinterpret_cast<uint8_t*>(const_cast<char*>(data.data()));
72   stream.avail_in = static_cast<unsigned int>(data.size());
73 
74   EXPECT_EQ(inflateInit(&stream), Z_OK);
75   std::string s;
76 
77   int ret;
78   do {
79     stream.next_out = out;
80     stream.avail_out = sizeof(out);
81     ret = inflate(&stream, Z_NO_FLUSH);
82     EXPECT_NE(ret, Z_STREAM_ERROR);
83     EXPECT_NE(ret, Z_NEED_DICT);
84     EXPECT_NE(ret, Z_DATA_ERROR);
85     EXPECT_NE(ret, Z_MEM_ERROR);
86     s.append(reinterpret_cast<char*>(out), sizeof(out) - stream.avail_out);
87   } while (ret != Z_STREAM_END);
88 
89   inflateEnd(&stream);
90   return s;
91 }
92 #endif  // PERFETTO_BUILDFLAG(PERFETTO_ZLIB)
93 
TEST(PacketWriterTest,FilePacketWriter)94 TEST(PacketWriterTest, FilePacketWriter) {
95   base::TempFile tmp = base::TempFile::CreateUnlinked();
96   base::ScopedResource<FILE*, fclose, nullptr> f(
97       fdopen(tmp.ReleaseFD().release(), "wb"));
98 
99   std::vector<perfetto::TracePacket> packets;
100   packets.push_back(CreateTracePacket([](TracePacketProto* msg) {
101     auto* for_testing = msg->mutable_for_testing();
102     for_testing->set_str("abc");
103   }));
104 
105   {
106     std::unique_ptr<PacketWriter> writer = CreateFilePacketWriter(*f);
107     EXPECT_TRUE(writer->WritePackets(std::move(packets)));
108   }
109 
110   fseek(*f, 0, SEEK_SET);
111   std::string s;
112   EXPECT_TRUE(base::ReadFileStream(*f, &s));
113   EXPECT_GT(s.size(), 0u);
114 
115   protos::gen::Trace trace;
116   EXPECT_TRUE(trace.ParseFromString(s));
117   EXPECT_EQ(trace.packet()[0].for_testing().str(), "abc");
118 }
119 
120 #if PERFETTO_BUILDFLAG(PERFETTO_ZLIB)
121 
TEST(PacketWriterTest,ZipPacketWriter)122 TEST(PacketWriterTest, ZipPacketWriter) {
123   base::TempFile tmp = base::TempFile::CreateUnlinked();
124   base::ScopedResource<FILE*, fclose, nullptr> f(
125       fdopen(tmp.ReleaseFD().release(), "wb"));
126 
127     std::vector<perfetto::TracePacket> packets;
128     packets.push_back(CreateTracePacket([](TracePacketProto* msg) {
129       auto* for_testing = msg->mutable_for_testing();
130       for_testing->set_str("abc");
131     }));
132 
133     {
134       std::unique_ptr<PacketWriter> writer =
135           CreateZipPacketWriter(CreateFilePacketWriter(*f));
136       EXPECT_TRUE(writer->WritePackets(std::move(packets)));
137     }
138 
139   std::string s;
140   fseek(*f, 0, SEEK_SET);
141   EXPECT_TRUE(base::ReadFileStream(*f, &s));
142   EXPECT_GT(s.size(), 0u);
143 
144   protos::gen::Trace trace;
145   EXPECT_TRUE(trace.ParseFromString(s));
146 
147   const std::string& data = trace.packet()[0].compressed_packets();
148   EXPECT_GT(data.size(), 0u);
149 
150   protos::gen::Trace subtrace;
151   EXPECT_TRUE(subtrace.ParseFromString(Decompress(data)));
152   EXPECT_EQ(subtrace.packet()[0].for_testing().str(), "abc");
153 }
154 
TEST(PacketWriterTest,ZipPacketWriter_Empty)155 TEST(PacketWriterTest, ZipPacketWriter_Empty) {
156   base::TempFile tmp = base::TempFile::CreateUnlinked();
157   base::ScopedResource<FILE*, fclose, nullptr> f(
158       fdopen(tmp.ReleaseFD().release(), "wb"));
159 
160   {
161     std::unique_ptr<PacketWriter> writer =
162         CreateZipPacketWriter(CreateFilePacketWriter(*f));
163   }
164 
165   EXPECT_EQ(fseek(*f, 0, SEEK_END), 0);
166 }
167 
TEST(PacketWriterTest,ZipPacketWriter_EmptyWithEmptyWrite)168 TEST(PacketWriterTest, ZipPacketWriter_EmptyWithEmptyWrite) {
169   base::TempFile tmp = base::TempFile::CreateUnlinked();
170   base::ScopedResource<FILE*, fclose, nullptr> f(
171       fdopen(tmp.ReleaseFD().release(), "wb"));
172 
173   {
174     std::unique_ptr<PacketWriter> writer =
175         CreateZipPacketWriter(CreateFilePacketWriter(*f));
176     writer->WritePackets(std::vector<TracePacket>());
177     writer->WritePackets(std::vector<TracePacket>());
178     writer->WritePackets(std::vector<TracePacket>());
179   }
180 
181   EXPECT_EQ(fseek(*f, 0, SEEK_END), 0);
182 }
183 
TEST(PacketWriterTest,ZipPacketWriter_ShouldCompress)184 TEST(PacketWriterTest, ZipPacketWriter_ShouldCompress) {
185   base::TempFile tmp = base::TempFile::CreateUnlinked();
186   base::ScopedResource<FILE*, fclose, nullptr> f(
187       fdopen(tmp.ReleaseFD().release(), "wb"));
188   size_t uncompressed_size = 0;
189 
190   std::vector<perfetto::TracePacket> packets;
191   for (size_t i = 0; i < 200; i++) {
192     packets.push_back(CreateTracePacket([](TracePacketProto* msg) {
193       auto* for_testing = msg->mutable_for_testing();
194       for_testing->set_str("abcdefghijklmn");
195     }));
196 
197     packets.push_back(CreateTracePacket([](TracePacketProto* msg) {
198       auto* for_testing = msg->mutable_for_testing();
199       for_testing->set_str("abcdefghijklmn");
200     }));
201 
202     for (const TracePacket& packet : packets)
203       uncompressed_size += packet.size();
204   }
205 
206   {
207     std::unique_ptr<PacketWriter> writer =
208         CreateZipPacketWriter(CreateFilePacketWriter(*f));
209     EXPECT_TRUE(writer->WritePackets(std::move(packets)));
210   }
211 
212   std::string s;
213   EXPECT_LT(fseek(*f, 0, SEEK_END), static_cast<int>(uncompressed_size));
214   fseek(*f, 0, SEEK_SET);
215   EXPECT_TRUE(base::ReadFileStream(*f, &s));
216   EXPECT_GT(s.size(), 0u);
217 
218   protos::gen::Trace trace;
219   EXPECT_TRUE(trace.ParseFromString(s));
220 
221   size_t packet_count = 0;
222   for (const auto& packet : trace.packet()) {
223     const std::string& data = packet.compressed_packets();
224     EXPECT_GT(data.size(), 0u);
225     EXPECT_LT(data.size(), 500 * 1024u);
226     protos::gen::Trace subtrace;
227     EXPECT_TRUE(subtrace.ParseFromString(Decompress(data)));
228     for (const auto& subpacket : subtrace.packet()) {
229       packet_count++;
230       EXPECT_EQ(subpacket.for_testing().str(), "abcdefghijklmn");
231     }
232   }
233 
234   EXPECT_EQ(packet_count, 200 * 2u);
235 }
236 
TEST(PacketWriterTest,ZipPacketWriter_LargePacket)237 TEST(PacketWriterTest, ZipPacketWriter_LargePacket) {
238   base::TempFile tmp = base::TempFile::CreateUnlinked();
239   base::ScopedResource<FILE*, fclose, nullptr> f(
240       fdopen(tmp.ReleaseFD().release(), "wb"));
241 
242   std::vector<perfetto::TracePacket> packets;
243 
244   auto add_packet = [&packets](size_t size) {
245     std::string s = RandomString(size);
246     packets.push_back(CreateTracePacket([&s](TracePacketProto* msg) {
247       auto* for_testing = msg->mutable_for_testing();
248       for_testing->set_str(s);
249     }));
250   };
251 
252   add_packet(1 * 1024 * 1024);
253   add_packet(10);
254   add_packet(1 * 1024 * 1024);
255   add_packet(1 * 1024);
256   add_packet(1 * 1024);
257   add_packet(2 * 1024 * 1024);
258 
259   {
260     std::unique_ptr<PacketWriter> writer =
261         CreateZipPacketWriter(CreateFilePacketWriter(*f));
262     EXPECT_TRUE(writer->WritePackets(std::move(packets)));
263   }
264 
265   std::string s;
266   fseek(*f, 0, SEEK_SET);
267   EXPECT_TRUE(base::ReadFileStream(*f, &s));
268   EXPECT_GT(s.size(), 0u);
269 
270   protos::gen::Trace trace;
271   EXPECT_TRUE(trace.ParseFromString(s));
272 }
273 
TEST(PacketWriterTest,ZipPacketWriter_ShouldSplitPackets)274 TEST(PacketWriterTest, ZipPacketWriter_ShouldSplitPackets) {
275   base::TempFile tmp = base::TempFile::CreateUnlinked();
276   base::ScopedResource<FILE*, fclose, nullptr> f(
277       fdopen(tmp.ReleaseFD().release(), "wb"));
278 
279   std::vector<perfetto::TracePacket> packets;
280 
281   for (uint32_t i = 0; i < 1000; i++) {
282     packets.push_back(CreateTracePacket([i](TracePacketProto* msg) {
283       auto* for_testing = msg->mutable_for_testing();
284       for_testing->set_seq_value(i);
285       for_testing->set_str(RandomString(1024));
286     }));
287   }
288 
289   {
290     std::unique_ptr<PacketWriter> writer =
291         CreateZipPacketWriter(CreateFilePacketWriter(*f));
292     EXPECT_TRUE(writer->WritePackets(std::move(packets)));
293   }
294 
295   std::string s;
296   fseek(*f, 0, SEEK_SET);
297   EXPECT_TRUE(base::ReadFileStream(*f, &s));
298   EXPECT_GT(s.size(), 0u);
299 
300   protos::gen::Trace trace;
301   EXPECT_TRUE(trace.ParseFromString(s));
302 
303   size_t packet_count = 0;
304   for (const auto& packet : trace.packet()) {
305     const std::string& data = packet.compressed_packets();
306     EXPECT_GT(data.size(), 0u);
307     EXPECT_LT(data.size(), 500 * 1024u);
308     protos::gen::Trace subtrace;
309     EXPECT_TRUE(subtrace.ParseFromString(Decompress(data)));
310     for (const auto& subpacket : subtrace.packet()) {
311       EXPECT_EQ(subpacket.for_testing().seq_value(), packet_count++);
312     }
313   }
314 
315   EXPECT_EQ(packet_count, 1000u);
316 }
317 
318 #endif  // PERFETTO_BUILDFLAG(PERFETTO_ZLIB)
319 
320 }  // namespace
321 }  // namespace perfetto
322