Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: src/snapshot/mksnapshot.cc

Issue 2767903002: [snapshot] only create snapshot files as last step in mksnapshot. (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. 1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <errno.h> 5 #include <errno.h>
6 #include <signal.h> 6 #include <signal.h>
7 #include <stdio.h> 7 #include <stdio.h>
8 8
9 #include "include/libplatform/libplatform.h" 9 #include "include/libplatform/libplatform.h"
10 #include "src/assembler.h" 10 #include "src/assembler.h"
11 #include "src/base/platform/platform.h" 11 #include "src/base/platform/platform.h"
12 #include "src/flags.h" 12 #include "src/flags.h"
13 #include "src/list.h" 13 #include "src/list.h"
14 #include "src/snapshot/natives.h" 14 #include "src/snapshot/natives.h"
15 #include "src/snapshot/partial-serializer.h" 15 #include "src/snapshot/partial-serializer.h"
16 #include "src/snapshot/startup-serializer.h" 16 #include "src/snapshot/startup-serializer.h"
17 17
18 using namespace v8; 18 using namespace v8;
19 19
20 class SnapshotWriter { 20 class SnapshotWriter {
21 public: 21 public:
22 SnapshotWriter() : fp_(NULL), startup_blob_file_(NULL) {} 22 SnapshotWriter() : snapshot_cpp_path_(NULL), snapshot_blob_path_(NULL) {}
23 23
24 ~SnapshotWriter() { 24 void SetSnapshotFile(const char* snapshot_cpp_file) {
25 if (fp_) fclose(fp_); 25 snapshot_cpp_path_ = snapshot_cpp_file;
26 if (startup_blob_file_) fclose(startup_blob_file_);
27 } 26 }
28 27
29 void SetSnapshotFile(const char* snapshot_file) { 28 void SetStartupBlobFile(const char* snapshot_blob_file) {
30 if (snapshot_file != NULL) fp_ = GetFileDescriptorOrDie(snapshot_file); 29 snapshot_blob_path_ = snapshot_blob_file;
31 } 30 }
32 31
33 void SetStartupBlobFile(const char* startup_blob_file) { 32 void WriteSnapshot(v8::StartupData blob) {
34 if (startup_blob_file != NULL)
35 startup_blob_file_ = GetFileDescriptorOrDie(startup_blob_file);
36 }
37
38 void WriteSnapshot(v8::StartupData blob) const {
39 i::Vector<const i::byte> blob_vector( 33 i::Vector<const i::byte> blob_vector(
40 reinterpret_cast<const i::byte*>(blob.data), blob.raw_size); 34 reinterpret_cast<const i::byte*>(blob.data), blob.raw_size);
41 MaybeWriteSnapshotFile(blob_vector); 35 MaybeWriteSnapshotFile(blob_vector);
42 MaybeWriteStartupBlob(blob_vector); 36 MaybeWriteStartupBlob(blob_vector);
43 } 37 }
44 38
45 private: 39 private:
46 void MaybeWriteStartupBlob(const i::Vector<const i::byte>& blob) const { 40 void MaybeWriteStartupBlob(const i::Vector<const i::byte>& blob) {
Leszek Swirski 2017/03/22 10:58:21 if this function fails, then we still have the sna
Yang 2017/03/22 11:41:42 That's correct. But I don't think this will happen
Leszek Swirski 2017/03/22 11:45:59 SGTM, maybe add a TODO for this rare error case si
47 if (!startup_blob_file_) return; 41 if (!snapshot_blob_path_) return;
48 42
49 size_t written = fwrite(blob.begin(), 1, blob.length(), startup_blob_file_); 43 FILE* fp = GetFileDescriptorOrDie(snapshot_blob_path_);
44 size_t written = fwrite(blob.begin(), 1, blob.length(), fp);
50 if (written != static_cast<size_t>(blob.length())) { 45 if (written != static_cast<size_t>(blob.length())) {
51 i::PrintF("Writing snapshot file failed.. Aborting.\n"); 46 i::PrintF("Writing snapshot file failed.. Aborting.\n");
47 remove(snapshot_blob_path_);
52 exit(1); 48 exit(1);
53 } 49 }
50 fclose(fp);
wiktorg 2017/03/22 11:01:15 nit but shouldn't it also go before remove @ line
Yang 2017/03/22 11:41:41 Done.
54 } 51 }
55 52
56 void MaybeWriteSnapshotFile(const i::Vector<const i::byte>& blob) const { 53 void MaybeWriteSnapshotFile(const i::Vector<const i::byte>& blob) {
57 if (!fp_) return; 54 if (!snapshot_cpp_path_) return;
58 55
59 WriteFilePrefix(); 56 FILE* fp = GetFileDescriptorOrDie(snapshot_cpp_path_);
60 WriteData(blob); 57
61 WriteFileSuffix(); 58 WriteFilePrefix(fp);
59 WriteData(fp, blob);
60 WriteFileSuffix(fp);
61
62 fclose(fp);
62 } 63 }
63 64
64 void WriteFilePrefix() const { 65 void WriteFilePrefix(FILE* fp) {
Leszek Swirski 2017/03/22 10:58:21 why not const? Also, could these be static now?
Yang 2017/03/22 11:41:42 I just didn't see much use in const here. Made the
65 fprintf(fp_, "// Autogenerated snapshot file. Do not edit.\n\n"); 66 fprintf(fp, "// Autogenerated snapshot file. Do not edit.\n\n");
66 fprintf(fp_, "#include \"src/v8.h\"\n"); 67 fprintf(fp, "#include \"src/v8.h\"\n");
67 fprintf(fp_, "#include \"src/base/platform/platform.h\"\n\n"); 68 fprintf(fp, "#include \"src/base/platform/platform.h\"\n\n");
68 fprintf(fp_, "#include \"src/snapshot/snapshot.h\"\n\n"); 69 fprintf(fp, "#include \"src/snapshot/snapshot.h\"\n\n");
69 fprintf(fp_, "namespace v8 {\n"); 70 fprintf(fp, "namespace v8 {\n");
70 fprintf(fp_, "namespace internal {\n\n"); 71 fprintf(fp, "namespace internal {\n\n");
71 } 72 }
72 73
73 void WriteFileSuffix() const { 74 void WriteFileSuffix(FILE* fp) {
74 fprintf(fp_, "const v8::StartupData* Snapshot::DefaultSnapshotBlob() {\n"); 75 fprintf(fp, "const v8::StartupData* Snapshot::DefaultSnapshotBlob() {\n");
75 fprintf(fp_, " return &blob;\n"); 76 fprintf(fp, " return &blob;\n");
76 fprintf(fp_, "}\n\n"); 77 fprintf(fp, "}\n\n");
77 fprintf(fp_, "} // namespace internal\n"); 78 fprintf(fp, "} // namespace internal\n");
78 fprintf(fp_, "} // namespace v8\n"); 79 fprintf(fp, "} // namespace v8\n");
79 } 80 }
80 81
81 void WriteData(const i::Vector<const i::byte>& blob) const { 82 void WriteData(FILE* fp, const i::Vector<const i::byte>& blob) {
82 fprintf(fp_, "static const byte blob_data[] = {\n"); 83 fprintf(fp, "static const byte blob_data[] = {\n");
83 WriteSnapshotData(blob); 84 WriteSnapshotData(fp, blob);
84 fprintf(fp_, "};\n"); 85 fprintf(fp, "};\n");
85 fprintf(fp_, "static const int blob_size = %d;\n", blob.length()); 86 fprintf(fp, "static const int blob_size = %d;\n", blob.length());
86 fprintf(fp_, "static const v8::StartupData blob =\n"); 87 fprintf(fp, "static const v8::StartupData blob =\n");
87 fprintf(fp_, "{ (const char*) blob_data, blob_size };\n"); 88 fprintf(fp, "{ (const char*) blob_data, blob_size };\n");
88 } 89 }
89 90
90 void WriteSnapshotData(const i::Vector<const i::byte>& blob) const { 91 void WriteSnapshotData(FILE* fp, const i::Vector<const i::byte>& blob) {
91 for (int i = 0; i < blob.length(); i++) { 92 for (int i = 0; i < blob.length(); i++) {
92 if ((i & 0x1f) == 0x1f) fprintf(fp_, "\n"); 93 if ((i & 0x1f) == 0x1f) fprintf(fp, "\n");
93 if (i > 0) fprintf(fp_, ","); 94 if (i > 0) fprintf(fp, ",");
94 fprintf(fp_, "%u", static_cast<unsigned char>(blob.at(i))); 95 fprintf(fp, "%u", static_cast<unsigned char>(blob.at(i)));
95 } 96 }
96 fprintf(fp_, "\n"); 97 fprintf(fp, "\n");
97 } 98 }
98 99
99 FILE* GetFileDescriptorOrDie(const char* filename) { 100 FILE* GetFileDescriptorOrDie(const char* filename) {
100 FILE* fp = base::OS::FOpen(filename, "wb"); 101 FILE* fp = base::OS::FOpen(filename, "wb");
101 if (fp == NULL) { 102 if (fp == NULL) {
102 i::PrintF("Unable to open file \"%s\" for writing.\n", filename); 103 i::PrintF("Unable to open file \"%s\" for writing.\n", filename);
103 exit(1); 104 exit(1);
104 } 105 }
105 return fp; 106 return fp;
106 } 107 }
107 108
108 FILE* fp_; 109 const char* snapshot_cpp_path_;
109 FILE* startup_blob_file_; 110 const char* snapshot_blob_path_;
110 }; 111 };
111 112
112 char* GetExtraCode(char* filename, const char* description) { 113 char* GetExtraCode(char* filename, const char* description) {
113 if (filename == NULL || strlen(filename) == 0) return NULL; 114 if (filename == NULL || strlen(filename) == 0) return NULL;
114 ::printf("Loading script for %s: %s\n", description, filename); 115 ::printf("Loading script for %s: %s\n", description, filename);
115 FILE* file = base::OS::FOpen(filename, "rb"); 116 FILE* file = base::OS::FOpen(filename, "rb");
116 if (file == NULL) { 117 if (file == NULL) {
117 fprintf(stderr, "Failed to open '%s': errno %d\n", filename, errno); 118 fprintf(stderr, "Failed to open '%s': errno %d\n", filename, errno);
118 exit(1); 119 exit(1);
119 } 120 }
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 CHECK(blob.data); 176 CHECK(blob.data);
176 writer.WriteSnapshot(blob); 177 writer.WriteSnapshot(blob);
177 delete[] blob.data; 178 delete[] blob.data;
178 } 179 }
179 180
180 V8::Dispose(); 181 V8::Dispose();
181 V8::ShutdownPlatform(); 182 V8::ShutdownPlatform();
182 delete platform; 183 delete platform;
183 return 0; 184 return 0;
184 } 185 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698