proxygen
TraceEvent.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree. An additional grant
7  * of patent rights can be found in the PATENTS file in the same directory.
8  *
9  */
10 #pragma once
11 
17 
18 #include <boost/variant.hpp>
19 
20 #include <folly/Conv.h>
21 
22 #include <map>
23 #include <string>
24 #include <vector>
25 
26 namespace proxygen {
27  // Helpers used to make TraceEventType/TraceFieldType can be used with GLOG
28  FB_EXPORT std::ostream& operator<<(
29  std::ostream& os, TraceEventType eventType);
30  FB_EXPORT std::ostream& operator<<(
31  std::ostream& os, TraceFieldType fieldType);
32 
37 class TraceEvent {
38  public:
39  struct MetaData {
40  public:
41  using MetaDataType =
42  boost::variant<int64_t, std::string, std::vector<std::string>>;
43 
44  template <typename T,
46  void>::type>
47  /* implicit */ MetaData(T value)
48  : value_(folly::to<int64_t>(value)) {}
49 
50  /* implicit */ MetaData(const std::string& value) :
51  value_(value) {
52  }
53 
54  /* implicit */ MetaData(std::string&& value) :
55  value_(std::move(value)) {
56  }
57 
58  /* implicit */ MetaData(const char* value) :
59  value_(std::string(value)) {
60  }
61 
62  /* implicit */ MetaData(const folly::fbstring& value) :
63  value_(value.toStdString()) {
64  }
65 
66  /* implicit */ MetaData(const std::vector<std::string>& value) :
67  value_(value) {
68  }
69 
70  /* implicit */ MetaData(std::vector<std::string>&& value) :
71  value_(std::move(value)) {
72  }
73 
74  template<typename T>
75  T getValueAs() const {
76  ConvVisitor<T> visitor;
77  return boost::apply_visitor(visitor, value_);
78  }
79 
80  const std::type_info& type() const {
81  return value_.type();
82  }
83 
84  template<typename T>
85  struct ConvVisitor : boost::static_visitor<T> {
86  T operator()(const std::vector<std::string>& /* Unused */) const {
87  throw Exception("Not supported for type");
88  }
89 
90  template<typename U>
91  T operator()(U& operand) const {
92  return folly::to<T>(operand);
93  }
94  };
95 
97  };
98 
99  using MetaDataMap = std::map<TraceFieldType, MetaData>;
100 
101  class Iterator {
102  public:
103  explicit Iterator(const TraceEvent& event) :
104  event_(event),
105  itr_(event.metaData_.begin()) {
106  }
107 
109 
110  void next() {
111  ++itr_;
112  }
113 
114  bool isValid() const {
115  return itr_ != event_.metaData_.end();
116  }
117 
119  return itr_->first;
120  }
121 
122  template<typename T>
123  T getValueAs() const {
124  return itr_->second.getValueAs<T>();
125  }
126 
127  const std::type_info& type() const {
128  return itr_->second.type();
129  }
130 
131  private:
133  MetaDataMap::const_iterator itr_;
134 
135  };
136 
137 
138  FB_EXPORT explicit TraceEvent(TraceEventType type, uint32_t parentID = 0);
139 
143  void start(const TimeUtil& tm);
144 
148  void start(TimePoint startTime);
149 
153  void end(const TimeUtil& tm);
154 
158  void end(TimePoint endTime);
159 
163  bool hasStarted() const;
164 
168  bool hasEnded() const;
169 
171  return start_;
172  }
173 
175  return end_;
176  }
177 
179  return type_;
180  }
181 
182  uint32_t getID() const {
183  return id_;
184  }
185 
187  parentID_ = parent;
188  }
189 
191  return parentID_;
192  }
193 
195  return metaData_.count(field);
196  }
197 
198  template<typename T>
200  const auto itr = metaData_.find(field);
201  CHECK(itr != metaData_.end());
202  return itr->second.getValueAs<T>();
203  }
204 
205  void setMetaData(MetaDataMap&& input) {
206  metaData_ = input;
207  }
208 
209  const MetaDataMap& getMetaData() const {
210  return metaData_;
211  }
212 
214  return Iterator(*this);
215  }
216 
217  template<typename T>
218  bool addMeta(TraceFieldType key, T&& value) {
219  MetaData val(std::forward<T>(value));
220  return addMetaInternal(key, std::move(val));
221  }
222 
223  template<typename T>
224  bool addMeta(TraceFieldType key, const T& value) {
225  MetaData val(value);
226  return addMetaInternal(key, std::move(val));
227  }
228 
229  template<typename T>
230  bool readIntMeta(TraceFieldType key, T& dest) const {
232  "readIntMeta should take an intergral type of paremeter");
233  return readMeta(key, dest);
234  }
235 
236  bool readBoolMeta(TraceFieldType key, bool& dest) const;
237 
238  bool readStrMeta(TraceFieldType key, std::string& dest) const;
239 
240  std::string toString() const;
241 
242  friend std::ostream& operator << (std::ostream& out,
243  const TraceEvent& event);
244 
245  friend class Iterator;
246 
247  private:
248  template<typename T>
249  bool readMeta(TraceFieldType key, T& dest) const {
250  const auto itr = metaData_.find(key);
251  if (itr != metaData_.end()) {
252  try {
253  dest = itr->second.getValueAs<T>();
254  return true;
255  } catch (const std::exception&) {
256  return false;
257  }
258  }
259  return false;
260  }
261 
263 
264  enum State {
266  STARTED = 1,
267  ENDED = 2,
268  };
269 
277 
278 };
279 
280 template<>
282  boost::static_visitor<std::vector<std::string>> {
283  std::vector<std::string> operator()(
284  const std::vector<std::string>& operand) const {
285  return operand;
286  }
287 
288  template<typename U>
289  std::vector<std::string> operator()(U& /* Unused */) const {
290  throw Exception("Not supported for type");
291  }
292 };
293 
294 template<>
296  boost::static_visitor<std::string> {
297  std::string operator()(const std::vector<std::string>& operand) const;
298 
299  template<typename U>
300  std::string operator()(U& operand) const {
301  return folly::to<std::string>(operand);
302  }
303 };
304 }
#define FB_EXPORT
Definition: Export.h:26
#define T(v)
Definition: http_parser.c:233
TraceEventType type_
Definition: TraceEvent.h:271
std::ostream & operator<<(std::ostream &os, const HeaderTable &table)
MetaDataMap metaData_
Definition: TraceEvent.h:276
bool readBoolMeta(TraceFieldType key, bool &dest) const
Definition: TraceEvent.cpp:65
Map field(FieldType Class::*field)
Definition: Base.h:641
bool readIntMeta(TraceFieldType key, T &dest) const
Definition: TraceEvent.h:230
MetaData(const std::vector< std::string > &value)
Definition: TraceEvent.h:66
const std::type_info & type() const
Definition: TraceEvent.h:127
MetaData(const folly::fbstring &value)
Definition: TraceEvent.h:62
friend std::ostream & operator<<(std::ostream &out, const TraceEvent &event)
Definition: TraceEvent.cpp:116
FB_EXPORT TraceEvent(TraceEventType type, uint32_t parentID=0)
Definition: TraceEvent.cpp:30
uint32_t getParentID() const
Definition: TraceEvent.h:190
const TraceEvent & event_
Definition: TraceEvent.h:132
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::string toString() const
Definition: TraceEvent.cpp:83
dest
Definition: upload.py:394
STL namespace.
auto begin(TestAdlIterable &instance)
Definition: ForeachTest.cpp:56
double val
Definition: String.cpp:273
MetaDataMap::const_iterator itr_
Definition: TraceEvent.h:133
Iterator getMetaDataItr() const
Definition: TraceEvent.h:213
bool hasStarted() const
Definition: TraceEvent.cpp:57
bool hasEnded() const
Definition: TraceEvent.cpp:61
bool readStrMeta(TraceFieldType key, std::string &dest) const
Definition: TraceEvent.cpp:69
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
MetaData(const std::string &value)
Definition: TraceEvent.h:50
std::string toStdString(const folly::fbstring &s)
Definition: String.h:41
bool readMeta(TraceFieldType key, T &dest) const
Definition: TraceEvent.h:249
std::map< TraceFieldType, MetaData > MetaDataMap
Definition: TraceEvent.h:99
TraceEventType getType() const
Definition: TraceEvent.h:178
MetaData(std::vector< std::string > &&value)
Definition: TraceEvent.h:70
Iterator(const TraceEvent &event)
Definition: TraceEvent.h:103
TimePoint getStartTime() const
Definition: TraceEvent.h:170
void end(const TimeUtil &tm)
Definition: TraceEvent.cpp:47
std::enable_if< detail::is_chrono_conversion< Tgt, Src >::value, Tgt >::type to(const Src &value)
Definition: Conv.h:677
uint32_t getID() const
Definition: TraceEvent.h:182
boost::variant< int64_t, std::string, std::vector< std::string >> MetaDataType
Definition: TraceEvent.h:42
TimePoint getEndTime() const
Definition: TraceEvent.h:174
T operator()(const std::vector< std::string > &) const
Definition: TraceEvent.h:86
MetaData(std::string &&value)
Definition: TraceEvent.h:54
const MetaDataMap & getMetaData() const
Definition: TraceEvent.h:209
Definition: Traits.h:588
bool addMeta(TraceFieldType key, T &&value)
Definition: TraceEvent.h:218
static const char *const value
Definition: Conv.cpp:50
bool addMeta(TraceFieldType key, const T &value)
Definition: TraceEvent.h:224
void setMetaData(MetaDataMap &&input)
Definition: TraceEvent.h:205
SteadyClock::time_point TimePoint
Definition: Time.h:25
T getTraceFieldDataAs(TraceFieldType field) const
Definition: TraceEvent.h:199
const char * string
Definition: Conv.cpp:212
decltype(auto) apply_visitor(Visitor &&visitor, const DiscriminatedPtr< Args... > &variant)
void start(const TimeUtil &tm)
Definition: TraceEvent.cpp:37
void setParentID(uint32_t parent)
Definition: TraceEvent.h:186
std::vector< std::string > operator()(const std::vector< std::string > &operand) const
Definition: TraceEvent.h:283
bool hasTraceField(TraceFieldType field) const
Definition: TraceEvent.h:194
MetaData(const char *value)
Definition: TraceEvent.h:58
friend class Iterator
Definition: TraceEvent.h:245
FB_EXPORT bool addMetaInternal(TraceFieldType key, MetaData &&val)
Definition: TraceEvent.cpp:72
folly::Function< void()> parent
Definition: AtFork.cpp:34
TraceFieldType getKey() const
Definition: TraceEvent.h:118
const std::type_info & type() const
Definition: TraceEvent.h:80