| OLD | NEW |
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 #include <signal.h> | 28 #include <signal.h> |
| 29 #include <string> | 29 #include <string> |
| 30 #include <map> | 30 #include <map> |
| 31 | 31 |
| 32 #include "v8.h" | 32 #include "v8.h" |
| 33 | 33 |
| 34 #include "bootstrapper.h" | 34 #include "bootstrapper.h" |
| 35 #include "natives.h" | 35 #include "natives.h" |
| 36 #include "platform.h" | 36 #include "platform.h" |
| 37 #include "serialize.h" | 37 #include "serialize.h" |
| 38 #include "list.h" |
| 38 | 39 |
| 39 // use explicit namespace to avoid clashing with types in namespace v8 | 40 // use explicit namespace to avoid clashing with types in namespace v8 |
| 40 namespace i = v8::internal; | 41 namespace i = v8::internal; |
| 41 using namespace v8; | 42 using namespace v8; |
| 42 | 43 |
| 43 static const unsigned int kMaxCounters = 256; | 44 static const unsigned int kMaxCounters = 256; |
| 44 | 45 |
| 45 // A single counter in a counter collection. | 46 // A single counter in a counter collection. |
| 46 class Counter { | 47 class Counter { |
| 47 public: | 48 public: |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 static CounterCollection local_counters; | 90 static CounterCollection local_counters; |
| 90 | 91 |
| 91 | 92 |
| 92 typedef std::map<std::string, int*> CounterMap; | 93 typedef std::map<std::string, int*> CounterMap; |
| 93 typedef std::map<std::string, int*>::iterator CounterMapIterator; | 94 typedef std::map<std::string, int*>::iterator CounterMapIterator; |
| 94 static CounterMap counter_table_; | 95 static CounterMap counter_table_; |
| 95 | 96 |
| 96 | 97 |
| 97 class CppByteSink : public i::SnapshotByteSink { | 98 class CppByteSink : public i::SnapshotByteSink { |
| 98 public: | 99 public: |
| 99 explicit CppByteSink(const char* snapshot_file) : bytes_written_(0) { | 100 explicit CppByteSink(const char* snapshot_file) |
| 101 : bytes_written_(0), |
| 102 partial_sink_(this) { |
| 100 fp_ = i::OS::FOpen(snapshot_file, "wb"); | 103 fp_ = i::OS::FOpen(snapshot_file, "wb"); |
| 101 if (fp_ == NULL) { | 104 if (fp_ == NULL) { |
| 102 i::PrintF("Unable to write to snapshot file \"%s\"\n", snapshot_file); | 105 i::PrintF("Unable to write to snapshot file \"%s\"\n", snapshot_file); |
| 103 exit(1); | 106 exit(1); |
| 104 } | 107 } |
| 105 fprintf(fp_, "// Autogenerated snapshot file. Do not edit.\n\n"); | 108 fprintf(fp_, "// Autogenerated snapshot file. Do not edit.\n\n"); |
| 106 fprintf(fp_, "#include \"v8.h\"\n"); | 109 fprintf(fp_, "#include \"v8.h\"\n"); |
| 107 fprintf(fp_, "#include \"platform.h\"\n\n"); | 110 fprintf(fp_, "#include \"platform.h\"\n\n"); |
| 108 fprintf(fp_, "#include \"snapshot.h\"\n\n"); | 111 fprintf(fp_, "#include \"snapshot.h\"\n\n"); |
| 109 fprintf(fp_, "namespace v8 {\nnamespace internal {\n\n"); | 112 fprintf(fp_, "namespace v8 {\nnamespace internal {\n\n"); |
| 110 fprintf(fp_, "const byte Snapshot::data_[] = {"); | 113 fprintf(fp_, "const byte Snapshot::data_[] = {"); |
| 111 } | 114 } |
| 112 | 115 |
| 113 virtual ~CppByteSink() { | 116 virtual ~CppByteSink() { |
| 114 if (fp_ != NULL) { | 117 fprintf(fp_, "const int Snapshot::size_ = %d;\n\n", bytes_written_); |
| 115 fprintf(fp_, "};\n\n"); | 118 fprintf(fp_, "} } // namespace v8::internal\n"); |
| 116 fprintf(fp_, "int Snapshot::size_ = %d;\n\n", bytes_written_); | 119 fclose(fp_); |
| 117 fprintf(fp_, "} } // namespace v8::internal\n"); | 120 } |
| 118 fclose(fp_); | 121 |
| 122 void WriteSpaceUsed( |
| 123 int new_space_used, |
| 124 int pointer_space_used, |
| 125 int data_space_used, |
| 126 int code_space_used, |
| 127 int map_space_used, |
| 128 int cell_space_used, |
| 129 int large_space_used) { |
| 130 fprintf(fp_, "};\n\n"); |
| 131 fprintf(fp_, "const int Snapshot::new_space_used_ = %d;\n", new_space_used); |
| 132 fprintf(fp_, |
| 133 "const int Snapshot::pointer_space_used_ = %d;\n", |
| 134 pointer_space_used); |
| 135 fprintf(fp_, |
| 136 "const int Snapshot::data_space_used_ = %d;\n", |
| 137 data_space_used); |
| 138 fprintf(fp_, |
| 139 "const int Snapshot::code_space_used_ = %d;\n", |
| 140 code_space_used); |
| 141 fprintf(fp_, "const int Snapshot::map_space_used_ = %d;\n", map_space_used); |
| 142 fprintf(fp_, |
| 143 "const int Snapshot::cell_space_used_ = %d;\n", |
| 144 cell_space_used); |
| 145 fprintf(fp_, |
| 146 "const int Snapshot::large_space_used_ = %d;\n", |
| 147 large_space_used); |
| 148 } |
| 149 |
| 150 void WritePartialSnapshot() { |
| 151 int length = partial_sink_.Position(); |
| 152 fprintf(fp_, "};\n\n"); |
| 153 fprintf(fp_, "const int Snapshot::context_size_ = %d;\n", length); |
| 154 fprintf(fp_, "const byte Snapshot::context_data_[] = {\n"); |
| 155 for (int j = 0; j < length; j++) { |
| 156 if ((j & 0x1f) == 0x1f) { |
| 157 fprintf(fp_, "\n"); |
| 158 } |
| 159 char byte = partial_sink_.at(j); |
| 160 if (j != 0) { |
| 161 fprintf(fp_, ","); |
| 162 } |
| 163 fprintf(fp_, "%d", byte); |
| 119 } | 164 } |
| 120 } | 165 } |
| 121 | 166 |
| 122 virtual void Put(int byte, const char* description) { | 167 virtual void Put(int byte, const char* description) { |
| 123 i::SerializerDeserializer::Trace("Put %s: %d\n", description, byte); | |
| 124 if (bytes_written_ != 0) { | 168 if (bytes_written_ != 0) { |
| 125 fprintf(fp_, ","); | 169 fprintf(fp_, ","); |
| 126 } | 170 } |
| 127 fprintf(fp_, "%d", byte); | 171 fprintf(fp_, "%d", byte); |
| 128 bytes_written_++; | 172 bytes_written_++; |
| 129 if ((bytes_written_ & 0x3f) == 0) { | 173 if ((bytes_written_ & 0x1f) == 0) { |
| 130 fprintf(fp_, "\n"); | 174 fprintf(fp_, "\n"); |
| 131 } | 175 } |
| 132 } | 176 } |
| 133 | 177 |
| 134 virtual int Position() { | 178 virtual int Position() { |
| 135 return bytes_written_; | 179 return bytes_written_; |
| 136 } | 180 } |
| 137 | 181 |
| 182 i::SnapshotByteSink* partial_sink() { return &partial_sink_; } |
| 183 |
| 184 class PartialSnapshotSink : public i::SnapshotByteSink { |
| 185 public: |
| 186 explicit PartialSnapshotSink(CppByteSink* parent) |
| 187 : parent_(parent), |
| 188 data_() { } |
| 189 virtual ~PartialSnapshotSink() { data_.Free(); } |
| 190 virtual void Put(int byte, const char* description) { |
| 191 data_.Add(byte); |
| 192 } |
| 193 virtual int Position() { return data_.length(); } |
| 194 char at(int i) { return data_[i]; } |
| 195 private: |
| 196 CppByteSink* parent_; |
| 197 i::List<char> data_; |
| 198 }; |
| 199 |
| 138 private: | 200 private: |
| 139 FILE* fp_; | 201 FILE* fp_; |
| 140 int bytes_written_; | 202 int bytes_written_; |
| 203 PartialSnapshotSink partial_sink_; |
| 141 }; | 204 }; |
| 142 | 205 |
| 143 | 206 |
| 144 int main(int argc, char** argv) { | 207 int main(int argc, char** argv) { |
| 145 #ifdef ENABLE_LOGGING_AND_PROFILING | 208 #ifdef ENABLE_LOGGING_AND_PROFILING |
| 146 // By default, log code create information in the snapshot. | 209 // By default, log code create information in the snapshot. |
| 147 i::FLAG_log_code = true; | 210 i::FLAG_log_code = true; |
| 148 #endif | 211 #endif |
| 149 // Print the usage if an error occurs when parsing the command line | 212 // Print the usage if an error occurs when parsing the command line |
| 150 // flags or if the help flag is set. | 213 // flags or if the help flag is set. |
| 151 int result = i::FlagList::SetFlagsFromCommandLine(&argc, argv, true); | 214 int result = i::FlagList::SetFlagsFromCommandLine(&argc, argv, true); |
| 152 if (result > 0 || argc != 2 || i::FLAG_help) { | 215 if (result > 0 || argc != 2 || i::FLAG_help) { |
| 153 ::printf("Usage: %s [flag] ... outfile\n", argv[0]); | 216 ::printf("Usage: %s [flag] ... outfile\n", argv[0]); |
| 154 i::FlagList::PrintHelp(); | 217 i::FlagList::PrintHelp(); |
| 155 return !i::FLAG_help; | 218 return !i::FLAG_help; |
| 156 } | 219 } |
| 157 i::Serializer::Enable(); | 220 i::Serializer::Enable(); |
| 158 Persistent<Context> context = v8::Context::New(); | 221 Persistent<Context> context = v8::Context::New(); |
| 159 ASSERT(!context.IsEmpty()); | 222 ASSERT(!context.IsEmpty()); |
| 160 // Make sure all builtin scripts are cached. | 223 // Make sure all builtin scripts are cached. |
| 161 { HandleScope scope; | 224 { HandleScope scope; |
| 162 for (int i = 0; i < i::Natives::GetBuiltinsCount(); i++) { | 225 for (int i = 0; i < i::Natives::GetBuiltinsCount(); i++) { |
| 163 i::Bootstrapper::NativesSourceLookup(i); | 226 i::Bootstrapper::NativesSourceLookup(i); |
| 164 } | 227 } |
| 165 } | 228 } |
| 229 // If we don't do this then we end up with a stray root pointing at the |
| 230 // context even after we have disposed of the context. |
| 231 i::Heap::CollectAllGarbage(true); |
| 232 i::AssertNoAllocation we_have_raw_pointers_here; |
| 233 i::Object* raw_context = *(v8::Utils::OpenHandle(*context)); |
| 166 context.Dispose(); | 234 context.Dispose(); |
| 167 CppByteSink sink(argv[1]); | 235 CppByteSink sink(argv[1]); |
| 168 // This results in a somewhat smaller snapshot, probably because it gets rid | 236 // This results in a somewhat smaller snapshot, probably because it gets rid |
| 169 // of some things that are cached between garbage collections. | 237 // of some things that are cached between garbage collections. |
| 170 i::Heap::CollectAllGarbage(true); | |
| 171 i::StartupSerializer ser(&sink); | 238 i::StartupSerializer ser(&sink); |
| 172 ser.Serialize(); | 239 ser.SerializeStrongReferences(); |
| 240 |
| 241 i::PartialSerializer partial_ser(&ser, sink.partial_sink()); |
| 242 partial_ser.Serialize(&raw_context); |
| 243 |
| 244 ser.SerializeWeakReferences(); |
| 245 |
| 246 sink.WritePartialSnapshot(); |
| 247 |
| 248 sink.WriteSpaceUsed( |
| 249 partial_ser.CurrentAllocationAddress(i::NEW_SPACE), |
| 250 partial_ser.CurrentAllocationAddress(i::OLD_POINTER_SPACE), |
| 251 partial_ser.CurrentAllocationAddress(i::OLD_DATA_SPACE), |
| 252 partial_ser.CurrentAllocationAddress(i::CODE_SPACE), |
| 253 partial_ser.CurrentAllocationAddress(i::MAP_SPACE), |
| 254 partial_ser.CurrentAllocationAddress(i::CELL_SPACE), |
| 255 partial_ser.CurrentAllocationAddress(i::LO_SPACE)); |
| 173 return 0; | 256 return 0; |
| 174 } | 257 } |
| OLD | NEW |