Phosphor
trace_log.h
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  */
17 
18 #pragma once
19 
20 #include <atomic>
21 #include <memory>
22 #include <mutex>
23 #include <set>
24 #include <unordered_map>
25 #include <unordered_set>
26 
27 #include "category_registry.h"
28 #include "chunk_lock.h"
29 #include "platform/thread.h"
30 #include "trace_buffer.h"
31 #include "trace_config.h"
32 #include "trace_context.h"
33 #include "trace_event.h"
34 
35 namespace phosphor {
36 
54  class PHOSPHOR_API TraceLog {
55  public:
62  TraceLog(const TraceLogConfig& _config);
63 
74  TraceLog();
75 
76  ~TraceLog();
77 
83  void configure(const TraceLogConfig& _config);
84 
93  static TraceLog& getInstance();
94 
100  void start(const TraceConfig& _trace_config);
101 
108  void start(std::lock_guard<TraceLog>&,
109  const TraceConfig& _trace_config);
110 
114  void stop(bool shutdown = false);
115 
121  void stop(std::lock_guard<TraceLog>&, bool shutdown = false);
122 
137  template <typename T, typename U>
138  void logEvent(const tracepoint_info* tpi,
139  TraceEvent::Type type,
140  T argA,
141  U argB) {
142  if (!enabled) {
143  return;
144  }
145  auto cl = getChunkTenant();
146  if (cl) {
147  cl.mutex()->chunk->addEvent() = TraceEvent(
148  tpi,
149  type,
150  platform::getCurrentThreadIDCached(),
155  }
156  }
157 
171  template <typename T>
172  void logEvent(const tracepoint_info* tpi,
173  TraceEvent::Type type,
174  T argA) {
175  logEvent(tpi, type, argA, NoneType());
176  }
177 
190  void logEvent(const tracepoint_info* tpi,
191  TraceEvent::Type type) {
192  logEvent(tpi, type, NoneType(), NoneType());
193  }
194 
205  const AtomicCategoryStatus& getCategoryStatus(
206  const char* category_group);
207 
218  std::unique_ptr<TraceBuffer> getBuffer();
219 
227  std::unique_ptr<TraceBuffer> getBuffer(std::lock_guard<TraceLog>&);
228 
236  TraceContext getTraceContext();
237 
244  TraceContext getTraceContext(std::lock_guard<TraceLog>&);
245 
251  bool isEnabled() const;
252 
273  void registerThread(const std::string& thread_name = "");
274 
283  void deregisterThread();
284 
290  void lock() {
291  mutex.lock();
292  }
293 
299  void unlock() {
300  mutex.unlock();
301  }
302 
303  TraceConfig getTraceConfig() const;
304 
310  void getStats(StatsCallback& addStats) const;
311 
312  protected:
313 
321  std::unique_lock<ChunkTenant> getChunkTenant();
322 
334  bool replaceChunk(ChunkTenant& ct);
335 
350  void evictThreads(std::lock_guard<TraceLog>& lh);
351 
356  void clearDeregisteredThreads();
357 
361  void maybe_stop(size_t _generation);
362 
372 
380  std::atomic<bool> enabled;
381 
391  mutable std::mutex mutex;
392 
403  std::unique_ptr<TraceBuffer> buffer;
404 
410  std::atomic<size_t> generation;
411 
415  std::unordered_set<ChunkTenant*> registered_chunk_tenants;
416 
421 
425  std::unordered_map<uint64_t, std::string> thread_names;
426 
430  std::set<uint64_t> deregistered_threads;
431  };
432 }
static constexpr TraceArgument asArgument(T arg)
Definition: tracepoint_info.h:31
void logEvent(const tracepoint_info *tpi, TraceEvent::Type type, T argA, U argB)
Definition: trace_log.h:138
void logEvent(const tracepoint_info *tpi, TraceEvent::Type type, T argA)
Definition: trace_log.h:172
Definition: trace_log.h:54
Definition: trace_config.h:188
CategoryRegistry registry
Definition: trace_log.h:420
Definition: category_registry.h:48
Definition: trace_context.h:39
Definition: trace_event.h:40
Type
Definition: trace_event.h:45
void logEvent(const tracepoint_info *tpi, TraceEvent::Type type)
Definition: trace_log.h:190
void lock()
Definition: trace_log.h:290
Definition: chunk_lock.h:168
std::unordered_set< ChunkTenant * > registered_chunk_tenants
Definition: trace_log.h:415
std::atomic< bool > enabled
Definition: trace_log.h:380
std::set< uint64_t > deregistered_threads
Definition: trace_log.h:430
Definition: trace_config.h:123
Definition: trace_argument.h:38
std::atomic< size_t > generation
Definition: trace_log.h:410
std::unordered_map< uint64_t, std::string > thread_names
Definition: trace_log.h:425
std::mutex mutex
Definition: trace_log.h:391
void unlock()
Definition: trace_log.h:299
Definition: category_registry.h:32
std::unique_ptr< TraceBuffer > buffer
Definition: trace_log.h:403
Definition: stats_callback.h:45
TraceConfig trace_config
Definition: trace_log.h:371
static constexpr TraceArgument::Type getType()