HeapProfilerModel.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. export default class HeapProfilerModel extends SDK.SDKModel{constructor(target){super(target);target.registerHeapProfilerDispatcher(new HeapProfilerDispatcher(this));this._enabled=false;this._heapProfilerAgent=target.heapProfilerAgent();this._memoryAgent=target.memoryAgent();this._runtimeModel=(target.model(SDK.RuntimeModel));this._samplingProfilerDepth=0;}
  2. debuggerModel(){return this._runtimeModel.debuggerModel();}
  3. runtimeModel(){return this._runtimeModel;}
  4. enable(){if(this._enabled){return;}
  5. this._enabled=true;this._heapProfilerAgent.enable();}
  6. startSampling(samplingRateInBytes){if(this._samplingProfilerDepth++){return;}
  7. const defaultSamplingIntervalInBytes=16384;this._heapProfilerAgent.startSampling(samplingRateInBytes||defaultSamplingIntervalInBytes);}
  8. stopSampling(){if(!this._samplingProfilerDepth){throw new Error('Sampling profiler is not running.');}
  9. if(--this._samplingProfilerDepth){return this.getSamplingProfile();}
  10. return this._heapProfilerAgent.stopSampling();}
  11. getSamplingProfile(){return this._heapProfilerAgent.getSamplingProfile();}
  12. startNativeSampling(){const defaultSamplingIntervalInBytes=65536;this._memoryAgent.startSampling(defaultSamplingIntervalInBytes);}
  13. async stopNativeSampling(){const rawProfile=(await this._memoryAgent.getSamplingProfile());this._memoryAgent.stopSampling();return this._convertNativeProfile(rawProfile);}
  14. async takeNativeSnapshot(){const rawProfile=(await this._memoryAgent.getAllTimeSamplingProfile());return this._convertNativeProfile(rawProfile);}
  15. async takeNativeBrowserSnapshot(){const rawProfile=(await this._memoryAgent.getBrowserSamplingProfile());return this._convertNativeProfile(rawProfile);}
  16. _convertNativeProfile(rawProfile){const head=({children:new Map(),selfSize:0,callFrame:{functionName:'(root)',url:''}});for(const sample of rawProfile.samples){const node=sample.stack.reverse().reduce((node,name)=>{let child=node.children.get(name);if(child){return child;}
  17. const namespace=/^([^:]*)::/.exec(name);child={children:new Map(),callFrame:{functionName:name,url:namespace&&namespace[1]||''},selfSize:0};node.children.set(name,child);return child;},head);node.selfSize+=sample.total;}
  18. function convertChildren(node){node.children=Array.from(node.children.values());node.children.forEach(convertChildren);}
  19. convertChildren(head);return new NativeHeapProfile(head,rawProfile.modules);}
  20. collectGarbage(){return this._heapProfilerAgent.collectGarbage();}
  21. snapshotObjectIdForObjectId(objectId){return this._heapProfilerAgent.getHeapObjectId(objectId);}
  22. async objectForSnapshotObjectId(snapshotObjectId,objectGroupName){const result=await this._heapProfilerAgent.getObjectByHeapObjectId(snapshotObjectId,objectGroupName);return result&&result.type&&this._runtimeModel.createRemoteObject(result)||null;}
  23. addInspectedHeapObject(snapshotObjectId){return this._heapProfilerAgent.addInspectedHeapObject(snapshotObjectId);}
  24. takeHeapSnapshot(reportProgress){return this._heapProfilerAgent.takeHeapSnapshot(reportProgress);}
  25. startTrackingHeapObjects(recordAllocationStacks){return this._heapProfilerAgent.startTrackingHeapObjects(recordAllocationStacks);}
  26. stopTrackingHeapObjects(reportProgress){return this._heapProfilerAgent.stopTrackingHeapObjects(reportProgress);}
  27. heapStatsUpdate(samples){this.dispatchEventToListeners(Events.HeapStatsUpdate,samples);}
  28. lastSeenObjectId(lastSeenObjectId,timestamp){this.dispatchEventToListeners(Events.LastSeenObjectId,{lastSeenObjectId:lastSeenObjectId,timestamp:timestamp});}
  29. addHeapSnapshotChunk(chunk){this.dispatchEventToListeners(Events.AddHeapSnapshotChunk,chunk);}
  30. reportHeapSnapshotProgress(done,total,finished){this.dispatchEventToListeners(Events.ReportHeapSnapshotProgress,{done:done,total:total,finished:finished});}
  31. resetProfiles(){this.dispatchEventToListeners(Events.ResetProfiles,this);}}
  32. export const Events={HeapStatsUpdate:Symbol('HeapStatsUpdate'),LastSeenObjectId:Symbol('LastSeenObjectId'),AddHeapSnapshotChunk:Symbol('AddHeapSnapshotChunk'),ReportHeapSnapshotProgress:Symbol('ReportHeapSnapshotProgress'),ResetProfiles:Symbol('ResetProfiles')};class NativeHeapProfile{constructor(head,modules){this.head=head;this.modules=modules;}}
  33. class HeapProfilerDispatcher{constructor(model){this._heapProfilerModel=model;}
  34. heapStatsUpdate(samples){this._heapProfilerModel.heapStatsUpdate(samples);}
  35. lastSeenObjectId(lastSeenObjectId,timestamp){this._heapProfilerModel.lastSeenObjectId(lastSeenObjectId,timestamp);}
  36. addHeapSnapshotChunk(chunk){this._heapProfilerModel.addHeapSnapshotChunk(chunk);}
  37. reportHeapSnapshotProgress(done,total,finished){this._heapProfilerModel.reportHeapSnapshotProgress(done,total,finished);}
  38. resetProfiles(){this._heapProfilerModel.resetProfiles();}}
  39. self.SDK=self.SDK||{};SDK=SDK||{};SDK.HeapProfilerModel=HeapProfilerModel;SDK.HeapProfilerModel.Events=Events;SDK.SDKModel.register(SDK.HeapProfilerModel,SDK.Target.Capability.JS,false);