• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_INTERNAL_WRITE_TRACK_EVENT_ARGS_H_
18 #define INCLUDE_PERFETTO_TRACING_INTERNAL_WRITE_TRACK_EVENT_ARGS_H_
19 
20 #include "perfetto/base/compiler.h"
21 #include "perfetto/tracing/event_context.h"
22 #include "perfetto/tracing/traced_proto.h"
23 #include "perfetto/tracing/track_event_args.h"
24 
25 namespace perfetto {
26 namespace internal {
27 
28 // No arguments means that we don't have to write anything.
WriteTrackEventArgs(EventContext)29 PERFETTO_ALWAYS_INLINE inline void WriteTrackEventArgs(EventContext) {}
30 
31 namespace {
32 
33 // A template helper for determining whether a type can be used as a track event
34 // lambda, i.e., it has the signature "void(EventContext)". This is achieved by
35 // checking that we can pass an EventContext value (the inner declval) into a T
36 // instance (the outer declval). If this is a valid expression, the result
37 // evaluates to sizeof(0), i.e., true.
38 // TODO(skyostil): Replace this with std::is_convertible<std::function<...>>
39 // once we have C++14.
40 template <typename T>
41 static constexpr bool IsValidTraceLambdaImpl(
42     typename std::enable_if<static_cast<bool>(
43         sizeof(std::declval<T>()(std::declval<EventContext>()), 0))>::type* =
44         nullptr) {
45   return true;
46 }
47 
48 template <typename T>
IsValidTraceLambdaImpl(...)49 static constexpr bool IsValidTraceLambdaImpl(...) {
50   return false;
51 }
52 
53 template <typename T>
IsValidTraceLambda()54 static constexpr bool IsValidTraceLambda() {
55   return IsValidTraceLambdaImpl<T>(nullptr);
56 }
57 
58 template <typename T>
59 static constexpr bool IsValidTraceLambdaTakingReferenceImpl(
60     typename std::enable_if<static_cast<bool>(
61         sizeof(std::declval<T>()(std::declval<EventContext&>()), 0))>::type* =
62         nullptr) {
63   return true;
64 }
65 
66 template <typename T>
IsValidTraceLambdaTakingReferenceImpl(...)67 static constexpr bool IsValidTraceLambdaTakingReferenceImpl(...) {
68   return false;
69 }
70 
71 template <typename T>
IsValidTraceLambdaTakingReference()72 static constexpr bool IsValidTraceLambdaTakingReference() {
73   return IsValidTraceLambdaTakingReferenceImpl<T>(nullptr);
74 }
75 
76 }  // namespace
77 
78 // Write an old-style lambda taking an EventContext (without a reference)
79 // as it will consume EventContext via std::move, it can only be the last
80 // argument.
81 template <typename ArgumentFunction,
82           typename ArgFunctionCheck = typename std::enable_if<
83               IsValidTraceLambda<ArgumentFunction>()>::type>
WriteTrackEventArgs(EventContext event_ctx,ArgumentFunction arg_function)84 PERFETTO_ALWAYS_INLINE void WriteTrackEventArgs(EventContext event_ctx,
85                                                 ArgumentFunction arg_function) {
86   arg_function(std::move(event_ctx));
87 }
88 
89 // Forward-declare the specification for writing untyped arguments to ensure
90 // that typed specification could recursively pick it up.
91 template <typename ArgValue, typename... Args>
92 PERFETTO_ALWAYS_INLINE void WriteTrackEventArgs(EventContext event_ctx,
93                                                 const char* arg_name,
94                                                 ArgValue&& arg_value,
95                                                 Args&&... args);
96 
97 template <typename FieldMetadataType, typename ArgValue, typename... Args>
98 PERFETTO_ALWAYS_INLINE void WriteTrackEventArgs(
99     EventContext event_ctx,
100     protozero::proto_utils::internal::FieldMetadataHelper<FieldMetadataType>
101         field_name,
102     ArgValue&& arg_value,
103     Args&&... args);
104 
105 template <typename ArgumentFunction,
106           typename... Args,
107           typename ArgFunctionCheck = typename std::enable_if<
108               IsValidTraceLambdaTakingReference<ArgumentFunction>()>::type>
WriteTrackEventArgs(EventContext event_ctx,ArgumentFunction arg_function,Args &&...args)109 PERFETTO_ALWAYS_INLINE void WriteTrackEventArgs(EventContext event_ctx,
110                                                 ArgumentFunction arg_function,
111                                                 Args&&... args) {
112   // |arg_function| will capture EventContext by reference, so std::move isn't
113   // needed.
114   arg_function(event_ctx);
115 
116   WriteTrackEventArgs(std::move(event_ctx), std::forward<Args>(args)...);
117 }
118 
119 // Write one typed message and recursively write the rest of the arguments.
120 template <typename FieldMetadataType, typename ArgValue, typename... Args>
WriteTrackEventArgs(EventContext event_ctx,protozero::proto_utils::internal::FieldMetadataHelper<FieldMetadataType> field_name,ArgValue && arg_value,Args &&...args)121 PERFETTO_ALWAYS_INLINE void WriteTrackEventArgs(
122     EventContext event_ctx,
123     protozero::proto_utils::internal::FieldMetadataHelper<FieldMetadataType>
124         field_name,
125     ArgValue&& arg_value,
126     Args&&... args) {
127   static_assert(std::is_base_of<protozero::proto_utils::FieldMetadataBase,
128                                 FieldMetadataType>::value,
129                 "");
130   static_assert(
131       std::is_base_of<protos::pbzero::TrackEvent,
132                       typename FieldMetadataType::message_type>::value,
133       "Only fields of TrackEvent (and TrackEvent's extensions) can "
134       "be passed to TRACE_EVENT");
135   auto track_event_proto = event_ctx.Wrap(
136       event_ctx.event<typename FieldMetadataType::message_type>());
137   WriteTracedProtoField(track_event_proto, field_name,
138                         std::forward<ArgValue>(arg_value));
139   WriteTrackEventArgs(std::move(event_ctx), std::forward<Args>(args)...);
140 }
141 
142 // Write one debug annotation and recursively write the rest of the arguments.
143 template <typename ArgValue, typename... Args>
WriteTrackEventArgs(EventContext event_ctx,const char * arg_name,ArgValue && arg_value,Args &&...args)144 PERFETTO_ALWAYS_INLINE void WriteTrackEventArgs(EventContext event_ctx,
145                                                 const char* arg_name,
146                                                 ArgValue&& arg_value,
147                                                 Args&&... args) {
148   event_ctx.AddDebugAnnotation(arg_name, std::forward<ArgValue>(arg_value));
149   WriteTrackEventArgs(std::move(event_ctx), std::forward<Args>(args)...);
150 }
151 
152 }  // namespace internal
153 }  // namespace perfetto
154 
155 #endif  // INCLUDE_PERFETTO_TRACING_INTERNAL_WRITE_TRACK_EVENT_ARGS_H_
156