From 6022fde2d52b6a7576cbff049c50d2ef56535217 Mon Sep 17 00:00:00 2001 From: twx1232375 Date: Mon, 16 Jun 2025 12:15:11 +0300 Subject: [PATCH 1/2] Profiler disable flag --- ui2abc/fast-arktsc/src/main.ts | 4 +- ui2abc/libarkts/src-host/es2panda.ts | 35 +++++++-------- .../libarkts/src/arkts-api/peers/AstNode.ts | 2 +- .../libarkts/src/arkts-api/static/global.ts | 3 -- .../libarkts/src/arkts-api/static/profiler.ts | 45 ++++++++++++------- .../src/arkts-api/utilities/public.ts | 3 +- ui2abc/libarkts/src/arkts-api/visitor.ts | 2 +- ui2abc/libarkts/src/index.ts | 2 + ui2abc/tests-memo/test/testUtils.ts | 12 ----- 9 files changed, 55 insertions(+), 53 deletions(-) diff --git a/ui2abc/fast-arktsc/src/main.ts b/ui2abc/fast-arktsc/src/main.ts index c56e7d7d3..53ff70b5d 100644 --- a/ui2abc/fast-arktsc/src/main.ts +++ b/ui2abc/fast-arktsc/src/main.ts @@ -38,6 +38,7 @@ export const options = program .option('--aot-libs ', 'Comma-separated AOT libraries to include') .option('--only-aot ', 'AOT an .abc taking --aot-libs into account') .option('--aot-target ', 'Compilation target for AOT') + .option('--enable-report', 'Enable profiler report') .parse() .opts() @@ -91,9 +92,10 @@ function produceNinjafile( let basename = path.basename(compiler) let linker = compiler.replace(basename, 'arklink') const stages = intermediateOutDirs.length + const enableReport = options.enableReport ? "--enable-report" : "" const buildCommand = (stage: number, _in: string, _out: string) => { - return `${tools_prefix}${compiler} --ets-module --arktsconfig ${path.resolve(config)} --output ${_out} ${options.restartStages ? `--restart-stages` : ``} ${stages > 1 ? `--stage ${stage}` : ``} ${_in}` + return `${tools_prefix}${compiler} --ets-module --arktsconfig ${path.resolve(config)} --output ${_out} ${options.restartStages ? `--restart-stages` : ``} ${stages > 1 ? `--stage ${stage}` : ``} ${enableReport} ${_in}` } let compilerPrefix = [...Array(stages).keys()].map((i) => ` diff --git a/ui2abc/libarkts/src-host/es2panda.ts b/ui2abc/libarkts/src-host/es2panda.ts index 8f314a43e..c9bfa871f 100644 --- a/ui2abc/libarkts/src-host/es2panda.ts +++ b/ui2abc/libarkts/src-host/es2panda.ts @@ -15,7 +15,7 @@ import * as fs from "node:fs" import * as path from "node:path" -import { checkSDK, arktsGlobal as global, metaDatabase, runTransformer, CompilationOptions, findStdlib } from "@koalaui/libarkts" +import { checkSDK, arktsGlobal as global, metaDatabase, runTransformer, CompilationOptions, findStdlib, Profiler, setProfiler, profiler } from "@koalaui/libarkts" import { CheckedBackFilter, PluginContextImpl } from "@koalaui/libarkts" import { Command } from "commander" import { filterSource, isNumber, throwError } from "@koalaui/libarkts" @@ -32,6 +32,7 @@ function parseCommandLineArgs() { .option('--dump-plugin-ast', 'Dump ast before and after each plugin') .option('--restart-stages', 'Restart the compiler to proceed to next stage') .option('--stage ', 'Stage of multistage compilation (from 0 to number of plugins in arktsconfig + 1)') + .option('--enable-report', 'Enable profiler report') .parse(process.argv) const cliOptions = commander.opts() @@ -56,8 +57,9 @@ function parseCommandLineArgs() { const dumpAst = cliOptions.dumpPluginAst ?? false const restartStages = cliOptions.restartStages ?? false const stage = cliOptions.stage ?? 0 + const enableReport = cliOptions.enableReport ?? false - return { files, configPath, outputs, dumpAst, restartStages, stage } + return { files, configPath, outputs, dumpAst, restartStages, stage, enableReport } } function insertPlugin( @@ -80,21 +82,18 @@ function insertPlugin( console.log(filterSource(script.dumpSrc())) } - global.profiler.curPlugin = pluginName - global.profiler.transformStarted() runTransformer(global.compilerContext.program, state, restart, transform, pluginContext, { onProgramTransformStart(options: CompilationOptions) { - if (!options.isMainProgram) global.profiler.transformDepStarted() + profiler()?.transformStarted(state, pluginName) + if (!options.isMainProgram) profiler()?.transformDepStarted() }, onProgramTransformEnd(options: CompilationOptions) { - if (!options.isMainProgram) global.profiler.transformDepEnded(state, pluginName) + profiler()?.transformEnded(state, pluginName) + if (!options.isMainProgram) profiler()?.transformDepEnded(state, pluginName) } }) - global.profiler.transformEnded(state, pluginName) - global.profiler.curPlugin = "" - if (dumpAst) { console.log(`AFTER ${stateName(state)}:`) if (restart) { @@ -152,7 +151,7 @@ function invokeWithPlugins( pluginContext: PluginContextImpl ): void { const stdlib = findStdlib() - global.profiler.compilationStarted(filePath) + profiler()?.compilationStarted(filePath) global.filePath = filePath const compilerConfig = Config.create([ @@ -201,7 +200,7 @@ function invokeWithPlugins( const options = Options.createOptions(new Config(global.config)) global.arktsconfig = options.getArkTsConfig() proceedToState(state) - global.profiler.restarted(after - before) + profiler()?.restarted(after - before) } else { pluginsApplied++ } @@ -216,7 +215,6 @@ function invokeWithPlugins( dumpArkTsConfigInfo(global.arktsconfig) dumpProgramInfo(compilerContext.program) - global.profiler.curContextState = Es2pandaContextState.ES2PANDA_STATE_PARSED pluginsByState.get(Es2pandaContextState.ES2PANDA_STATE_PARSED)?.forEach(plugin => { if (!terminate) { insertPlugin(plugin, Es2pandaContextState.ES2PANDA_STATE_PARSED, pluginNames[pluginsApplied], dumpAst, restart, pluginContext) @@ -226,7 +224,6 @@ function invokeWithPlugins( if (!terminate) { proceedToState(Es2pandaContextState.ES2PANDA_STATE_BOUND) - global.profiler.curContextState = Es2pandaContextState.ES2PANDA_STATE_BOUND } pluginsByState.get(Es2pandaContextState.ES2PANDA_STATE_BOUND)?.forEach(plugin => { @@ -238,7 +235,6 @@ function invokeWithPlugins( if (!terminate) { proceedToState(Es2pandaContextState.ES2PANDA_STATE_CHECKED) - global.profiler.curContextState = Es2pandaContextState.ES2PANDA_STATE_CHECKED } pluginsByState.get(Es2pandaContextState.ES2PANDA_STATE_CHECKED)?.forEach(plugin => { @@ -249,13 +245,12 @@ function invokeWithPlugins( }) if (!terminate) { - global.profiler.curContextState = Es2pandaContextState.ES2PANDA_STATE_BIN_GENERATED proceedToState(Es2pandaContextState.ES2PANDA_STATE_BIN_GENERATED) } - global.profiler.compilationEnded() - global.profiler.report() - global.profiler.reportToFile(true) + profiler()?.compilationEnded() + profiler()?.report() + profiler()?.reportToFile(true) compilerContext.destroy() compilerConfig.destroy() @@ -318,7 +313,8 @@ function readAndSortPlugins(configDir: string, plugins: any[]) { export function main() { checkSDK() - const { files, configPath, outputs, dumpAst, restartStages, stage } = parseCommandLineArgs() + const { files, configPath, outputs, dumpAst, restartStages, stage, enableReport } = parseCommandLineArgs() + if (files.length != outputs.length) { reportErrorAndExit("Different length of inputs and outputs") } @@ -331,6 +327,7 @@ export function main() { const pluginsByState = readAndSortPlugins(configDir, plugins) for (var i = 0; i < files.length; i++) { + if (enableReport) setProfiler(new Profiler()) invokeWithPlugins(configPath, files[i], outputs[i], pluginsByState, dumpAst, restartStages, stage, pluginNames, pluginContext) } } diff --git a/ui2abc/libarkts/src/arkts-api/peers/AstNode.ts b/ui2abc/libarkts/src/arkts-api/peers/AstNode.ts index 55fbcf5a0..276b5cfd6 100644 --- a/ui2abc/libarkts/src/arkts-api/peers/AstNode.ts +++ b/ui2abc/libarkts/src/arkts-api/peers/AstNode.ts @@ -22,7 +22,7 @@ import { ArktsObject } from "./ArktsObject" export abstract class AstNode extends ArktsObject { protected constructor(peer: KNativePointer) { - global.profiler.nodeCreated() + // profiler?.nodeCreated() if (isNullPtr(peer)) { throwError(`attempted to create AstNode from nullptr`) } diff --git a/ui2abc/libarkts/src/arkts-api/static/global.ts b/ui2abc/libarkts/src/arkts-api/static/global.ts index a8bc02825..271ff9e4f 100644 --- a/ui2abc/libarkts/src/arkts-api/static/global.ts +++ b/ui2abc/libarkts/src/arkts-api/static/global.ts @@ -19,7 +19,6 @@ import { initEs2panda, Es2pandaNativeModule, initGeneratedEs2panda } from "../.. import { Es2pandaNativeModule as GeneratedEs2pandaNativeModule } from "../../generated/Es2pandaNativeModule" import { initInterop, InteropNativeModule } from "../../InteropNativeModule" import { Context } from "../peers/Context" -import { Profiler } from "./profiler" import { ArkTsConfig } from "../../generated" export class global { @@ -71,6 +70,4 @@ export class global { return this._interop } - - public static profiler = new Profiler() } diff --git a/ui2abc/libarkts/src/arkts-api/static/profiler.ts b/ui2abc/libarkts/src/arkts-api/static/profiler.ts index f96f85875..0a7f552ad 100644 --- a/ui2abc/libarkts/src/arkts-api/static/profiler.ts +++ b/ui2abc/libarkts/src/arkts-api/static/profiler.ts @@ -56,11 +56,25 @@ interface PerformanceDataFile { } function parseFile(performanceFile: string): PerformanceDataFile | undefined { + performanceFile = path.resolve(performanceFile) if (!fs.existsSync(performanceFile)) return undefined - const data = fs.readFileSync(path.resolve(performanceFile)).toString() - if (!data.length) return undefined - return JSON.parse(data) as PerformanceDataFile + const fileData = fs.readFileSync(performanceFile).toString() + if (!fileData.length) return undefined + try { + return JSON.parse(fileData) as PerformanceDataFile + } catch (error) { + return undefined + } +} + +let currentProfiler: Profiler | undefined + +export function setProfiler(profiler: Profiler) { + currentProfiler = profiler +} +export function profiler(): Profiler | undefined { + return currentProfiler } export class Profiler implements PerformanceData { @@ -72,8 +86,8 @@ export class Profiler implements PerformanceData { totalTime: number = 0 pluginsByName: Record = {} - curPlugin: string = "" - curContextState?: Es2pandaContextState + private curPlugin: string = "" + private curContextState?: Es2pandaContextState private getPluginData(pluginName: string, contextState?: Es2pandaContextState): PluginData { if (!(pluginName in this.pluginsByName)) { @@ -82,8 +96,6 @@ export class Profiler implements PerformanceData { return this.pluginsByName[pluginName] } - disableReport = false - nodeCreated() { this.createdNodes++ if (this.curPlugin) this.getPluginData(this.curPlugin, this.curContextState).createdNodes++ @@ -95,7 +107,9 @@ export class Profiler implements PerformanceData { } private transformStartTime = 0 - transformStarted() { + transformStarted(state: Es2pandaContextState, pluginName: string) { + this.curPlugin = pluginName + this.curContextState = state this.transformStartTime = Date.now() } private transformDepStartTime = 0 @@ -107,6 +121,8 @@ export class Profiler implements PerformanceData { const transformEndTime = Date.now() const consumedTime = transformEndTime - this.transformStartTime this.getPluginData(pluginName, state).transformTime += consumedTime + this.curPlugin = "" + this.curContextState = undefined } transformDepEnded(state: Es2pandaContextState, pluginName: string) { @@ -144,23 +160,22 @@ export class Profiler implements PerformanceData { } report() { - Object.entries(this.pluginsByName).forEach((data, key) => { - console.log(data[0], "totalTransformTime =", data[1].transformTime, "ms") - console.log(data[0], "totalDepsTransformTime =", data[1].transformTimeDeps, "ms") + Object.entries(this.pluginsByName).forEach(([pluginName, data], key) => { + console.log(pluginName, "totalTransformTime =", data.transformTime, "ms") + console.log(pluginName, "totalDepsTransformTime =", data.transformTimeDeps, "ms") }) } reportToFile(withSummary: boolean = false) { - if (this.disableReport) return const outDir = path.resolve(global.arktsconfig!.outDir, PERFORMANCE_DATA_DIR) fs.mkdirSync(outDir, { recursive: true }) const outFilePath = path.resolve(outDir, path.basename(this.filePath)) + ".json" - const data: PerformanceDataFile = { data: [this as PerformanceData] } + const curData: PerformanceDataFile = { data: [this as PerformanceData] } if (!fs.existsSync(outFilePath)) { - fs.writeFileSync(outFilePath, JSON.stringify(data)) + fs.writeFileSync(outFilePath, JSON.stringify(curData)) } else { - const savedData: PerformanceDataFile | undefined = parseFile(outFilePath) ?? data + const savedData: PerformanceDataFile | undefined = parseFile(outFilePath) ?? curData savedData.data.push(this as PerformanceData) if (withSummary) { diff --git a/ui2abc/libarkts/src/arkts-api/utilities/public.ts b/ui2abc/libarkts/src/arkts-api/utilities/public.ts index dfca57230..a7d0e9ac5 100644 --- a/ui2abc/libarkts/src/arkts-api/utilities/public.ts +++ b/ui2abc/libarkts/src/arkts-api/utilities/public.ts @@ -35,6 +35,7 @@ import { Config } from "../peers/Config" import { Context } from "../peers/Context" import { NodeCache } from "../node-cache" import { listPrograms } from "../plugins" +import { profiler } from "../static/profiler" export function createETSModuleFromContext(): ETSModule { let program = global.es2panda._ContextProgram(global.context) @@ -93,7 +94,7 @@ export function proceedToState(state: Es2pandaContextState): void { const before = Date.now() global.es2panda._ProceedToState(global.context, state) const after = Date.now() - global.profiler.proceededToState(after-before) + profiler()?.proceededToState(after-before) NodeCache.clear() checkErrors() } diff --git a/ui2abc/libarkts/src/arkts-api/visitor.ts b/ui2abc/libarkts/src/arkts-api/visitor.ts index f1fb3340f..05934d906 100644 --- a/ui2abc/libarkts/src/arkts-api/visitor.ts +++ b/ui2abc/libarkts/src/arkts-api/visitor.ts @@ -144,7 +144,7 @@ export function visitEachChild( node: AstNode, visitor: Visitor ): AstNode { - global.profiler.nodeVisited() + // profiler?.nodeVisited() if (isETSModule(node)) { return updateETSModuleByStatements( node, diff --git a/ui2abc/libarkts/src/index.ts b/ui2abc/libarkts/src/index.ts index c1738ed5e..495423e0e 100644 --- a/ui2abc/libarkts/src/index.ts +++ b/ui2abc/libarkts/src/index.ts @@ -41,6 +41,8 @@ export * from "./arkts-api/peers/ExternalSource" export * from "./arkts-api/peers/ImportPathManager" export * from "./arkts-api/peers/Options" export { global as arktsGlobal } from "./arkts-api/static/global" +export { Profiler, setProfiler, profiler } from "./arkts-api/static/profiler" + export * as arkts from "./arkts-api" export * from "./plugin-utils" diff --git a/ui2abc/tests-memo/test/testUtils.ts b/ui2abc/tests-memo/test/testUtils.ts index 6586d7b62..67ad55bef 100644 --- a/ui2abc/tests-memo/test/testUtils.ts +++ b/ui2abc/tests-memo/test/testUtils.ts @@ -62,16 +62,4 @@ export function isArktsTest() { export function isTSTest() { return TEST_LANGUAGE == Language.TS -} - -export function key(name: string): KoalaCallsiteKey { - if (isArktsTest()) { - let key: KoalaCallsiteKey = 0 - for (let i = 0; i < name.length; i++) { - key = (key << 3) | (key >> 29) ^ (name[i] as int32) - } - return key - } else { - return parseInt(new UniqueId().addString(name).compute().slice(0, 10), 16) as KoalaCallsiteKey - } } \ No newline at end of file -- Gitee From 225964278823860e89cf5aea54c4104803ca64c5 Mon Sep 17 00:00:00 2001 From: twx1232375 Date: Mon, 16 Jun 2025 15:48:52 +0300 Subject: [PATCH 2/2] added nodeCreated, nodeVisited --- ui2abc/libarkts/src/arkts-api/peers/AstNode.ts | 3 ++- ui2abc/libarkts/src/arkts-api/visitor.ts | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/ui2abc/libarkts/src/arkts-api/peers/AstNode.ts b/ui2abc/libarkts/src/arkts-api/peers/AstNode.ts index 276b5cfd6..ee3f0ec3c 100644 --- a/ui2abc/libarkts/src/arkts-api/peers/AstNode.ts +++ b/ui2abc/libarkts/src/arkts-api/peers/AstNode.ts @@ -19,10 +19,11 @@ import { allFlags, unpackNodeArray, unpackNonNullableNode, unpackString } from " import { throwError } from "../../utils" import { Es2pandaModifierFlags } from "../../generated/Es2pandaEnums" import { ArktsObject } from "./ArktsObject" +import { profiler } from "../static/profiler" export abstract class AstNode extends ArktsObject { protected constructor(peer: KNativePointer) { - // profiler?.nodeCreated() + profiler()?.nodeCreated() if (isNullPtr(peer)) { throwError(`attempted to create AstNode from nullptr`) } diff --git a/ui2abc/libarkts/src/arkts-api/visitor.ts b/ui2abc/libarkts/src/arkts-api/visitor.ts index 05934d906..13063baa6 100644 --- a/ui2abc/libarkts/src/arkts-api/visitor.ts +++ b/ui2abc/libarkts/src/arkts-api/visitor.ts @@ -70,7 +70,7 @@ import { import { Es2pandaImportKinds } from "../generated/Es2pandaEnums" import { factory } from "./factory/nodeFactory" import { AstNode } from "./peers/AstNode" -import { global } from "./static/global" +import { profiler } from "./static/profiler" import { updateETSModuleByStatements } from "./utilities/public" type Visitor = (node: AstNode, options?: object) => AstNode @@ -144,7 +144,8 @@ export function visitEachChild( node: AstNode, visitor: Visitor ): AstNode { - // profiler?.nodeVisited() + profiler()?.nodeVisited() + if (isETSModule(node)) { return updateETSModuleByStatements( node, -- Gitee