| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library dart2js.common.tasks; | 5 library dart2js.common.tasks; |
| 6 | 6 |
| 7 import 'dart:async' | 7 import 'dart:async' |
| 8 show Future, Zone, ZoneDelegate, ZoneSpecification, runZoned; | 8 show Future, Zone, ZoneDelegate, ZoneSpecification, runZoned; |
| 9 | 9 |
| 10 /// Used to measure where time is spent in the compiler. | 10 /// Used to measure where time is spent in the compiler. |
| 11 /// | 11 /// |
| 12 /// This exposes [measure] and [measureIo], which wrap an action and associate | 12 /// This exposes [measure] and [measureIo], which wrap an action and associate |
| 13 /// the time spent during that action with this task. Nested measurementsccan be | 13 /// the time spent during that action with this task. Nested measurementsccan be |
| 14 /// introduced by using [measureSubtask]. | 14 /// introduced by using [measureSubtask]. |
| 15 // TODO(sigmund): rename to MeasurableTask | 15 // TODO(sigmund): rename to MeasurableTask |
| 16 abstract class CompilerTask { | 16 abstract class CompilerTask { |
| 17 final Measurer measurer; | 17 final Measurer measurer; |
| 18 final Stopwatch _watch; | 18 final Stopwatch _watch; |
| 19 final Map<String, GenericTask> _subtasks = <String, GenericTask>{}; | 19 final Map<String, GenericTask> _subtasks = <String, GenericTask>{}; |
| 20 | 20 |
| 21 int asyncCount = 0; | 21 int asyncCount = 0; |
| 22 | 22 |
| 23 // Each task has a fixed, lazily computed, ZoneSpecification and zoneValues |
| 24 // for [_measureZoned]. |
| 25 ZoneSpecification _zoneSpecification; |
| 26 Map _zoneValues; |
| 27 |
| 23 CompilerTask(Measurer measurer) | 28 CompilerTask(Measurer measurer) |
| 24 : measurer = measurer, | 29 : measurer = measurer, |
| 25 _watch = measurer.enableTaskMeasurements ? new Stopwatch() : null; | 30 _watch = measurer.enableTaskMeasurements ? new Stopwatch() : null; |
| 26 | 31 |
| 27 /// Whether measurement is disabled. The functions [measure] and [measureIo] | 32 /// Whether measurement is disabled. The functions [measure] and [measureIo] |
| 28 /// only measure time if measurements are enabled. | 33 /// only measure time if measurements are enabled. |
| 29 bool get _isDisabled => _watch == null; | 34 bool get _isDisabled => _watch == null; |
| 30 | 35 |
| 31 /// Name to use for reporting timing information. Subclasses should override | 36 /// Name to use for reporting timing information. Subclasses should override |
| 32 /// this with a proper name, otherwise we use the runtime type of the task. | 37 /// this with a proper name, otherwise we use the runtime type of the task. |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 // Using zones, we're able to track asynchronous operations correctly, as | 95 // Using zones, we're able to track asynchronous operations correctly, as |
| 91 // our zone will be asked to invoke `then` blocks. Then blocks (the closure | 96 // our zone will be asked to invoke `then` blocks. Then blocks (the closure |
| 92 // passed to runZoned, and other closures) are run via the `run` functions | 97 // passed to runZoned, and other closures) are run via the `run` functions |
| 93 // below. | 98 // below. |
| 94 | 99 |
| 95 assert(_watch != null); | 100 assert(_watch != null); |
| 96 | 101 |
| 97 // The current zone is already measuring `this` task. | 102 // The current zone is already measuring `this` task. |
| 98 if (Zone.current[measurer] == this) return action(); | 103 if (Zone.current[measurer] == this) return action(); |
| 99 | 104 |
| 100 /// Run [f] in [zone]. Running must be delegated to [parent] to ensure that | 105 return runZoned(action, |
| 101 /// various state is set up correctly (in particular that `Zone.current` | 106 zoneValues: _zoneValues ??= {measurer: this}, |
| 102 /// has the right value). Since [_measureZoned] can be called recursively | 107 zoneSpecification: _zoneSpecification ??= new ZoneSpecification( |
| 103 /// (synchronously), some of the measuring zones we create will be parents | 108 run: _run, runUnary: _runUnary, runBinary: _runBinary)); |
| 104 /// of other measuring zones, but we still need to call through the parent | 109 } |
| 105 /// chain. Consequently, we use a zone value keyed by [measurer] to see if | 110 |
| 106 /// we should measure or not when delegating. | 111 /// Run [f] in [zone]. Running must be delegated to [parent] to ensure that |
| 107 run(Zone self, ZoneDelegate parent, Zone zone, f()) { | 112 /// various state is set up correctly (in particular that `Zone.current` |
| 108 if (zone[measurer] != this) return parent.run(zone, f); | 113 /// has the right value). Since [_measureZoned] can be called recursively |
| 109 CompilerTask previous = _start(); | 114 /// (synchronously), some of the measuring zones we create will be parents |
| 110 try { | 115 /// of other measuring zones, but we still need to call through the parent |
| 111 return parent.run(zone, f); | 116 /// chain. Consequently, we use a zone value keyed by [measurer] to see if |
| 112 } finally { | 117 /// we should measure or not when delegating. |
| 113 _stop(previous); | 118 _run(Zone self, ZoneDelegate parent, Zone zone, f()) { |
| 114 } | 119 if (zone[measurer] != this) return parent.run(zone, f); |
| 120 CompilerTask previous = _start(); |
| 121 try { |
| 122 return parent.run(zone, f); |
| 123 } finally { |
| 124 _stop(previous); |
| 115 } | 125 } |
| 126 } |
| 116 | 127 |
| 117 /// Same as [run] except that [f] takes one argument, [arg]. | 128 /// Same as [run] except that [f] takes one argument, [arg]. |
| 118 runUnary(Zone self, ZoneDelegate parent, Zone zone, f(arg), arg) { | 129 _runUnary(Zone self, ZoneDelegate parent, Zone zone, f(arg), arg) { |
| 119 if (zone[measurer] != this) return parent.runUnary(zone, f, arg); | 130 if (zone[measurer] != this) return parent.runUnary(zone, f, arg); |
| 120 CompilerTask previous = _start(); | 131 CompilerTask previous = _start(); |
| 121 try { | 132 try { |
| 122 return parent.runUnary(zone, f, arg); | 133 return parent.runUnary(zone, f, arg); |
| 123 } finally { | 134 } finally { |
| 124 _stop(previous); | 135 _stop(previous); |
| 125 } | |
| 126 } | 136 } |
| 137 } |
| 127 | 138 |
| 128 /// Same as [run] except that [f] takes two arguments ([a1] and [a2]). | 139 /// Same as [run] except that [f] takes two arguments ([a1] and [a2]). |
| 129 runBinary(Zone self, ZoneDelegate parent, Zone zone, f(a1, a2), a1, a2) { | 140 _runBinary(Zone self, ZoneDelegate parent, Zone zone, f(a1, a2), a1, a2) { |
| 130 if (zone[measurer] != this) return parent.runBinary(zone, f, a1, a2); | 141 if (zone[measurer] != this) return parent.runBinary(zone, f, a1, a2); |
| 131 CompilerTask previous = _start(); | 142 CompilerTask previous = _start(); |
| 132 try { | 143 try { |
| 133 return parent.runBinary(zone, f, a1, a2); | 144 return parent.runBinary(zone, f, a1, a2); |
| 134 } finally { | 145 } finally { |
| 135 _stop(previous); | 146 _stop(previous); |
| 136 } | |
| 137 } | 147 } |
| 138 | |
| 139 return runZoned(action, | |
| 140 zoneValues: {measurer: this}, | |
| 141 zoneSpecification: new ZoneSpecification( | |
| 142 run: run, runUnary: runUnary, runBinary: runBinary)); | |
| 143 } | 148 } |
| 144 | 149 |
| 145 /// Asynchronous version of [measure]. Use this when action returns a future | 150 /// Asynchronous version of [measure]. Use this when action returns a future |
| 146 /// that's truly asynchronous, such I/O. Only one task can use this method | 151 /// that's truly asynchronous, such I/O. Only one task can use this method |
| 147 /// concurrently. | 152 /// concurrently. |
| 148 /// | 153 /// |
| 149 /// Note: we assume that this method is used only by the compiler input | 154 /// Note: we assume that this method is used only by the compiler input |
| 150 /// provider, but it could be used by other tasks as long as the input | 155 /// provider, but it could be used by other tasks as long as the input |
| 151 /// provider will not be called by those tasks. | 156 /// provider will not be called by those tasks. |
| 152 measureIo(Future action()) { | 157 measureIo(Future action()) { |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 } | 248 } |
| 244 | 249 |
| 245 /// Call this when the eventloop returns control to us. | 250 /// Call this when the eventloop returns control to us. |
| 246 void stopAsyncWallClock() { | 251 void stopAsyncWallClock() { |
| 247 if (currentAsyncTask != null) { | 252 if (currentAsyncTask != null) { |
| 248 currentAsyncTask._watch.stop(); | 253 currentAsyncTask._watch.stop(); |
| 249 } | 254 } |
| 250 asyncWallClock.stop(); | 255 asyncWallClock.stop(); |
| 251 } | 256 } |
| 252 } | 257 } |
| OLD | NEW |