xref: /aosp_15_r20/external/perfetto/src/tracing/event_context.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2019 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/event_context.h"
18 
19 #include "perfetto/tracing/internal/track_event_interned_fields.h"
20 #include "protos/perfetto/trace/interned_data/interned_data.pbzero.h"
21 #include "protos/perfetto/trace/track_event/track_event.pbzero.h"
22 
23 namespace perfetto {
24 
EventContext(TraceWriterBase * trace_writer,EventContext::TracePacketHandle trace_packet,internal::TrackEventIncrementalState * incremental_state,internal::TrackEventTlsState * tls_state)25 EventContext::EventContext(
26     TraceWriterBase* trace_writer,
27     EventContext::TracePacketHandle trace_packet,
28     internal::TrackEventIncrementalState* incremental_state,
29     internal::TrackEventTlsState* tls_state)
30     : trace_writer_(trace_writer),
31       trace_packet_(std::move(trace_packet)),
32       event_(trace_packet_->set_track_event()),
33       incremental_state_(incremental_state),
34       tls_state_(tls_state) {}
35 
~EventContext()36 EventContext::~EventContext() {
37   if (!trace_packet_)
38     return;
39 
40   // When the track event is finalized (i.e., the context is destroyed), we
41   // should flush any newly seen interned data to the trace. The data has
42   // earlier been written to a heap allocated protobuf message
43   // (|serialized_interned_data|). Here we just need to flush it to the main
44   // trace.
45   auto& serialized_interned_data = incremental_state_->serialized_interned_data;
46   if (PERFETTO_UNLIKELY(!serialized_interned_data.empty())) {
47     auto ranges = serialized_interned_data.GetRanges();
48     trace_packet_->AppendScatteredBytes(
49         perfetto::protos::pbzero::TracePacket::kInternedDataFieldNumber,
50         &ranges[0], ranges.size());
51 
52     // Reset the message but keep one buffer allocated for future use.
53     serialized_interned_data.Reset();
54   }
55 }
56 
AddDebugAnnotation(const char * name)57 protos::pbzero::DebugAnnotation* EventContext::AddDebugAnnotation(
58     const char* name) {
59   auto annotation = event()->add_debug_annotations();
60   annotation->set_name_iid(
61       internal::InternedDebugAnnotationName::Get(this, name));
62   return annotation;
63 }
64 
AddDebugAnnotation(::perfetto::DynamicString name)65 protos::pbzero::DebugAnnotation* EventContext::AddDebugAnnotation(
66     ::perfetto::DynamicString name) {
67   auto annotation = event()->add_debug_annotations();
68   annotation->set_name(name.value);
69   return annotation;
70 }
71 
GetTlsUserData(const void * key)72 TrackEventTlsStateUserData* EventContext::GetTlsUserData(const void* key) {
73   PERFETTO_CHECK(tls_state_);
74   PERFETTO_CHECK(key);
75   auto it = tls_state_->user_data.find(key);
76   if (it != tls_state_->user_data.end()) {
77     return it->second.get();
78   }
79   return nullptr;
80 }
81 
SetTlsUserData(const void * key,std::unique_ptr<TrackEventTlsStateUserData> data)82 void EventContext::SetTlsUserData(
83     const void* key,
84     std::unique_ptr<TrackEventTlsStateUserData> data) {
85   PERFETTO_CHECK(tls_state_);
86   PERFETTO_CHECK(key);
87   tls_state_->user_data[key] = std::move(data);
88 }
89 
90 }  // namespace perfetto
91