Skip to content

Commit 759ee85

Browse files
committed
src: add utilities to help debugging reproducibility of snapshots
- Print offsets in blob serializer - Add a special node:generate_default_snapshot ID to generate the built-in snapshot. - Improve logging
1 parent 4de8eaa commit 759ee85

File tree

3 files changed

+35
-15
lines changed

3 files changed

+35
-15
lines changed

src/blob_serializer_deserializer-inl.h

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ size_t BlobSerializer<Impl>::WriteVector(const std::vector<T>& data) {
238238
if (is_debug) {
239239
std::string str = std::is_arithmetic_v<T> ? "" : ToStr(data);
240240
std::string name = GetName<T>();
241-
Debug("\nWriteVector<%s>() (%d-byte), count=%d: %s\n",
241+
Debug("\nAt 0x%x: WriteVector<%s>() (%d-byte), count=%d: %s\n",
242+
sink.size(),
242243
name.c_str(),
243244
sizeof(T),
244245
data.size(),
@@ -270,7 +271,10 @@ size_t BlobSerializer<Impl>::WriteVector(const std::vector<T>& data) {
270271
template <typename Impl>
271272
size_t BlobSerializer<Impl>::WriteStringView(std::string_view data,
272273
StringLogMode mode) {
273-
Debug("WriteStringView(), length=%zu: %p\n", data.size(), data.data());
274+
Debug("At 0x%x: WriteStringView(), length=%zu: %p\n",
275+
sink.size(),
276+
data.size(),
277+
data.data());
274278
size_t written_total = WriteArithmetic<size_t>(data.size());
275279

276280
size_t length = data.size();
@@ -294,17 +298,27 @@ size_t BlobSerializer<Impl>::WriteString(const std::string& data) {
294298
return WriteStringView(data, StringLogMode::kAddressAndContent);
295299
}
296300

301+
static size_t kPreviewCount = 16;
302+
297303
// Helper for writing an array of numeric types.
298304
template <typename Impl>
299305
template <typename T>
300306
size_t BlobSerializer<Impl>::WriteArithmetic(const T* data, size_t count) {
301307
static_assert(std::is_arithmetic_v<T>, "Arithmetic type");
302308
DCHECK_GT(count, 0); // Should not write contents for vectors of size 0.
303309
if (is_debug) {
304-
std::string str =
305-
"{ " + std::to_string(data[0]) + (count > 1 ? ", ... }" : " }");
310+
size_t preview_count = count < kPreviewCount ? count : kPreviewCount;
311+
std::string str = "{ ";
312+
for (size_t i = 0; i < preview_count; ++i) {
313+
str += (std::to_string(data[i]) + ",");
314+
}
315+
if (count > preview_count) {
316+
str += "...";
317+
}
318+
str += "}";
306319
std::string name = GetName<T>();
307-
Debug("Write<%s>() (%zu-byte), count=%zu: %s",
320+
Debug("At 0x%x: Write<%s>() (%zu-byte), count=%zu: %s",
321+
sink.size(),
308322
name.c_str(),
309323
sizeof(T),
310324
count,

src/node.cc

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,18 +1306,24 @@ ExitCode GenerateAndWriteSnapshotData(const SnapshotData** snapshot_data_ptr,
13061306
return exit_code;
13071307
}
13081308
} else {
1309+
std::optional<std::string> builder_script_content;
13091310
// Otherwise, load and run the specified builder script.
13101311
std::unique_ptr<SnapshotData> generated_data =
13111312
std::make_unique<SnapshotData>();
1312-
std::string builder_script_content;
1313-
int r = ReadFileSync(&builder_script_content, builder_script.c_str());
1314-
if (r != 0) {
1315-
FPrintF(stderr,
1316-
"Cannot read builder script %s for building snapshot. %s: %s",
1317-
builder_script,
1318-
uv_err_name(r),
1319-
uv_strerror(r));
1320-
return ExitCode::kGenericUserError;
1313+
if (builder_script != "node:generate_default_snapshot") {
1314+
builder_script_content = std::string();
1315+
int r = ReadFileSync(&(builder_script_content.value()),
1316+
builder_script.c_str());
1317+
if (r != 0) {
1318+
FPrintF(stderr,
1319+
"Cannot read builder script %s for building snapshot. %s: %s\n",
1320+
builder_script,
1321+
uv_err_name(r),
1322+
uv_strerror(r));
1323+
return ExitCode::kGenericUserError;
1324+
}
1325+
} else {
1326+
snapshot_config.builder_script_path = std::nullopt;
13211327
}
13221328

13231329
exit_code = node::SnapshotBuilder::Generate(generated_data.get(),

src/node_snapshotable.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ std::vector<char> SnapshotData::ToBlob() const {
603603
written_total += w.WriteArithmetic<uint32_t>(kMagic);
604604
w.Debug("Write metadata\n");
605605
written_total += w.Write<SnapshotMetadata>(metadata);
606-
606+
w.Debug("Write snapshot blob\n");
607607
written_total += w.Write<v8::StartupData>(v8_snapshot_blob_data);
608608
w.Debug("Write isolate_data_indices\n");
609609
written_total += w.Write<IsolateDataSerializeInfo>(isolate_data_info);

0 commit comments

Comments
 (0)