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 library dart.profiler; | 5 library dart.profiler; |
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 factory UserTag(String label) => new _FakeUserTag(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. | 17 /// Make [this] the current tag for the isolate. Returns the current tag |
18 makeCurrent(); | 18 /// before setting. |
| 19 UserTag makeCurrent(); |
| 20 |
| 21 /// The default [UserTag] with label 'Default'. |
| 22 static UserTag get defaultTag => _FakeUserTag._defaultTag; |
19 } | 23 } |
20 | 24 |
21 // This is a fake implementation of UserTag so that code can compile and run | 25 // This is a fake implementation of UserTag so that code can compile and run |
22 // in dart2js. | 26 // in dart2js. |
23 class _FakeUserTag implements UserTag { | 27 class _FakeUserTag implements UserTag { |
24 static List _instances = []; | 28 static Map _instances = {}; |
25 | 29 |
26 _FakeUserTag.real(this.label); | 30 _FakeUserTag.real(this.label); |
27 | 31 |
28 factory _FakeUserTag(String label) { | 32 factory _FakeUserTag(String label) { |
29 // Canonicalize by name. | 33 // Canonicalize by name. |
30 for (var tag in _instances) { | 34 var existingTag = _instances[label]; |
31 if (tag.label == label) { | 35 if (existingTag != null) { |
32 return tag; | 36 return existingTag; |
33 } | |
34 } | 37 } |
35 // Throw an exception if we've reached the maximum number of user tags. | 38 // Throw an exception if we've reached the maximum number of user tags. |
36 if (_instances.length == UserTag.MAX_USER_TAGS) { | 39 if (_instances.length == UserTag.MAX_USER_TAGS) { |
37 throw new UnsupportedError( | 40 throw new UnsupportedError( |
38 'UserTag instance limit (${UserTag.MAX_USER_TAGS}) reached.'); | 41 'UserTag instance limit (${UserTag.MAX_USER_TAGS}) reached.'); |
39 } | 42 } |
40 // Create a new instance and add it to the instance list. | 43 // Create a new instance and add it to the instance map. |
41 var instance = new _FakeUserTag.real(label); | 44 var instance = new _FakeUserTag.real(label); |
42 _instances.add(instance); | 45 _instances[label] = instance; |
43 return instance; | 46 return instance; |
44 } | 47 } |
45 | 48 |
46 final String label; | 49 final String label; |
47 | 50 |
48 makeCurrent() { | 51 UserTag makeCurrent() { |
| 52 var old = _currentTag; |
49 _currentTag = this; | 53 _currentTag = this; |
| 54 return old; |
50 } | 55 } |
| 56 |
| 57 static final UserTag _defaultTag = new _FakeUserTag('Default'); |
51 } | 58 } |
52 | 59 |
53 var _currentTag = null; | 60 var _currentTag = _FakeUserTag._defaultTag; |
54 | 61 |
55 /// Returns the current [UserTag] for the isolate. | 62 /// Returns the current [UserTag] for the isolate. |
56 UserTag getCurrentTag() { | 63 UserTag getCurrentTag() { |
57 return _currentTag; | 64 return _currentTag; |
58 } | 65 } |
59 | |
60 /// Sets the current [UserTag] for the isolate to null. Returns current tag | |
61 /// before clearing. | |
62 UserTag clearCurrentTag() { | |
63 var old = _currentTag; | |
64 _currentTag = null; | |
65 return old; | |
66 } | |
OLD | NEW |