aboutsummaryrefslogtreecommitdiff
path: root/src/trace_processor/importers/etw/etw_tokenizer.cc
blob: 2474454b39e5b48e1914e004300aaf1c8042a34b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <optional>

#include "src/trace_processor/importers/etw/etw_tokenizer.h"

#include "perfetto/base/status.h"
#include "perfetto/ext/base/status_or.h"
#include "perfetto/protozero/proto_decoder.h"
#include "perfetto/protozero/proto_utils.h"
#include "src/trace_processor/importers/proto/packet_sequence_state.h"
#include "src/trace_processor/sorter/trace_sorter.h"
#include "src/trace_processor/storage/trace_storage.h"

#include "protos/perfetto/common/builtin_clock.pbzero.h"
#include "protos/perfetto/trace/etw/etw_event.pbzero.h"
#include "protos/perfetto/trace/etw/etw_event_bundle.pbzero.h"

namespace perfetto {
namespace trace_processor {

using protozero::ProtoDecoder;
using protozero::proto_utils::MakeTagVarInt;
using protozero::proto_utils::ParseVarInt;

using protos::pbzero::BuiltinClock;
using protos::pbzero::EtwTraceEventBundle;

PERFETTO_ALWAYS_INLINE
base::Status EtwTokenizer::TokenizeEtwBundle(TraceBlobView bundle,
                                             PacketSequenceState* state) {
  protos::pbzero::EtwTraceEventBundle::Decoder decoder(bundle.data(),
                                                       bundle.length());
  // Cpu id can either be in the etw bundle or inside the individual
  // EtwTraceEvent. If present at this level, we pass it to the TokenizeEtwEvent
  // in case the EtwTraceEvent does not contain the cpu.
  std::optional<uint32_t> bundle_cpu =
      decoder.has_cpu() ? std::make_optional(decoder.cpu()) : std::nullopt;

  for (auto it = decoder.event(); it; ++it) {
    TokenizeEtwEvent(bundle_cpu, bundle.slice(it->data(), it->size()), state);
  }
  return base::OkStatus();
}

PERFETTO_ALWAYS_INLINE
base::Status EtwTokenizer::TokenizeEtwEvent(
    std::optional<uint32_t> fallback_cpu,
    TraceBlobView event,
    PacketSequenceState* state) {
  const uint8_t* data = event.data();
  const size_t length = event.length();
  ProtoDecoder decoder(data, length);

  protos::pbzero::EtwTraceEvent::Decoder etw_decoder(data, length);
  // Some ETW events lack CPU info; in that case, the bundle may
  // provide it.
  uint32_t cpu;
  if (etw_decoder.has_cpu()) {
    cpu = etw_decoder.cpu();
  } else {
    if (!fallback_cpu.has_value()) {
      return base::ErrStatus(
          "CPU field not found in EtwEvent and/or EtwEventBundle");
    }
    cpu = fallback_cpu.value();
  }

  static constexpr uint32_t kMaxCpuCount = 1024;
  if (PERFETTO_UNLIKELY(cpu >= kMaxCpuCount)) {
    return base::ErrStatus(
        "CPU %u is greater than maximum allowed of %u. This is likely because "
        "of trace corruption",
        cpu, kMaxCpuCount);
  }

  uint64_t raw_timestamp = 0;
  if (etw_decoder.has_timestamp()) {
    raw_timestamp = etw_decoder.timestamp();
  } else {
    return base::ErrStatus("Timestamp field not found in EtwEvent");
  }

  base::StatusOr<int64_t> timestamp = static_cast<int64_t>(raw_timestamp);

  // ClockTracker will increment some error stats if it failed to convert the
  // timestamp so just return.
  if (!timestamp.ok()) {
    return timestamp.status();
  }

  context_->sorter->PushEtwEvent(cpu, *timestamp, std::move(event),
                                 state->current_generation());

  return base::OkStatus();
}

}  // namespace trace_processor
}  // namespace perfetto