• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <memory>
9 #include <string>
10 #include <thread>
11 
12 #include "util/ticks.h"
13 
14 class Label;
15 
16 namespace base {
17 class CommandLine;
18 class FilePath;
19 }  // namespace base
20 
21 class TraceItem {
22  public:
23   enum Type {
24     TRACE_SETUP,
25     TRACE_FILE_LOAD,
26     TRACE_FILE_PARSE,
27     TRACE_FILE_EXECUTE,
28     TRACE_FILE_EXECUTE_TEMPLATE,
29     TRACE_FILE_WRITE,
30     TRACE_FILE_WRITE_GENERATED,
31     TRACE_FILE_WRITE_NINJA,
32     TRACE_IMPORT_LOAD,
33     TRACE_IMPORT_BLOCK,
34     TRACE_SCRIPT_EXECUTE,
35     TRACE_DEFINE_TARGET,
36     TRACE_ON_RESOLVED,
37     TRACE_CHECK_HEADER,   // One file.
38     TRACE_CHECK_HEADERS,  // All files.
39     TRACE_WALK_METADATA,
40   };
41 
42   TraceItem(Type type, const std::string& name, std::thread::id thread_id);
43   ~TraceItem();
44 
type()45   Type type() const { return type_; }
name()46   const std::string& name() const { return name_; }
thread_id()47   std::thread::id thread_id() const { return thread_id_; }
48 
begin()49   Ticks begin() const { return begin_; }
set_begin(Ticks b)50   void set_begin(Ticks b) { begin_ = b; }
end()51   Ticks end() const { return end_; }
set_end(Ticks e)52   void set_end(Ticks e) { end_ = e; }
53 
delta()54   TickDelta delta() const { return TicksDelta(end_, begin_); }
55 
56   // Optional toolchain label.
toolchain()57   const std::string& toolchain() const { return toolchain_; }
set_toolchain(const std::string & t)58   void set_toolchain(const std::string& t) { toolchain_ = t; }
59 
60   // Optional command line.
cmdline()61   const std::string& cmdline() const { return cmdline_; }
set_cmdline(const std::string & c)62   void set_cmdline(const std::string& c) { cmdline_ = c; }
63 
64  private:
65   Type type_;
66   std::string name_;
67   std::thread::id thread_id_;
68 
69   Ticks begin_;
70   Ticks end_;
71 
72   std::string toolchain_;
73   std::string cmdline_;
74 };
75 
76 class ScopedTrace {
77  public:
78   ScopedTrace(TraceItem::Type t, const std::string& name);
79   ScopedTrace(TraceItem::Type t, const Label& label);
80   ~ScopedTrace();
81 
82   void SetToolchain(const Label& label);
83   void SetCommandLine(const base::CommandLine& cmdline);
84 
85   void Done();
86 
87  private:
88   std::unique_ptr<TraceItem> item_;
89   bool done_;
90 };
91 
92 // Call to turn tracing on. It's off by default.
93 void EnableTracing();
94 
95 // Returns whether tracing is enabled.
96 bool TracingEnabled();
97 
98 // Adds a trace event to the log.
99 void AddTrace(std::unique_ptr<TraceItem> item);
100 
101 // Returns a summary of the current traces, or the empty string if tracing is
102 // not enabled.
103 std::string SummarizeTraces();
104 
105 // Saves the current traces to the given filename in JSON format.
106 void SaveTraces(const base::FilePath& file_name);
107 
108 #endif  // TOOLS_GN_TRACE_H_
109