The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / deps / v8 / src / mksnapshot.cc @ 40c0f755

History | View | Annotate | Download (6.12 KB)

1
// Copyright 2006-2008 the V8 project authors. All rights reserved.
2
// Redistribution and use in source and binary forms, with or without
3
// modification, are permitted provided that the following conditions are
4
// met:
5
//
6
//     * Redistributions of source code must retain the above copyright
7
//       notice, this list of conditions and the following disclaimer.
8
//     * Redistributions in binary form must reproduce the above
9
//       copyright notice, this list of conditions and the following
10
//       disclaimer in the documentation and/or other materials provided
11
//       with the distribution.
12
//     * Neither the name of Google Inc. nor the names of its
13
//       contributors may be used to endorse or promote products derived
14
//       from this software without specific prior written permission.
15
//
16
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27

    
28
#include <signal.h>
29
#include <string>
30
#include <map>
31

    
32
#include "v8.h"
33

    
34
#include "bootstrapper.h"
35
#include "natives.h"
36
#include "platform.h"
37
#include "serialize.h"
38

    
39
// use explicit namespace to avoid clashing with types in namespace v8
40
namespace i = v8::internal;
41
using namespace v8;
42

    
43
static const unsigned int kMaxCounters = 256;
44

    
45
// A single counter in a counter collection.
46
class Counter {
47
 public:
48
  static const int kMaxNameSize = 64;
49
  int32_t* Bind(const char* name) {
50
    int i;
51
    for (i = 0; i < kMaxNameSize - 1 && name[i]; i++) {
52
      name_[i] = name[i];
53
    }
54
    name_[i] = '\0';
55
    return &counter_;
56
  }
57
 private:
58
  int32_t counter_;
59
  uint8_t name_[kMaxNameSize];
60
};
61

    
62

    
63
// A set of counters and associated information.  An instance of this
64
// class is stored directly in the memory-mapped counters file if
65
// the --save-counters options is used
66
class CounterCollection {
67
 public:
68
  CounterCollection() {
69
    magic_number_ = 0xDEADFACE;
70
    max_counters_ = kMaxCounters;
71
    max_name_size_ = Counter::kMaxNameSize;
72
    counters_in_use_ = 0;
73
  }
74
  Counter* GetNextCounter() {
75
    if (counters_in_use_ == kMaxCounters) return NULL;
76
    return &counters_[counters_in_use_++];
77
  }
78
 private:
79
  uint32_t magic_number_;
80
  uint32_t max_counters_;
81
  uint32_t max_name_size_;
82
  uint32_t counters_in_use_;
83
  Counter counters_[kMaxCounters];
84
};
85

    
86

    
87
// We statically allocate a set of local counters to be used if we
88
// don't want to store the stats in a memory-mapped file
89
static CounterCollection local_counters;
90
static CounterCollection* counters = &local_counters;
91

    
92

    
93
typedef std::map<std::string, int*> CounterMap;
94
typedef std::map<std::string, int*>::iterator CounterMapIterator;
95
static CounterMap counter_table_;
96

    
97
// Callback receiver when v8 has a counter to track.
98
static int* counter_callback(const char* name) {
99
  std::string counter = name;
100
  // See if this counter name is already known.
101
  if (counter_table_.find(counter) != counter_table_.end())
102
    return counter_table_[counter];
103

    
104
  Counter* ctr = counters->GetNextCounter();
105
  if (ctr == NULL) return NULL;
106
  int* ptr = ctr->Bind(name);
107
  counter_table_[counter] = ptr;
108
  return ptr;
109
}
110

    
111

    
112
// Write C++ code that defines Snapshot::snapshot_ to contain the snapshot
113
// to the file given by filename. Only the first size chars are written.
114
static int WriteInternalSnapshotToFile(const char* filename,
115
                                       const v8::internal::byte* bytes,
116
                                       int size) {
117
  FILE* f = i::OS::FOpen(filename, "wb");
118
  if (f == NULL) {
119
    i::OS::PrintError("Cannot open file %s for reading.\n", filename);
120
    return 0;
121
  }
122
  fprintf(f, "// Autogenerated snapshot file. Do not edit.\n\n");
123
  fprintf(f, "#include \"v8.h\"\n");
124
  fprintf(f, "#include \"platform.h\"\n\n");
125
  fprintf(f, "#include \"snapshot.h\"\n\n");
126
  fprintf(f, "namespace v8 {\nnamespace internal {\n\n");
127
  fprintf(f, "const byte Snapshot::data_[] = {");
128
  int written = 0;
129
  written += fprintf(f, "0x%x", bytes[0]);
130
  for (int i = 1; i < size; ++i) {
131
    written += fprintf(f, ",0x%x", bytes[i]);
132
    // The following is needed to keep the line length low on Visual C++:
133
    if (i % 512 == 0) fprintf(f, "\n");
134
  }
135
  fprintf(f, "};\n\n");
136
  fprintf(f, "int Snapshot::size_ = %d;\n\n", size);
137
  fprintf(f, "} }  // namespace v8::internal\n");
138
  fclose(f);
139
  return written;
140
}
141

    
142

    
143
int main(int argc, char** argv) {
144
#ifdef ENABLE_LOGGING_AND_PROFILING
145
  // By default, log code create information in the snapshot.
146
  i::FLAG_log_code = true;
147
#endif
148
  // Print the usage if an error occurs when parsing the command line
149
  // flags or if the help flag is set.
150
  int result = i::FlagList::SetFlagsFromCommandLine(&argc, argv, true);
151
  if (result > 0 || argc != 2 || i::FLAG_help) {
152
    ::printf("Usage: %s [flag] ... outfile\n", argv[0]);
153
    i::FlagList::PrintHelp();
154
    return !i::FLAG_help;
155
  }
156

    
157
  v8::V8::SetCounterFunction(counter_callback);
158
  v8::HandleScope scope;
159

    
160
  const int kExtensionCount = 1;
161
  const char* extension_list[kExtensionCount] = { "v8/gc" };
162
  v8::ExtensionConfiguration extensions(kExtensionCount, extension_list);
163

    
164
  i::Serializer::Enable();
165
  v8::Context::New(&extensions);
166

    
167
  // Make sure all builtin scripts are cached.
168
  { HandleScope scope;
169
    for (int i = 0; i < i::Natives::GetBuiltinsCount(); i++) {
170
      i::Bootstrapper::NativesSourceLookup(i);
171
    }
172
  }
173
  // Get rid of unreferenced scripts with a global GC.
174
  i::Heap::CollectAllGarbage();
175
  i::Serializer ser;
176
  ser.Serialize();
177
  v8::internal::byte* bytes;
178
  int len;
179
  ser.Finalize(&bytes, &len);
180

    
181
  WriteInternalSnapshotToFile(argv[1], bytes, len);
182

    
183
  i::DeleteArray(bytes);
184

    
185
  return 0;
186
}