OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
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. | |
7 | |
8 library engine.utilities.general; | |
9 | |
10 import 'dart:profiler'; | |
11 | |
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 hash3(a, b, c) => finish(combine(combine(combine(0, a), b), c)); | |
32 | |
33 static int hash4(a, b, c, d) => | |
34 finish(combine(combine(combine(combine(0, a), b), c), d)); | |
35 } | |
36 | |
37 /** | |
38 * Helper class for gathering performance statistics. This class is modeled on | |
39 * the UserTag class in dart:profiler so that it can interoperate easily with | |
40 * it. | |
41 */ | |
42 abstract class PerformanceTag { | |
43 /** | |
44 * Return a list of all [PerformanceTag]s which have been created. | |
45 */ | |
46 static List<PerformanceTag> get all => _PerformanceTagImpl.all.toList(); | |
47 | |
48 /** | |
49 * Return the current [PerformanceTag] for the isolate. | |
50 */ | |
51 static PerformanceTag get current => _PerformanceTagImpl.current; | |
52 | |
53 /** | |
54 * Return the [PerformanceTag] that is initially current. This is intended | |
55 * to track time when the system is performing unknown operations. | |
56 */ | |
57 static PerformanceTag get UNKNOWN => _PerformanceTagImpl.UNKNOWN; | |
58 | |
59 /** | |
60 * Create a [PerformanceTag] having the given [label]. A [UserTag] will also | |
61 * be created, having the same [label], so that performance information can | |
62 * be queried using the observatory. | |
63 */ | |
64 factory PerformanceTag(String label) = _PerformanceTagImpl; | |
65 | |
66 /** | |
67 * Return the total number of milliseconds that this [PerformanceTag] has | |
68 * been the current [PerformanceTag] for the isolate. | |
69 * | |
70 * This call is safe even if this [PerformanceTag] is current. | |
71 */ | |
72 int get elapsedMs; | |
73 | |
74 /** | |
75 * Return the label for this [PerformanceTag]. | |
76 */ | |
77 String get label; | |
78 | |
79 /** | |
80 * Make this the current tag for the isolate, and return the previous tag. | |
81 */ | |
82 PerformanceTag makeCurrent(); | |
83 | |
84 /** | |
85 * Make this the current tag for the isolate, run [f], and restore the | |
86 * previous tag. Returns the result of invoking [f]. | |
87 */ | |
88 makeCurrentWhile(f()); | |
89 | |
90 /** | |
91 * Reset the total time tracked by all [PerformanceTag]s to zero. | |
92 */ | |
93 static void reset() { | |
94 for (_PerformanceTagImpl tag in _PerformanceTagImpl.all) { | |
95 tag.stopwatch.reset(); | |
96 } | |
97 } | |
98 } | |
99 | |
100 class _PerformanceTagImpl implements PerformanceTag { | |
101 /** | |
102 * The current performance tag for the isolate. | |
103 */ | |
104 static _PerformanceTagImpl current = UNKNOWN; | |
105 | |
106 static final _PerformanceTagImpl UNKNOWN = new _PerformanceTagImpl('unknown'); | |
107 | |
108 /** | |
109 * A list of all performance tags that have been created so far. | |
110 */ | |
111 static List<_PerformanceTagImpl> all = <_PerformanceTagImpl>[]; | |
112 | |
113 /** | |
114 * The [UserTag] associated with this [PerformanceTag]. | |
115 */ | |
116 final UserTag userTag; | |
117 | |
118 /** | |
119 * Stopwatch tracking the amount of time this [PerformanceTag] has been the | |
120 * current tag for the isolate. | |
121 */ | |
122 final Stopwatch stopwatch; | |
123 | |
124 _PerformanceTagImpl(String label) | |
125 : userTag = new UserTag(label), | |
126 stopwatch = new Stopwatch() { | |
127 all.add(this); | |
128 } | |
129 | |
130 @override | |
131 int get elapsedMs => stopwatch.elapsedMilliseconds; | |
132 | |
133 @override | |
134 String get label => userTag.label; | |
135 | |
136 @override | |
137 PerformanceTag makeCurrent() { | |
138 if (identical(this, current)) { | |
139 return current; | |
140 } | |
141 _PerformanceTagImpl previous = current; | |
142 previous.stopwatch.stop(); | |
143 stopwatch.start(); | |
144 current = this; | |
145 userTag.makeCurrent(); | |
146 return previous; | |
147 } | |
148 | |
149 makeCurrentWhile(f()) { | |
150 PerformanceTag prevTag = makeCurrent(); | |
151 try { | |
152 return f(); | |
153 } finally { | |
154 prevTag.makeCurrent(); | |
155 } | |
156 } | |
157 } | |
OLD | NEW |