diff --git a/compiler/src/gen_abc.ts b/compiler/src/gen_abc.ts index 8bcd6ff330b4787b5e954760c3ffc6da21b3871e..2c4be9d5672f8989a76794469867dacd600b089e 100644 --- a/compiler/src/gen_abc.ts +++ b/compiler/src/gen_abc.ts @@ -1,32 +1,40 @@ -import * as process from 'child_process'; +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import * as childProcess from 'child_process'; +import * as process from 'process'; import * as fs from 'fs'; +import cluster from 'cluster'; import { logger } from './compile_info'; -const {workerData, threadId} = require('worker_threads'); - -interface File { - path: string, - size: number -} const red: string = '\u001b[31m'; const reset: string = '\u001b[39m'; -function js2abcByWorkers(inputPaths: File[], cmd: string): Promise { +function js2abcByWorkers(jsonInput: string, cmd: string): Promise { + const inputPaths = JSON.parse(jsonInput); for (let i = 0; i < inputPaths.length; ++i) { const input = inputPaths[i].path; const singleCmd = `${cmd} "${input}"`; - logger.debug('gen abc cmd is: ', singleCmd); + logger.debug('gen abc cmd is: ', singleCmd, ' ,file size is:', inputPaths[i].size, ' byte'); try { - process.execSync(singleCmd); + childProcess.execSync(singleCmd); } catch (e) { logger.error(red, `ETS:ERROR Failed to convert file ${input} to abc `, reset); return; } - if (fs.existsSync(input)) { - fs.unlinkSync(input); - } - const abcFile: string = input.replace(/\.js$/, '.abc'); if (fs.existsSync(abcFile)) { const abcFileNew: string = abcFile.replace(/_.abc$/, '.abc'); @@ -37,8 +45,10 @@ function js2abcByWorkers(inputPaths: File[], cmd: string): Promise { } } -logger.debug('worker data is: ', JSON.stringify(workerData)); -if (JSON.stringify(workerData) !== 'null') { - logger.debug('==>worker #', threadId, 'started!'); - js2abcByWorkers(workerData.input, workerData.cmd); +logger.debug('worker data is: ', JSON.stringify(process.env)); +logger.debug('gen_abc isWorker is: ', cluster.isWorker); +if (cluster.isWorker && process.env['inputs'] !== undefined && process.env['cmd'] !== undefined) { + logger.debug('==>worker #', cluster.worker.id, 'started!'); + js2abcByWorkers(process.env['inputs'], process.env['cmd']); + process.exit(); } diff --git a/compiler/src/gen_abc_plugin.ts b/compiler/src/gen_abc_plugin.ts index 3de104e3e54970c157ca4c736b4f04d1324badb4..26bc3e3a2265c16c4fc776cebeec3f4677add488 100644 --- a/compiler/src/gen_abc_plugin.ts +++ b/compiler/src/gen_abc_plugin.ts @@ -15,10 +15,12 @@ import * as fs from 'fs'; import * as path from 'path'; +import cluster from 'cluster'; +import process from 'process'; import Compiler from 'webpack/lib/Compiler'; import { logger } from './compile_info'; +import { toUnixPath, toHashData } from './utils'; -const {Worker, isMainThread} = require('worker_threads'); const firstFileEXT: string = '_.js'; const genAbcScript = 'gen_abc.js'; let output: string; @@ -33,9 +35,14 @@ interface File { size: number } const intermediateJsBundle: Array = []; +let fileterIntermediateJsBundle: Array = []; +let hashJsonObject = {}; +let buildPathInfo = ''; const red: string = '\u001b[31m'; const reset: string = '\u001b[39m'; +const hashFile = 'gen_hash.json'; +const ARK = '/ark/'; export class GenAbcPlugin { constructor(output_, arkDir_, nodeJs_, isDebug_) { @@ -70,31 +77,8 @@ export class GenAbcPlugin { }); compiler.hooks.afterEmit.tap('GenAbcPluginMultiThread', () => { - let param: string = ''; - if (isDebug) { - param += ' --debug'; - } - - if (isMainThread) { - let js2abc: string = path.join(arkDir, 'build', 'src', 'index.js'); - if (isWin) { - js2abc = path.join(arkDir, 'build-win', 'src', 'index.js'); - } else if (isMac) { - js2abc = path.join(arkDir, 'build-mac', 'src', 'index.js'); - } - const maxWorkerNumber = 3; - const splitedBundles = splitJsBundlesBySize(intermediateJsBundle, maxWorkerNumber); - const workerNumber = maxWorkerNumber < splitedBundles.length ? maxWorkerNumber : splitedBundles.length; - const cmdPrefix: string = `${nodeJs} --expose-gc "${js2abc}" ${param} `; - const workers = []; - for (let i = 0; i < workerNumber; ++i) { - workers.push(new Worker(path.resolve(__dirname, genAbcScript), - {workerData: {input: splitedBundles[i], cmd: cmdPrefix} })); - workers[i].on('exit', () => { - logger.debug('worker ', i, 'finished!'); - }); - } - } + buildPathInfo = output; + invokeWorkersToGenAbc(); }); } } @@ -124,9 +108,9 @@ function mkDir(path_: string): void { function getSmallestSizeGroup(groupSize: Map) { const groupSizeArray = Array.from(groupSize); groupSizeArray.sort(function(g1, g2) { - return g1[1] - g2[1]; // sort by value + return g1[1] - g2[1]; // sort by size }); - return groupSizeArray[0][0]; // return key + return groupSizeArray[0][0]; } function splitJsBundlesBySize(bundleArray: Array, groupNumber: number) { @@ -155,3 +139,139 @@ function splitJsBundlesBySize(bundleArray: Array, groupNumber: number) { } return result; } + +function invokeWorkersToGenAbc() { + let param: string = ''; + if (isDebug) { + param += ' --debug'; + } + + let js2abc: string = path.join(arkDir, 'build', 'src', 'index.js'); + if (isWin) { + js2abc = path.join(arkDir, 'build-win', 'src', 'index.js'); + } else if (isMac) { + js2abc = path.join(arkDir, 'build-mac', 'src', 'index.js'); + } + + filterIntermediateJsBundleByHashJson(buildPathInfo, intermediateJsBundle); + const maxWorkerNumber = 3; + const splitedBundles = splitJsBundlesBySize(fileterIntermediateJsBundle, maxWorkerNumber); + const workerNumber = maxWorkerNumber < splitedBundles.length ? maxWorkerNumber : splitedBundles.length; + const cmdPrefix: string = `${nodeJs} --expose-gc "${js2abc}" ${param} `; + + const clusterNewApiVersion = 16; + const currentNodeVersion = parseInt(process.version.split('.')[0]); + const useNewApi = currentNodeVersion >= clusterNewApiVersion; + + if (useNewApi && cluster.isPrimary || !useNewApi && cluster.isMaster) { + if (useNewApi) { + cluster.setupPrimary({ + exec: path.resolve(__dirname, genAbcScript) + }); + } else { + cluster.setupMaster({ + exec: path.resolve(__dirname, genAbcScript) + }); + } + + for (let i = 0; i < workerNumber; ++i) { + const workerData = { + 'inputs': JSON.stringify(splitedBundles[i]), + 'cmd': cmdPrefix + }; + cluster.fork(workerData); + } + + cluster.on('exit', (worker, code, signal) => { + logger.debug(`worker ${worker.process.pid} finished`); + }); + + process.on('exit', (code) => { + writeHashJson(); + }); + } +} + +function filterIntermediateJsBundleByHashJson(buildPath: string, inputPaths: File[]) { + for (let i = 0; i < inputPaths.length; ++i) { + fileterIntermediateJsBundle.push(inputPaths[i]); + } + const hashFilePath = genHashJsonPath(buildPath); + if (hashFilePath.length === 0) { + return; + } + const updateJsonObject = {}; + let jsonObject = {}; + let jsonFile = ''; + if (fs.existsSync(hashFilePath)) { + jsonFile = fs.readFileSync(hashFilePath).toString(); + jsonObject = JSON.parse(jsonFile); + fileterIntermediateJsBundle = []; + for (let i = 0; i < inputPaths.length; ++i) { + const input = inputPaths[i].path; + const abcPath = input.replace(/_.js$/, '.abc'); + if (!fs.existsSync(input)) { + logger.error(red, `ETS:ERROR ${input} is lost`, reset); + continue; + } + if (fs.existsSync(abcPath)) { + const hashInputContentData = toHashData(input); + const hashAbcContentData = toHashData(abcPath); + if (jsonObject[input] === hashInputContentData && jsonObject[abcPath] === hashAbcContentData) { + updateJsonObject[input] = hashInputContentData; + updateJsonObject[abcPath] = hashAbcContentData; + fs.unlinkSync(input); + } else { + fileterIntermediateJsBundle.push(inputPaths[i]); + } + } else { + fileterIntermediateJsBundle.push(inputPaths[i]); + } + } + } + + hashJsonObject = updateJsonObject; +} + +function writeHashJson() { + for (let i = 0; i < fileterIntermediateJsBundle.length; ++i) { + const input = fileterIntermediateJsBundle[i].path; + const abcPath = input.replace(/_.js$/, '.abc'); + if (!fs.existsSync(input) || !fs.existsSync(abcPath)) { + logger.error(red, `ETS:ERROR ${input} is lost`, reset); + continue; + } + const hashInputContentData = toHashData(input); + const hashAbcContentData = toHashData(abcPath); + hashJsonObject[input] = hashInputContentData; + hashJsonObject[abcPath] = hashAbcContentData; + fs.unlinkSync(input); + } + const hashFilePath = genHashJsonPath(buildPathInfo); + if (hashFilePath.length === 0) { + return; + } + fs.writeFileSync(hashFilePath, JSON.stringify(hashJsonObject)); +} + +function genHashJsonPath(buildPath: string) { + buildPath = toUnixPath(buildPath); + if (process.env.cachePath) { + if (!fs.existsSync(process.env.cachePath) || !fs.statSync(process.env.cachePath).isDirectory()) { + logger.error(red, `ETS:ERROR hash path does not exist`, reset); + return ''; + } + return path.join(process.env.cachePath, hashFile); + } else if (buildPath.indexOf(ARK) >= 0) { + const dataTmps = buildPath.split(ARK); + const hashPath = path.join(dataTmps[0], ARK); + if (!fs.existsSync(hashPath) || !fs.statSync(hashPath).isDirectory()) { + logger.error(red, `ETS:ERROR hash path does not exist`, reset); + return ''; + } + return path.join(hashPath, hashFile); + } else { + logger.debug(red, `ETS:ERROR not cache exist`, reset); + return ''; + } +} diff --git a/compiler/src/utils.ts b/compiler/src/utils.ts index 59e5b9b66b3e840ac6ba0a1cde0c376b97d4d28a..7af185cc9367f30c9c3eadbba2277aef34b388a0 100644 --- a/compiler/src/utils.ts +++ b/compiler/src/utils.ts @@ -16,6 +16,7 @@ import ts from 'typescript'; import path from 'path'; import fs from 'fs'; +import { createHash } from 'crypto'; export enum LogType { ERROR = 'ERROR', @@ -206,3 +207,19 @@ export function mkDir(path_: string): void { } fs.mkdirSync(path_); } + +export function toUnixPath(data: string): string { + if (/^win/.test(require('os').platform())) { + const fileTmps: string[] = data.split(path.sep); + const newData: string = path.posix.join(...fileTmps); + return newData; + } + return data; +} + +export function toHashData(path: string) { + const content = fs.readFileSync(path); + const hash = createHash('sha256'); + hash.update(content); + return hash.digest('hex'); +}