1/* 2 * Copyright (C) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { HeapDataInterface } from '../HeapDataInterface'; 17import { EdgeType, NodeType } from './DatabaseStruct'; 18const ROW_TYPE = 'js-memory'; 19export enum FileType { 20 SNAPSHOT, 21 TIMELINE, 22} 23 24export enum ConstructorType { 25 ClassType, 26 InstanceType, 27 FiledType, 28 RetainersType, 29 ComparisonType, 30} 31 32export class ConstructorItem { 33 rowName = ROW_TYPE; 34 fileId = -1; 35 nodeName = ''; 36 edgeName = ''; 37 childCount = 1; // child count 38 distance = -1; 39 shallowSize = -1; 40 retainedSize = -1; 41 retainedPercent = ''; //retained percent 42 shallowPercent = ''; //shallow percent 43 hasNext = true; 44 status = true; 45 isSelected: boolean = false; 46 objectName = ''; 47 expanded: boolean = true; 48 49 edgeCount = 0; 50 edgeType!: EdgeType; 51 type!: ConstructorType; 52 nodeType!: NodeType; 53 nextId: [] = []; 54 id = -1; 55 index = -1; 56 traceNodeId = -1; 57 parent!: ConstructorItem; 58 children: Array<ConstructorItem> = []; 59 retains: Array<ConstructorItem> = []; 60 classChildren: Array<ConstructorItem> = []; 61 62 getChildren(): ConstructorItem[] { 63 if (!this.hasNext) return []; 64 let data = HeapDataInterface.getInstance(); 65 switch (this.type) { 66 case ConstructorType.ClassType: 67 case ConstructorType.InstanceType: 68 case ConstructorType.FiledType: 69 this.children = data.getNextForConstructor(this); 70 break; 71 case ConstructorType.RetainersType: 72 this.children = data.getRetains(this); 73 break; 74 } 75 return this.children; 76 } 77 78 isString(): boolean { 79 return [NodeType.STRING, NodeType.CONCATENATED_STRING, NodeType.SLICED_STRING].includes(this.nodeType); 80 } 81 82 clone(): ConstructorItem { 83 let copyItem = new ConstructorItem(); 84 this.cloneContent(copyItem); 85 return copyItem; 86 } 87 88 protected cloneContent(copyItem: ConstructorItem) { 89 copyItem.fileId = this.fileId; 90 copyItem.distance = this.distance; 91 copyItem.shallowSize = this.shallowSize; 92 copyItem.nodeName = this.nodeName; 93 copyItem.edgeCount = this.edgeCount; 94 copyItem.edgeType = this.edgeType; 95 copyItem.childCount = this.childCount; 96 copyItem.hasNext = this.hasNext; 97 } 98} 99 100export class ConstructorComparison extends ConstructorItem { 101 targetFileId = -1; 102 addedCount = 0; 103 removedCount = 0; 104 deltaCount = 0; 105 106 addedSize = 0; 107 removedSize = 0; 108 deltaSize = 0; 109 110 deletedIdx: Array<number> = []; 111 addedIndx: Array<number> = []; 112 113 isAdd = false; 114 115 getChildren(): ConstructorItem[] { 116 if (this.type !== ConstructorType.ComparisonType) { 117 return super.getChildren(); 118 } 119 if (!this.hasNext) return []; 120 let data = HeapDataInterface.getInstance(); 121 if (this.type == ConstructorType.ComparisonType) { 122 this.children = data.getNextForComparison(this); 123 } 124 return this.children; 125 } 126 127 clone(): ConstructorComparison { 128 let copyItem = new ConstructorComparison(); 129 this.cloneContent(copyItem); 130 return copyItem; 131 } 132} 133export class AllocationFunction { 134 fileId = -1; 135 functionIndex = -1; 136 parentsId: Array<number>; 137 parents: Array<AllocationFunction>; 138 combineId: Set<number>; 139 status = true; 140 141 id: number; 142 name: string; 143 scriptName: string; 144 scriptId: number; 145 line: number; 146 column: number; 147 count: number; 148 size: number; 149 liveCount: number; 150 liveSize: number; 151 hasParent: boolean; 152 153 constructor( 154 nodeId: number, 155 functionName: string, 156 scriptName: string, 157 scriptId: number, 158 line: number, 159 column: number, 160 count: number, 161 size: number, 162 liveCount: number, 163 liveSize: number, 164 hasParent: boolean 165 ) { 166 this.combineId = new Set<number>(); 167 this.parentsId = new Array<number>(); 168 this.parents = new Array<AllocationFunction>(); 169 this.id = nodeId; 170 this.name = functionName; 171 this.scriptName = scriptName; 172 this.scriptId = scriptId; 173 this.line = line; 174 this.column = column; 175 this.count = count; 176 this.size = size; 177 this.liveCount = liveCount; 178 this.liveSize = liveSize; 179 this.hasParent = hasParent; 180 } 181 182 /** 183 * bottom up next level is parent 184 * return Parents 185 */ 186 getChildren(): AllocationFunction[] { 187 if (!this.hasParent) return []; 188 let data = HeapDataInterface.getInstance(); 189 //bottom up next level is parent 190 data.getParentFunction(this); 191 return this.parents; 192 } 193 194 clone(): AllocationFunction { 195 let cloneItem = new AllocationFunction( 196 this.id, 197 this.name, 198 this.scriptName, 199 this.scriptId, 200 this.line, 201 this.column, 202 this.count, 203 this.size, 204 this.liveCount, 205 this.liveSize, 206 this.hasParent 207 ); 208 cloneItem.parentsId = this.parentsId; 209 return cloneItem; 210 } 211} 212 213export class FileInfo { 214 id: number = -1; 215 name: string = ''; 216 type!: FileType; 217 startTs: number = 0; 218 endTs: number = 0; 219 pid: number = 0; 220 size: number = 0; 221} 222