Add a Notification Settings Button to all web notifications behind the web platform...
[chromium-blink-merge.git] / base / trace_event / trace_log.h
blob3acb9181379c66a5530576ac2c970bda03a0f9f0
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef BASE_TRACE_EVENT_TRACE_LOG_H_
6 #define BASE_TRACE_EVENT_TRACE_LOG_H_
8 #include "base/trace_event/memory_dump_provider.h"
9 #include "base/trace_event/trace_config.h"
10 #include "base/trace_event/trace_event_impl.h"
12 // Older style trace macros with explicit id and extra data
13 // Only these macros result in publishing data to ETW as currently implemented.
14 // TODO(georgesak): Update/replace these with new ETW macros.
15 #define TRACE_EVENT_BEGIN_ETW(name, id, extra) \
16 base::trace_event::TraceLog::AddTraceEventEtw( \
17 TRACE_EVENT_PHASE_BEGIN, name, reinterpret_cast<const void*>(id), extra)
19 #define TRACE_EVENT_END_ETW(name, id, extra) \
20 base::trace_event::TraceLog::AddTraceEventEtw( \
21 TRACE_EVENT_PHASE_END, name, reinterpret_cast<const void*>(id), extra)
23 #define TRACE_EVENT_INSTANT_ETW(name, id, extra) \
24 base::trace_event::TraceLog::AddTraceEventEtw( \
25 TRACE_EVENT_PHASE_INSTANT, name, reinterpret_cast<const void*>(id), \
26 extra)
28 namespace base {
30 template <typename Type>
31 struct DefaultSingletonTraits;
32 class RefCountedString;
34 namespace trace_event {
36 class TraceBuffer;
37 class TraceBufferChunk;
38 class TraceEvent;
39 class TraceEventMemoryOverhead;
40 class TraceSamplingThread;
42 struct BASE_EXPORT TraceLogStatus {
43 TraceLogStatus();
44 ~TraceLogStatus();
45 size_t event_capacity;
46 size_t event_count;
49 class BASE_EXPORT TraceLog : public MemoryDumpProvider {
50 public:
51 enum Mode {
52 DISABLED = 0,
53 RECORDING_MODE,
54 MONITORING_MODE,
57 // The pointer returned from GetCategoryGroupEnabledInternal() points to a
58 // value with zero or more of the following bits. Used in this class only.
59 // The TRACE_EVENT macros should only use the value as a bool.
60 // These values must be in sync with macro values in TraceEvent.h in Blink.
61 enum CategoryGroupEnabledFlags {
62 // Category group enabled for the recording mode.
63 ENABLED_FOR_RECORDING = 1 << 0,
64 // Category group enabled for the monitoring mode.
65 ENABLED_FOR_MONITORING = 1 << 1,
66 // Category group enabled by SetEventCallbackEnabled().
67 ENABLED_FOR_EVENT_CALLBACK = 1 << 2,
68 // Category group enabled to export events to ETW.
69 ENABLED_FOR_ETW_EXPORT = 1 << 3
72 static TraceLog* GetInstance();
74 // Get set of known category groups. This can change as new code paths are
75 // reached. The known category groups are inserted into |category_groups|.
76 void GetKnownCategoryGroups(std::vector<std::string>* category_groups);
78 // Retrieves a copy (for thread-safety) of the current TraceConfig.
79 TraceConfig GetCurrentTraceConfig() const;
81 // Initializes the thread-local event buffer, if not already initialized and
82 // if the current thread supports that (has a message loop).
83 void InitializeThreadLocalEventBufferIfSupported();
85 // Enables normal tracing (recording trace events in the trace buffer).
86 // See TraceConfig comments for details on how to control what categories
87 // will be traced. If tracing has already been enabled, |category_filter| will
88 // be merged into the current category filter.
89 void SetEnabled(const TraceConfig& trace_config, Mode mode);
91 // Disables normal tracing for all categories.
92 void SetDisabled();
94 bool IsEnabled() { return mode_ != DISABLED; }
96 // The number of times we have begun recording traces. If tracing is off,
97 // returns -1. If tracing is on, then it returns the number of times we have
98 // recorded a trace. By watching for this number to increment, you can
99 // passively discover when a new trace has begun. This is then used to
100 // implement the TRACE_EVENT_IS_NEW_TRACE() primitive.
101 int GetNumTracesRecorded();
103 #if defined(OS_ANDROID)
104 void StartATrace();
105 void StopATrace();
106 void AddClockSyncMetadataEvent();
107 #endif
109 // Enabled state listeners give a callback when tracing is enabled or
110 // disabled. This can be used to tie into other library's tracing systems
111 // on-demand.
112 class BASE_EXPORT EnabledStateObserver {
113 public:
114 virtual ~EnabledStateObserver() = default;
116 // Called just after the tracing system becomes enabled, outside of the
117 // |lock_|. TraceLog::IsEnabled() is true at this point.
118 virtual void OnTraceLogEnabled() = 0;
120 // Called just after the tracing system disables, outside of the |lock_|.
121 // TraceLog::IsEnabled() is false at this point.
122 virtual void OnTraceLogDisabled() = 0;
124 void AddEnabledStateObserver(EnabledStateObserver* listener);
125 void RemoveEnabledStateObserver(EnabledStateObserver* listener);
126 bool HasEnabledStateObserver(EnabledStateObserver* listener) const;
128 TraceLogStatus GetStatus() const;
129 bool BufferIsFull() const;
131 // Computes an estimate of the size of the TraceLog including all the retained
132 // objects.
133 void EstimateTraceMemoryOverhead(TraceEventMemoryOverhead* overhead);
135 // Not using base::Callback because of its limited by 7 parameters.
136 // Also, using primitive type allows directly passing callback from WebCore.
137 // WARNING: It is possible for the previously set callback to be called
138 // after a call to SetEventCallbackEnabled() that replaces or a call to
139 // SetEventCallbackDisabled() that disables the callback.
140 // This callback may be invoked on any thread.
141 // For TRACE_EVENT_PHASE_COMPLETE events, the client will still receive pairs
142 // of TRACE_EVENT_PHASE_BEGIN and TRACE_EVENT_PHASE_END events to keep the
143 // interface simple.
144 typedef void (*EventCallback)(TraceTicks timestamp,
145 char phase,
146 const unsigned char* category_group_enabled,
147 const char* name,
148 unsigned long long id,
149 int num_args,
150 const char* const arg_names[],
151 const unsigned char arg_types[],
152 const unsigned long long arg_values[],
153 unsigned int flags);
155 // Enable tracing for EventCallback.
156 void SetEventCallbackEnabled(const TraceConfig& trace_config,
157 EventCallback cb);
158 void SetEventCallbackDisabled();
159 void SetArgumentFilterPredicate(
160 const ArgumentFilterPredicate& argument_filter_predicate);
162 // Flush all collected events to the given output callback. The callback will
163 // be called one or more times either synchronously or asynchronously from
164 // the current thread with IPC-bite-size chunks. The string format is
165 // undefined. Use TraceResultBuffer to convert one or more trace strings to
166 // JSON. The callback can be null if the caller doesn't want any data.
167 // Due to the implementation of thread-local buffers, flush can't be
168 // done when tracing is enabled. If called when tracing is enabled, the
169 // callback will be called directly with (empty_string, false) to indicate
170 // the end of this unsuccessful flush. Flush does the serialization
171 // on the same thread if the caller doesn't set use_worker_thread explicitly.
172 typedef base::Callback<void(const scoped_refptr<base::RefCountedString>&,
173 bool has_more_events)> OutputCallback;
174 void Flush(const OutputCallback& cb, bool use_worker_thread = false);
175 void FlushButLeaveBufferIntact(const OutputCallback& flush_output_callback);
177 // Cancels tracing and discards collected data.
178 void CancelTracing(const OutputCallback& cb);
180 // Called by TRACE_EVENT* macros, don't call this directly.
181 // The name parameter is a category group for example:
182 // TRACE_EVENT0("renderer,webkit", "WebViewImpl::HandleInputEvent")
183 static const unsigned char* GetCategoryGroupEnabled(const char* name);
184 static const char* GetCategoryGroupName(
185 const unsigned char* category_group_enabled);
187 // Called by TRACE_EVENT* macros, don't call this directly.
188 // If |copy| is set, |name|, |arg_name1| and |arg_name2| will be deep copied
189 // into the event; see "Memory scoping note" and TRACE_EVENT_COPY_XXX above.
190 TraceEventHandle AddTraceEvent(
191 char phase,
192 const unsigned char* category_group_enabled,
193 const char* name,
194 unsigned long long id,
195 int num_args,
196 const char** arg_names,
197 const unsigned char* arg_types,
198 const unsigned long long* arg_values,
199 const scoped_refptr<ConvertableToTraceFormat>* convertable_values,
200 unsigned int flags);
201 TraceEventHandle AddTraceEventWithContextId(
202 char phase,
203 const unsigned char* category_group_enabled,
204 const char* name,
205 unsigned long long id,
206 unsigned long long context_id,
207 int num_args,
208 const char** arg_names,
209 const unsigned char* arg_types,
210 const unsigned long long* arg_values,
211 const scoped_refptr<ConvertableToTraceFormat>* convertable_values,
212 unsigned int flags);
213 TraceEventHandle AddTraceEventWithThreadIdAndTimestamp(
214 char phase,
215 const unsigned char* category_group_enabled,
216 const char* name,
217 unsigned long long id,
218 unsigned long long context_id,
219 int thread_id,
220 const TraceTicks& timestamp,
221 int num_args,
222 const char** arg_names,
223 const unsigned char* arg_types,
224 const unsigned long long* arg_values,
225 const scoped_refptr<ConvertableToTraceFormat>* convertable_values,
226 unsigned int flags);
227 TraceEventHandle AddTraceEventWithThreadIdAndTimestamp(
228 char phase,
229 const unsigned char* category_group_enabled,
230 const char* name,
231 unsigned long long id,
232 unsigned long long context_id,
233 unsigned long long bind_id,
234 int thread_id,
235 const TraceTicks& timestamp,
236 int num_args,
237 const char** arg_names,
238 const unsigned char* arg_types,
239 const unsigned long long* arg_values,
240 const scoped_refptr<ConvertableToTraceFormat>* convertable_values,
241 unsigned int flags);
242 static void AddTraceEventEtw(char phase,
243 const char* category_group,
244 const void* id,
245 const char* extra);
246 static void AddTraceEventEtw(char phase,
247 const char* category_group,
248 const void* id,
249 const std::string& extra);
251 void UpdateTraceEventDuration(const unsigned char* category_group_enabled,
252 const char* name,
253 TraceEventHandle handle);
255 // For every matching event, the callback will be called.
256 typedef base::Callback<void()> WatchEventCallback;
257 void SetWatchEvent(const std::string& category_name,
258 const std::string& event_name,
259 const WatchEventCallback& callback);
260 // Cancel the watch event. If tracing is enabled, this may race with the
261 // watch event notification firing.
262 void CancelWatchEvent();
264 int process_id() const { return process_id_; }
266 uint64 MangleEventId(uint64 id);
268 // Exposed for unittesting:
270 void WaitSamplingEventForTesting();
272 // Allows deleting our singleton instance.
273 static void DeleteForTesting();
275 // Allow tests to inspect TraceEvents.
276 TraceEvent* GetEventByHandle(TraceEventHandle handle);
278 void SetProcessID(int process_id);
280 // Process sort indices, if set, override the order of a process will appear
281 // relative to other processes in the trace viewer. Processes are sorted first
282 // on their sort index, ascending, then by their name, and then tid.
283 void SetProcessSortIndex(int sort_index);
285 // Sets the name of the process.
286 void SetProcessName(const std::string& process_name);
288 // Processes can have labels in addition to their names. Use labels, for
289 // instance, to list out the web page titles that a process is handling.
290 void UpdateProcessLabel(int label_id, const std::string& current_label);
291 void RemoveProcessLabel(int label_id);
293 // Thread sort indices, if set, override the order of a thread will appear
294 // within its process in the trace viewer. Threads are sorted first on their
295 // sort index, ascending, then by their name, and then tid.
296 void SetThreadSortIndex(PlatformThreadId thread_id, int sort_index);
298 // Allow setting an offset between the current TraceTicks time and the time
299 // that should be reported.
300 void SetTimeOffset(TimeDelta offset);
302 size_t GetObserverCountForTest() const;
304 // Call this method if the current thread may block the message loop to
305 // prevent the thread from using the thread-local buffer because the thread
306 // may not handle the flush request in time causing lost of unflushed events.
307 void SetCurrentThreadBlocksMessageLoop();
309 #if defined(OS_WIN)
310 // This function is called by the ETW exporting module whenever the ETW
311 // keyword (flags) changes. This keyword indicates which categories should be
312 // exported, so whenever it changes, we adjust accordingly.
313 void UpdateETWCategoryGroupEnabledFlags();
314 #endif
316 private:
317 typedef unsigned int InternalTraceOptions;
319 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture,
320 TraceBufferRingBufferGetReturnChunk);
321 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture,
322 TraceBufferRingBufferHalfIteration);
323 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture,
324 TraceBufferRingBufferFullIteration);
325 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, TraceBufferVectorReportFull);
326 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture,
327 ConvertTraceConfigToInternalOptions);
328 FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture,
329 TraceRecordAsMuchAsPossibleMode);
331 // This allows constructor and destructor to be private and usable only
332 // by the Singleton class.
333 friend struct DefaultSingletonTraits<TraceLog>;
335 // MemoryDumpProvider implementation.
336 bool OnMemoryDump(const MemoryDumpArgs& args,
337 ProcessMemoryDump* pmd) override;
339 // Enable/disable each category group based on the current mode_,
340 // category_filter_, event_callback_ and event_callback_category_filter_.
341 // Enable the category group in the enabled mode if category_filter_ matches
342 // the category group, or event_callback_ is not null and
343 // event_callback_category_filter_ matches the category group.
344 void UpdateCategoryGroupEnabledFlags();
345 void UpdateCategoryGroupEnabledFlag(size_t category_index);
347 // Configure synthetic delays based on the values set in the current
348 // trace config.
349 void UpdateSyntheticDelaysFromTraceConfig();
351 InternalTraceOptions GetInternalOptionsFromTraceConfig(
352 const TraceConfig& config);
354 class ThreadLocalEventBuffer;
355 class OptionalAutoLock;
357 TraceLog();
358 ~TraceLog() override;
359 const unsigned char* GetCategoryGroupEnabledInternal(const char* name);
360 void AddMetadataEventsWhileLocked();
362 InternalTraceOptions trace_options() const {
363 return static_cast<InternalTraceOptions>(
364 subtle::NoBarrier_Load(&trace_options_));
367 TraceBuffer* trace_buffer() const { return logged_events_.get(); }
368 TraceBuffer* CreateTraceBuffer();
370 std::string EventToConsoleMessage(unsigned char phase,
371 const TraceTicks& timestamp,
372 TraceEvent* trace_event);
374 TraceEvent* AddEventToThreadSharedChunkWhileLocked(TraceEventHandle* handle,
375 bool check_buffer_is_full);
376 void CheckIfBufferIsFullWhileLocked();
377 void SetDisabledWhileLocked();
379 TraceEvent* GetEventByHandleInternal(TraceEventHandle handle,
380 OptionalAutoLock* lock);
382 void FlushInternal(const OutputCallback& cb,
383 bool use_worker_thread,
384 bool discard_events);
386 // |generation| is used in the following callbacks to check if the callback
387 // is called for the flush of the current |logged_events_|.
388 void FlushCurrentThread(int generation, bool discard_events);
389 // Usually it runs on a different thread.
390 static void ConvertTraceEventsToTraceFormat(
391 scoped_ptr<TraceBuffer> logged_events,
392 const TraceLog::OutputCallback& flush_output_callback,
393 const ArgumentFilterPredicate& argument_filter_predicate);
394 void FinishFlush(int generation, bool discard_events);
395 void OnFlushTimeout(int generation, bool discard_events);
397 int generation() const {
398 return static_cast<int>(subtle::NoBarrier_Load(&generation_));
400 bool CheckGeneration(int generation) const {
401 return generation == this->generation();
403 void UseNextTraceBuffer();
405 TraceTicks OffsetNow() const { return OffsetTimestamp(TraceTicks::Now()); }
406 TraceTicks OffsetTimestamp(const TraceTicks& timestamp) const {
407 return timestamp - time_offset_;
410 // Internal representation of trace options since we store the currently used
411 // trace option as an AtomicWord.
412 static const InternalTraceOptions kInternalNone;
413 static const InternalTraceOptions kInternalRecordUntilFull;
414 static const InternalTraceOptions kInternalRecordContinuously;
415 static const InternalTraceOptions kInternalEchoToConsole;
416 static const InternalTraceOptions kInternalEnableSampling;
417 static const InternalTraceOptions kInternalRecordAsMuchAsPossible;
418 static const InternalTraceOptions kInternalEnableArgumentFilter;
420 // This lock protects TraceLog member accesses (except for members protected
421 // by thread_info_lock_) from arbitrary threads.
422 mutable Lock lock_;
423 // This lock protects accesses to thread_names_, thread_event_start_times_
424 // and thread_colors_.
425 Lock thread_info_lock_;
426 Mode mode_;
427 int num_traces_recorded_;
428 scoped_ptr<TraceBuffer> logged_events_;
429 subtle::AtomicWord /* EventCallback */ event_callback_;
430 bool dispatching_to_observer_list_;
431 std::vector<EnabledStateObserver*> enabled_state_observer_list_;
433 std::string process_name_;
434 base::hash_map<int, std::string> process_labels_;
435 int process_sort_index_;
436 base::hash_map<int, int> thread_sort_indices_;
437 base::hash_map<int, std::string> thread_names_;
439 // The following two maps are used only when ECHO_TO_CONSOLE.
440 base::hash_map<int, std::stack<TraceTicks>> thread_event_start_times_;
441 base::hash_map<std::string, int> thread_colors_;
443 TraceTicks buffer_limit_reached_timestamp_;
445 // XORed with TraceID to make it unlikely to collide with other processes.
446 unsigned long long process_id_hash_;
448 int process_id_;
450 TimeDelta time_offset_;
452 // Allow tests to wake up when certain events occur.
453 WatchEventCallback watch_event_callback_;
454 subtle::AtomicWord /* const unsigned char* */ watch_category_;
455 std::string watch_event_name_;
457 subtle::AtomicWord /* Options */ trace_options_;
459 // Sampling thread handles.
460 scoped_ptr<TraceSamplingThread> sampling_thread_;
461 PlatformThreadHandle sampling_thread_handle_;
463 TraceConfig trace_config_;
464 TraceConfig event_callback_trace_config_;
466 ThreadLocalPointer<ThreadLocalEventBuffer> thread_local_event_buffer_;
467 ThreadLocalBoolean thread_blocks_message_loop_;
468 ThreadLocalBoolean thread_is_in_trace_event_;
470 // Contains the message loops of threads that have had at least one event
471 // added into the local event buffer. Not using SingleThreadTaskRunner
472 // because we need to know the life time of the message loops.
473 hash_set<MessageLoop*> thread_message_loops_;
475 // For events which can't be added into the thread local buffer, e.g. events
476 // from threads without a message loop.
477 scoped_ptr<TraceBufferChunk> thread_shared_chunk_;
478 size_t thread_shared_chunk_index_;
480 // Set when asynchronous Flush is in progress.
481 OutputCallback flush_output_callback_;
482 scoped_refptr<SingleThreadTaskRunner> flush_task_runner_;
483 ArgumentFilterPredicate argument_filter_predicate_;
484 subtle::AtomicWord generation_;
485 bool use_worker_thread_;
487 DISALLOW_COPY_AND_ASSIGN(TraceLog);
490 } // namespace trace_event
491 } // namespace base
493 #endif // BASE_TRACE_EVENT_TRACE_LOG_H_