ipc/chromium/src/base/trace_event.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4
michael@0 5 // Trace events to track application performance. Events consist of a name
michael@0 6 // a type (BEGIN, END or INSTANT), a tracking id and extra string data.
michael@0 7 // In addition, the current process id, thread id, a timestamp down to the
michael@0 8 // microsecond and a file and line number of the calling location.
michael@0 9 //
michael@0 10 // The current implementation logs these events into a log file of the form
michael@0 11 // trace_<pid>.log where it's designed to be post-processed to generate a
michael@0 12 // trace report. In the future, it may use another mechansim to facilitate
michael@0 13 // real-time analysis.
michael@0 14
michael@0 15 #ifndef BASE_TRACE_EVENT_H_
michael@0 16 #define BASE_TRACE_EVENT_H_
michael@0 17
michael@0 18 #include "build/build_config.h"
michael@0 19
michael@0 20 #if defined(OS_WIN)
michael@0 21 #include <windows.h>
michael@0 22 #endif
michael@0 23
michael@0 24 #include <string>
michael@0 25
michael@0 26 #include "base/lock.h"
michael@0 27 #include "base/scoped_ptr.h"
michael@0 28 #include "base/singleton.h"
michael@0 29 #include "base/time.h"
michael@0 30 #include "base/timer.h"
michael@0 31
michael@0 32 // Use the following macros rather than using the TraceLog class directly as the
michael@0 33 // underlying implementation may change in the future. Here's a sample usage:
michael@0 34 // TRACE_EVENT_BEGIN("v8.run", documentId, scriptLocation);
michael@0 35 // RunScript(script);
michael@0 36 // TRACE_EVENT_END("v8.run", documentId, scriptLocation);
michael@0 37
michael@0 38 // Record that an event (of name, id) has begun. All BEGIN events should have
michael@0 39 // corresponding END events with a matching (name, id).
michael@0 40 #define TRACE_EVENT_BEGIN(name, id, extra) \
michael@0 41 Singleton<base::TraceLog>::get()->Trace(name, \
michael@0 42 base::TraceLog::EVENT_BEGIN, \
michael@0 43 reinterpret_cast<const void*>(id), \
michael@0 44 extra, \
michael@0 45 __FILE__, \
michael@0 46 __LINE__)
michael@0 47
michael@0 48 // Record that an event (of name, id) has ended. All END events should have
michael@0 49 // corresponding BEGIN events with a matching (name, id).
michael@0 50 #define TRACE_EVENT_END(name, id, extra) \
michael@0 51 Singleton<base::TraceLog>::get()->Trace(name, \
michael@0 52 base::TraceLog::EVENT_END, \
michael@0 53 reinterpret_cast<const void*>(id), \
michael@0 54 extra, \
michael@0 55 __FILE__, \
michael@0 56 __LINE__)
michael@0 57
michael@0 58 // Record that an event (of name, id) with no duration has happened.
michael@0 59 #define TRACE_EVENT_INSTANT(name, id, extra) \
michael@0 60 Singleton<base::TraceLog>::get()->Trace(name, \
michael@0 61 base::TraceLog::EVENT_INSTANT, \
michael@0 62 reinterpret_cast<const void*>(id), \
michael@0 63 extra, \
michael@0 64 __FILE__, \
michael@0 65 __LINE__)
michael@0 66
michael@0 67 namespace base {
michael@0 68 class ProcessMetrics;
michael@0 69 }
michael@0 70
michael@0 71 namespace base {
michael@0 72
michael@0 73 class TraceLog {
michael@0 74 public:
michael@0 75 enum EventType {
michael@0 76 EVENT_BEGIN,
michael@0 77 EVENT_END,
michael@0 78 EVENT_INSTANT
michael@0 79 };
michael@0 80
michael@0 81 // Is tracing currently enabled.
michael@0 82 static bool IsTracing();
michael@0 83 // Start logging trace events.
michael@0 84 static bool StartTracing();
michael@0 85 // Stop logging trace events.
michael@0 86 static void StopTracing();
michael@0 87
michael@0 88 // Log a trace event of (name, type, id) with the optional extra string.
michael@0 89 void Trace(const std::string& name,
michael@0 90 EventType type,
michael@0 91 const void* id,
michael@0 92 const std::wstring& extra,
michael@0 93 const char* file,
michael@0 94 int line);
michael@0 95 void Trace(const std::string& name,
michael@0 96 EventType type,
michael@0 97 const void* id,
michael@0 98 const std::string& extra,
michael@0 99 const char* file,
michael@0 100 int line);
michael@0 101
michael@0 102 private:
michael@0 103 // This allows constructor and destructor to be private and usable only
michael@0 104 // by the Singleton class.
michael@0 105 friend struct DefaultSingletonTraits<TraceLog>;
michael@0 106
michael@0 107 TraceLog();
michael@0 108 ~TraceLog();
michael@0 109 bool OpenLogFile();
michael@0 110 void CloseLogFile();
michael@0 111 bool Start();
michael@0 112 void Stop();
michael@0 113 void Heartbeat();
michael@0 114 void Log(const std::string& msg);
michael@0 115
michael@0 116 bool enabled_;
michael@0 117 FILE* log_file_;
michael@0 118 Lock file_lock_;
michael@0 119 TimeTicks trace_start_time_;
michael@0 120 scoped_ptr<base::ProcessMetrics> process_metrics_;
michael@0 121 RepeatingTimer<TraceLog> timer_;
michael@0 122 };
michael@0 123
michael@0 124 } // namespace base
michael@0 125
michael@0 126 #endif // BASE_TRACE_EVENT_H_

mercurial