blob: 4eab33eb03b001e6bf9425d81fab4a67d74af7cb [file] [log] [blame]
Sami Kyostila91f38e42020-02-03 13:59:23 +00001/*
2 * Copyright (C) 2020 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 "perfetto/tracing/track_event_legacy.h"
Mohit Saini42326bf2022-03-28 10:54:55 +010018#include "perfetto/ext/base/hash.h"
Sami Kyostila91f38e42020-02-03 13:59:23 +000019
20#include "perfetto/tracing/track.h"
21
22namespace perfetto {
23namespace legacy {
24
25template <>
Sami Kyostila481bc4a2021-04-29 08:54:26 +000026ThreadTrack ConvertThreadId(const PerfettoLegacyCurrentThreadId&) {
27 // Because of the short-circuit in PERFETTO_INTERNAL_LEGACY_EVENT, we should
28 // never get here.
29 PERFETTO_DCHECK(false);
30 return ThreadTrack::Current();
Sami Kyostila91f38e42020-02-03 13:59:23 +000031}
32
33} // namespace legacy
34
35namespace internal {
36
37void LegacyTraceId::Write(protos::pbzero::TrackEvent::LegacyEvent* event,
38 uint32_t event_flags) const {
39 // Legacy flow events always use bind_id.
Sami Kyostilab9b24d12020-03-03 18:13:30 +000040 if (event_flags &
41 (legacy::kTraceEventFlagFlowOut | legacy::kTraceEventFlagFlowIn)) {
Sami Kyostila91f38e42020-02-03 13:59:23 +000042 // Flow bind_ids don't have scopes, so we need to mangle in-process ones to
43 // avoid collisions.
Sami Kyostilab9b24d12020-03-03 18:13:30 +000044 if (id_flags_ & legacy::kTraceEventFlagHasLocalId) {
Sami Kyostila91f38e42020-02-03 13:59:23 +000045 event->set_bind_id(raw_id_ ^ ProcessTrack::Current().uuid);
46 } else {
47 event->set_bind_id(raw_id_);
48 }
49 return;
50 }
51
Sami Kyostilab9b24d12020-03-03 18:13:30 +000052 uint32_t scope_flags = id_flags_ & (legacy::kTraceEventFlagHasId |
53 legacy::kTraceEventFlagHasLocalId |
54 legacy::kTraceEventFlagHasGlobalId);
Mohit Saini42326bf2022-03-28 10:54:55 +010055 uint64_t id = raw_id_;
56 if (scope_ && scope_flags != legacy::kTraceEventFlagHasGlobalId) {
Alexander Timinc2bb1b42022-10-17 18:46:16 +000057 id = base::Hasher::Combine(id, scope_);
Mohit Saini42326bf2022-03-28 10:54:55 +010058 }
59
Sami Kyostila91f38e42020-02-03 13:59:23 +000060 switch (scope_flags) {
Sami Kyostilab9b24d12020-03-03 18:13:30 +000061 case legacy::kTraceEventFlagHasId:
Mohit Saini42326bf2022-03-28 10:54:55 +010062 event->set_unscoped_id(id);
Sami Kyostila91f38e42020-02-03 13:59:23 +000063 break;
Sami Kyostilab9b24d12020-03-03 18:13:30 +000064 case legacy::kTraceEventFlagHasLocalId:
Mohit Saini42326bf2022-03-28 10:54:55 +010065 event->set_local_id(id);
Sami Kyostila91f38e42020-02-03 13:59:23 +000066 break;
Sami Kyostilab9b24d12020-03-03 18:13:30 +000067 case legacy::kTraceEventFlagHasGlobalId:
Mohit Saini42326bf2022-03-28 10:54:55 +010068 event->set_global_id(id);
Sami Kyostila91f38e42020-02-03 13:59:23 +000069 break;
70 }
71 if (scope_)
72 event->set_id_scope(scope_);
73}
74
75} // namespace internal
76} // namespace perfetto