js on-microcontrollers
DESCRIPTION
2nd Samsung Web Tech TalkTRANSCRIPT
1Samsung Open Source Group
JavaScript on Microcontrollers
Tilmann SchellerLLVM Compiler [email protected]
Samsung Open Source GroupSamsung Research UK
2nd Samsung Web Tech TalkSan Jose, USA, October 30, 2014
2Samsung Open Source Group
Overview
● Introduction
● JavaScript engine code size
● JavaScript engine correctness/performance
● Summary
3Samsung Open Source Group
Introduction
4Samsung Open Source Group
Introduction
● Wearable devices becoming more and more widespread
● Weight and energy efficiency a big concern
● JavaScript very popular, easy to learn
● Scales from embedded to servers
● Execute JavaScript on a microcontroller!
5Samsung Open Source Group
Target hardware
● Cortex-M3 microcontroller (32-bit ARM Thumb-2)
● 96KB of RAM
● 512KB of flash memory
● Clocked at 84MHz
6Samsung Open Source Group
JS Engines
● Goal: JS engine which fits in the microcontroller constraints
● Option one: Pick an established JS engine and strip it down to fit on a microcontroller
● Option two: Pick a lightweight engine and make sure that it meets all the requirements
7Samsung Open Source Group
JS Engines
● Two approaches pursued in parallel:
– Try to minimize JavaScriptCore so that it fits on a microcontroller
– Get the lightweight Duktape engine running on a microcontroller
8Samsung Open Source Group
JavaScriptCore
● Well-established JS engine running on millions of devices
● High-performance (four distinct levels of optimization)
● Never intended to run in resource-constrained environments, primary focus is maximum performance
● Written in C++
9Samsung Open Source Group
Duktape
● Lightweight JS engine
● Focus on portability and a low footprint
● Written in C and about 40k lines of code (excluding comments)
● ECMAScript E5/E5.1 compliant
● Liberal license
● Active community, has been around for several years
10Samsung Open Source Group
Code size
11Samsung Open Source Group
JavaScriptCore minimization
● Challenge: JavaScriptCore was never intended for environments like this
● First attempt:
– Took r170522 (June 26, 2014) of WebKit and built the EFL configuration for Thumb-2
● Original (3.63 MB)● Reduced (2.39 MB)● Static (21.21 MB)
– Several MB of heap memory consumed during runtime
12Samsung Open Source Group
JavaScriptCore minimization
● Current snapshot: way too big!
● Second try: Use a snapshot from 2008 just before SquirrelFish was merged
● Performed minimizations:
– Qt dependency was removed
– Reduced the size of ICU data by disabling extra features
– Rebuilt ICU with the –Os compiler option
– Pthread was removed
– Built older ICU libs
– Stripped static JSC binary is now 1,647 KB (was: 19,578 KB)
– The engine part of is 311 KB
– The Unicode libraries are 562 Kbyte and the remaining size comes from libc, libm and libstdc++ libraries.
13Samsung Open Source Group
JavaScriptCore minimization
● Engine size close to target size
● However, libraries rely on too many system calls to make it feasible to run on a baremetal system
● Too much effort to get JSC working on a baremetal system
● Aborting JSC investigation and directing all efforts towards Duktape
14Samsung Open Source Group
Duktape
● Compiled for Thumb-2
● Using the newlib C standard library
● No OS running on the microcontroller
● Statically linked binary
● C library has a big impact on the size of the executable
● Disabling certain optimizations (through preprocessor macros), helps to reduce code size even further
15Samsung Open Source Group
Code size
Libraries
Engine
Static binary
0 200 400 600 800 1000 1200 1400 1600 1800
59
83
187
70
100
212
127
161
318
655
108
790
1207
311
1647
Duktape Thumb-2 baremetal noopt
Duktape Thumb-2 baremetal
Duktape ARM baremetal
Duktape ARM Linux glibc
JSC Thumb-2 Linux glibc
KB
Compiled at -Os ..., noopt = Duktape optimizations not compiled in,baremetal builds link against Newlib
16Samsung Open Source Group
Correctness/Performance
17Samsung Open Source Group
Correctness - test262
● Duktape 0.12 and Arduino library 1.5.8, without DUK_OPT defines
● test262 (ECMAScript conformance test suite) results:
– Ran 11824 tests● Passed 11411 tests (96.5%)● Failed 413 tests (3.5%)
● Failure details:
– 144 tests ran out of memory
– 10 tests timed out (300 secs)
– 90 tests failed because of an undefined gettimeofday() function
– 169 tests fail because of other reasons (have to be investigated)
– Summary: 167 failures specific to the Arduino Due and 246 tests which fail on x86-64 as well
18Samsung Open Source Group
test262
Min Avg Max0
50000
100000
150000
200000
250000
300000
350000
85 713
288957
Execution time (ms)
Min Avg Max0
10
20
30
40
50
60
70
80
5457
75
Memory usage (KB)
19Samsung Open Source Group
SunSpider
● Duktape 0.12 and Arduino library 1.5.8, without DUK_OPT defines
● Ran 26 tests
– Passed 9 tests (34.6%)
– Failed 17 tests (65.4%)
● Failure details:
– 3 tests were too big (string-unpack-code.js (164K), string-tagcloud.js (172K), regexp-dna.js (400K))
– 13 tests run out of memory (3d-cube.js, 3d-morph.js, 3d-raytrace.js, access-binary-trees.js, access-nsieve.js, bitops-nsieve-bits.js, crypto-aes.js, crypto-md5.js, crypto-sha1.js, date-format-tofte.js, date-format-xparb.js, string-base64.js, string-validate-input.js)
– 1 test failed because of undefined gettimeofday() function (math-cordic.js)
20Samsung Open Source Group
SunSpider
access-fannkuchaccess-nbody
bitops-3bit-bits-in-bytebitops-bits-in-byte
bitops-bitwise-andcontrolflow-recursive
math-partial-sumsmath-spectral-norm
string-fasta
0
20
40
60
80
100
120
140
160149
41.5
64.3
74.8
140.2
25.9
62.3
21.8
119.9
54
64
50 49 50
70
51 5357
Execution time (s)
Memory usage (KB)
21Samsung Open Source Group
Summary
22Samsung Open Source Group
Summary
● JavaScriptCore proved to be unsuitable for microcontrollers
● Duktape fits well into flash and main memory
● Duktape has promising correctness results
● Ongoing effort to evaluate Duktape performance
Thank you.
23Samsung Open Source Group
24Samsung Open Source Group
We are hiring!
Contact Information:
Tilmann [email protected]
Samsung Open Source GroupSamsung Research UK