Index: pkg/dev_compiler/tool/input_sdk/private/ddc_runtime/operations.dart |
diff --git a/pkg/dev_compiler/tool/input_sdk/private/ddc_runtime/operations.dart b/pkg/dev_compiler/tool/input_sdk/private/ddc_runtime/operations.dart |
index ee909234851c7681b04721fa3fdc39eb122ef938..7117de2fb82d7d9157c7f0995013647fd46f95bc 100644 |
--- a/pkg/dev_compiler/tool/input_sdk/private/ddc_runtime/operations.dart |
+++ b/pkg/dev_compiler/tool/input_sdk/private/ddc_runtime/operations.dart |
@@ -364,25 +364,101 @@ dgsendRepl(obj, typeArgs, method, @rest args) => |
class _MethodStats { |
final String typeName; |
final String frame; |
- int count; |
+ double count; |
Jacob
2017/04/12 18:24:42
Switch to a a random sampling profiler to make pro
|
_MethodStats(this.typeName, this.frame) { |
- count = 0; |
+ count = 0.0; |
} |
} |
+class _CallMethodRecord { |
+ var jsError; |
+ var type; |
+ |
+ _CallMethodRecord(this.jsError, this.type); |
+} |
+ |
Map<String, _MethodStats> _callMethodStats = new Map(); |
+/// Size for the random sample of dynamic calls. |
+int _callRecordSampleSize = 5000; |
+ |
+/// If the number of dynamic calls exceeds [_callRecordSampleSize] this list |
+/// will represent a random sample of the dynamic calls made. |
+List<_CallMethodRecord> _callMethodRecords = new List(); |
+ |
+/// If the number of dynamic calls exceeds [_callRecordSampleSize] this value |
+/// will be greater than [_callMethodRecords.length]. |
+int _totalCallRecords = 0; |
+ |
+/// Minimum number of samples to consider a profile entry relevant. |
+/// This could be set a lot higher. We set this value so users are not |
+/// confused into thinking that a dynamic call that occurred once but was |
+/// randomly included in the sample is relevant. |
+num _minCount = 2; |
+ |
+/// Cache mapping from raw stack frames to source mapped stack frames to |
+/// speedup lookup of source map frames when running the profiler. |
+/// The number of source map entries looked up makes caching more important |
+/// in this case than for typical source map use cases. |
+Map<String, String> _frameMappingCache = new Map(); |
+ |
List<List<Object>> getDynamicStats() { |
List<List<Object>> ret = []; |
+ // Process the accumulated method stats. This may be quite slow as processing |
+ // stack traces is expensive. If there are performance blockers, we should |
+ // switch to a sampling approach that caps the number of _callMethodRecords |
+ // and uses random sampling to decide whether to add each additional record |
+ // to the sample. Main change required is that we need to still show the total |
+ // raw number of dynamic calls so that the magnitude of the dynamic call |
+ // performance hit is clear to users. |
+ |
+ if (_callMethodRecords.length > 0) { |
+ // Ratio between total record count and sampled records count. |
+ var recordRatio = _totalCallRecords / _callMethodRecords.length; |
+ for (var record in _callMethodRecords) { |
+ var stackStr = JS('String', '#.stack', record.jsError); |
+ // Truncate the stacktrace as we are only interested in the top 3-4 frames. |
+ var frames = stackStr.split('\n'); |
+ var src = ''; |
+ for (int i = 2; i < frames.length; ++i) { |
+ var frame = frames[i]; |
+ var mappedFrame = _frameMappingCache.putIfAbsent(frame, () { |
+ return stackTraceMapper('\n${frame}'); |
+ }); |
+ if (!mappedFrame.contains( |
+ 'dart:_internal/js_runtime/lib/ddc_runtime/operations.dart')) { |
+ src = mappedFrame; |
+ break; |
+ } |
+ } |
+ var actualTypeName = typeName(record.type); |
+ _callMethodStats |
+ .putIfAbsent("$actualTypeName <$src>", |
+ () => new _MethodStats(actualTypeName, src)) |
+ .count += recordRatio; |
+ } |
+ |
+ for (var k in _callMethodStats.keys.toList()) { |
+ var stats = _callMethodStats[k]; |
+ // filter out all calls that did not occur at least _minCount times in the |
+ // random sample. |
+ var threshold = _minCount * recordRatio; |
+ if (stats.count + 0.001 < threshold) { |
+ _callMethodStats.remove(k); |
+ } |
+ } |
+ } |
+ _callMethodRecords.clear(); |
+ _totalCallRecords = 0; |
var keys = _callMethodStats.keys.toList(); |
keys.sort( |
(a, b) => _callMethodStats[b].count.compareTo(_callMethodStats[a].count)); |
for (var key in keys) { |
var stats = _callMethodStats[key]; |
- ret.add([stats.typeName, stats.frame, stats.count]); |
+ ret.add([stats.typeName, stats.frame, stats.count.round()]); |
} |
return ret; |
@@ -390,30 +466,28 @@ List<List<Object>> getDynamicStats() { |
clearDynamicStats() { |
_callMethodStats.clear(); |
+ _callMethodRecords.clear(); |
} |
bool trackProfile = JS('bool', 'dart.global.trackDdcProfile'); |
_trackCall(obj) { |
if (JS('bool', '!#', trackProfile)) return; |
- |
- var actual = getReifiedType(obj); |
- String stackStr = JS('String', "new Error().stack"); |
- var stack = stackStr.split('\n at '); |
- var src = ''; |
- for (int i = 2; i < stack.length; ++i) { |
- var frame = stack[i]; |
- if (!frame.contains('dart_sdk.js')) { |
- src = frame; |
- break; |
- } |
+ int index = -1; |
+ _totalCallRecords++; |
+ if (_callMethodRecords.length == _callRecordSampleSize) { |
+ // Unfortunately we can't use the excellent Random.nextInt method defined |
+ // by Dart from within this library. |
+ index = JS('int', 'Math.floor(Math.random() * #)', _totalCallRecords); |
+ if (index >= _callMethodRecords.length) return; // don't sample |
+ } |
+ var record = |
+ new _CallMethodRecord(JS('', 'new Error()'), getReifiedType(obj)); |
+ if (index == -1) { |
+ _callMethodRecords.add(record); |
+ } else { |
+ _callMethodRecords[index] = record; |
} |
- |
- var actualTypeName = typeName(actual); |
- _callMethodStats |
- .putIfAbsent( |
- "$actualTypeName <$src>", () => new _MethodStats(actualTypeName, src)) |
- .count++; |
} |
/// Shared code for dsend, dindex, and dsetindex. |