Implement snapshot functionality.
JerryScript-DCO-1.0-Signed-off-by: Ruben Ayrapetyan r.ayrapetyan@samsung.com JerryScript-DCO-1.0-Signed-off-by: Andrey Shitov a.shitov@samsung.com
This commit is contained in:
committed by
Ruben Ayrapetyan
parent
7481fb606f
commit
311cc65b33
@@ -14,6 +14,7 @@
|
||||
*/
|
||||
|
||||
#include "lit-id-hash-table.h"
|
||||
#include "lit-literal-storage.h"
|
||||
#include "bytecode-data.h"
|
||||
|
||||
/** \addtogroup jsparser ECMAScript parser
|
||||
@@ -122,6 +123,203 @@ lit_id_hash_table_lookup (lit_id_hash_table *table_p, /**< table's header */
|
||||
return table_p->buckets[block_id][uid];
|
||||
} /* lit_id_hash_table_lookup */
|
||||
|
||||
/**
|
||||
* Dump literal identifiers hash table to snapshot buffer
|
||||
*
|
||||
* @return number of bytes dumper - upon success,
|
||||
* 0 - upon failure
|
||||
*/
|
||||
uint32_t
|
||||
lit_id_hash_table_dump_for_snapshot (uint8_t *buffer_p, /**< buffer to dump to */
|
||||
size_t buffer_size, /**< buffer size */
|
||||
size_t *in_out_buffer_offset_p, /**< in-out: buffer write offset */
|
||||
lit_id_hash_table *table_p, /**< hash table to dump */
|
||||
const lit_mem_to_snapshot_id_map_entry_t *lit_map_p, /**< map from literal
|
||||
* identifiers in
|
||||
* literal storage
|
||||
* to literal offsets
|
||||
* in snapshot */
|
||||
uint32_t literals_num, /**< number of literals */
|
||||
vm_instr_counter_t instrs_num) /**< number of instructions in corresponding
|
||||
* byte-code array */
|
||||
{
|
||||
size_t begin_offset = *in_out_buffer_offset_p;
|
||||
|
||||
uint32_t idx_num_total = (uint32_t) table_p->current_bucket_pos;
|
||||
JERRY_ASSERT (idx_num_total == table_p->current_bucket_pos);
|
||||
|
||||
if (!jrt_write_to_buffer_by_offset (buffer_p, buffer_size, in_out_buffer_offset_p, idx_num_total))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t blocks_num = JERRY_ALIGNUP (instrs_num, BLOCK_SIZE) / BLOCK_SIZE;
|
||||
|
||||
for (size_t block_index = 0, next_block_index;
|
||||
block_index < blocks_num;
|
||||
)
|
||||
{
|
||||
uint32_t idx_num_in_block;
|
||||
|
||||
next_block_index = block_index + 1u;
|
||||
|
||||
while (next_block_index < blocks_num
|
||||
&& table_p->buckets[next_block_index] == NULL)
|
||||
{
|
||||
next_block_index++;
|
||||
}
|
||||
|
||||
if (next_block_index != blocks_num)
|
||||
{
|
||||
idx_num_in_block = (uint32_t) (table_p->buckets[next_block_index] - table_p->buckets[block_index]);
|
||||
}
|
||||
else if (table_p->buckets[block_index] != NULL)
|
||||
{
|
||||
idx_num_in_block = (uint32_t) (table_p->current_bucket_pos
|
||||
- (size_t) (table_p->buckets[block_index] - table_p->buckets[0]));
|
||||
}
|
||||
else
|
||||
{
|
||||
idx_num_in_block = 0;
|
||||
}
|
||||
|
||||
if (!jrt_write_to_buffer_by_offset (buffer_p, buffer_size, in_out_buffer_offset_p, idx_num_in_block))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (size_t block_idx_pair_index = 0;
|
||||
block_idx_pair_index < idx_num_in_block;
|
||||
block_idx_pair_index++)
|
||||
{
|
||||
lit_cpointer_t lit_cp = table_p->buckets[block_index][block_idx_pair_index];
|
||||
|
||||
uint32_t lit_index;
|
||||
for (lit_index = 0; lit_index < literals_num; lit_index++)
|
||||
{
|
||||
if (lit_map_p[lit_index].literal_id.packed_value == lit_cp.packed_value)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
JERRY_ASSERT (lit_index < literals_num);
|
||||
|
||||
uint32_t offset = lit_map_p[lit_index].literal_offset;
|
||||
if (!jrt_write_to_buffer_by_offset (buffer_p, buffer_size, in_out_buffer_offset_p, offset))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
while (++block_index < next_block_index)
|
||||
{
|
||||
idx_num_in_block = 0;
|
||||
|
||||
if (!jrt_write_to_buffer_by_offset (buffer_p, buffer_size, in_out_buffer_offset_p, idx_num_in_block))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
size_t bytes_written = (*in_out_buffer_offset_p - begin_offset);
|
||||
|
||||
JERRY_ASSERT (bytes_written == (uint32_t) bytes_written);
|
||||
return (uint32_t) bytes_written;
|
||||
} /* lit_id_hash_table_dump_for_snapshot */
|
||||
|
||||
/**
|
||||
* Load literal identifiers hash table from specified snapshot buffer
|
||||
*
|
||||
* @return true - upon successful load (i.e. data in snapshot is consistent),
|
||||
* false - upon failure (in case, snapshot is incorrect)
|
||||
*/
|
||||
bool
|
||||
lit_id_hash_table_load_from_snapshot (size_t blocks_count, /**< number of byte-code blocks
|
||||
* in corresponding byte-code array */
|
||||
uint32_t idx_num_total, /**< total number of (byte-code block, idx)
|
||||
* pairs in snapshot */
|
||||
const uint8_t *idx_to_lit_map_p, /**< idx-to-lit map in snapshot */
|
||||
size_t idx_to_lit_map_size, /**< size of the map */
|
||||
const lit_mem_to_snapshot_id_map_entry_t *lit_map_p, /**< map of in-snapshot
|
||||
* literal offsets
|
||||
* to literal identifiers,
|
||||
* created in literal
|
||||
* storage */
|
||||
uint32_t literals_num, /**< number of literals in snapshot */
|
||||
uint8_t *buffer_for_hash_table_p, /**< buffer to initialize hash table in */
|
||||
size_t buffer_for_hash_table_size) /**< size of the buffer */
|
||||
{
|
||||
lit_id_hash_table *hash_table_p = lit_id_hash_table_init (buffer_for_hash_table_p,
|
||||
buffer_for_hash_table_size,
|
||||
idx_num_total,
|
||||
blocks_count);
|
||||
|
||||
size_t idx_to_lit_map_offset = 0;
|
||||
uint32_t idx_num_counter = 0;
|
||||
for (size_t block_idx = 0; block_idx < blocks_count; block_idx++)
|
||||
{
|
||||
uint32_t idx_num_in_block;
|
||||
if (!jrt_read_from_buffer_by_offset (idx_to_lit_map_p,
|
||||
idx_to_lit_map_size,
|
||||
&idx_to_lit_map_offset,
|
||||
&idx_num_in_block))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
hash_table_p->buckets[block_idx] = hash_table_p->raw_buckets + hash_table_p->current_bucket_pos;
|
||||
|
||||
if (idx_num_counter + idx_num_in_block < idx_num_counter)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
idx_num_counter += idx_num_in_block;
|
||||
|
||||
if (idx_num_counter > idx_num_total)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for (uint32_t idx_in_block = 0; idx_in_block < idx_num_in_block; idx_in_block++)
|
||||
{
|
||||
uint32_t lit_offset_from_snapshot;
|
||||
if (!jrt_read_from_buffer_by_offset (idx_to_lit_map_p,
|
||||
idx_to_lit_map_size,
|
||||
&idx_to_lit_map_offset,
|
||||
&lit_offset_from_snapshot))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO: implement binary search here
|
||||
*/
|
||||
lit_cpointer_t lit_cp = lit_cpointer_t::null_cp ();
|
||||
uint32_t i;
|
||||
for (i = 0; i < literals_num; i++)
|
||||
{
|
||||
if (lit_map_p[i].literal_offset == lit_offset_from_snapshot)
|
||||
{
|
||||
lit_cp.packed_value = lit_map_p[i].literal_id.packed_value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (i == literals_num)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
JERRY_ASSERT (hash_table_p->current_bucket_pos < idx_num_total);
|
||||
hash_table_p->raw_buckets[hash_table_p->current_bucket_pos++] = lit_cp;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
} /* lit_id_hash_table_load_from_snapshot */
|
||||
|
||||
|
||||
/**
|
||||
* @}
|
||||
* @}
|
||||
|
||||
@@ -33,5 +33,19 @@ size_t lit_id_hash_table_get_size_for_table (size_t, size_t);
|
||||
void lit_id_hash_table_free (lit_id_hash_table *);
|
||||
void lit_id_hash_table_insert (lit_id_hash_table *, vm_idx_t, vm_instr_counter_t, lit_cpointer_t);
|
||||
lit_cpointer_t lit_id_hash_table_lookup (lit_id_hash_table *, vm_idx_t, vm_instr_counter_t);
|
||||
|
||||
uint32_t lit_id_hash_table_dump_for_snapshot (uint8_t *buffer_p,
|
||||
size_t buffer_size,
|
||||
size_t *in_out_buffer_offset_p,
|
||||
lit_id_hash_table *table_p,
|
||||
const lit_mem_to_snapshot_id_map_entry_t *lit_map_p,
|
||||
uint32_t literals_num,
|
||||
vm_instr_counter_t instrs_num);
|
||||
bool lit_id_hash_table_load_from_snapshot (size_t blocks_count,
|
||||
uint32_t idx_num_total,
|
||||
const uint8_t *idx_to_lit_map_p,
|
||||
size_t idx_to_lit_map_size,
|
||||
const lit_mem_to_snapshot_id_map_entry_t *lit_map_p,
|
||||
uint32_t literals_num,
|
||||
uint8_t *buffer_for_hash_table_p,
|
||||
size_t buffer_for_hash_table_size);
|
||||
#endif /* LIT_ID_HASH_TABLE */
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "lit-id-hash-table.h"
|
||||
#include "serializer.h"
|
||||
#include "bytecode-data.h"
|
||||
#include "pretty-printer.h"
|
||||
@@ -343,3 +344,152 @@ serializer_free (void)
|
||||
mem_heap_free_block (header_p);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef JERRY_ENABLE_SNAPSHOT
|
||||
/**
|
||||
* Dump byte-code and idx-to-literal map to snapshot
|
||||
*
|
||||
* @return true, upon success (i.e. buffer size is enough),
|
||||
* false - otherwise.
|
||||
*/
|
||||
bool
|
||||
serializer_dump_bytecode_with_idx_map (uint8_t *buffer_p, /**< buffer to dump to */
|
||||
size_t buffer_size, /**< buffer size */
|
||||
size_t *in_out_buffer_offset_p, /**< in-out: buffer write offset */
|
||||
const bytecode_data_header_t *bytecode_data_p, /**< byte-code data */
|
||||
const lit_mem_to_snapshot_id_map_entry_t *lit_map_p, /**< map from literal
|
||||
* identifiers in
|
||||
* literal storage
|
||||
* to literal offsets
|
||||
* in snapshot */
|
||||
uint32_t literals_num, /**< literals number */
|
||||
uint32_t *out_bytecode_size_p, /**< out: size of dumped instructions array */
|
||||
uint32_t *out_idx_to_lit_map_size_p) /**< out: side of dumped
|
||||
* idx to literals map */
|
||||
{
|
||||
JERRY_ASSERT (bytecode_data_p->next_header_cp == MEM_CP_NULL);
|
||||
|
||||
vm_instr_counter_t instrs_num = bytecode_data_p->instrs_count;
|
||||
|
||||
const size_t instrs_array_size = sizeof (vm_instr_t) * instrs_num;
|
||||
if (*in_out_buffer_offset_p + instrs_array_size > buffer_size)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
memcpy (buffer_p + *in_out_buffer_offset_p, bytecode_data_p->instrs_p, instrs_array_size);
|
||||
*in_out_buffer_offset_p += instrs_array_size;
|
||||
|
||||
*out_bytecode_size_p = (uint32_t) (sizeof (vm_instr_t) * instrs_num);
|
||||
|
||||
lit_id_hash_table *lit_id_hash_p = MEM_CP_GET_POINTER (lit_id_hash_table, bytecode_data_p->lit_id_hash_cp);
|
||||
uint32_t idx_to_lit_map_size = lit_id_hash_table_dump_for_snapshot (buffer_p,
|
||||
buffer_size,
|
||||
in_out_buffer_offset_p,
|
||||
lit_id_hash_p,
|
||||
lit_map_p,
|
||||
literals_num,
|
||||
instrs_num);
|
||||
|
||||
if (idx_to_lit_map_size == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
*out_idx_to_lit_map_size_p = idx_to_lit_map_size;
|
||||
|
||||
return true;
|
||||
}
|
||||
} /* serializer_dump_bytecode_with_idx_map */
|
||||
|
||||
/**
|
||||
* Register bytecode and idx map from snapshot
|
||||
*
|
||||
* NOTE:
|
||||
* If is_copy flag is set, bytecode is copied from snapshot, else bytecode is referenced directly
|
||||
* from snapshot
|
||||
*
|
||||
* @return pointer to byte-code header, upon success,
|
||||
* NULL - upon failure (i.e., in case snapshot format is not valid)
|
||||
*/
|
||||
const bytecode_data_header_t *
|
||||
serializer_load_bytecode_with_idx_map (const uint8_t *bytecode_and_idx_map_p, /**< buffer with instructions array
|
||||
* and idx to literals map from
|
||||
* snapshot */
|
||||
uint32_t bytecode_size, /**< size of instructions array */
|
||||
uint32_t idx_to_lit_map_size, /**< size of the idx to literals map */
|
||||
const lit_mem_to_snapshot_id_map_entry_t *lit_map_p, /**< map of in-snapshot
|
||||
* literal offsets
|
||||
* to literal identifiers,
|
||||
* created in literal
|
||||
* storage */
|
||||
uint32_t literals_num, /**< number of literals */
|
||||
bool is_copy) /** flag, indicating whether the passed in-snapshot data
|
||||
* should be copied to engine's memory (true),
|
||||
* or it can be referenced until engine is stopped
|
||||
* (i.e. until call to jerry_cleanup) */
|
||||
{
|
||||
const uint8_t *idx_to_lit_map_p = bytecode_and_idx_map_p + bytecode_size;
|
||||
|
||||
size_t instructions_number = bytecode_size / sizeof (vm_instr_t);
|
||||
size_t blocks_count = JERRY_ALIGNUP (instructions_number, BLOCK_SIZE) / BLOCK_SIZE;
|
||||
|
||||
uint32_t idx_num_total;
|
||||
size_t idx_to_lit_map_offset = 0;
|
||||
if (!jrt_read_from_buffer_by_offset (idx_to_lit_map_p,
|
||||
idx_to_lit_map_size,
|
||||
&idx_to_lit_map_offset,
|
||||
&idx_num_total))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const size_t bytecode_alloc_size = JERRY_ALIGNUP (bytecode_size, MEM_ALIGNMENT);
|
||||
const size_t hash_table_size = lit_id_hash_table_get_size_for_table (idx_num_total, blocks_count);
|
||||
const size_t header_and_hash_table_size = JERRY_ALIGNUP (sizeof (bytecode_data_header_t) + hash_table_size,
|
||||
MEM_ALIGNMENT);
|
||||
const size_t alloc_size = header_and_hash_table_size + (is_copy ? bytecode_alloc_size : 0);
|
||||
|
||||
uint8_t *buffer_p = (uint8_t*) mem_heap_alloc_block (alloc_size, MEM_HEAP_ALLOC_LONG_TERM);
|
||||
bytecode_data_header_t *header_p = (bytecode_data_header_t *) buffer_p;
|
||||
|
||||
vm_instr_t *instrs_p;
|
||||
vm_instr_t *snapshot_instrs_p = (vm_instr_t *) bytecode_and_idx_map_p;
|
||||
if (is_copy)
|
||||
{
|
||||
instrs_p = (vm_instr_t *) (buffer_p + header_and_hash_table_size);
|
||||
memcpy (instrs_p, snapshot_instrs_p, bytecode_size);
|
||||
}
|
||||
else
|
||||
{
|
||||
instrs_p = snapshot_instrs_p;
|
||||
}
|
||||
|
||||
uint8_t *lit_id_hash_table_buffer_p = buffer_p + sizeof (bytecode_data_header_t);
|
||||
if (lit_id_hash_table_load_from_snapshot (blocks_count,
|
||||
idx_num_total,
|
||||
idx_to_lit_map_p + idx_to_lit_map_offset,
|
||||
idx_to_lit_map_size - idx_to_lit_map_offset,
|
||||
lit_map_p,
|
||||
literals_num,
|
||||
lit_id_hash_table_buffer_p,
|
||||
hash_table_size)
|
||||
&& (vm_instr_counter_t) instructions_number == instructions_number)
|
||||
{
|
||||
MEM_CP_SET_NON_NULL_POINTER (header_p->lit_id_hash_cp, lit_id_hash_table_buffer_p);
|
||||
header_p->instrs_p = instrs_p;
|
||||
header_p->instrs_count = (vm_instr_counter_t) instructions_number;
|
||||
MEM_CP_SET_POINTER (header_p->next_header_cp, first_bytecode_header_p);
|
||||
|
||||
first_bytecode_header_p = header_p;
|
||||
|
||||
return header_p;
|
||||
}
|
||||
else
|
||||
{
|
||||
mem_heap_free_block (buffer_p);
|
||||
return NULL;
|
||||
}
|
||||
} /* serializer_load_bytecode_with_idx_map */
|
||||
|
||||
#endif /* JERRY_ENABLE_SNAPSHOT */
|
||||
|
||||
@@ -44,4 +44,26 @@ void serializer_rewrite_op_meta (vm_instr_counter_t, op_meta);
|
||||
void serializer_remove_bytecode_data (const bytecode_data_header_t *);
|
||||
void serializer_free (void);
|
||||
|
||||
#endif // SERIALIZER_H
|
||||
#ifdef JERRY_ENABLE_SNAPSHOT
|
||||
/*
|
||||
* Snapshot-related
|
||||
*/
|
||||
bool serializer_dump_bytecode_with_idx_map (uint8_t *buffer_p,
|
||||
size_t buffer_size,
|
||||
size_t *in_out_buffer_offset_p,
|
||||
const bytecode_data_header_t *bytecode_data_p,
|
||||
const lit_mem_to_snapshot_id_map_entry_t *lit_map_p,
|
||||
uint32_t literals_num,
|
||||
uint32_t *out_bytecode_size_p,
|
||||
uint32_t *out_idx_to_lit_map_size_p);
|
||||
|
||||
const bytecode_data_header_t *
|
||||
serializer_load_bytecode_with_idx_map (const uint8_t *bytecode_and_idx_map_p,
|
||||
uint32_t bytecode_size,
|
||||
uint32_t idx_to_lit_map_size,
|
||||
const lit_mem_to_snapshot_id_map_entry_t *lit_map_p,
|
||||
uint32_t literals_num,
|
||||
bool is_copy);
|
||||
#endif /* JERRY_ENABLE_SNAPSHOT */
|
||||
|
||||
#endif /* SERIALIZER_H */
|
||||
|
||||
Reference in New Issue
Block a user