summaryrefslogtreecommitdiff
path: root/tools/generate_code_cache.js
blob: b185f6246d0ef652c7ee930f1d469ea40e38e561 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
'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 {
  getSource,
  getCodeCache,
  cachableBuiltins
} = require('internal/bootstrap/cache');

const {
  types: {
    isUint8Array
  }
} = require('util');

function hash(str) {
  if (process.versions.openssl) {
    return require('crypto').createHash('sha256').update(str).digest('hex');
  }
  return '';
}

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
 * @param {Uint8Array} cache Code cache of the builtin module
 * @return { definition: string, initializer: string }
 */
function getInitalizer(key, cache) {
  const defName = `${key.replace(/\//g, '_').replace(/-/g, '_')}_raw`;
  const definition = `static const uint8_t ${defName}[] = {\n` +
                     `${cache.join(',')}\n};`;
  const source = getSource(key);
  const sourceHash = hash(source);
  const initializer =
    'code_cache_.emplace(\n' +
    `  "${key}",\n` +
    `  UnionBytes(${defName}, arraysize(${defName}))\n` +
    ');';
  const hashIntializer =
    'code_cache_hash_.emplace(\n' +
    `  "${key}",\n` +
    `  "${sourceHash}"\n` +
    ');';
  return {
    definition, initializer, hashIntializer, sourceHash
  };
}

const cacheDefinitions = [];
const cacheInitializers = [];
const cacheHashInitializers = [];
let totalCacheSize = 0;

function lexical(a, b) {
  if (a < b) {
    return -1;
  }
  if (a > b) {
    return 1;
  }
  return 0;
}

for (const key of cachableBuiltins.sort(lexical)) {
  const cachedData = getCodeCache(key);
  if (!isUint8Array(cachedData)) {
    console.error(`Failed to generate code cache for '${key}'`);
    process.exit(1);
  }

  const size = cachedData.byteLength;
  totalCacheSize += size;
  const {
    definition, initializer, hashIntializer, sourceHash
  } = getInitalizer(key, cachedData);
  cacheDefinitions.push(definition);
  cacheInitializers.push(initializer);
  cacheHashInitializers.push(hashIntializer);
  console.log(`Generated cache for '${key}', size = ${formatSize(size)}` +
              `, hash = ${sourceHash}, total = ${formatSize(totalCacheSize)}`);
}

const result = `#include "node_native_module.h"
#include "node_internals.h"

// This file is generated by tools/generate_code_cache.js
// and is used when configure is run with \`--code-cache-path\`

namespace node {
namespace native_module {
${cacheDefinitions.join('\n\n')}

void NativeModuleLoader::LoadCodeCache() {
  has_code_cache_ = true;
  ${cacheInitializers.join('\n  ')}
}

void NativeModuleLoader::LoadCodeCacheHash() {
  ${cacheHashInitializers.join('\n  ')}
}

}  // namespace native_module
}  // namespace node
`;

fs.writeFileSync(resultPath, result);
console.log(`Generated code cache C++ file to ${resultPath}`);