1 // Copyright (c) 2013 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. 4 5 #ifndef TOOLS_GN_TRACE_H_ 6 #define TOOLS_GN_TRACE_H_ 7 8 #include <string> 9 10 #include "base/basictypes.h" 11 #include "base/command_line.h" 12 #include "base/files/file_path.h" 13 #include "base/memory/scoped_ptr.h" 14 #include "base/threading/platform_thread.h" 15 #include "base/time/time.h" 16 17 class Label; 18 19 class TraceItem { 20 public: 21 enum Type { 22 TRACE_FILE_LOAD, 23 TRACE_FILE_PARSE, 24 TRACE_FILE_EXECUTE, 25 TRACE_FILE_WRITE, 26 TRACE_SCRIPT_EXECUTE, 27 TRACE_DEFINE_TARGET 28 }; 29 30 TraceItem(Type type, 31 const std::string& name, 32 base::PlatformThreadId thread_id); 33 ~TraceItem(); 34 type()35 Type type() const { return type_; } name()36 const std::string& name() const { return name_; } thread_id()37 base::PlatformThreadId thread_id() const { return thread_id_; } 38 begin()39 base::TimeTicks begin() const { return begin_; } set_begin(base::TimeTicks b)40 void set_begin(base::TimeTicks b) { begin_ = b; } end()41 base::TimeTicks end() const { return end_; } set_end(base::TimeTicks e)42 void set_end(base::TimeTicks e) { end_ = e; } 43 delta()44 base::TimeDelta delta() const { return end_ - begin_; } 45 46 // Optional toolchain label. toolchain()47 const std::string& toolchain() const { return toolchain_; } set_toolchain(const std::string & t)48 void set_toolchain(const std::string& t) { toolchain_ = t; } 49 50 // Optional command line. cmdline()51 const std::string& cmdline() const { return cmdline_; } set_cmdline(const std::string & c)52 void set_cmdline(const std::string& c) { cmdline_ = c; } 53 54 private: 55 Type type_; 56 std::string name_; 57 base::PlatformThreadId thread_id_; 58 59 base::TimeTicks begin_; 60 base::TimeTicks end_; 61 62 std::string toolchain_; 63 std::string cmdline_; 64 }; 65 66 class ScopedTrace { 67 public: 68 ScopedTrace(TraceItem::Type t, const std::string& name); 69 ScopedTrace(TraceItem::Type t, const Label& label); 70 ~ScopedTrace(); 71 72 void SetToolchain(const Label& label); 73 void SetCommandLine(const CommandLine& cmdline); 74 75 void Done(); 76 77 private: 78 TraceItem* item_; 79 bool done_; 80 }; 81 82 // Call to turn tracing on. It's off by default. 83 void EnableTracing(); 84 85 // Adds a trace event to the log. Takes ownership of the pointer. 86 void AddTrace(TraceItem* item); 87 88 // Returns a summary of the current traces, or the empty string if tracing is 89 // not enabled. 90 std::string SummarizeTraces(); 91 92 // Saves the current traces to the given filename in JSON format. 93 void SaveTraces(const base::FilePath& file_name); 94 95 #endif // TOOLS_GN_TRACE_H_ 96