|
| 1 | +/** |
| 2 | + * Copyright 2026 Arm Limited |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import * as vscode from 'vscode'; |
| 18 | +import { URI } from 'vscode-uri'; |
| 19 | +import { parseStringPromise, ParserOptions } from 'xml2js'; |
| 20 | +import { Json } from './model/scvd-base'; |
| 21 | +import { Resolver } from './resolver'; |
| 22 | +import { ScvdComponentViewer } from './model/scvd-component-viewer'; |
| 23 | +import { ScvdBase } from './model/scvd-base'; |
| 24 | +import { StatementEngine } from './statement-engine/statement-engine'; |
| 25 | +import { ScvdEvalContext } from './scvd-eval-context'; |
| 26 | +import { GDBTargetDebugSession, GDBTargetDebugTracker } from '../../debug-session'; |
| 27 | +import { ScvdGuiTree } from './scvd-gui-tree'; |
| 28 | + |
| 29 | + |
| 30 | +const xmlOpts: ParserOptions = { |
| 31 | + explicitArray: false, |
| 32 | + mergeAttrs: true, |
| 33 | + explicitRoot: true, |
| 34 | + trim: true, |
| 35 | + // Add child objects that carry their original tag name via '#name' |
| 36 | + explicitChildren: true, |
| 37 | + preserveChildrenOrder: true |
| 38 | +}; |
| 39 | + |
| 40 | +export class ComponentViewerInstance { |
| 41 | + private _model: ScvdComponentViewer | undefined; |
| 42 | + private _memUsageStart: number = 0; |
| 43 | + private _memUsageLast: number = 0; |
| 44 | + private _timeUsageLast: number = 0; |
| 45 | + private _statementEngine: StatementEngine | undefined; |
| 46 | + private _guiTree: ScvdGuiTree | undefined; |
| 47 | + |
| 48 | + public constructor( |
| 49 | + ) { |
| 50 | + } |
| 51 | + |
| 52 | + private injectLineNumbers(xml: string): string { |
| 53 | + const lines = xml.split(/\r?\n/); |
| 54 | + const result: string[] = []; |
| 55 | + for (const [idx, line] of lines.entries()) { |
| 56 | + result.push(line.replace( |
| 57 | + /<((?!\/|!|\?)([A-Za-z_][A-Za-z0-9._:-]*))/g, |
| 58 | + `<$1 __line="${idx + 1}"` |
| 59 | + )); |
| 60 | + } |
| 61 | + return result.join('\n'); |
| 62 | + } |
| 63 | + |
| 64 | + public getGuiTree(): ScvdGuiTree[] | undefined { |
| 65 | + return this._guiTree?.children; |
| 66 | + } |
| 67 | + |
| 68 | + public getStats(text: string): string { |
| 69 | + const mem = process.memoryUsage(); |
| 70 | + const memCurrent = Math.round(mem.heapUsed / 1024 / 1024); |
| 71 | + const timeCurrent = Date.now(); |
| 72 | + |
| 73 | + if (this._timeUsageLast === 0) { |
| 74 | + this._timeUsageLast = timeCurrent; |
| 75 | + } |
| 76 | + if (this._memUsageStart === 0) { |
| 77 | + this._memUsageStart = memCurrent; |
| 78 | + this._memUsageLast = memCurrent; |
| 79 | + } |
| 80 | + |
| 81 | + const memUsage = memCurrent - this._memUsageLast; |
| 82 | + const timeUsage = timeCurrent - this._timeUsageLast; |
| 83 | + const memIncrease = memCurrent - this._memUsageStart; |
| 84 | + |
| 85 | + this._memUsageLast = memCurrent; |
| 86 | + this._timeUsageLast = timeCurrent; |
| 87 | + |
| 88 | + return `${text}, Time: ${timeUsage} ms, Mem: ${memUsage}, Mem Increase: ${memIncrease} MB, (Total: ${memCurrent} MB)`; |
| 89 | + } |
| 90 | + |
| 91 | + public async readModel(filename: URI, debugSession: GDBTargetDebugSession, debugTracker: GDBTargetDebugTracker): Promise<void> { |
| 92 | + const stats: string[] = []; |
| 93 | + |
| 94 | + stats.push(this.getStats(` Start reading SCVD file ${filename}`)); |
| 95 | + const buf = (await this.readFileToBuffer(filename)).toString('utf-8'); |
| 96 | + stats.push(this.getStats(' read')); |
| 97 | + const bufLineNo = this.injectLineNumbers(buf); |
| 98 | + stats.push(this.getStats(' inject')); |
| 99 | + const xml: Json = await this.parseXml(bufLineNo); |
| 100 | + stats.push(this.getStats(' parse')); |
| 101 | + |
| 102 | + if (xml === undefined) { |
| 103 | + console.error('Failed to parse SCVD XML'); |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + ScvdBase.resetIds(); |
| 108 | + this.model = new ScvdComponentViewer(undefined); |
| 109 | + if (!this.model) { |
| 110 | + console.error('Failed to create SCVD model'); |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + this.model.readXml(xml); |
| 115 | + stats.push(this.getStats(' model.readXml')); |
| 116 | + |
| 117 | + const scvdEvalContext = new ScvdEvalContext(this.model); |
| 118 | + scvdEvalContext.init(debugSession, debugTracker); |
| 119 | + stats.push(this.getStats(' evalContext.init')); |
| 120 | + |
| 121 | + const executionContext = scvdEvalContext.getExecutionContext(); |
| 122 | + this.model.setExecutionContextAll(executionContext); |
| 123 | + stats.push(this.getStats(' model.setExecutionContextAll')); |
| 124 | + |
| 125 | + this.model.configureAll(); |
| 126 | + stats.push(this.getStats(' model.configureAll')); |
| 127 | + this.model.validateAll(true); |
| 128 | + stats.push(this.getStats(' model.validateAll')); |
| 129 | + |
| 130 | + const resolver = new Resolver(this.model); |
| 131 | + resolver.resolve(); |
| 132 | + stats.push(this.getStats(' resolver.resolve')); |
| 133 | + |
| 134 | + await this.model.calculateTypedefs(); |
| 135 | + stats.push(this.getStats(' model.calculateTypedefs')); |
| 136 | + |
| 137 | + this.statementEngine = new StatementEngine(this.model, executionContext); |
| 138 | + this.statementEngine.initialize(); |
| 139 | + stats.push(this.getStats(' statementEngine.initialize')); |
| 140 | + this._guiTree = new ScvdGuiTree(undefined, 'component-viewer-root'); |
| 141 | + |
| 142 | + console.log('ComponentViewerInstance readModel stats:\n' + stats.join('\n ')); |
| 143 | + } |
| 144 | + |
| 145 | + public async update(): Promise<void> { |
| 146 | + const stats: string[] = []; |
| 147 | + stats.push(this.getStats(' start')); |
| 148 | + if (this._statementEngine === undefined || this._guiTree === undefined) { |
| 149 | + return; |
| 150 | + } |
| 151 | + const epoch = this._guiTree.beginUpdate(); |
| 152 | + await this._statementEngine.executeAll(this._guiTree); |
| 153 | + this._guiTree.finalizeUpdate(epoch); |
| 154 | + stats.push(this.getStats('end')); |
| 155 | + console.log('ComponentViewerInstance update stats:\n' + stats.join('\n ')); |
| 156 | + } |
| 157 | + |
| 158 | + private async readFileToBuffer(filePath: URI): Promise<Buffer> { |
| 159 | + try { |
| 160 | + const buffer = await vscode.workspace.fs.readFile(filePath); |
| 161 | + return Buffer.from(buffer); |
| 162 | + } catch (error) { |
| 163 | + console.error('Error reading file:', error); |
| 164 | + throw error; |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + private async parseXml(text: string) { |
| 169 | + try { |
| 170 | + const json = await parseStringPromise(text, xmlOpts); |
| 171 | + //console.log(JSON.stringify(json, null, 2)); |
| 172 | + return json; |
| 173 | + } catch (err) { |
| 174 | + console.error('Error parsing XML:', err); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + public get model(): ScvdComponentViewer | undefined { |
| 179 | + return this._model; |
| 180 | + } |
| 181 | + |
| 182 | + private set model(value: ScvdComponentViewer | undefined) { |
| 183 | + this._model = value; |
| 184 | + } |
| 185 | + |
| 186 | + public get statementEngine(): StatementEngine | undefined { |
| 187 | + return this._statementEngine; |
| 188 | + } |
| 189 | + |
| 190 | + private set statementEngine(value: StatementEngine | undefined) { |
| 191 | + this._statementEngine = value; |
| 192 | + } |
| 193 | + |
| 194 | + public async executeStatements(guiTree: ScvdGuiTree): Promise<void> { |
| 195 | + if (this._statementEngine !== undefined) { |
| 196 | + await this._statementEngine.executeAll(guiTree); |
| 197 | + } |
| 198 | + } |
| 199 | +} |
0 commit comments