1 /* 2 * Copyright (C) 2021 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 #ifndef INCLUDE_PERFETTO_TRACING_TRACK_EVENT_ARGS_H_ 18 #define INCLUDE_PERFETTO_TRACING_TRACK_EVENT_ARGS_H_ 19 20 #include "perfetto/tracing/event_context.h" 21 #include "perfetto/tracing/track.h" 22 23 namespace perfetto { 24 25 // A helper to add |flow_id| as a non-terminating flow id to TRACE_EVENT 26 // inline: TRACE_EVENT(..., perfetto::Flow::ProcessScoped(42)); 27 class Flow { 28 public: 29 // |flow_id| which is local within a given process (e.g. atomic counter xor'ed 30 // with feature-specific value). This value is xor'ed with Perfetto's internal 31 // process track id to attempt to ensure that it's globally-unique. ProcessScoped(uint64_t flow_id)32 static PERFETTO_ALWAYS_INLINE inline Flow ProcessScoped(uint64_t flow_id) { 33 return Global(flow_id ^ Track::process_uuid); 34 } 35 36 // Same as above, but construct an id from a pointer. 37 // NOTE: After the object is destroyed, the value of |ptr| can be reused for a 38 // different object (in particular if the object is allocated on a stack). 39 // Please ensure that you emit a trace event with the flow id of 40 // perfetto::TerminatingFlow::FromPointer(this) from the destructor of the 41 // object to avoid accidental conflicts. FromPointer(void * ptr)42 static PERFETTO_ALWAYS_INLINE inline Flow FromPointer(void* ptr) { 43 return ProcessScoped(reinterpret_cast<uintptr_t>(ptr)); 44 } 45 46 // Add the |flow_id|. The caller is responsible for ensuring that it's 47 // globally-unique (e.g. by generating a random value). This should be used 48 // only for flow events which cross the process boundary (e.g. IPCs). Global(uint64_t flow_id)49 static PERFETTO_ALWAYS_INLINE inline Flow Global(uint64_t flow_id) { 50 return Flow(flow_id); 51 } 52 53 // TODO(altimin): Remove once converting a single usage in Chromium. Flow(uint64_t flow_id)54 explicit constexpr Flow(uint64_t flow_id) : flow_id_(flow_id) {} 55 operator()56 void operator()(EventContext& ctx) const { 57 ctx.event()->add_flow_ids(flow_id_); 58 } 59 60 private: 61 uint64_t flow_id_; 62 }; 63 64 // A helper to add a given |flow_id| as a terminating flow to TRACE_EVENT 65 // inline. 66 class TerminatingFlow { 67 public: 68 // See `Flow::ProcessScoped(uint64_t)`. ProcessScoped(uint64_t flow_id)69 static PERFETTO_ALWAYS_INLINE inline TerminatingFlow ProcessScoped( 70 uint64_t flow_id) { 71 return Global(flow_id ^ Track::process_uuid); 72 } 73 74 // See `Flow::FromPointer(void*)`. FromPointer(void * ptr)75 static PERFETTO_ALWAYS_INLINE inline TerminatingFlow FromPointer(void* ptr) { 76 return ProcessScoped(reinterpret_cast<uintptr_t>(ptr)); 77 } 78 79 // See `Flow::Global(uint64_t)`. Global(uint64_t flow_id)80 static PERFETTO_ALWAYS_INLINE inline TerminatingFlow Global( 81 uint64_t flow_id) { 82 TerminatingFlow tf; 83 tf.flow_id_ = flow_id; 84 return tf; 85 } 86 operator()87 void operator()(EventContext& ctx) const { 88 ctx.event()->add_terminating_flow_ids(flow_id_); 89 } 90 91 private: 92 uint64_t flow_id_; 93 }; 94 95 } // namespace perfetto 96 97 #endif // INCLUDE_PERFETTO_TRACING_TRACK_EVENT_ARGS_H_ 98