Phosphor
trace_event.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  * Copyright 2016 Couchbase, Inc
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
22 #pragma once
23 
24 #include <array>
25 #include <chrono>
26 #include <cstdint>
27 #include <iostream>
28 #include <string>
29 #include <thread>
30 #include <type_traits>
31 
32 #include "phosphor/platform/core.h"
33 #include "trace_argument.h"
34 #include "tracepoint_info.h"
35 
36 namespace phosphor {
37 
38  constexpr auto arg_count = 2;
39 
40  class PHOSPHOR_API TraceEvent {
41  public:
45  enum class Type : char {
46  AsyncStart,
47  AsyncEnd,
48  SyncStart,
49  SyncEnd,
50  Instant,
51  GlobalInstant
52  };
53 
57  TraceEvent() = default;
58 
67  TraceEvent(const tracepoint_info* _tpi,
68  Type _type,
69  uint32_t _thread_id,
70  std::array<TraceArgument, arg_count>&& _args,
71  std::array<TraceArgument::Type, arg_count>&& _arg_types);
72 
78  std::string to_string() const;
79 
85  std::string to_json() const;
86 
92  static const char* typeToString(Type type);
93 
97  const char* getName() const;
98 
102  const char* getCategory() const;
103 
107  Type getType() const;
108 
112  uint64_t getThreadID() const;
113 
117  const std::array<TraceArgument, arg_count>& getArgs() const;
118 
122  const std::array<TraceArgument::Type, arg_count>& getArgTypes() const;
123 
127  const std::array<const char*, arg_count>& getArgNames() const;
128 
133  int64_t getTime() const;
134 
135  protected:
136  class ToJsonResult {
137  public:
138  const char* type;
139  std::string extras;
140  };
141 
150  ToJsonResult typeToJSON() const;
151 
152  private:
153  const tracepoint_info* tpi;
154  std::array<TraceArgument, arg_count> args;
155  uint64_t time;
156 
157  uint32_t thread_id;
158  std::array<TraceArgument::Type, arg_count> arg_types;
159  Type type;
160  };
161 
174  PHOSPHOR_API
175  std::ostream& operator<<(std::ostream& os, const TraceEvent& trace_event);
176 
177  static_assert(
178  sizeof(TraceEvent) <= 64,
179  "TraceEvent should fit inside a cache-line for performance reasons");
180 }
Definition: trace_event.h:136
Definition: tracepoint_info.h:31
Definition: trace_event.h:40
Type
Definition: trace_event.h:45
Definition: category_registry.h:32