]> gitweb.factorcode.org Git - factor.git/blob - vm/image.cpp
io.streams.256color: faster by caching styles
[factor.git] / vm / image.cpp
1 #include "master.hpp"
2
3 namespace factor {
4
5 bool factor_arg(const vm_char* str, const vm_char* arg, cell* value) {
6   int val;
7   if (SSCANF(str, arg, &val) > 0) {
8     *value = val;
9     return true;
10   }
11   return false;
12 }
13
14 vm_parameters::vm_parameters() {
15   embedded_image = false;
16   image_path = NULL;
17   executable_path = NULL;
18
19   datastack_size = 32 * sizeof(cell);
20   retainstack_size = 32 * sizeof(cell);
21
22 #if defined(FACTOR_PPC)
23   callstack_size = 256 * sizeof(cell);
24 #else
25   callstack_size = 128 * sizeof(cell);
26 #endif
27
28   code_size = 96;
29   young_size = sizeof(cell) / 4;
30   aging_size = sizeof(cell) / 2;
31   tenured_size = 24 * sizeof(cell);
32
33   max_pic_size = 3;
34
35   fep = false;
36   signals = true;
37
38 #ifdef WINDOWS
39   console = GetConsoleWindow() != NULL;
40 #else
41   console = true;
42 #endif
43
44   callback_size = 256;
45 }
46
47 vm_parameters::~vm_parameters() {
48   free((vm_char *)image_path);
49   free((vm_char *)executable_path);
50 }
51
52 void vm_parameters::init_from_args(int argc, vm_char** argv) {
53   int i = 0;
54
55   for (i = 1; i < argc; i++) {
56     vm_char* arg = argv[i];
57     if (STRCMP(arg, STRING_LITERAL("--")) == 0)
58       break;
59     else if (factor_arg(arg, STRING_LITERAL("-datastack=%d"),
60                         &datastack_size))
61       ;
62     else if (factor_arg(arg, STRING_LITERAL("-retainstack=%d"),
63                         &retainstack_size))
64       ;
65     else if (factor_arg(arg, STRING_LITERAL("-callstack=%d"),
66                         &callstack_size))
67       ;
68     else if (factor_arg(arg, STRING_LITERAL("-young=%d"),
69                         &young_size))
70       ;
71     else if (factor_arg(arg, STRING_LITERAL("-aging=%d"),
72                         &aging_size))
73       ;
74     else if (factor_arg(arg, STRING_LITERAL("-tenured=%d"),
75                         &tenured_size))
76       ;
77     else if (factor_arg(arg, STRING_LITERAL("-codeheap=%d"),
78                         &code_size))
79       ;
80     else if (factor_arg(arg, STRING_LITERAL("-pic=%d"),
81                         &max_pic_size))
82       ;
83     else if (factor_arg(arg, STRING_LITERAL("-callbacks=%d"),
84                         &callback_size))
85       ;
86     else if (STRNCMP(arg, STRING_LITERAL("-i="), 3) == 0) {
87       // In case you specify -i more than once.
88       if (image_path) {
89         free((vm_char *)image_path);
90       }
91       image_path = safe_strdup(arg + 3);
92     }
93     else if (STRCMP(arg, STRING_LITERAL("-fep")) == 0)
94       fep = true;
95     else if (STRCMP(arg, STRING_LITERAL("-no-signals")) == 0)
96       signals = false;
97   }
98 }
99
100 void factor_vm::load_data_heap(FILE* file, image_header* h, vm_parameters* p) {
101   p->tenured_size = std::max((h->data_size * 3) / 2, p->tenured_size);
102
103   data_heap *d = new data_heap(&nursery,
104                                p->young_size, p->aging_size, p->tenured_size);
105   set_data_heap(d);
106   fixnum bytes_read =
107       raw_fread((void*)data->tenured->start, 1, h->data_size, file);
108
109   if ((cell)bytes_read != h->data_size) {
110     std::cout << "truncated image: " << bytes_read << " bytes read, ";
111     std::cout << h->data_size << " bytes expected\n";
112     fatal_error("load_data_heap failed", 0);
113   }
114
115   data->tenured->initial_free_list(h->data_size);
116 }
117
118 void factor_vm::load_code_heap(FILE* file, image_header* h, vm_parameters* p) {
119   if (h->code_size > p->code_size)
120     fatal_error("Code heap too small to fit image", h->code_size);
121
122   code = new code_heap(p->code_size);
123
124   if (h->code_size != 0) {
125     size_t bytes_read =
126         raw_fread((void*)code->allocator->start, 1, h->code_size, file);
127     if (bytes_read != h->code_size) {
128       std::cout << "truncated image: " << bytes_read << " bytes read, ";
129       std::cout << h->code_size << " bytes expected\n";
130       fatal_error("load_code_heap failed", 0);
131     }
132   }
133
134   code->allocator->initial_free_list(h->code_size);
135   code->initialize_all_blocks_set();
136 }
137
138 struct startup_fixup {
139   static const bool translated_code_block_map = true;
140
141   cell data_offset;
142   cell code_offset;
143
144   startup_fixup(cell data_offset, cell code_offset)
145       : data_offset(data_offset), code_offset(code_offset) {}
146
147   object* fixup_data(object* obj) {
148     return (object*)((cell)obj + data_offset);
149   }
150
151   code_block* fixup_code(code_block* obj) {
152     return (code_block*)((cell)obj + code_offset);
153   }
154
155   object* translate_data(const object* obj) {
156     return fixup_data((object*)obj);
157   }
158
159   code_block* translate_code(const code_block* compiled) {
160     return fixup_code((code_block*)compiled);
161   }
162
163   cell size(const object* obj) {
164     return obj->size(*this);
165   }
166
167   cell size(code_block* compiled) {
168     return compiled->size(*this);
169   }
170 };
171
172 void factor_vm::fixup_heaps(cell data_offset, cell code_offset) {
173   startup_fixup fixup(data_offset, code_offset);
174   slot_visitor<startup_fixup> visitor(this, fixup);
175   visitor.visit_all_roots();
176
177   auto start_object_updater = [&](object *obj, cell size) {
178     (void)size;
179     data->tenured->starts.record_object_start_offset(obj);
180     visitor.visit_slots(obj);
181     switch (obj->type()) {
182       case ALIEN_TYPE: {
183         alien* ptr = (alien*)obj;
184         if (to_boolean(ptr->base))
185           ptr->update_address();
186         else
187           ptr->expired = special_objects[OBJ_CANONICAL_TRUE];
188         break;
189       }
190       case DLL_TYPE: {
191         ffi_dlopen((dll*)obj);
192         break;
193       }
194       default: {
195         visitor.visit_object_code_block(obj);
196         break;
197       }
198     }
199   };
200   data->tenured->iterate(start_object_updater, fixup);
201
202   auto updater = [&](code_block* compiled, cell size) {
203     (void)size;
204     visitor.visit_code_block_objects(compiled);
205     cell rel_base = compiled->entry_point() - fixup.code_offset;
206     visitor.visit_instruction_operands(compiled, rel_base);
207   };
208   code->allocator->iterate(updater, fixup);
209 }
210
211 bool factor_vm::read_embedded_image_footer(FILE* file,
212                                            embedded_image_footer* footer) {
213   safe_fseek(file, -(off_t)sizeof(embedded_image_footer), SEEK_END);
214   safe_fread(footer, (off_t)sizeof(embedded_image_footer), 1, file);
215   return footer->magic == image_magic;
216 }
217
218 char *threadsafe_strerror(int errnum) {
219   char *buf = (char *) malloc(STRERROR_BUFFER_SIZE);
220   if (!buf) {
221     fatal_error("Out of memory in threadsafe_strerror, errno", errnum);
222   }
223   THREADSAFE_STRERROR(errnum, buf, STRERROR_BUFFER_SIZE);
224   return buf;
225 }
226
227 // Read an image file from disk, only done once during startup
228 // This function also initializes the data and code heaps
229 void factor_vm::load_image(vm_parameters* p) {
230
231   FILE* file = OPEN_READ(p->image_path);
232   if (file == NULL) {
233     std::cout << "Cannot open image file: " << AS_UTF8(p->image_path) << std::endl;
234     char *msg = threadsafe_strerror(errno);
235     std::cout << "strerror: " << msg << std::endl;
236     free(msg);
237     exit(1);
238   }
239   if (p->embedded_image) {
240     embedded_image_footer footer;
241     if (!read_embedded_image_footer(file, &footer)) {
242       std::cout << "No embedded image" << std::endl;
243       exit(1);
244     }
245     safe_fseek(file, (off_t)footer.image_offset, SEEK_SET);
246   }
247
248   image_header h;
249   if (raw_fread(&h, sizeof(image_header), 1, file) != 1)
250     fatal_error("Cannot read image header", 0);
251
252   if (h.magic != image_magic)
253     fatal_error("Bad image: magic number check failed", h.magic);
254
255   if (h.version != image_version)
256     fatal_error("Bad image: version number check failed", h.version);
257
258   load_data_heap(file, &h, p);
259   load_code_heap(file, &h, p);
260
261   raw_fclose(file);
262
263   // Certain special objects in the image are known to the runtime
264   memcpy(special_objects, h.special_objects, sizeof(special_objects));
265
266   cell data_offset = data->tenured->start - h.data_relocation_base;
267   cell code_offset = code->allocator->start - h.code_relocation_base;
268   fixup_heaps(data_offset, code_offset);
269 }
270
271 // Save the current image to disk. We don't throw any exceptions here
272 // because if the 'then-die' argument is t it is not safe to do
273 // so. Instead we signal failure by returning false.
274 bool factor_vm::save_image(const vm_char* saving_filename,
275                            const vm_char* filename) {
276   image_header h = {};
277
278   h.magic = image_magic;
279   h.version = image_version;
280   h.data_relocation_base = data->tenured->start;
281   h.data_size = data->tenured->occupied_space();
282   h.code_relocation_base = code->allocator->start;
283   h.code_size = code->allocator->occupied_space();
284
285   for (cell i = 0; i < special_object_count; i++)
286     h.special_objects[i] =
287         (save_special_p(i) ? special_objects[i] : false_object);
288
289   FILE* file = OPEN_WRITE(saving_filename);
290   if (file == NULL)
291     return false;
292   if (safe_fwrite(&h, sizeof(image_header), 1, file) != 1)
293     return false;
294   if (h.data_size > 0 &&
295       safe_fwrite((void*)data->tenured->start, h.data_size, 1, file) != 1)
296     return false;
297   if (h.code_size > 0 &&
298       safe_fwrite((void*)code->allocator->start, h.code_size, 1, file) != 1)
299     return false;
300   if (raw_fclose(file) == -1)
301     return false;
302   if (!move_file(saving_filename, filename))
303     return false;
304   return true;
305 }
306
307 // Allocates memory
308 void factor_vm::primitive_save_image() {
309   // We unbox this before doing anything else. This is the only point
310   // where we might throw an error, so we have to throw an error here since
311   // later steps destroy the current image.
312   bool then_die = to_boolean(ctx->pop());
313   byte_array* path2 = untag_check<byte_array>(ctx->pop());
314   byte_array* path1 = untag_check<byte_array>(ctx->pop());
315
316   // Copy the paths to non-gc memory to avoid them hanging around in
317   // the saved image.
318   vm_char* path1_saved = safe_strdup(path1->data<vm_char>());
319   vm_char* path2_saved = safe_strdup(path2->data<vm_char>());
320
321   if (then_die) {
322     // strip out special_objects data which is set on startup anyway
323     for (cell i = 0; i < special_object_count; i++)
324       if (!save_special_p(i))
325         special_objects[i] = false_object;
326
327     // dont trace objects only reachable from context stacks so we don't
328     // get volatile data saved in the image.
329     active_contexts.clear();
330     code->uninitialized_blocks.clear();
331
332     // I think clearing the callback heap should be fine too.
333     callbacks->allocator->initial_free_list(0);
334   }
335
336   // do a full GC to push everything remaining into tenured space
337   primitive_compact_gc();
338
339   // Save the image
340   bool ret = save_image(path1_saved, path2_saved);
341   if (then_die) {
342     exit(ret ? 0 : 1);
343   }
344   free(path1_saved);
345   free(path2_saved);
346
347   if (!ret) {
348     general_error(ERROR_IO, tag_fixnum(errno), false_object);
349   }
350 }
351
352 bool factor_vm::embedded_image_p() {
353   const vm_char* vm_path = vm_executable_path();
354   FILE* file = OPEN_READ(vm_path);
355   if (!file) {
356     free((vm_char *)vm_path);
357     return false;
358   }
359   embedded_image_footer footer;
360   bool embedded_p = read_embedded_image_footer(file, &footer);
361   fclose(file);
362   free((vm_char *)vm_path);
363   return embedded_p;
364 }
365
366 }