1 /*
2 * Copyright (C) 2023 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 <optional>
18
19 #include "src/trace_processor/importers/etw/etw_tokenizer.h"
20
21 #include "perfetto/base/status.h"
22 #include "perfetto/ext/base/status_or.h"
23 #include "perfetto/protozero/proto_decoder.h"
24 #include "perfetto/protozero/proto_utils.h"
25 #include "src/trace_processor/importers/proto/packet_sequence_state_generation.h"
26 #include "src/trace_processor/sorter/trace_sorter.h"
27 #include "src/trace_processor/storage/trace_storage.h"
28
29 #include "protos/perfetto/common/builtin_clock.pbzero.h"
30 #include "protos/perfetto/trace/etw/etw_event.pbzero.h"
31 #include "protos/perfetto/trace/etw/etw_event_bundle.pbzero.h"
32
33 namespace perfetto {
34 namespace trace_processor {
35
36 using protozero::ProtoDecoder;
37 using protozero::proto_utils::MakeTagVarInt;
38 using protozero::proto_utils::ParseVarInt;
39
40 using protos::pbzero::BuiltinClock;
41 using protos::pbzero::EtwTraceEventBundle;
42
43 PERFETTO_ALWAYS_INLINE
TokenizeEtwBundle(TraceBlobView bundle,RefPtr<PacketSequenceStateGeneration> state)44 base::Status EtwTokenizer::TokenizeEtwBundle(
45 TraceBlobView bundle,
46 RefPtr<PacketSequenceStateGeneration> state) {
47 protos::pbzero::EtwTraceEventBundle::Decoder decoder(bundle.data(),
48 bundle.length());
49 // Cpu id can either be in the etw bundle or inside the individual
50 // EtwTraceEvent. If present at this level, we pass it to the TokenizeEtwEvent
51 // in case the EtwTraceEvent does not contain the cpu.
52 std::optional<uint32_t> bundle_cpu =
53 decoder.has_cpu() ? std::make_optional(decoder.cpu()) : std::nullopt;
54
55 for (auto it = decoder.event(); it; ++it) {
56 TokenizeEtwEvent(bundle_cpu, bundle.slice(it->data(), it->size()), state);
57 }
58 return base::OkStatus();
59 }
60
61 PERFETTO_ALWAYS_INLINE
TokenizeEtwEvent(std::optional<uint32_t> fallback_cpu,TraceBlobView event,RefPtr<PacketSequenceStateGeneration> state)62 base::Status EtwTokenizer::TokenizeEtwEvent(
63 std::optional<uint32_t> fallback_cpu,
64 TraceBlobView event,
65 RefPtr<PacketSequenceStateGeneration> state) {
66 const uint8_t* data = event.data();
67 const size_t length = event.length();
68 ProtoDecoder decoder(data, length);
69
70 protos::pbzero::EtwTraceEvent::Decoder etw_decoder(data, length);
71 // Some ETW events lack CPU info; in that case, the bundle may
72 // provide it.
73 uint32_t cpu;
74 if (etw_decoder.has_cpu()) {
75 cpu = etw_decoder.cpu();
76 } else {
77 if (!fallback_cpu.has_value()) {
78 return base::ErrStatus(
79 "CPU field not found in EtwEvent and/or EtwEventBundle");
80 }
81 cpu = fallback_cpu.value();
82 }
83
84 static constexpr uint32_t kMaxCpuCount = 1024;
85 if (PERFETTO_UNLIKELY(cpu >= kMaxCpuCount)) {
86 return base::ErrStatus(
87 "CPU %u is greater than maximum allowed of %u. This is likely because "
88 "of trace corruption",
89 cpu, kMaxCpuCount);
90 }
91
92 uint64_t raw_timestamp = 0;
93 if (etw_decoder.has_timestamp()) {
94 raw_timestamp = etw_decoder.timestamp();
95 } else {
96 return base::ErrStatus("Timestamp field not found in EtwEvent");
97 }
98
99 base::StatusOr<int64_t> timestamp = static_cast<int64_t>(raw_timestamp);
100
101 // ClockTracker will increment some error stats if it failed to convert the
102 // timestamp so just return.
103 if (!timestamp.ok()) {
104 return timestamp.status();
105 }
106
107 context_->sorter->PushEtwEvent(cpu, *timestamp, std::move(event),
108 std::move(state));
109
110 return base::OkStatus();
111 }
112
113 } // namespace trace_processor
114 } // namespace perfetto
115