1// Copyright 2020 the V8 project authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5'use strict'; 6 7export class Isolate { 8 constructor(address) { 9 this.address = address; 10 this.start = null; 11 this.end = null; 12 this.peakUsageTime = null; 13 // Maps zone name to per-zone statistics. 14 this.zones = new Map(); 15 // Zone names sorted by memory usage (from low to high). 16 this.sorted_zone_names = []; 17 // Maps time to total and per-zone memory usages. 18 this.samples = new Map(); 19 20 this.peakAllocatedMemory = 0; 21 22 // Maps zone name to their max memory consumption. 23 this.zonePeakMemory = Object.create(null); 24 // Peak memory consumed by a single zone. 25 this.singleZonePeakMemory = 0; 26 } 27 28 finalize() { 29 this.samples.forEach(sample => this.finalizeSample(sample)); 30 this.start = Math.floor(this.start); 31 this.end = Math.ceil(this.end); 32 this.sortZoneNamesByPeakMemory(); 33 } 34 35 getLabel() { 36 let label = `${this.address}: `; 37 label += ` peak=${formatBytes(this.peakAllocatedMemory)}`; 38 label += ` time=[${this.start}, ${this.end}] ms`; 39 return label; 40 } 41 42 finalizeSample(sample) { 43 const time = sample.time; 44 if (this.start == null) { 45 this.start = time; 46 this.end = time; 47 } else { 48 this.end = Math.max(this.end, time); 49 } 50 51 const allocated = sample.allocated; 52 if (allocated > this.peakAllocatedMemory) { 53 this.peakUsageTime = time; 54 this.peakAllocatedMemory = allocated; 55 } 56 57 const sample_zones = sample.zones; 58 if (sample_zones !== undefined) { 59 sample.zones.forEach((zone_sample, zone_name) => { 60 let zone_stats = this.zones.get(zone_name); 61 if (zone_stats === undefined) { 62 zone_stats = {max_allocated: 0, max_used: 0}; 63 this.zones.set(zone_name, zone_stats); 64 } 65 66 zone_stats.max_allocated = 67 Math.max(zone_stats.max_allocated, zone_sample.allocated); 68 zone_stats.max_used = Math.max(zone_stats.max_used, zone_sample.used); 69 }); 70 } 71 } 72 73 sortZoneNamesByPeakMemory() { 74 let entries = [...this.zones.keys()]; 75 entries.sort((a, b) => 76 this.zones.get(a).max_allocated - this.zones.get(b).max_allocated 77 ); 78 this.sorted_zone_names = entries; 79 80 let max = 0; 81 for (let [key, value] of entries) { 82 this.zonePeakMemory[key] = value; 83 max = Math.max(max, value); 84 } 85 this.singleZonePeakMemory = max; 86 } 87 88 getInstanceTypePeakMemory(type) { 89 if (!(type in this.zonePeakMemory)) return 0; 90 return this.zonePeakMemory[type]; 91 } 92} 93