OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 /// A UserTag can be used to group samples in the Observatory profiler. | 7 /// A UserTag can be used to group samples in the Observatory profiler. |
8 abstract class UserTag { | 8 abstract class UserTag { |
9 /// The maximum number of UserTag instances that can be created by a program. | 9 /// The maximum number of UserTag instances that can be created by a program. |
10 static const MAX_USER_TAGS = 64; | 10 static const MAX_USER_TAGS = 64; |
11 | 11 |
12 factory UserTag(String label) => new _FakeUserTag(label); | 12 external factory UserTag(String label); |
13 | 13 |
14 /// Label of [this]. | 14 /// Label of [this]. |
15 String get label; | 15 String get label; |
16 | 16 |
17 /// Make [this] the current tag for the isolate. Returns the current tag | 17 /// Make [this] the current tag for the isolate. Returns the current tag |
18 /// before setting. | 18 /// before setting. |
19 UserTag makeCurrent(); | 19 UserTag makeCurrent(); |
20 | 20 |
21 /// The default [UserTag] with label 'Default'. | 21 /// The default [UserTag] with label 'Default'. |
22 static UserTag get defaultTag => _FakeUserTag._defaultTag; | 22 external static UserTag get defaultTag; |
23 } | 23 } |
24 | 24 |
25 // This is a fake implementation of UserTag so that code can compile and run | |
26 // in dart2js. | |
27 class _FakeUserTag implements UserTag { | |
28 static Map _instances = {}; | |
29 | |
30 _FakeUserTag.real(this.label); | |
31 | |
32 factory _FakeUserTag(String label) { | |
33 // Canonicalize by name. | |
34 var existingTag = _instances[label]; | |
35 if (existingTag != null) { | |
36 return existingTag; | |
37 } | |
38 // Throw an exception if we've reached the maximum number of user tags. | |
39 if (_instances.length == UserTag.MAX_USER_TAGS) { | |
40 throw new UnsupportedError( | |
41 'UserTag instance limit (${UserTag.MAX_USER_TAGS}) reached.'); | |
42 } | |
43 // Create a new instance and add it to the instance map. | |
44 var instance = new _FakeUserTag.real(label); | |
45 _instances[label] = instance; | |
46 return instance; | |
47 } | |
48 | |
49 final String label; | |
50 | |
51 UserTag makeCurrent() { | |
52 var old = _currentTag; | |
53 _currentTag = this; | |
54 return old; | |
55 } | |
56 | |
57 static final UserTag _defaultTag = new _FakeUserTag('Default'); | |
58 } | |
59 | |
60 var _currentTag = _FakeUserTag._defaultTag; | |
61 | |
62 /// Returns the current [UserTag] for the isolate. | 25 /// Returns the current [UserTag] for the isolate. |
63 UserTag getCurrentTag() { | 26 external UserTag getCurrentTag(); |
64 return _currentTag; | |
65 } | |
66 | 27 |
67 /// Abstract [Metric] class. Metric names must be unique, are hierarchical, | 28 /// Abstract [Metric] class. Metric names must be unique, are hierarchical, |
68 /// and use periods as separators. For example, 'a.b.c'. Uniqueness is only | 29 /// and use periods as separators. For example, 'a.b.c'. Uniqueness is only |
69 /// enforced when a Metric is registered. The name of a metric cannot contain | 30 /// enforced when a Metric is registered. The name of a metric cannot contain |
70 /// the slash ('/') character. | 31 /// the slash ('/') character. |
71 abstract class Metric { | 32 abstract class Metric { |
72 /// [name] of this metric. | 33 /// [name] of this metric. |
73 final String name; | 34 final String name; |
74 /// [description] of this metric. | 35 /// [description] of this metric. |
75 final String description; | 36 final String description; |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 for (var metric in _metrics.values) { | 149 for (var metric in _metrics.values) { |
189 metrics.add(metric._toJSON()); | 150 metrics.add(metric._toJSON()); |
190 } | 151 } |
191 var map = { | 152 var map = { |
192 'type': 'MetricList', | 153 'type': 'MetricList', |
193 'metrics': metrics, | 154 'metrics': metrics, |
194 }; | 155 }; |
195 return JSON.encode(map); | 156 return JSON.encode(map); |
196 } | 157 } |
197 } | 158 } |
OLD | NEW |