| 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:profiler' show | 7 import 'dart:profiler' show |
| 8 UserTag; | 8 UserTag; |
| 9 import '../compiler.dart' show | 9 import '../compiler.dart' show |
| 10 Compiler; | 10 Compiler; |
| 11 import '../elements/elements.dart' show | 11 import '../elements/elements.dart' show |
| 12 Element; | 12 Element; |
| 13 | 13 |
| 14 typedef void DeferredAction(); | 14 typedef void DeferredAction(); |
| 15 | 15 |
| 16 class DeferredTask { | 16 class DeferredTask { |
| 17 final Element element; | 17 final Element element; |
| 18 final DeferredAction action; | 18 final DeferredAction action; |
| 19 | 19 |
| 20 DeferredTask(this.element, this.action); | 20 DeferredTask(this.element, this.action); |
| 21 } | 21 } |
| 22 | 22 |
| 23 class CompilerTask { | 23 class CompilerTask { |
| 24 final Compiler compiler; | 24 final Compiler compiler; |
| 25 final Stopwatch watch; | 25 final Stopwatch watch; |
| 26 UserTag profilerTag; | 26 UserTag profilerTag; |
| 27 final Map<String, GenericTask> _subtasks = <String, GenericTask>{}; |
| 27 | 28 |
| 28 CompilerTask(Compiler compiler) | 29 CompilerTask(Compiler compiler) |
| 29 : this.compiler = compiler, | 30 : this.compiler = compiler, |
| 30 watch = (compiler.verbose) ? new Stopwatch() : null; | 31 watch = (compiler.verbose) ? new Stopwatch() : null; |
| 31 | 32 |
| 32 String get name => "Unknown task '${this.runtimeType}'"; | 33 String get name => "Unknown task '${this.runtimeType}'"; |
| 33 int get timing => (watch != null) ? watch.elapsedMilliseconds : 0; | |
| 34 | 34 |
| 35 int get timingMicroseconds => (watch != null) ? watch.elapsedMicroseconds : 0; | 35 int get timing { |
| 36 if (watch == null) return 0; |
| 37 int total = watch.elapsedMilliseconds; |
| 38 for (GenericTask subtask in _subtasks.values) { |
| 39 total += subtask.timing; |
| 40 } |
| 41 return total; |
| 42 } |
| 36 | 43 |
| 37 UserTag getProfilerTag() { | 44 UserTag getProfilerTag() { |
| 38 if (profilerTag == null) profilerTag = new UserTag(name); | 45 if (profilerTag == null) profilerTag = new UserTag(name); |
| 39 return profilerTag; | 46 return profilerTag; |
| 40 } | 47 } |
| 41 | 48 |
| 42 measure(action()) { | 49 measure(action()) { |
| 43 // In verbose mode when watch != null. | 50 // In verbose mode when watch != null. |
| 44 if (watch == null) return action(); | 51 if (watch == null) return action(); |
| 45 CompilerTask previous = compiler.measuredTask; | 52 CompilerTask previous = compiler.measuredTask; |
| 46 if (identical(this, previous)) return action(); | 53 if (identical(this, previous)) return action(); |
| 47 compiler.measuredTask = this; | 54 compiler.measuredTask = this; |
| 48 if (previous != null) previous.watch.stop(); | 55 if (previous != null) previous.watch.stop(); |
| 49 watch.start(); | 56 watch.start(); |
| 50 UserTag oldTag = getProfilerTag().makeCurrent(); | 57 UserTag oldTag = getProfilerTag().makeCurrent(); |
| 51 try { | 58 try { |
| 52 return action(); | 59 return action(); |
| 53 } finally { | 60 } finally { |
| 54 watch.stop(); | 61 watch.stop(); |
| 55 oldTag.makeCurrent(); | 62 oldTag.makeCurrent(); |
| 56 if (previous != null) previous.watch.start(); | 63 if (previous != null) previous.watch.start(); |
| 57 compiler.measuredTask = previous; | 64 compiler.measuredTask = previous; |
| 58 } | 65 } |
| 59 } | 66 } |
| 60 | 67 |
| 61 measureElement(Element element, action()) { | 68 measureElement(Element element, action()) { |
| 62 compiler.withCurrentElement(element, () => measure(action)); | 69 compiler.withCurrentElement(element, () => measure(action)); |
| 63 } | 70 } |
| 71 |
| 72 /// Measure the time spent in [action] (if in verbose mode) and accumulate it |
| 73 /// under a subtask with the given name. |
| 74 measureSubtask(String name, action()) { |
| 75 if (watch == null) return action(); |
| 76 // Use a nested CompilerTask for the measurement to ensure nested [measure] |
| 77 // calls work correctly. The subtasks will never themselves have nested |
| 78 // subtasks because they are not accessible outside. |
| 79 GenericTask subtask = _subtasks.putIfAbsent(name, |
| 80 () => new GenericTask(name, compiler)); |
| 81 return subtask.measure(action); |
| 82 } |
| 83 |
| 84 Iterable<String> get subtasks => _subtasks.keys; |
| 85 |
| 86 int getSubtaskTime(String subtask) => _subtasks[subtask].timing; |
| 64 } | 87 } |
| 65 | 88 |
| 66 class GenericTask extends CompilerTask { | 89 class GenericTask extends CompilerTask { |
| 67 final String name; | 90 final String name; |
| 68 | 91 |
| 69 GenericTask(this.name, Compiler compiler) | 92 GenericTask(this.name, Compiler compiler) |
| 70 : super(compiler); | 93 : super(compiler); |
| 71 } | 94 } |
| OLD | NEW |