This document discusses running JavaScript on microcontrollers. It evaluates two JavaScript engines: JavaScriptCore and Duktape. JavaScriptCore was too large, with initial attempts resulting in binaries over 2 MB. Duktape produced smaller binaries down to 108 KB and passed 96.5% of ECMAScript tests. While most SunSpider tests failed due to timeouts or memory limits, Duktape shows promise for running JavaScript on resource-constrained microcontroller devices.
08448380779 Call Girls In Civil Lines Women Seeking Men
JavaScript on Microcontrollers: Running JS on Embedded Devices
1. JavaScript on Microcontrollers
Samsung Open Source Group 1
Tilmann Scheller
LLVM Compiler Engineer
t.scheller@samsung.com
Samsung Open Source Group
Samsung Research UK
2nd Samsung Web Tech Talk
San Jose, USA, October 30, 2014
2. Overview
● Introduction
● JavaScript engine code size
● JavaScript engine correctness/performance
● Summary
Samsung Open Source Group 2
4. 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!
Samsung Open Source Group 4
5. Target hardware
● Cortex-M3 microcontroller (32-bit ARM Thumb-2)
● 96KB of RAM
● 512KB of flash memory
● Clocked at 84MHz
Samsung Open Source Group 5
6. 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
Samsung Open Source Group 6
7. 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
Samsung Open Source Group 7
8. 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++
Samsung Open Source Group 8
9. 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
Samsung Open Source Group 9
11. 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
Samsung Open Source Group 11
12. 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.
Samsung Open Source Group 12
13. 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
Samsung Open Source Group 13
14. 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
Samsung Open Source Group 14
15. Code size
Static binary
Engine
Libraries
108
161
100
83
70
59
212
187
127
318
655
790
1207
311
0 200 400 600 800 1000 1200 1400 1600 1800
Samsung Open Source Group 15
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
17. 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
Samsung Open Source Group 17
18. test262
85 713
288957
Min Avg Max
350000
300000
250000
200000
150000
100000
50000
0
Execution time (ms)
Samsung Open Source Group 18
Min Avg Max
80
70
60
50
40
30
20
10
0
54
57
75
Memory usage (KB)
19. 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)
Samsung Open Source Group 19
22. 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
Samsung Open Source Group 22
24. We are hiring!
Contact Information:
Tilmann Scheller
t.scheller@samsung.com
Samsung Open Source Group
Samsung Research UK
Samsung Open Source Group 24