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 // This code was auto-generated, is not intended to be edited, and is subject to | 5 // This code was auto-generated, is not intended to be edited, and is subject to |
6 // significant change. Please see the README file for more information. | 6 // significant change. Please see the README file for more information. |
7 | 7 |
8 library engine.utilities.general; | 8 library engine.utilities.general; |
9 | 9 |
10 import 'dart:profiler'; | 10 import 'dart:profiler'; |
11 | 11 |
12 /** | 12 /** |
| 13 * Jenkins hash function, optimized for small integers. |
| 14 * Borrowed from sdk/lib/math/jenkins_smi_hash.dart. |
| 15 */ |
| 16 class JenkinsSmiHash { |
| 17 static int combine(int hash, int value) { |
| 18 hash = 0x1fffffff & (hash + value); |
| 19 hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10)); |
| 20 return hash ^ (hash >> 6); |
| 21 } |
| 22 |
| 23 static int finish(int hash) { |
| 24 hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3)); |
| 25 hash = hash ^ (hash >> 11); |
| 26 return 0x1fffffff & (hash + ((0x00003fff & hash) << 15)); |
| 27 } |
| 28 |
| 29 static int hash2(a, b) => finish(combine(combine(0, a), b)); |
| 30 |
| 31 static int hash4(a, b, c, d) => |
| 32 finish(combine(combine(combine(combine(0, a), b), c), d)); |
| 33 } |
| 34 |
| 35 /** |
13 * Helper class for gathering performance statistics. This class is modeled on | 36 * Helper class for gathering performance statistics. This class is modeled on |
14 * the UserTag class in dart:profiler so that it can interoperate easily with | 37 * the UserTag class in dart:profiler so that it can interoperate easily with |
15 * it. | 38 * it. |
16 */ | 39 */ |
17 abstract class PerformanceTag { | 40 abstract class PerformanceTag { |
18 /** | 41 /** |
19 * Return the [PerformanceTag] that is initially current. This is intended | 42 * Return a list of all [PerformanceTag]s which have been created. |
20 * to track time when the system is performing unknown operations. | |
21 */ | 43 */ |
22 static PerformanceTag get UNKNOWN => _PerformanceTagImpl.UNKNOWN; | 44 static List<PerformanceTag> get all => _PerformanceTagImpl.all.toList(); |
23 | 45 |
24 /** | 46 /** |
25 * Return the current [PerformanceTag] for the isolate. | 47 * Return the current [PerformanceTag] for the isolate. |
26 */ | 48 */ |
27 static PerformanceTag get current => _PerformanceTagImpl.current; | 49 static PerformanceTag get current => _PerformanceTagImpl.current; |
28 | 50 |
29 /** | 51 /** |
| 52 * Return the [PerformanceTag] that is initially current. This is intended |
| 53 * to track time when the system is performing unknown operations. |
| 54 */ |
| 55 static PerformanceTag get UNKNOWN => _PerformanceTagImpl.UNKNOWN; |
| 56 |
| 57 /** |
30 * Create a [PerformanceTag] having the given [label]. A [UserTag] will also | 58 * Create a [PerformanceTag] having the given [label]. A [UserTag] will also |
31 * be created, having the same [label], so that performance information can | 59 * be created, having the same [label], so that performance information can |
32 * be queried using the observatory. | 60 * be queried using the observatory. |
33 */ | 61 */ |
34 factory PerformanceTag(String label) = _PerformanceTagImpl; | 62 factory PerformanceTag(String label) = _PerformanceTagImpl; |
35 | 63 |
36 /** | 64 /** |
37 * Return the label for this [PerformanceTag]. | |
38 */ | |
39 String get label; | |
40 | |
41 /** | |
42 * Return a list of all [PerformanceTag]s which have been created. | |
43 */ | |
44 static List<PerformanceTag> get all => _PerformanceTagImpl.all.toList(); | |
45 | |
46 /** | |
47 * Return the total number of milliseconds that this [PerformanceTag] has | 65 * Return the total number of milliseconds that this [PerformanceTag] has |
48 * been the current [PerformanceTag] for the isolate. | 66 * been the current [PerformanceTag] for the isolate. |
49 * | 67 * |
50 * This call is safe even if this [PerformanceTag] is current. | 68 * This call is safe even if this [PerformanceTag] is current. |
51 */ | 69 */ |
52 int get elapsedMs; | 70 int get elapsedMs; |
53 | 71 |
54 /** | 72 /** |
| 73 * Return the label for this [PerformanceTag]. |
| 74 */ |
| 75 String get label; |
| 76 |
| 77 /** |
55 * Make this the current tag for the isolate, and return the previous tag. | 78 * Make this the current tag for the isolate, and return the previous tag. |
56 */ | 79 */ |
57 PerformanceTag makeCurrent(); | 80 PerformanceTag makeCurrent(); |
58 | 81 |
59 /** | 82 /** |
60 * Reset the total time tracked by all [PerformanceTag]s to zero. | 83 * Reset the total time tracked by all [PerformanceTag]s to zero. |
61 */ | 84 */ |
62 static void reset() { | 85 static void reset() { |
63 for (_PerformanceTagImpl tag in _PerformanceTagImpl.all) { | 86 for (_PerformanceTagImpl tag in _PerformanceTagImpl.all) { |
64 tag.stopwatch.reset(); | 87 tag.stopwatch.reset(); |
65 } | 88 } |
66 } | 89 } |
67 } | 90 } |
68 | 91 |
69 class _PerformanceTagImpl implements PerformanceTag { | 92 class _PerformanceTagImpl implements PerformanceTag { |
70 /** | 93 /** |
71 * The current performance tag for the isolate. | 94 * The current performance tag for the isolate. |
72 */ | 95 */ |
73 static _PerformanceTagImpl current = UNKNOWN; | 96 static _PerformanceTagImpl current = UNKNOWN; |
74 | 97 |
75 static final _PerformanceTagImpl UNKNOWN = new _PerformanceTagImpl('unknown'); | 98 static final _PerformanceTagImpl UNKNOWN = new _PerformanceTagImpl('unknown'); |
76 | 99 |
77 /** | 100 /** |
78 * A list of all performance tags that have been created so far. | 101 * A list of all performance tags that have been created so far. |
79 */ | 102 */ |
80 static List<_PerformanceTagImpl> all = <_PerformanceTagImpl>[]; | 103 static List<_PerformanceTagImpl> all = <_PerformanceTagImpl>[]; |
81 | 104 |
82 @override | |
83 String get label => userTag.label; | |
84 | |
85 /** | 105 /** |
86 * The [UserTag] associated with this [PerformanceTag]. | 106 * The [UserTag] associated with this [PerformanceTag]. |
87 */ | 107 */ |
88 final UserTag userTag; | 108 final UserTag userTag; |
89 | 109 |
90 /** | 110 /** |
91 * Stopwatch tracking the amount of time this [PerformanceTag] has been the | 111 * Stopwatch tracking the amount of time this [PerformanceTag] has been the |
92 * current tag for the isolate. | 112 * current tag for the isolate. |
93 */ | 113 */ |
94 final Stopwatch stopwatch; | 114 final Stopwatch stopwatch; |
95 | 115 |
96 _PerformanceTagImpl(String label) | 116 _PerformanceTagImpl(String label) |
97 : userTag = new UserTag(label), stopwatch = new Stopwatch() { | 117 : userTag = new UserTag(label), stopwatch = new Stopwatch() { |
98 all.add(this); | 118 all.add(this); |
99 } | 119 } |
100 | 120 |
101 @override | 121 @override |
| 122 int get elapsedMs => stopwatch.elapsedMilliseconds; |
| 123 |
| 124 @override |
| 125 String get label => userTag.label; |
| 126 |
| 127 @override |
102 PerformanceTag makeCurrent() { | 128 PerformanceTag makeCurrent() { |
103 if (identical(this, current)) { | 129 if (identical(this, current)) { |
104 return current; | 130 return current; |
105 } | 131 } |
106 _PerformanceTagImpl previous = current; | 132 _PerformanceTagImpl previous = current; |
107 previous.stopwatch.stop(); | 133 previous.stopwatch.stop(); |
108 stopwatch.start(); | 134 stopwatch.start(); |
109 current = this; | 135 current = this; |
110 userTag.makeCurrent(); | 136 userTag.makeCurrent(); |
111 return previous; | 137 return previous; |
112 } | 138 } |
113 | |
114 @override | |
115 int get elapsedMs => stopwatch.elapsedMilliseconds; | |
116 } | 139 } |
OLD | NEW |