blob: bdefa2eafe3ab6b8554c16c43f5558bf1edf5f3c [file] [log] [blame]
Primiano Tuccid933d912018-09-04 09:15:07 +01001/*
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
Eric Seckler137a4672019-10-24 08:51:14 +010017#ifndef SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_PROTO_TRACE_TOKENIZER_H_
18#define SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_PROTO_TRACE_TOKENIZER_H_
Primiano Tuccid933d912018-09-04 09:15:07 +010019
20#include <stdint.h>
21
22#include <memory>
23#include <vector>
24
25#include "src/trace_processor/chunked_trace_reader.h"
Eric Seckler771960c2019-10-22 15:37:12 +010026#include "src/trace_processor/importers/proto/proto_incremental_state.h"
27#include "src/trace_processor/trace_blob_view.h"
Eric Seckler684a4f72019-04-26 14:34:07 +010028
Primiano Tucci6756fb02019-08-14 15:49:18 +020029namespace protozero {
30struct ConstBytes;
31}
32
Primiano Tuccid933d912018-09-04 09:15:07 +010033namespace perfetto {
Eric Secklerd2af9892019-11-01 10:10:53 +000034
35namespace protos {
36namespace pbzero {
37class TracePacket_Decoder;
38} // namespace pbzero
39} // namespace protos
40
Primiano Tuccid933d912018-09-04 09:15:07 +010041namespace trace_processor {
42
Eric Seckler771960c2019-10-22 15:37:12 +010043class PacketSequenceState;
Primiano Tuccid933d912018-09-04 09:15:07 +010044class TraceProcessorContext;
Primiano Tuccid933d912018-09-04 09:15:07 +010045class TraceSorter;
Primiano Tucci0e38a142019-01-07 20:51:09 +000046class TraceStorage;
Primiano Tuccid933d912018-09-04 09:15:07 +010047
48// Reads a protobuf trace in chunks and extracts boundaries of trace packets
49// (or subfields, for the case of ftrace) with their timestamps.
50class ProtoTraceTokenizer : public ChunkedTraceReader {
51 public:
52 // |reader| is the abstract method of getting chunks of size |chunk_size_b|
53 // from a trace file with these chunks parsed into |trace|.
54 explicit ProtoTraceTokenizer(TraceProcessorContext*);
55 ~ProtoTraceTokenizer() override;
56
57 // ChunkedTraceReader implementation.
Lalit Magantid71a9452019-05-09 15:13:24 +010058 util::Status Parse(std::unique_ptr<uint8_t[]>, size_t size) override;
Primiano Tuccid933d912018-09-04 09:15:07 +010059
60 private:
Primiano Tucci6756fb02019-08-14 15:49:18 +020061 using ConstBytes = protozero::ConstBytes;
Lalit Magantid71a9452019-05-09 15:13:24 +010062 util::Status ParseInternal(std::unique_ptr<uint8_t[]> owned_buf,
63 uint8_t* data,
64 size_t size);
65 util::Status ParsePacket(TraceBlobView);
Primiano Tucci6756fb02019-08-14 15:49:18 +020066 util::Status ParseClockSnapshot(ConstBytes blob, uint32_t seq_id);
Eric Seckler684a4f72019-04-26 14:34:07 +010067 void HandleIncrementalStateCleared(
Eric Secklerd2af9892019-11-01 10:10:53 +000068 const protos::pbzero::TracePacket_Decoder&);
69 void HandlePreviousPacketDropped(const protos::pbzero::TracePacket_Decoder&);
70 void ParseInternedData(const protos::pbzero::TracePacket_Decoder&,
Primiano Tucci6756fb02019-08-14 15:49:18 +020071 TraceBlobView interned_data);
Eric Seckler771960c2019-10-22 15:37:12 +010072 PacketSequenceState* GetIncrementalStateForPacketSequence(
73 uint32_t sequence_id) {
Eric Seckler684a4f72019-04-26 14:34:07 +010074 if (!incremental_state)
Eric Seckler02cdcef2019-10-14 08:56:28 +010075 incremental_state.reset(new ProtoIncrementalState(context_));
Eric Seckler684a4f72019-04-26 14:34:07 +010076 return incremental_state->GetOrCreateStateForPacketSequence(sequence_id);
77 }
78
Deepanjan Roy01994ca2019-04-02 11:05:34 -070079 TraceProcessorContext* context_;
Primiano Tuccid933d912018-09-04 09:15:07 +010080
81 // Used to glue together trace packets that span across two (or more)
82 // Parse() boundaries.
83 std::vector<uint8_t> partial_buf_;
84
85 // Temporary. Currently trace packets do not have a timestamp, so the
Lalit Magantifa21a282019-01-17 19:03:04 +000086 // timestamp given is latest_timestamp_.
87 int64_t latest_timestamp_ = 0;
Eric Seckler684a4f72019-04-26 14:34:07 +010088
89 // Stores incremental state and references to interned data, e.g. for track
90 // event protos.
91 std::unique_ptr<ProtoIncrementalState> incremental_state;
Primiano Tuccid933d912018-09-04 09:15:07 +010092};
93
94} // namespace trace_processor
95} // namespace perfetto
96
Eric Seckler137a4672019-10-24 08:51:14 +010097#endif // SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_PROTO_TRACE_TOKENIZER_H_