OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 part of dart.developer; |
| 6 |
| 7 typedef dynamic TimelineSyncFunction(); |
| 8 |
| 9 /// Add to the timeline. |
| 10 class Timeline { |
| 11 /// Start a synchronous operation labeled [name]. Optionally takes |
| 12 /// a [Map] of [arguments]. This operation must be finished before |
| 13 /// returning to the event queue. |
| 14 static void startSync(String name, {Map arguments}) { |
| 15 if (name is! String) { |
| 16 throw new ArgumentError.value(name, |
| 17 'name', |
| 18 'Must be a String'); |
| 19 } |
| 20 var block = new _SyncBlock._(name, _getTraceClock()); |
| 21 if (arguments is Map) { |
| 22 block.arguments.addAll(arguments); |
| 23 } |
| 24 _stack.add(block); |
| 25 } |
| 26 |
| 27 /// Finish the last synchronous operation that was started. |
| 28 static void finishSync() { |
| 29 if (_stack.length == 0) { |
| 30 throw new StateError( |
| 31 'Uneven calls to startSync and finishSync'); |
| 32 } |
| 33 // Pop top item off of stack. |
| 34 var block = _stack.removeLast(); |
| 35 // Close it. |
| 36 block.close(); |
| 37 } |
| 38 |
| 39 /// A utility method to time a synchronous [function]. Internally calls |
| 40 /// [function] bracketed by calls to [startSync] and [finishSync]. |
| 41 static dynamic timeSync(String name, |
| 42 TimelineSyncFunction function, |
| 43 {Map arguments}) { |
| 44 beginSync(name, arguments: arguments); |
| 45 try { |
| 46 return function(); |
| 47 } finally { |
| 48 finishSync(); |
| 49 } |
| 50 } |
| 51 |
| 52 static final List<_SyncBlock> _stack = new List<_SyncBlock>(); |
| 53 } |
| 54 |
| 55 /// A synchronous block of time on the timeline. This block should not be |
| 56 /// kept open across isolate messages. |
| 57 class _SyncBlock { |
| 58 /// The category this block belongs to. |
| 59 final String category = 'Dart'; |
| 60 /// The name of this block. |
| 61 final String name; |
| 62 /// An (optional) set of arguments which will be serialized to JSON and |
| 63 /// associated with this block. |
| 64 final Map arguments = {}; |
| 65 // The start time stamp. |
| 66 final int _start; |
| 67 // Has this block been closed? |
| 68 bool _closed = false; |
| 69 |
| 70 _SyncBlock._(this.name, |
| 71 this._start); |
| 72 |
| 73 /// Close this block of time. At this point, this block can no longer be |
| 74 /// used. |
| 75 void close() { |
| 76 if (_closed) { |
| 77 throw new StateError( |
| 78 'It is illegal to call close twice on the same _SyncBlock'); |
| 79 } |
| 80 _closed = true; |
| 81 var end = _getTraceClock(); |
| 82 |
| 83 // Encode arguments map as JSON before reporting. |
| 84 var argumentsAsJson = JSON.encode(arguments); |
| 85 |
| 86 // Report event to runtime. |
| 87 _reportCompleteEvent(_start, |
| 88 end, |
| 89 category, |
| 90 name, |
| 91 argumentsAsJson); |
| 92 } |
| 93 } |
| 94 |
| 95 /// Returns the current value from the trace clock. |
| 96 external int _getTraceClock(); |
| 97 |
| 98 /// Reports a complete synchronous event. |
| 99 external void _reportCompleteEvent(int start, |
| 100 int end, |
| 101 String category, |
| 102 String name, |
| 103 String argumentsAsJson); |
OLD | NEW |