1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/chromium/src/base/trace_event.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,126 @@ 1.4 +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +// Trace events to track application performance. Events consist of a name 1.9 +// a type (BEGIN, END or INSTANT), a tracking id and extra string data. 1.10 +// In addition, the current process id, thread id, a timestamp down to the 1.11 +// microsecond and a file and line number of the calling location. 1.12 +// 1.13 +// The current implementation logs these events into a log file of the form 1.14 +// trace_<pid>.log where it's designed to be post-processed to generate a 1.15 +// trace report. In the future, it may use another mechansim to facilitate 1.16 +// real-time analysis. 1.17 + 1.18 +#ifndef BASE_TRACE_EVENT_H_ 1.19 +#define BASE_TRACE_EVENT_H_ 1.20 + 1.21 +#include "build/build_config.h" 1.22 + 1.23 +#if defined(OS_WIN) 1.24 +#include <windows.h> 1.25 +#endif 1.26 + 1.27 +#include <string> 1.28 + 1.29 +#include "base/lock.h" 1.30 +#include "base/scoped_ptr.h" 1.31 +#include "base/singleton.h" 1.32 +#include "base/time.h" 1.33 +#include "base/timer.h" 1.34 + 1.35 +// Use the following macros rather than using the TraceLog class directly as the 1.36 +// underlying implementation may change in the future. Here's a sample usage: 1.37 +// TRACE_EVENT_BEGIN("v8.run", documentId, scriptLocation); 1.38 +// RunScript(script); 1.39 +// TRACE_EVENT_END("v8.run", documentId, scriptLocation); 1.40 + 1.41 +// Record that an event (of name, id) has begun. All BEGIN events should have 1.42 +// corresponding END events with a matching (name, id). 1.43 +#define TRACE_EVENT_BEGIN(name, id, extra) \ 1.44 + Singleton<base::TraceLog>::get()->Trace(name, \ 1.45 + base::TraceLog::EVENT_BEGIN, \ 1.46 + reinterpret_cast<const void*>(id), \ 1.47 + extra, \ 1.48 + __FILE__, \ 1.49 + __LINE__) 1.50 + 1.51 +// Record that an event (of name, id) has ended. All END events should have 1.52 +// corresponding BEGIN events with a matching (name, id). 1.53 +#define TRACE_EVENT_END(name, id, extra) \ 1.54 + Singleton<base::TraceLog>::get()->Trace(name, \ 1.55 + base::TraceLog::EVENT_END, \ 1.56 + reinterpret_cast<const void*>(id), \ 1.57 + extra, \ 1.58 + __FILE__, \ 1.59 + __LINE__) 1.60 + 1.61 +// Record that an event (of name, id) with no duration has happened. 1.62 +#define TRACE_EVENT_INSTANT(name, id, extra) \ 1.63 + Singleton<base::TraceLog>::get()->Trace(name, \ 1.64 + base::TraceLog::EVENT_INSTANT, \ 1.65 + reinterpret_cast<const void*>(id), \ 1.66 + extra, \ 1.67 + __FILE__, \ 1.68 + __LINE__) 1.69 + 1.70 +namespace base { 1.71 +class ProcessMetrics; 1.72 +} 1.73 + 1.74 +namespace base { 1.75 + 1.76 +class TraceLog { 1.77 + public: 1.78 + enum EventType { 1.79 + EVENT_BEGIN, 1.80 + EVENT_END, 1.81 + EVENT_INSTANT 1.82 + }; 1.83 + 1.84 + // Is tracing currently enabled. 1.85 + static bool IsTracing(); 1.86 + // Start logging trace events. 1.87 + static bool StartTracing(); 1.88 + // Stop logging trace events. 1.89 + static void StopTracing(); 1.90 + 1.91 + // Log a trace event of (name, type, id) with the optional extra string. 1.92 + void Trace(const std::string& name, 1.93 + EventType type, 1.94 + const void* id, 1.95 + const std::wstring& extra, 1.96 + const char* file, 1.97 + int line); 1.98 + void Trace(const std::string& name, 1.99 + EventType type, 1.100 + const void* id, 1.101 + const std::string& extra, 1.102 + const char* file, 1.103 + int line); 1.104 + 1.105 + private: 1.106 + // This allows constructor and destructor to be private and usable only 1.107 + // by the Singleton class. 1.108 + friend struct DefaultSingletonTraits<TraceLog>; 1.109 + 1.110 + TraceLog(); 1.111 + ~TraceLog(); 1.112 + bool OpenLogFile(); 1.113 + void CloseLogFile(); 1.114 + bool Start(); 1.115 + void Stop(); 1.116 + void Heartbeat(); 1.117 + void Log(const std::string& msg); 1.118 + 1.119 + bool enabled_; 1.120 + FILE* log_file_; 1.121 + Lock file_lock_; 1.122 + TimeTicks trace_start_time_; 1.123 + scoped_ptr<base::ProcessMetrics> process_metrics_; 1.124 + RepeatingTimer<TraceLog> timer_; 1.125 +}; 1.126 + 1.127 +} // namespace base 1.128 + 1.129 +#endif // BASE_TRACE_EVENT_H_