2018-06-18 19:02:57 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// Flags: --expose-internals
|
|
|
|
|
|
|
|
// This file generates the code cache for builtin modules and
|
|
|
|
// writes them into static char arrays of a C++ file that can be
|
|
|
|
// compiled into the binary using the `--code-cache-path` option
|
|
|
|
// of `configure`.
|
|
|
|
|
|
|
|
const {
|
2018-11-03 07:26:32 +01:00
|
|
|
getCodeCache,
|
2018-12-03 14:47:57 +01:00
|
|
|
compileFunction,
|
2018-06-27 16:31:01 +02:00
|
|
|
cachableBuiltins
|
2018-06-18 19:02:57 +02:00
|
|
|
} = require('internal/bootstrap/cache');
|
|
|
|
|
2018-11-14 15:38:12 +01:00
|
|
|
const {
|
|
|
|
types: {
|
|
|
|
isUint8Array
|
|
|
|
}
|
|
|
|
} = require('util');
|
|
|
|
|
2018-06-18 19:02:57 +02:00
|
|
|
const fs = require('fs');
|
|
|
|
|
|
|
|
const resultPath = process.argv[2];
|
|
|
|
if (!resultPath) {
|
|
|
|
console.error(`Usage: ${process.argv[0]} ${process.argv[1]}` +
|
|
|
|
'path/to/node_code_cache.cc');
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Format a number of a size in bytes into human-readable strings
|
|
|
|
* @param {number} num
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
function formatSize(num) {
|
|
|
|
if (num < 1024) {
|
|
|
|
return `${(num).toFixed(2)}B`;
|
|
|
|
} else if (num < 1024 ** 2) {
|
|
|
|
return `${(num / 1024).toFixed(2)}KB`;
|
|
|
|
} else if (num < 1024 ** 3) {
|
|
|
|
return `${(num / (1024 ** 2)).toFixed(2)}MB`;
|
|
|
|
} else {
|
|
|
|
return `${(num / (1024 ** 3)).toFixed(2)}GB`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates the source code of definitions of the char arrays
|
|
|
|
* that contains the code cache and the source code of the
|
|
|
|
* initializers of the code cache.
|
|
|
|
*
|
|
|
|
* @param {string} key ID of the builtin module
|
2018-11-14 15:38:12 +01:00
|
|
|
* @param {Uint8Array} cache Code cache of the builtin module
|
2018-06-18 19:02:57 +02:00
|
|
|
* @return { definition: string, initializer: string }
|
|
|
|
*/
|
|
|
|
function getInitalizer(key, cache) {
|
2018-11-14 15:38:12 +01:00
|
|
|
const defName = `${key.replace(/\//g, '_').replace(/-/g, '_')}_raw`;
|
|
|
|
const definition = `static const uint8_t ${defName}[] = {\n` +
|
2018-06-18 19:02:57 +02:00
|
|
|
`${cache.join(',')}\n};`;
|
2018-12-03 14:47:57 +01:00
|
|
|
const dataDef = 'std::make_unique<v8::ScriptCompiler::CachedData>(' +
|
|
|
|
`${defName}, static_cast<int>(arraysize(${defName})), ` +
|
|
|
|
'policy)';
|
2018-11-14 15:38:12 +01:00
|
|
|
const initializer =
|
|
|
|
'code_cache_.emplace(\n' +
|
|
|
|
` "${key}",\n` +
|
2018-12-03 14:47:57 +01:00
|
|
|
` ${dataDef}\n` +
|
2018-11-14 15:38:12 +01:00
|
|
|
');';
|
2018-06-18 19:02:57 +02:00
|
|
|
return {
|
2018-12-03 01:23:34 +01:00
|
|
|
definition, initializer
|
2018-06-18 19:02:57 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const cacheDefinitions = [];
|
|
|
|
const cacheInitializers = [];
|
|
|
|
let totalCacheSize = 0;
|
|
|
|
|
2018-11-14 15:38:12 +01:00
|
|
|
function lexical(a, b) {
|
|
|
|
if (a < b) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (a > b) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2018-06-18 19:02:57 +02:00
|
|
|
|
2018-11-14 15:38:12 +01:00
|
|
|
for (const key of cachableBuiltins.sort(lexical)) {
|
2018-12-03 14:47:57 +01:00
|
|
|
compileFunction(key); // compile it
|
2018-06-27 16:31:01 +02:00
|
|
|
const cachedData = getCodeCache(key);
|
2018-11-14 15:38:12 +01:00
|
|
|
if (!isUint8Array(cachedData)) {
|
2018-06-18 19:02:57 +02:00
|
|
|
console.error(`Failed to generate code cache for '${key}'`);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2018-11-14 15:38:12 +01:00
|
|
|
const size = cachedData.byteLength;
|
|
|
|
totalCacheSize += size;
|
2018-08-06 17:27:03 +02:00
|
|
|
const {
|
2018-12-03 01:23:34 +01:00
|
|
|
definition, initializer,
|
2018-08-06 17:27:03 +02:00
|
|
|
} = getInitalizer(key, cachedData);
|
2018-06-18 19:02:57 +02:00
|
|
|
cacheDefinitions.push(definition);
|
|
|
|
cacheInitializers.push(initializer);
|
2018-11-14 15:38:12 +01:00
|
|
|
console.log(`Generated cache for '${key}', size = ${formatSize(size)}` +
|
2018-12-03 01:23:34 +01:00
|
|
|
`, total = ${formatSize(totalCacheSize)}`);
|
2018-06-18 19:02:57 +02:00
|
|
|
}
|
|
|
|
|
2018-11-14 15:38:12 +01:00
|
|
|
const result = `#include "node_native_module.h"
|
|
|
|
#include "node_internals.h"
|
2018-06-18 19:02:57 +02:00
|
|
|
|
|
|
|
// This file is generated by tools/generate_code_cache.js
|
|
|
|
// and is used when configure is run with \`--code-cache-path\`
|
|
|
|
|
|
|
|
namespace node {
|
2018-11-14 15:38:12 +01:00
|
|
|
namespace native_module {
|
2018-06-18 19:02:57 +02:00
|
|
|
${cacheDefinitions.join('\n\n')}
|
|
|
|
|
2018-11-14 15:38:12 +01:00
|
|
|
void NativeModuleLoader::LoadCodeCache() {
|
2018-12-03 14:47:57 +01:00
|
|
|
auto policy = v8::ScriptCompiler::CachedData::BufferPolicy::BufferNotOwned;
|
2018-11-14 15:38:12 +01:00
|
|
|
${cacheInitializers.join('\n ')}
|
2018-06-18 19:02:57 +02:00
|
|
|
}
|
|
|
|
|
2018-11-14 15:38:12 +01:00
|
|
|
} // namespace native_module
|
2018-06-18 19:02:57 +02:00
|
|
|
} // namespace node
|
|
|
|
`;
|
|
|
|
|
|
|
|
fs.writeFileSync(resultPath, result);
|
|
|
|
console.log(`Generated code cache C++ file to ${resultPath}`);
|