proxygen
TraceEvent.cpp
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  */
11 
12 #include <atomic>
13 #include <sstream>
14 #include <string>
15 #include <folly/json.h>
16 
17 namespace proxygen {
18 
19  // Helpers used to make TraceEventType/TraceFieldType can be used with GLOG
20 std::ostream& operator<<(std::ostream& os, TraceEventType eventType) {
21  os << getTraceEventTypeString(eventType);
22  return os;
23 }
24 
25 std::ostream& operator<<(std::ostream& os, TraceFieldType fieldType) {
26  os << getTraceFieldTypeString(fieldType);
27  return os;
28 }
29 
31  type_(type),
32  parentID_(parentID) {
33  static std::atomic<uint32_t> counter(0);
34  id_ = counter++;
35 }
36 
37 void TraceEvent::start(const TimeUtil& tm) {
38  stateFlags_ |= State::STARTED;
39  start_ = tm.now();
40 }
41 
42 void TraceEvent::start(TimePoint startTime) {
43  stateFlags_ |= State::STARTED;
44  start_ = startTime;
45 }
46 
47 void TraceEvent::end(const TimeUtil& tm) {
48  stateFlags_ |= State::ENDED;
49  end_ = tm.now();
50 }
51 
52 void TraceEvent::end(TimePoint endTime) {
53  stateFlags_ |= State::ENDED;
54  end_ = endTime;
55 }
56 
57 bool TraceEvent::hasStarted() const {
58  return stateFlags_ & State::STARTED;
59 }
60 
61 bool TraceEvent::hasEnded() const {
62  return stateFlags_ & State::ENDED;
63 }
64 
66  return readMeta(key, dest);
67 }
68 
70  return readMeta(key, dest);
71 }
73  auto rc = metaData_.emplace(key, value);
74 
75  // replace if key already exist
76  if (!rc.second) {
77  rc.first->second = value;
78  }
79 
80  return rc.second;
81 }
82 
84  std::ostringstream out;
85  int startSinceEpoch = std::chrono::duration_cast<std::chrono::milliseconds>(
86  start_.time_since_epoch()).count();
87  int endSinceEpoch = std::chrono::duration_cast<std::chrono::milliseconds>(
88  end_.time_since_epoch()).count();
89  out << "TraceEvent(";
90  out << "type='" << getTraceEventTypeString(type_) << "', ";
91  out << "id='" << id_ << "', ";
92  out << "parentID='" << parentID_ << "', ";
93  out << "start='" << startSinceEpoch << "', ";
94  out << "end='" << endSinceEpoch << "', ";
95  out << "metaData='{";
96  auto itr = getMetaDataItr();
97  while (itr.isValid()) {
98  out << getTraceFieldTypeString(itr.getKey()) << ": "
99  << itr.getValueAs<std::string>() << ", ";
100  itr.next();
101  }
102  out << "}')";
103  return out.str();
104 }
105 
107  const std::vector<std::string>& operand) const {
108  // parse string vector to json string.
110  for (auto item : operand) {
111  data.push_back(item);
112  }
113  return folly::toJson(data);
114 }
115 
116 std::ostream& operator << (std::ostream& out, const TraceEvent& event) {
117  out << event.toString();
118  return out;
119 }
120 }
const std::string & getTraceFieldTypeString(TraceFieldType type)
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
PskType type
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
std::string toString() const
Definition: TraceEvent.cpp:83
dest
Definition: upload.py:394
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
bool readMeta(TraceFieldType key, T &dest) const
Definition: TraceEvent.h:249
void end(const TimeUtil &tm)
Definition: TraceEvent.cpp:47
Type type_
Definition: JSONSchema.cpp:208
T operator()(const std::vector< std::string > &) const
Definition: TraceEvent.h:86
virtual std::chrono::time_point< ClockType > now() const
Definition: Time.h:174
void push_back(dynamic const &)
Definition: dynamic-inl.h:969
static const char *const value
Definition: Conv.cpp:50
int * count
SteadyClock::time_point TimePoint
Definition: Time.h:25
std::atomic< int > counter
const char * string
Definition: Conv.cpp:212
void start(const TimeUtil &tm)
Definition: TraceEvent.cpp:37
static void array(EmptyArrayTag)
Definition: dynamic-inl.h:233
std::string toJson(dynamic const &dyn)
Definition: json.cpp:915
FB_EXPORT bool addMetaInternal(TraceFieldType key, MetaData &&val)
Definition: TraceEvent.cpp:72
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43
const std::string & getTraceEventTypeString(TraceEventType type)