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