Primiano Tucci | 5c59901 | 2018-03-01 17:52:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | #ifndef SRC_TRACING_TEST_FAKE_PACKET_H_ |
| 18 | #define SRC_TRACING_TEST_FAKE_PACKET_H_ |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | |
| 22 | #include <array> |
| 23 | #include <initializer_list> |
| 24 | #include <string> |
| 25 | #include <vector> |
| 26 | |
| 27 | #include "perfetto/tracing/core/basic_types.h" |
| 28 | |
| 29 | namespace perfetto { |
| 30 | |
Hector Dearman | 6214c8f | 2018-03-27 16:16:22 +0100 | [diff] [blame] | 31 | class TraceBuffer; |
Primiano Tucci | 5c59901 | 2018-03-01 17:52:07 +0000 | [diff] [blame] | 32 | |
| 33 | class FakePacketFragment { |
| 34 | public: |
| 35 | // Generates a packet of given |size| with pseudo random payload. The |size| |
| 36 | // argument includes the size of the varint header. |
| 37 | FakePacketFragment(size_t size, char prefix); |
| 38 | FakePacketFragment(const void* payload, size_t payload_size); |
| 39 | void CopyInto(std::vector<uint8_t>* data) const; |
| 40 | size_t GetSizeHeader() const; |
| 41 | bool operator==(const FakePacketFragment& other) const; |
| 42 | const std::string& payload() const { return payload_; } |
| 43 | |
| 44 | private: |
| 45 | std::array<uint8_t, 2> header_{}; |
| 46 | size_t header_size_ = 0; |
| 47 | std::string payload_; |
| 48 | }; |
| 49 | |
| 50 | std::ostream& operator<<(std::ostream&, const FakePacketFragment&); |
| 51 | |
| 52 | class FakeChunk { |
| 53 | public: |
Hector Dearman | 6214c8f | 2018-03-27 16:16:22 +0100 | [diff] [blame] | 54 | FakeChunk(TraceBuffer* t, ProducerID p, WriterID w, ChunkID c); |
Primiano Tucci | 5c59901 | 2018-03-01 17:52:07 +0000 | [diff] [blame] | 55 | |
| 56 | // Appends a packet of exactly |size| bytes (including the varint header |
| 57 | // that states the size of the packet itself. The payload of the packet is |
| 58 | // NOT a valid proto and is just filled with pseudo-random bytes generated |
| 59 | // from |seed|. |
| 60 | FakeChunk& AddPacket(size_t size, char seed, uint8_t packet_flag = 0); |
| 61 | |
| 62 | // Appends a packet with the given raw content (including varint header). |
| 63 | FakeChunk& AddPacket(std::initializer_list<uint8_t>); |
| 64 | |
Primiano Tucci | 7b14f65 | 2018-04-06 12:16:34 +0200 | [diff] [blame] | 65 | // Increments the number of packets in the chunk without adding new data. |
| 66 | FakeChunk& IncrementNumPackets(); |
| 67 | |
Primiano Tucci | 5c59901 | 2018-03-01 17:52:07 +0000 | [diff] [blame] | 68 | FakeChunk& ClearBytes(size_t offset, size_t len); |
| 69 | |
Primiano Tucci | 09db827 | 2018-03-08 17:47:47 +0000 | [diff] [blame] | 70 | FakeChunk& SetUID(uid_t); |
| 71 | |
Primiano Tucci | 5c59901 | 2018-03-01 17:52:07 +0000 | [diff] [blame] | 72 | // Returns the full size of the chunk including the ChunkRecord header. |
| 73 | size_t CopyIntoTraceBuffer(); |
| 74 | |
| 75 | private: |
Hector Dearman | 6214c8f | 2018-03-27 16:16:22 +0100 | [diff] [blame] | 76 | TraceBuffer* trace_buffer_; |
Primiano Tucci | 5c59901 | 2018-03-01 17:52:07 +0000 | [diff] [blame] | 77 | ProducerID producer_id; |
| 78 | WriterID writer_id; |
| 79 | ChunkID chunk_id; |
| 80 | uint8_t flags = 0; |
| 81 | uint16_t num_packets = 0; |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 82 | uid_t uid = kInvalidUid; |
Primiano Tucci | 5c59901 | 2018-03-01 17:52:07 +0000 | [diff] [blame] | 83 | std::vector<uint8_t> data; |
| 84 | }; |
| 85 | |
| 86 | } // namespace perfetto |
| 87 | |
| 88 | #endif // SRC_TRACING_TEST_FAKE_PACKET_H_ |