1 //===- Tracing.h ------------------------------------------------*- C++ -*-===//
2 //
3 // This source file is part of the Swift.org open source project
4 //
5 // Copyright (c) 2017 Apple Inc. and the Swift project authors
6 // Licensed under Apache License v2.0 with Runtime Library Exception
7 //
8 // See http://swift.org/LICENSE.txt for license information
9 // See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLBUILD_BASIC_TRACING_H
14 #define LLBUILD_BASIC_TRACING_H
15 
16 #include "llvm/ADT/StringRef.h"
17 
18 namespace llbuild {
19 
20 /// Tracing Kind Codes
21 ///
22 /// These are currently global across the entire library, please take care to
23 /// not unnecessarily cause them to reorder as it will prevent use of prior
24 /// tracing data.
25 enum class TraceEventKind {
26   // Execution Queue
27 
28   /// An individual job execution interval.
29   ExecutionQueueJob = 0,
30 
31   /// A subprocess launch.
32   ExecutionQueueSubprocess = 1,
33 
34   /// A callback from the task, \see EngineTaskCallbackKind.
35   EngineTaskCallback = 2,
36 
37   /// An event on the engine processing queue, \see EngineQueueItemKind.
38   EngineQueueItemEvent = 3,
39 
40   /// A point event to track the depth of the execution queue
41   ExecutionQueueDepth = 4,
42 };
43 
44 // Engine Task Callbacks
45 enum class EngineTaskCallbackKind {
46   Start = 0,
47   ProvidePriorValue,
48   ProvideValue,
49   InputsAvailable,
50 };
51 
52 // Engine Queue Processing
53 enum class EngineQueueItemKind {
54   RuleToScan = 0,
55   InputRequest,
56   FinishedInputRequest,
57   ReadyTask,
58   FinishedTask,
59   Waiting,
60 };
61 
62 /// An RAII type to define an individual tracing point.
63 struct TracingPoint {
64   const uint32_t kind;
65   const uint64_t arg1;
66   const uint64_t arg2;
67   const uint64_t arg3;
68   const uint64_t arg4;
69 
70   TracingPoint(TraceEventKind kind, uint64_t arg1 = 0, uint64_t arg2 = 0,
71                 uint64_t arg3 = 0, uint64_t arg4 = 0)
kindTracingPoint72     : kind(uint32_t(kind)), arg1(arg1), arg2(arg2), arg3(arg3), arg4(arg4)
73   {
74   }
75 };
76 
77 /// An RAII type to define an individual tracing interval.
78 ///
79 /// The client may modify the values of the arguments after initialization, for
80 /// example to submit additional metrics for the event kind as part of the
81 /// interval completion event.
82 struct TracingInterval {
83   const uint32_t kind;
84   uint64_t arg1;
85   uint64_t arg2;
86   uint64_t arg3;
87   uint64_t arg4;
88 
89   TracingInterval(TraceEventKind kind, uint64_t arg1 = 0, uint64_t arg2 = 0,
90                   uint64_t arg3 = 0, uint64_t arg4 = 0)
kindTracingInterval91       : kind(uint32_t(kind)), arg1(arg1), arg2(arg2), arg3(arg3), arg4(arg4)
92   {
93   }
~TracingIntervalTracingInterval94   ~TracingInterval() {
95   }
96 
97   // MARK: Utility Wrappers
98 
TracingIntervalTracingInterval99   TracingInterval(EngineTaskCallbackKind arg1)
100     : TracingInterval(TraceEventKind::EngineTaskCallback, uint64_t(arg1)) {}
101 
TracingIntervalTracingInterval102   TracingInterval(EngineQueueItemKind arg1)
103     : TracingInterval(TraceEventKind::EngineQueueItemEvent, uint64_t(arg1)) {}
104 };
105 
106 /// An RAII type to define a string.
107 struct TracingString {
108   const uint32_t kind;
109 
110   /// The integer code for the string, which can be provided to a trace point or
111   /// interval.
112   const uint64_t value;
113 
TracingStringTracingString114   TracingString(TraceEventKind kind, llvm::StringRef str)
115       : kind(uint32_t(kind)), value(0) {}
116 
uint64_tTracingString117   operator uint64_t() const { return value; }
118 };
119 
120 }
121 
122 #endif
123