Tizen/Artik IoT Lecture Chapter 4. JerryScript ECMA ......JerryScript ECMA Internal and Memory...
Transcript of Tizen/Artik IoT Lecture Chapter 4. JerryScript ECMA ......JerryScript ECMA Internal and Memory...
Embedded Software Lab. @ SKKU
25
1
Sungkyunkwan University
Tizen/Artik IoT Lecture Chapter 4.JerryScript ECMA Internal and
Memory Management
Embedded Software Lab. @ SKKU
25
2
• ECMAScript
– ECMAScript: Script-language specification standardized by ECMA International in ECMA-262
– JerryScript is full ECMA-262 edition 5.1 compatible
JerryScript and ECMAScript Overview
<JerryScript>
ECMA-262 provides definitions of operation and data representation
Every JavaScript engine satisfies ECMA requirements in their own way
Embedded Software Lab. @ SKKU
25
3Heap Memory Architecture
• Heap memory consists of maximum 64K chunks (512KB)
• 8 byte alloc & free– Alloc from pool Alloc from heap
– Free to pool Free to heap
• > 8 byte alloc & free– Alloc from heap
– Free to heap
Heap area (512KB – 8Byte)
Free list head ( 8byte )
heap region representation
size +
next free block
offsetdata
free block
(8-byte aligned)
in-use block
(8-byte aligned)
Heap Size : 512KB Max
Chunk pool
8-byte
Free
Chunk
8-byte
Free
Chunk
8-byte
Free
Chunk
Alloc block (8 byte)
Use Free Chunk
Free
pool
Alloc ChunkAlloc block (>8 byte)
Use
Free block
MAX(16bit)
Embedded Software Lab. @ SKKU
25
4
• When pointer operations occur, compressed pointer will be decompressed and compressed
ECMA Compressed Pointer (Compressing)
Compressing Algorithm
1. Make sure the Decompressed Address is 8Bytes aligned2. Decompressed Address -= heap Start Address3. Decompressed Address >>= 3
0x158BBE8 - 0x1587BE8 = 0x4000
0x158BBE8 % 8=0
0x4000 >> 3 = 0x800
8Bytes Alignment
4-byte
Memory Address
2-byte
Compressed Pointer
Base Address0x1587BE8 ECMA
element
(8 byte)
Heap (512KB or 65536 ECMA elements)
0x158BBE8
Chunk Offset
0x800
Embedded Software Lab. @ SKKU
25
5ECMA Compressed Pointer (Decompressing)
1. Make sure the compressed Address is 8Bytes aligned2. Compressed Address <<= 3 3. Compressed Address += heapstart
0x800 % 8 = 0decompressing Address
0x800 << 3 = 0x40000x4000 + 0x1587BE8 = 0x158BBE8
4-byte
Memory Address
2-byte
Compressed Pointer
Base Address0x1587BE8
ECMA
element
(8 byte)
Heap (512KB or 65536 ECMA elements)
0x158BBE8 Chunk Offset
0x800
0x158BBE8
Embedded Software Lab. @ SKKU
25
6Function Call Routine
Ecma_alloc_##type Ecma_alloc_property_pair()
Jmem_pools_alloc()
Jmem_heap_alloc_block()
Jmem_heap_gc_and_alloc_block()
Jmem_run_free_unused_memory_callbacks()
Ecma_gc_run()
Ecma_gc_mark() Ecma_gc_sweep()
Jmem_pools_collect_empty()
Jmem_heap_alloc_block_internal()
Allocation routine
GC routine
Embedded Software Lab. @ SKKU
25
7
• Fast Path for 8 byte block (A chunk)
– Every free region is guaranteed to be sufficient for 8 byte chunk Just alloc from first free region (No need to check the region size)
• Slow Path
– Check each free region if it is sufficient for required memory size Required Memory size (8-byte aligned) <= Free region size
• If a free region first-fitted with required size is found, split required-sized block from the region.
Jmem_heap_alloc_block_internal
Free list head ( 8byte ) Free list head ( 8byte )
Return required block
Need this block!
Embedded Software Lab. @ SKKU
25
8
• When a heap block is freed, each free region in heap should be checked if it is able to be merged.
– Check the neighbor free region of the block to be freed(Previous region and Next region)
• Lookup the neighbor free region
– “Next block of previously freed block would be freed successively”
– Linear Search from first OR from previously freed region
Jmem_heap_free_block
Free list head ( 8byte )Free this block!
Free list head ( 8byte )
skip_pfirst skip_p
Merged
Embedded Software Lab. @ SKKU
25
9
• ECMA component of the engine is responsible for the following notions
– Data representation { Object, Number, String, Simple }
– Runtime representation { Hashing, Lcache, Property Lookup, etc … }
– Garbage collection (GC)
• ECMA Value Representation
ECMA Representation
Compressed Pointer
Encoded Value
16bit
29bit
Embedded Software Lab. @ SKKU
25
10
• Mostly ECMA values stores the compressed pointer which points the object, number, string
ECMA Value Structure
Compressed Pointer
ECMA Object
ECMA String
ECMA Number
16bit
Embedded Software Lab. @ SKKU
25
11
• Statically assigned string data stores in .rodata section
• Dynamically allocated string data stores in heap– The static string data which located in JavaScript file is stored in literal storage at parsing
phase
– The dynamic string data is allocated in jerry heap
• In order to reduce the memory overhead and computational overhead, jerry has a magic id
String Representation
ECMA String
Literal Number
magic_string_idUnio
n
e.g var num = 295731;
e.g var str =‘eslab’;
Embedded Software Lab. @ SKKU
25
12String Structure
Initialized Parsing Phase
Insert Literal Storage node
Jerry Heap
Magic ID String
85465 Seminar
4662 ESLAB
<Magic ID and String mapping>
Embedded Software Lab. @ SKKU
25
13
• Jerryscript has two kinds of number representation
– 4-byte (Compact Profile)
– 8-byte (Full Profile)
– Small value in 29bit (ECMA_value section)
Number Representation
Compressed Pointer
Small Integer
Embedded Software Lab. @ SKKU
25
14
• Object Types & Flags– Types : 1. ecma_object, 2. ecma_lexical_environment
– Flags : built-in Object, lexical environment, GC visited, Extensible
– Reference-count : 10bit Reference Count for GC
Object Representation
For Embedded APIPointing to Native Function
Extension
Built-in Objects(Built-in Object)
Function Object(Lexical Environment)
Ecma_object
Lexical environment
Embedded Software Lab. @ SKKU
25
15
• Each property’s metadata needs 72bit data structure – In order to improve the CPU efficiency, jerry makes property pair (128bit)
Object and Property Representation
Linked List
Compressed Pointer ECMA Object
var obj = { var lemon; var apple; };
Embedded Software Lab. @ SKKU
25
16
• Property hashmap is allocated in following condition.– namedaccessor and nameddata property can be stored in hashmap
– Property hashmap is dynamically allocated when the object has the number of properties more than 16 (Property Pair : 8)
– The default # of properties is 32 entries
– If the remainder of hash entries is under # of properties/8, jerry dynamically reallocates the bigger size of the hashmap
• Property hashmap is allocated as a first entry in property linked-list
Property Hashmap cont’
Embedded Software Lab. @ SKKU
25
17
• The Hash function is FNV-1 algorithm– FNV hashes are designed to be fast while maintaining a low collision rate.
– The end of two bytes (two character) of property name as a hashing key
Property Hashmap
var obj = { var lemon; var apple; };
FNV-1FNV-1
ECMA String
Embedded Software Lab. @ SKKU
25
18
• Property Hashmap Structure
Property Hashmap
ECMA Object ECMA String
111000100000 ….
Embedded Software Lab. @ SKKU
25
19
• In order to find a property efficiently, jerry has global object-property mapping table (4KBytes)– Property Find Algorithm
Lcache
Find Property
Property in Lcache?
Property in Hashmap?
Dictionary Lookup
Return Property
Insert Property in Lcache
No
No
Yes
Yes
Even if hash allocation threshold (16 # of Properties) is too high, Lcache can improves performance
Embedded Software Lab. @ SKKU
25
20
• Lcache map size
– Row Length : 2, # of Row : 128
– (32bit + 16bit + 16bit)*2 (Row Length)* 128 (# of Row) = 4KBytes
– Lcache statically allocated in global section (.bss)
Lcache Structure
Embedded Software Lab. @ SKKU
25
21
• Indexing : Object address ^ Hash Value
Lcache Operation
Compressed Pointer
16bit
ExculsiveOr
<Lcache>Row entry & Hashing Index
Embedded Software Lab. @ SKKU
25
22
• If collision occurs, use LRU eviction policy– Same hash index property need to be inserted
– lcache[index][1] = lcache[index][0]; move to old
– lcache[index][0] = new property;
• if the property located in old block is accessed, then promoted to young block
Lcache Policy
Row entry & Hashing Index
12
Row entry & Hashing Index
2 1 Eviction
3
Row entry & Hashing Index
1
<only one block> <block is full> <promotion>
Access2
Promotion
Embedded Software Lab. @ SKKU
25
23
• Memory reclamation triggered– At every 8KB allocation & At out of memory situation
• Memory Reclamation ( Garbage Collection)1. (GC with lower severity)
• Invoked only if new objects are sufficiently allocated after last GC“Most of garbage collected is newly created post the previous GC cycle”
• Not free property hashmap
2. Reclaim free chunks in the pool(pool is only for performance)
3. GC with higher severity
• Free property hashmap
Memory Reclamation
jmem_pools_collect_empty()
Embedded Software Lab. @ SKKU
25
24
• Tri-color marking
– White (Unvisited): Not referenced by a live object or the reference not found yet.
– Gray (Visited): Referenced by some live object
– Black (Marked): Visited all the references of the object
Garbage Collection (Mark)
WHITE GRAY BLACK
Object
Visit Ref.cnt > 0 object
Ref.cnt increased in object creation or object copy.
Ref.cnt decreased when object life-cycle ends
Object
Visit the objects referenced by this object
(Insert them into GRAY)
Object
Object
Object
Object
Object
Object
Object
Object Object
Object
Object
Object
Embedded Software Lab. @ SKKU
25
25
• After marking objects,– In white
• Garbage objects remain
– In gray• No objects
– In black• Live objects remain
• Sweep all white objects and black objects transformed to white objects for next GC.– Sweep: Free the object (jmem_heap_free_block invoked)
Garbage Collection (Sweep)