1 /* 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" 18 #include "perfetto/ext/base/hash.h" 19 20 #include "perfetto/tracing/track.h" 21 22 namespace perfetto { 23 namespace legacy { 24 25 template <> ConvertThreadId(const PerfettoLegacyCurrentThreadId &)26ThreadTrack 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(); 31 } 32 33 } // namespace legacy 34 35 namespace internal { 36 Write(protos::pbzero::TrackEvent::LegacyEvent * event,uint32_t event_flags) const37void LegacyTraceId::Write(protos::pbzero::TrackEvent::LegacyEvent* event, 38 uint32_t event_flags) const { 39 // Legacy flow events always use bind_id. 40 if (event_flags & 41 (legacy::kTraceEventFlagFlowOut | legacy::kTraceEventFlagFlowIn)) { 42 // Flow bind_ids don't have scopes, so we need to mangle in-process ones to 43 // avoid collisions. 44 if (id_flags_ & legacy::kTraceEventFlagHasLocalId) { 45 event->set_bind_id(raw_id_ ^ ProcessTrack::Current().uuid); 46 } else { 47 event->set_bind_id(raw_id_); 48 } 49 return; 50 } 51 52 uint32_t scope_flags = id_flags_ & (legacy::kTraceEventFlagHasId | 53 legacy::kTraceEventFlagHasLocalId | 54 legacy::kTraceEventFlagHasGlobalId); 55 uint64_t id = raw_id_; 56 if (scope_ && scope_flags != legacy::kTraceEventFlagHasGlobalId) { 57 id = base::Hasher::Combine(id, scope_); 58 } 59 60 switch (scope_flags) { 61 case legacy::kTraceEventFlagHasId: 62 event->set_unscoped_id(id); 63 break; 64 case legacy::kTraceEventFlagHasLocalId: 65 event->set_local_id(id); 66 break; 67 case legacy::kTraceEventFlagHasGlobalId: 68 event->set_global_id(id); 69 break; 70 } 71 if (scope_) 72 event->set_id_scope(scope_); 73 } 74 75 } // namespace internal 76 } // namespace perfetto 77