OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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 part of dart.developer; | 5 part of dart.developer; |
6 | 6 |
7 typedef dynamic TimelineSyncFunction(); | 7 typedef dynamic TimelineSyncFunction(); |
8 | 8 |
9 /// Add to the timeline. | 9 /// Add to the timeline. |
10 class Timeline { | 10 class Timeline { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 finishSync(); | 48 finishSync(); |
49 } | 49 } |
50 } | 50 } |
51 | 51 |
52 static final List<_SyncBlock> _stack = new List<_SyncBlock>(); | 52 static final List<_SyncBlock> _stack = new List<_SyncBlock>(); |
53 } | 53 } |
54 | 54 |
55 /// An asynchronous task on the timeline. Asynchronous tasks can live | 55 /// An asynchronous task on the timeline. Asynchronous tasks can live |
56 /// longer than the current event and can even be shared between isolates. | 56 /// longer than the current event and can even be shared between isolates. |
57 /// An asynchronous task can have many (nested) blocks. To share a | 57 /// An asynchronous task can have many (nested) blocks. To share a |
58 /// [TimelineTask] across isolates, you must construct a [TimelineTask] in | 58 /// [_TimelineTask] across isolates, you must construct a [_TimelineTask] in |
59 /// both isolates using the same [taskId] and [category]. | 59 /// both isolates using the same [taskId] and [category]. |
60 class TimelineTask { | 60 class _TimelineTask { |
61 /// Create a task. [taskId] will be set by the system. | 61 /// Create a task. [taskId] will be set by the system. |
62 /// Optionally you can specify a [category] name. | 62 /// Optionally you can specify a [category] name. |
63 TimelineTask({String category: 'Dart'}) | 63 _TimelineTask({String category: 'Dart'}) |
64 : _taskId = _getNextAsyncId(), | 64 : _taskId = _getNextAsyncId(), |
65 category = category { | 65 category = category { |
66 if (category is! String) { | 66 if (category is! String) { |
67 throw new ArgumentError.value(category, | 67 throw new ArgumentError.value(category, |
68 'category', | 68 'category', |
69 'Must be a String'); | 69 'Must be a String'); |
70 } | 70 } |
71 } | 71 } |
72 | 72 |
73 /// Create a task with an explicit [taskId]. This is useful if you are | 73 /// Create a task with an explicit [taskId]. This is useful if you are |
74 /// passing a task between isolates. Optionally you can specify a [category] | 74 /// passing a task between isolates. Optionally you can specify a [category] |
75 /// name. | 75 /// name. |
76 TimelineTask.withTaskId(int taskId, {String category: 'Dart'}) | 76 _TimelineTask.withTaskId(int taskId, {String category: 'Dart'}) |
77 : _taskId = taskId, | 77 : _taskId = taskId, |
78 category = category { | 78 category = category { |
79 if (taskId is! int) { | 79 if (taskId is! int) { |
80 throw new ArgumentError.value(taskId, | 80 throw new ArgumentError.value(taskId, |
81 'taskId', | 81 'taskId', |
82 'Must be an int'); | 82 'Must be an int'); |
83 } | 83 } |
84 if (category is! String) { | 84 if (category is! String) { |
85 throw new ArgumentError.value(category, | 85 throw new ArgumentError.value(category, |
86 'category', | 86 'category', |
87 'Must be a String'); | 87 'Must be a String'); |
88 } | 88 } |
89 } | 89 } |
90 | 90 |
91 /// Start a block in this task named [name]. Optionally takes | 91 /// Start a block in this task named [name]. Optionally takes |
92 /// a [Map] of [arguments]. | 92 /// a [Map] of [arguments]. |
93 /// Returns an [AsyncBlock] which is used to finish this block. | 93 /// Returns an [_AsyncBlock] which is used to finish this block. |
94 AsyncBlock start(String name, {Map arguments}) { | 94 _AsyncBlock start(String name, {Map arguments}) { |
95 if (name is! String) { | 95 if (name is! String) { |
96 throw new ArgumentError.value(name, | 96 throw new ArgumentError.value(name, |
97 'name', | 97 'name', |
98 'Must be a String'); | 98 'Must be a String'); |
99 } | 99 } |
100 var block = new AsyncBlock._(name, _taskId, category); | 100 var block = new _AsyncBlock._(name, _taskId, category); |
101 if (arguments is Map) { | 101 if (arguments is Map) { |
102 block.arguments.addAll(arguments); | 102 block.arguments.addAll(arguments); |
103 } | 103 } |
104 /// Emit start event. | 104 /// Emit start event. |
105 block._start(); | 105 block._start(); |
106 return block; | 106 return block; |
107 } | 107 } |
108 | 108 |
109 /// Retrieve the asynchronous task's id. Can be used to construct a | 109 /// Retrieve the asynchronous task's id. Can be used to construct a |
110 /// [TimelineTask] in another isolate. | 110 /// [_TimelineTask] in another isolate. |
111 int get taskId => _taskId; | 111 int get taskId => _taskId; |
112 final int _taskId; | 112 final int _taskId; |
113 /// Retrieve the asynchronous task's category. Can be used to construct a | 113 /// Retrieve the asynchronous task's category. Can be used to construct a |
114 /// [TimelineTask] in another isolate. | 114 /// [_TimelineTask] in another isolate. |
115 final String category; | 115 final String category; |
116 } | 116 } |
117 | 117 |
118 /// An asynchronous block of time on the timeline. This block can be kept | 118 /// An asynchronous block of time on the timeline. This block can be kept |
119 /// open across isolate messages. | 119 /// open across isolate messages. |
120 class AsyncBlock { | 120 class _AsyncBlock { |
121 /// The category this block belongs to. | 121 /// The category this block belongs to. |
122 final String category; | 122 final String category; |
123 /// The name of this block. | 123 /// The name of this block. |
124 final String name; | 124 final String name; |
125 /// The asynchronous task id. | 125 /// The asynchronous task id. |
126 final int _taskId; | 126 final int _taskId; |
127 /// An (optional) set of arguments which will be serialized to JSON and | 127 /// An (optional) set of arguments which will be serialized to JSON and |
128 /// associated with this block. | 128 /// associated with this block. |
129 final Map arguments = {}; | 129 final Map arguments = {}; |
130 bool _finished = false; | 130 bool _finished = false; |
131 | 131 |
132 AsyncBlock._(this.name, this._taskId, this.category); | 132 _AsyncBlock._(this.name, this._taskId, this.category); |
133 | 133 |
134 // Emit the start event. | 134 // Emit the start event. |
135 void _start() { | 135 void _start() { |
136 String argumentsAsJson = JSON.encode(arguments); | 136 String argumentsAsJson = JSON.encode(arguments); |
137 _reportTaskEvent(_getTraceClock(), | 137 _reportTaskEvent(_getTraceClock(), |
138 _taskId, | 138 _taskId, |
139 'b', | 139 'b', |
140 category, | 140 category, |
141 name, | 141 name, |
142 argumentsAsJson); | 142 argumentsAsJson); |
143 } | 143 } |
144 | 144 |
145 // Emit the finish event. | 145 // Emit the finish event. |
146 void _finish() { | 146 void _finish() { |
147 _reportTaskEvent(_getTraceClock(), | 147 _reportTaskEvent(_getTraceClock(), |
148 _taskId, | 148 _taskId, |
149 'e', | 149 'e', |
150 category, | 150 category, |
151 name, | 151 name, |
152 JSON.encode({})); | 152 JSON.encode({})); |
153 } | 153 } |
154 | 154 |
155 /// Finish this block. Cannot be called twice. | 155 /// Finish this block. Cannot be called twice. |
156 void finish() { | 156 void finish() { |
157 if (_finished) { | 157 if (_finished) { |
158 throw new StateError( | 158 throw new StateError( |
159 'It is illegal to call finish twice on the same AsyncBlock'); | 159 'It is illegal to call finish twice on the same _AsyncBlock'); |
160 } | 160 } |
161 _finished = true; | 161 _finished = true; |
162 _finish(); | 162 _finish(); |
163 } | 163 } |
164 | 164 |
165 /// Finishes this block when [future] completes. Returns a [Future] | 165 /// Finishes this block when [future] completes. Returns a [Future] |
166 /// chained to [future]. | 166 /// chained to [future]. |
167 Future finishWhenComplete(Future future) { | 167 Future finishWhenComplete(Future future) { |
168 if (future is! Future) { | 168 if (future is! Future) { |
169 throw new ArgumentError.value(future, | 169 throw new ArgumentError.value(future, |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 String category, | 222 String category, |
223 String name, | 223 String name, |
224 String argumentsAsJson); | 224 String argumentsAsJson); |
225 | 225 |
226 /// Reports a complete synchronous event. | 226 /// Reports a complete synchronous event. |
227 external void _reportCompleteEvent(int start, | 227 external void _reportCompleteEvent(int start, |
228 int end, | 228 int end, |
229 String category, | 229 String category, |
230 String name, | 230 String name, |
231 String argumentsAsJson); | 231 String argumentsAsJson); |
OLD | NEW |