| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2016, 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 part of repositories; |
| 6 |
| 7 String _tagToString(M.SampleProfileTag tag) { |
| 8 switch (tag) { |
| 9 case M.SampleProfileTag.userVM: return 'UserVM'; |
| 10 case M.SampleProfileTag.userOnly: return 'UserOnly'; |
| 11 case M.SampleProfileTag.vmUser: return 'VMUser'; |
| 12 case M.SampleProfileTag.vmOnly: return 'VMOnly'; |
| 13 case M.SampleProfileTag.none: return 'None'; |
| 14 } |
| 15 throw new Exception('Unknown SampleProfileTag: $tag'); |
| 16 } |
| 17 |
| 18 class SampleProfileLoadingProgressEvent |
| 19 implements M.SampleProfileLoadingProgressEvent { |
| 20 final SampleProfileLoadingProgress progress; |
| 21 SampleProfileLoadingProgressEvent(this.progress); |
| 22 } |
| 23 |
| 24 class SampleProfileLoadingProgress extends M.SampleProfileLoadingProgress { |
| 25 StreamController<SampleProfileLoadingProgressEvent> _onProgress = |
| 26 new StreamController<SampleProfileLoadingProgressEvent>.broadcast(); |
| 27 Stream<SampleProfileLoadingProgressEvent> get onProgress => |
| 28 _onProgress.stream; |
| 29 |
| 30 final S.Isolate isolate; |
| 31 final S.Class cls; |
| 32 final M.SampleProfileTag tag; |
| 33 final bool clear; |
| 34 |
| 35 M.SampleProfileLoadingStatus _status = M.SampleProfileLoadingStatus.fetching; |
| 36 double _progress = 0.0; |
| 37 final Stopwatch _fetchingTime = new Stopwatch(); |
| 38 final Stopwatch _loadingTime = new Stopwatch(); |
| 39 CpuProfile _profile; |
| 40 |
| 41 M.SampleProfileLoadingStatus get status => _status; |
| 42 double get progress => _progress; |
| 43 Duration get fetchingTime => _fetchingTime.elapsed; |
| 44 Duration get loadingTime => _loadingTime.elapsed; |
| 45 CpuProfile get profile => _profile; |
| 46 |
| 47 SampleProfileLoadingProgress(this.isolate, this.tag, |
| 48 this.clear, {this.cls}) { |
| 49 _run(); |
| 50 } |
| 51 |
| 52 Future _run() async { |
| 53 _fetchingTime.start(); |
| 54 try { |
| 55 if (clear) { |
| 56 await isolate.invokeRpc('_clearCpuProfile', { }); |
| 57 } |
| 58 |
| 59 final response = cls != null |
| 60 ? await cls.getAllocationSamples(_tagToString(tag)) |
| 61 : await isolate.invokeRpc('_getCpuProfile', |
| 62 { 'tags': _tagToString(tag) }); |
| 63 |
| 64 _fetchingTime.stop(); |
| 65 _loadingTime.start(); |
| 66 _status = M.SampleProfileLoadingStatus.loading; |
| 67 _triggerOnProgress(); |
| 68 |
| 69 CpuProfile profile = new CpuProfile(); |
| 70 |
| 71 Stream<double> progress = profile.loadProgress(isolate, response); |
| 72 progress.listen((value) { _progress = value; _triggerOnProgress(); }); |
| 73 |
| 74 await progress.drain(); |
| 75 |
| 76 profile.buildFunctionCallerAndCallees(); |
| 77 _profile = profile; |
| 78 |
| 79 _loadingTime.stop(); |
| 80 _status = M.SampleProfileLoadingStatus.loaded; |
| 81 _triggerOnProgress(); |
| 82 } catch (e) { |
| 83 if (e is S.ServerRpcException) { |
| 84 if (e.code == S.ServerRpcException.kFeatureDisabled) { |
| 85 _status = M.SampleProfileLoadingStatus.disabled; |
| 86 _triggerOnProgress(); |
| 87 } |
| 88 } |
| 89 rethrow; |
| 90 } finally { |
| 91 _onProgress.close(); |
| 92 } |
| 93 } |
| 94 |
| 95 void _triggerOnProgress() { |
| 96 _onProgress.add(new SampleProfileLoadingProgressEvent(this)); |
| 97 } |
| 98 |
| 99 void reuse() { |
| 100 _onProgress = |
| 101 new StreamController<SampleProfileLoadingProgressEvent>.broadcast(); |
| 102 (() async { |
| 103 _triggerOnProgress(); |
| 104 _onProgress.close(); |
| 105 }()); |
| 106 } |
| 107 } |
| 108 |
| 109 class IsolateSampleProfileRepository |
| 110 implements M.IsolateSampleProfileRepository { |
| 111 SampleProfileLoadingProgress _last; |
| 112 |
| 113 Stream<SampleProfileLoadingProgressEvent> get(M.IsolateRef i, |
| 114 M.SampleProfileTag t, {bool clear: false, bool forceFetch: false}) { |
| 115 assert(clear != null); |
| 116 assert(forceFetch != null); |
| 117 S.Isolate isolate = i as S.Isolate; |
| 118 assert(isolate != null); |
| 119 if (_last != null && !clear && !forceFetch && _last.isolate == isolate) { |
| 120 _last.reuse(); |
| 121 } else { |
| 122 _last = new SampleProfileLoadingProgress(isolate, t, clear); |
| 123 } |
| 124 return _last.onProgress; |
| 125 } |
| 126 } |
| 127 |
| 128 class ClassSampleProfileRepository |
| 129 implements M.ClassSampleProfileRepository { |
| 130 Stream<SampleProfileLoadingProgressEvent> get(M.ClassRef c, |
| 131 M.SampleProfileTag t, {bool clear: false}) { |
| 132 S.Class cls = c as S.Class; |
| 133 assert(cls != null); |
| 134 return new SampleProfileLoadingProgress(cls.isolate, t, |
| 135 clear, cls: cls).onProgress; |
| 136 } |
| 137 } |
| OLD | NEW |