| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Tracer objects uresed to record an annotated timeline of events for use in | 5 // Tracer objects uresed to record an annotated timeline of events for use in |
| 6 // gathering performance data. It wraps a TraceBuffer which is the raw data | 6 // gathering performance data. It wraps a TraceBuffer which is the raw data |
| 7 // for a trace. Tracer is threadsafe. | 7 // for a trace. Tracer is threadsafe. |
| 8 // | 8 // |
| 9 // TraceContext is a singleton that is used to give information for the current | 9 // TraceContext is a singleton that is used to give information for the current |
| 10 // trace. Clients should query TraceContext to find the current tracer and then | 10 // trace. Clients should query TraceContext to find the current tracer and then |
| 11 // use that for logging annotations. TraceContext is threadsafe. | 11 // use that for logging annotations. TraceContext is threadsafe. |
| 12 // | 12 // |
| 13 // ScopedTracer pushes a new Tracer on the TraceContext. It's scoped in that | 13 // ScopedTracer pushes a new Tracer on the TraceContext. It's scoped in that |
| 14 // this tracer is popped off the context when ScopedTracer goes out of scope. | 14 // this tracer is popped off the context when ScopedTracer goes out of scope. |
| 15 // However, if a call to NewTracedMethod is made while the ScopedTracer is in | 15 // However, if a call to NewTracedMethod is made while the ScopedTracer is in |
| 16 // scope, then a reference to the Tracer will be kept in the resulting Task and | 16 // scope, then a reference to the Tracer will be kept in the resulting Task and |
| 17 // repushed onto the stack when the Task is run. Conceptually, this simulates | 17 // repushed onto the stack when the Task is run. Conceptually, this simulates |
| 18 // the current context being continued when the Task is invoked. You usually | 18 // the current context being continued when the Task is invoked. You usually |
| 19 // will want to declare a ScopedTracer at the start of a logical flow of | 19 // will want to declare a ScopedTracer at the start of a logical flow of |
| 20 // operations. | 20 // operations. |
| 21 // | 21 // |
| 22 // Example Usage: | 22 // Example Usage: |
| 23 // | 23 // |
| 24 // void Decoder::StartDecode() { | 24 // void Decoder::StartDecode() { |
| 25 // ScopedTracer tracer("decode_start"); | 25 // ScopedTracer tracer("decode_start"); |
| 26 // | 26 // |
| 27 // TraceContext::current()->PrintString("Decode starting"); | 27 // TraceContext::tracer()->PrintString("Decode starting"); |
| 28 // | 28 // |
| 29 // // DoDecode takes 2 parameters. The first is a callback invoked for each | 29 // // DoDecode takes 2 parameters. The first is a callback invoked for each |
| 30 // // finished frame of output. The second is invoked when the task is done. | 30 // // finished frame of output. The second is invoked when the task is done. |
| 31 // DoDecode(NewTracedMethod(this, &Decoder::OnFrameOutput), | 31 // DoDecode(NewTracedMethod(this, &Decoder::OnFrameOutput), |
| 32 // NewTracedMethod(this, &Decoder::DecodeDone)); | 32 // NewTracedMethod(this, &Decoder::DecodeDone)); |
| 33 // } | 33 // } |
| 34 // } | 34 // } |
| 35 // | 35 // |
| 36 // void Decoder::OnFrameOutput() { | 36 // void Decoder::OnFrameOutput() { |
| 37 // TraceContext::current()->PrintString("Frame outputed"); | 37 // TraceContext::tracer()->PrintString("Frame outputed"); |
| 38 // ... | 38 // ... |
| 39 // } | 39 // } |
| 40 // | 40 // |
| 41 // void Decoder::DecodeDone() { | 41 // void Decoder::DecodeDone() { |
| 42 // TraceContext::current()->PrintString("decode done"); | 42 // TraceContext::tracer()->PrintString("decode done"); |
| 43 // ... | 43 // ... |
| 44 // } | 44 // } |
| 45 // | 45 // |
| 46 // For each call of StartDecode(), the related calls to OnFrameOutput() and | 46 // For each call of StartDecode(), the related calls to OnFrameOutput() and |
| 47 // DecodeDone() will be annotated to the Tracer created by the ScopedTracer | 47 // DecodeDone() will be annotated to the Tracer created by the ScopedTracer |
| 48 // declaration allowing for creating of timing information over the related | 48 // declaration allowing for creating of timing information over the related |
| 49 // asynchronous Task invocations. | 49 // asynchronous Task invocations. |
| 50 | 50 |
| 51 #ifndef REMOTING_BASE_TRACER_H_ | 51 #ifndef REMOTING_BASE_TRACER_H_ |
| 52 #define REMOTING_BASE_TRACER_H_ | 52 #define REMOTING_BASE_TRACER_H_ |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 virtual ~Tracer(); | 84 virtual ~Tracer(); |
| 85 | 85 |
| 86 Lock lock_; | 86 Lock lock_; |
| 87 scoped_ptr<TraceBuffer> buffer_; | 87 scoped_ptr<TraceBuffer> buffer_; |
| 88 | 88 |
| 89 DISALLOW_COPY_AND_ASSIGN(Tracer); | 89 DISALLOW_COPY_AND_ASSIGN(Tracer); |
| 90 }; | 90 }; |
| 91 | 91 |
| 92 class TraceContext { | 92 class TraceContext { |
| 93 public: | 93 public: |
| 94 // Set the current tracer. | 94 // Get the current tracer. |
| 95 static Tracer* tracer() { | 95 static Tracer* tracer() { |
| 96 return Get()->GetTracerInternal(); | 96 return Get()->GetTracerInternal(); |
| 97 } | 97 } |
| 98 | 98 |
| 99 static void PushTracer(Tracer* tracer) { | 99 static void PushTracer(Tracer* tracer) { |
| 100 Get()->PushTracerInternal(tracer); | 100 Get()->PushTracerInternal(tracer); |
| 101 } | 101 } |
| 102 | 102 |
| 103 static void PopTracer() { | 103 static void PopTracer() { |
| 104 Get()->PopTracerInternal(); | 104 Get()->PopTracerInternal(); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 135 // Used to create a new tracer that NewRunnableMethod can propogate from. | 135 // Used to create a new tracer that NewRunnableMethod can propogate from. |
| 136 // | 136 // |
| 137 // Declare this at the logical start of a "trace." Calls to NewTracedMethod | 137 // Declare this at the logical start of a "trace." Calls to NewTracedMethod |
| 138 // that are done with the ScopedTracer object is alive will take a reference | 138 // that are done with the ScopedTracer object is alive will take a reference |
| 139 // to this tracer. When such a method is invoked, it will push the trace back | 139 // to this tracer. When such a method is invoked, it will push the trace back |
| 140 // onto the top of the TraceContext stack. The result is that all asynchronous | 140 // onto the top of the TraceContext stack. The result is that all asynchronous |
| 141 // tasks that are part of one logical flow will share the same trace. | 141 // tasks that are part of one logical flow will share the same trace. |
| 142 class ScopedTracer { | 142 class ScopedTracer { |
| 143 public: | 143 public: |
| 144 ScopedTracer(const std::string& name) { | 144 ScopedTracer(const std::string& name) { |
| 145 #if defined(USE_TRACE) |
| 145 scoped_refptr<Tracer> tracer = new Tracer(name, 1.00); | 146 scoped_refptr<Tracer> tracer = new Tracer(name, 1.00); |
| 146 TraceContext::PushTracer(tracer); | 147 TraceContext::PushTracer(tracer); |
| 148 #endif |
| 147 } | 149 } |
| 148 | 150 |
| 149 ~ScopedTracer() { | 151 ~ScopedTracer() { |
| 152 #if defined(USE_TRACE) |
| 150 TraceContext::PopTracer(); | 153 TraceContext::PopTracer(); |
| 154 #endif |
| 151 } | 155 } |
| 152 }; | 156 }; |
| 153 | 157 |
| 154 // This is experimental code. I'm creating a set of analogues to | 158 // This is experimental code. I'm creating a set of analogues to |
| 155 // the NewRunnableMethod functions called NewTracedMethod, which should be | 159 // the NewRunnableMethod functions called NewTracedMethod, which should be |
| 156 // API equivalent to the former. In fact, they must be enabled by setting | 160 // API equivalent to the former. In fact, they must be enabled by setting |
| 157 // USE_TRACE 1 for now. | 161 // USE_TRACE 1 for now. |
| 158 // | 162 // |
| 159 // The idea is to add hooks for performance traces into the Task/Callback | 163 // The idea is to add hooks for performance traces into the Task/Callback |
| 160 // mechanisms. If it works well enough, will think about generalizing into the | 164 // mechanisms. If it works well enough, will think about generalizing into the |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 e, f, g)); | 260 e, f, g)); |
| 257 } | 261 } |
| 258 | 262 |
| 259 #else | 263 #else |
| 260 # define NewTracedMethod NewRunnableMethod | 264 # define NewTracedMethod NewRunnableMethod |
| 261 #endif | 265 #endif |
| 262 | 266 |
| 263 } // namespace remoting | 267 } // namespace remoting |
| 264 | 268 |
| 265 #endif // REMOTING_BASE_TRACER_H_ | 269 #endif // REMOTING_BASE_TRACER_H_ |
| OLD | NEW |