| 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 import 'dart:collection'; | |
| 6 | |
| 7 import 'package:analysis_server/src/analysis_server.dart'; | |
| 8 import 'package:analyzer/dart/ast/ast.dart'; | |
| 9 import 'package:analyzer/dart/ast/visitor.dart'; | |
| 10 import 'package:analyzer/dart/element/element.dart'; | |
| 11 import 'package:analyzer/dart/element/visitor.dart'; | |
| 12 import 'package:analyzer/src/context/cache.dart'; | |
| 13 import 'package:analyzer/src/context/context.dart' show AnalysisContextImpl; | |
| 14 import 'package:analyzer/src/dart/element/element.dart'; | |
| 15 import 'package:analyzer/src/generated/engine.dart'; | |
| 16 import 'package:analyzer/src/generated/sdk.dart'; | |
| 17 import 'package:analyzer/task/dart.dart'; | |
| 18 import 'package:analyzer/task/model.dart'; | |
| 19 | |
| 20 /** | |
| 21 * A visitor that will count the number of instances of each type of AST node. | |
| 22 */ | |
| 23 class AstNodeCounter extends UnifyingAstVisitor<Null> { | |
| 24 /** | |
| 25 * A table mapping the types of the AST nodes to the number of instances | |
| 26 * visited. | |
| 27 */ | |
| 28 final Map<Type, int> nodeCounts; | |
| 29 | |
| 30 /** | |
| 31 * Initialize a newly created counter to increment the counts in the given map | |
| 32 * of [nodeCounts]. | |
| 33 */ | |
| 34 AstNodeCounter(this.nodeCounts); | |
| 35 | |
| 36 @override | |
| 37 visitNode(AstNode node) { | |
| 38 Type type = node.runtimeType; | |
| 39 int count = nodeCounts[type] ?? 0; | |
| 40 nodeCounts[type] = count + 1; | |
| 41 super.visitNode(node); | |
| 42 } | |
| 43 } | |
| 44 | |
| 45 /** | |
| 46 * A visitor that will count the number of instances of each type of element. | |
| 47 */ | |
| 48 class ElementCounter extends GeneralizingElementVisitor<Null> { | |
| 49 /** | |
| 50 * A table mapping the types of the elements to the number of instances | |
| 51 * visited. | |
| 52 */ | |
| 53 final Map<Type, int> elementCounts; | |
| 54 | |
| 55 /** | |
| 56 * A table mapping the types of the AST nodes to the number of instances | |
| 57 * visited. | |
| 58 */ | |
| 59 final Map<Type, int> nodeCounts; | |
| 60 | |
| 61 /** | |
| 62 * Initialize a newly created counter to increment the counts in the given map | |
| 63 * of [elementCounts]. | |
| 64 */ | |
| 65 ElementCounter(this.elementCounts, this.nodeCounts); | |
| 66 | |
| 67 @override | |
| 68 visitConstructorElement(ConstructorElement element) { | |
| 69 if (element is ConstructorElementImpl) { | |
| 70 List<ConstructorInitializer> initializers = element.constantInitializers; | |
| 71 if (initializers != null) { | |
| 72 initializers.forEach((ConstructorInitializer initializer) { | |
| 73 _countNodes(initializer); | |
| 74 }); | |
| 75 } | |
| 76 } | |
| 77 visitElement(element); | |
| 78 } | |
| 79 | |
| 80 @override | |
| 81 visitElement(Element element) { | |
| 82 Type type = element.runtimeType; | |
| 83 int count = elementCounts[type] ?? 0; | |
| 84 elementCounts[type] = count + 1; | |
| 85 element.metadata.forEach((ElementAnnotation annotation) { | |
| 86 if (annotation is ElementAnnotationImpl) { | |
| 87 _countNodes(annotation.annotationAst); | |
| 88 } | |
| 89 }); | |
| 90 super.visitElement(element); | |
| 91 } | |
| 92 | |
| 93 visitFieldElement(FieldElement element) { | |
| 94 if (element is ConstVariableElement) { | |
| 95 _countInitializer(element as ConstVariableElement); | |
| 96 } | |
| 97 visitElement(element); | |
| 98 } | |
| 99 | |
| 100 visitLocalVariableElement(LocalVariableElement element) { | |
| 101 if (element is ConstVariableElement) { | |
| 102 _countInitializer(element as ConstVariableElement); | |
| 103 } | |
| 104 visitElement(element); | |
| 105 } | |
| 106 | |
| 107 visitParameterElement(ParameterElement element) { | |
| 108 if (element is ConstVariableElement) { | |
| 109 _countInitializer(element as ConstVariableElement); | |
| 110 } | |
| 111 visitElement(element); | |
| 112 } | |
| 113 | |
| 114 visitTopLevelVariableElement(TopLevelVariableElement element) { | |
| 115 if (element is ConstVariableElement) { | |
| 116 _countInitializer(element as ConstVariableElement); | |
| 117 } | |
| 118 visitElement(element); | |
| 119 } | |
| 120 | |
| 121 void _countInitializer(ConstVariableElement element) { | |
| 122 _countNodes(element.constantInitializer); | |
| 123 } | |
| 124 | |
| 125 void _countNodes(AstNode node) { | |
| 126 if (node != null) { | |
| 127 node.accept(new AstNodeCounter(nodeCounts)); | |
| 128 } | |
| 129 } | |
| 130 } | |
| 131 | |
| 132 /** | |
| 133 * A set used when the number of instances of some type is too large to be kept. | |
| 134 */ | |
| 135 class InfiniteSet implements Set { | |
| 136 /** | |
| 137 * The unique instance of this class. | |
| 138 */ | |
| 139 static final InfiniteSet instance = new InfiniteSet(); | |
| 140 | |
| 141 @override | |
| 142 int get length => -1; | |
| 143 | |
| 144 @override | |
| 145 dynamic noSuchMethod(Invocation invocation) { | |
| 146 throw new UnsupportedError('Do not use instances of InfiniteSet'); | |
| 147 } | |
| 148 } | |
| 149 | |
| 150 /** | |
| 151 * Computes memory usage data by traversing the data structures reachable from | |
| 152 * an analysis server. | |
| 153 */ | |
| 154 class MemoryUseData { | |
| 155 /** | |
| 156 * The maximum size of an instance set. | |
| 157 */ | |
| 158 static const int maxInstanceSetSize = 1000000; | |
| 159 | |
| 160 /** | |
| 161 * A table mapping classes to instances of the class. | |
| 162 */ | |
| 163 Map<Type, Set> instances = new HashMap<Type, Set>(); | |
| 164 | |
| 165 /** | |
| 166 * A table mapping classes to the classes of objects from which they were | |
| 167 * reached. | |
| 168 */ | |
| 169 Map<Type, Set<Type>> ownerMap = new HashMap<Type, Set<Type>>(); | |
| 170 | |
| 171 /** | |
| 172 * A set of all the library specific units, using equality rather than | |
| 173 * identity in order to determine whether re-using equal instances would save | |
| 174 * significant space. | |
| 175 */ | |
| 176 Set<LibrarySpecificUnit> uniqueLSUs = new HashSet<LibrarySpecificUnit>(); | |
| 177 | |
| 178 /** | |
| 179 * A set of all the targeted results, using equality rather than identity in | |
| 180 * order to determine whether re-using equal instances would save significant | |
| 181 * space. | |
| 182 */ | |
| 183 Set<TargetedResult> uniqueTargetedResults = new HashSet<TargetedResult>(); | |
| 184 | |
| 185 /** | |
| 186 * A set containing all of the analysis targets for which the key in the | |
| 187 * cache partition is not the same instance as the target stored in the entry. | |
| 188 */ | |
| 189 Set<AnalysisTarget> mismatchedTargets = new HashSet<AnalysisTarget>(); | |
| 190 | |
| 191 /** | |
| 192 * A table mapping the types of AST nodes to the number of instances being | |
| 193 * held directly (as values in the cache). | |
| 194 */ | |
| 195 Map<Type, int> directNodeCounts = new HashMap<Type, int>(); | |
| 196 | |
| 197 /** | |
| 198 * A table mapping the types of AST nodes to the number of instances being | |
| 199 * held indirectly (such as nodes reachable from element models). | |
| 200 */ | |
| 201 Map<Type, int> indirectNodeCounts = new HashMap<Type, int>(); | |
| 202 | |
| 203 /** | |
| 204 * A table mapping the types of the elements to the number of instances being | |
| 205 * held directly (as values in the cache). | |
| 206 */ | |
| 207 final Map<Type, int> elementCounts = new HashMap<Type, int>(); | |
| 208 | |
| 209 /** | |
| 210 * Initialize a newly created instance. | |
| 211 */ | |
| 212 MemoryUseData(); | |
| 213 | |
| 214 /** | |
| 215 * Traverse an analysis [server] to compute memory usage data. | |
| 216 */ | |
| 217 void processAnalysisServer(AnalysisServer server) { | |
| 218 _recordInstance(server, null); | |
| 219 Iterable<AnalysisContext> contexts = server.analysisContexts; | |
| 220 for (AnalysisContextImpl context in contexts) { | |
| 221 _processAnalysisContext(context, server); | |
| 222 } | |
| 223 DartSdkManager manager = server.sdkManager; | |
| 224 List<SdkDescription> descriptors = manager.sdkDescriptors; | |
| 225 for (SdkDescription descriptor in descriptors) { | |
| 226 DartSdk sdk = manager.getSdk(descriptor, () => null); | |
| 227 if (sdk != null) { | |
| 228 _processAnalysisContext(sdk.context, manager); | |
| 229 } | |
| 230 } | |
| 231 } | |
| 232 | |
| 233 void _processAnalysisContext(AnalysisContextImpl context, Object owner) { | |
| 234 _recordInstance(context, owner); | |
| 235 _recordInstance(context.analysisCache, context); | |
| 236 CachePartition partition = context.privateAnalysisCachePartition; | |
| 237 Map<AnalysisTarget, CacheEntry> map = partition.entryMap; | |
| 238 map.forEach((AnalysisTarget target, CacheEntry entry) { | |
| 239 _processAnalysisTarget(target, partition); | |
| 240 _processCacheEntry(entry, partition); | |
| 241 if (!identical(entry.target, target)) { | |
| 242 mismatchedTargets.add(target); | |
| 243 } | |
| 244 }); | |
| 245 } | |
| 246 | |
| 247 void _processAnalysisTarget(AnalysisTarget target, Object owner) { | |
| 248 _recordInstance(target, owner); | |
| 249 } | |
| 250 | |
| 251 void _processCacheEntry(CacheEntry entry, Object owner) { | |
| 252 _recordInstance(entry, owner); | |
| 253 List<ResultDescriptor> descriptors = entry.nonInvalidResults; | |
| 254 for (ResultDescriptor descriptor in descriptors) { | |
| 255 _recordInstance(descriptor, entry); | |
| 256 _processResultData(entry.getResultDataOrNull(descriptor), entry); | |
| 257 } | |
| 258 } | |
| 259 | |
| 260 void _processResultData(ResultData resultData, Object owner) { | |
| 261 _recordInstance(resultData, owner); | |
| 262 if (resultData != null) { | |
| 263 _recordInstance(resultData.state, resultData); | |
| 264 _recordInstance(resultData.value, resultData, | |
| 265 onFirstOccurrence: (Object object) { | |
| 266 if (object is AstNode) { | |
| 267 object.accept(new AstNodeCounter(directNodeCounts)); | |
| 268 } else if (object is Element) { | |
| 269 object.accept(new ElementCounter(elementCounts, indirectNodeCounts)); | |
| 270 } | |
| 271 }); | |
| 272 resultData.dependedOnResults.forEach((TargetedResult result) => | |
| 273 _processTargetedResult(result, resultData)); | |
| 274 resultData.dependentResults.forEach((TargetedResult result) => | |
| 275 _processTargetedResult(result, resultData)); | |
| 276 } | |
| 277 } | |
| 278 | |
| 279 void _processTargetedResult(TargetedResult result, Object owner) { | |
| 280 _recordInstance(result, owner); | |
| 281 uniqueTargetedResults.add(result); | |
| 282 _recordInstance(result.target, result); | |
| 283 _recordInstance(result.result, result); | |
| 284 } | |
| 285 | |
| 286 /** | |
| 287 * Record the given [instance] that was found. If this is the first time that | |
| 288 * the instance has been found, execute the [onFirstOccurrence] function. | |
| 289 * | |
| 290 * Note that instances will not be recorded if there are more than | |
| 291 * [maxInstanceSetSize] instances of the same type, and that the | |
| 292 * [onFirstOccurrence] function will not be executed if the instance is not | |
| 293 * recorded. | |
| 294 */ | |
| 295 void _recordInstance(Object instance, Object owner, | |
| 296 {void onFirstOccurrence(Object object)}) { | |
| 297 Type type = instance.runtimeType; | |
| 298 Set instanceSet = instances.putIfAbsent(type, () => new HashSet.identity()); | |
| 299 if (instanceSet != InfiniteSet.instance) { | |
| 300 if (instanceSet.add(instance) && onFirstOccurrence != null) { | |
| 301 onFirstOccurrence(instance); | |
| 302 } | |
| 303 if (instanceSet.length >= maxInstanceSetSize) { | |
| 304 instances[type] = InfiniteSet.instance; | |
| 305 } | |
| 306 } | |
| 307 ownerMap | |
| 308 .putIfAbsent(instance.runtimeType, () => new HashSet<Type>()) | |
| 309 .add(owner.runtimeType); | |
| 310 if (instance is LibrarySpecificUnit) { | |
| 311 uniqueLSUs.add(instance); | |
| 312 } | |
| 313 } | |
| 314 } | |
| OLD | NEW |