GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../tools/code_cache/cache_builder.cc Lines: 66 69 95.7 %
Date: 2020-06-24 22:13:30 Branches: 25 30 83.3 %

Line Branch Exec Source
1
#include "cache_builder.h"
2
#include "debug_utils-inl.h"
3
#include "node_native_module.h"
4
#include "util.h"
5
6
#include <iostream>
7
#include <map>
8
#include <sstream>
9
#include <vector>
10
#include <cstdlib>
11
12
namespace node {
13
namespace native_module {
14
15
using v8::Context;
16
using v8::Local;
17
using v8::ScriptCompiler;
18
19
360
static std::string GetDefName(const std::string& id) {
20
360
  char buf[64] = {0};
21
360
  size_t size = id.size();
22
360
  CHECK_LT(size, sizeof(buf));
23
7028
  for (size_t i = 0; i < size; ++i) {
24
6668
    char ch = id[i];
25

6668
    buf[i] = (ch == '-' || ch == '/') ? '_' : ch;
26
  }
27
360
  return buf;
28
}
29
30
360
static std::string FormatSize(size_t size) {
31
360
  char buf[64] = {0};
32
360
  if (size < 1024) {
33
9
    snprintf(buf, sizeof(buf), "%.2fB", static_cast<double>(size));
34
351
  } else if (size < 1024 * 1024) {
35
351
    snprintf(buf, sizeof(buf), "%.2fKB", static_cast<double>(size / 1024));
36
  } else {
37
    snprintf(
38
        buf, sizeof(buf), "%.2fMB", static_cast<double>(size / 1024 / 1024));
39
  }
40
360
  return buf;
41
}
42
43
180
static std::string GetDefinition(const std::string& id,
44
                                 size_t size,
45
                                 const uint8_t* data) {
46
360
  std::stringstream ss;
47
180
  ss << "static const uint8_t " << GetDefName(id) << "[] = {\n";
48
826524
  for (size_t i = 0; i < size; ++i) {
49
826344
    uint8_t ch = data[i];
50
826344
    ss << std::to_string(ch) << (i == size - 1 ? '\n' : ',');
51
  }
52
180
  ss << "};";
53
360
  return ss.str();
54
}
55
56
180
static void GetInitializer(const std::string& id, std::stringstream& ss) {
57
360
  std::string def_name = GetDefName(id);
58
180
  ss << "  code_cache.emplace(\n";
59
180
  ss << "    \"" << id << "\",\n";
60
180
  ss << "    std::make_unique<v8::ScriptCompiler::CachedData>(\n";
61
180
  ss << "      " << def_name << ",\n";
62
180
  ss << "      static_cast<int>(arraysize(" << def_name << ")), policy\n";
63
180
  ss << "    )\n";
64
180
  ss << "  );";
65
180
}
66
67
1
static std::string GenerateCodeCache(
68
    const std::map<std::string, ScriptCompiler::CachedData*>& data) {
69
2
  std::stringstream ss;
70
  ss << R"(#include <cinttypes>
71
#include "node_native_module_env.h"
72
73
// This file is generated by mkcodecache (tools/code_cache/mkcodecache.cc)
74
75
namespace node {
76
namespace native_module {
77
78
const bool has_code_cache = true;
79
80
1
)";
81
82
1
  size_t total = 0;
83
181
  for (const auto& x : data) {
84
180
    const std::string& id = x.first;
85
180
    ScriptCompiler::CachedData* cached_data = x.second;
86
180
    total += cached_data->length;
87
360
    std::string def = GetDefinition(id, cached_data->length, cached_data->data);
88
180
    ss << def << "\n\n";
89
360
    std::string size_str = FormatSize(cached_data->length);
90
360
    std::string total_str = FormatSize(total);
91
    per_process::Debug(DebugCategory::CODE_CACHE,
92
                       "Generated cache for %s, size = %s, total = %s\n",
93
360
                       id.c_str(),
94
360
                       size_str.c_str(),
95
360
                       total_str.c_str());
96
  }
97
98
  ss << R"(void NativeModuleEnv::InitializeCodeCache() {
99
  NativeModuleCacheMap& code_cache =
100
      *NativeModuleLoader::GetInstance()->code_cache();
101
  CHECK(code_cache.empty());
102
  auto policy = v8::ScriptCompiler::CachedData::BufferPolicy::BufferNotOwned;
103
1
)";
104
105
181
  for (const auto& x : data) {
106
180
    GetInitializer(x.first, ss);
107
180
    ss << "\n\n";
108
  }
109
110
  ss << R"(
111
}
112
113
}  // namespace native_module
114
}  // namespace node
115
1
)";
116
2
  return ss.str();
117
}
118
119
1
std::string CodeCacheBuilder::Generate(Local<Context> context) {
120
1
  NativeModuleLoader* loader = NativeModuleLoader::GetInstance();
121
2
  std::vector<std::string> ids = loader->GetModuleIds();
122
123
2
  std::map<std::string, ScriptCompiler::CachedData*> data;
124
125
229
  for (const auto& id : ids) {
126
    // TODO(joyeecheung): we can only compile the modules that can be
127
    // required here because the parameters for other types of builtins
128
    // are still very flexible. We should look into auto-generating
129
    // the parameters from the source somehow.
130
228
    if (loader->CanBeRequired(id.c_str())) {
131
      NativeModuleLoader::Result result;
132
180
      USE(loader->CompileAsModule(context, id.c_str(), &result));
133
      ScriptCompiler::CachedData* cached_data =
134
180
          loader->GetCodeCache(id.c_str());
135
180
      if (cached_data == nullptr) {
136
        // TODO(joyeecheung): display syntax errors
137
        std::cerr << "Failed to compile " << id << "\n";
138
      } else {
139
180
        data.emplace(id, cached_data);
140
      }
141
    }
142
  }
143
144
2
  return GenerateCodeCache(data);
145
}
146
147
}  // namespace native_module
148

3
}  // namespace node