Introduction to IoT.js

Tilmann Scheller Principal Compiler Engineer [email protected]

Samsung Open Source Group Samsung Research UK

OpenIoT Summit 2016 San Diego, USA, April 4 – 6, 2016

Samsung Open Source Group 1 Overview

● Introduction

● JerryScript

● IoT.js

● Memory consumption/Performance

● Demo

● Future work

● Summary

Samsung Open Source Group 2 Introduction

Samsung Open Source Group 3 What is IoT.js?

● A lightweight version of Node.js

● Attempt to bring the success of Node.js to the embedded world

● Retains backwards compatibility with Node.js as much as possible

● Runs on top of JerryScript

Samsung Open Source Group 4 IoT Hardware

http://postscapes.com/internet-of-things-hardware Samsung Open Source Group 5 What is JerryScript?

● A really lightweight JavaScript engine

● Has a base footprint of 10KB of RAM

● Optimized for microcontrollers

● Developed from scratch by Samsung

Samsung Open Source Group 6 Why JavaScript on microcontrollers?

● There's a huge pool of JavaScript developers

● Opens up the possibility for web developers to easily write for embedded devices

● Performance overhead of JavaScript less of an issue for control tasks

● Increased productivity, shorter time to market

● Ability to load code dynamically over the network

● Security: Executing JavaScript code is safer than executing arbitrary native code

Samsung Open Source Group 7 Open source

● Actively developed on GitHub

● JerryScript and IoT.js are both open source released under the Apache 2.0 license

● Building up a community around IoT.js/JerryScript

Samsung Open Source Group 8 JerryScript

Samsung Open Source Group 9 JerryScript History

● Development started in June 2014

● Released as open source in June 2015

● JerryScript passed 100% of the test262 conformance test suite in August 2015

● Rewritten compact byte code implementation landed in January 2016

● Current focus on performance optimization

Samsung Open Source Group 10 JerryScript

● Heavily optimized for a low memory footprint

● Interpreter-only

● Compact object representation

● Compressed pointers

● No AST, directly creating byte code

● Compact byte code heavily optimized for low memory consumption

Samsung Open Source Group 11 JerryScript Portability

● Extremely portable

● Self-contained

● Small library

● Can run bare-metal

● Supports the STM32F4, ESP8266 boards

● Runs on Linux/OS X as well

Samsung Open Source Group 12 JerryScript

● Written in C99

● About 74KLOC

● Code size ~190KB when compiled with GCC LTO for ARM Thumb-2

● Implements the entire ECMAScript 5.1 standard, passes 100% of the test262 conformance test suite

● C API for embedding JerryScript

● Byte code snapshot feature

Samsung Open Source Group 13 JerryScript C API

Samsung Open Source Group 14 JerryScript C API

Samsung Open Source Group 15 IoT.js

Samsung Open Source Group 16 IoT.js

● Lightweight version of Node.js

● Focus on resource-constrained devices

● Tries to retain backwards compatibility

● Mostly written in JavaScript

Samsung Open Source Group 17 IoT.js Architecture

Samsung Open Source Group 18 IoT.js Modules

● Assert ● Net

● Buffer ● Modules

● DNS ● Process

● Events ● Stream

● File System ● Timers

● HTTP ● GPIO

Samsung Open Source Group 19 Target hardware

● STM32F4 developer board

● Cortex-M4F clocked at 168 MHz

● 192KB of RAM

● 1MB of flash memory

Samsung Open Source Group 20 NuttX

● Real-time

● Open source (BSD license)

● Scales from 8-bit microcontrollers to 32-bit microcontrollers

● Supports the STM32F4 developer board

Samsung Open Source Group 21 libtuv

● libuv - Support library for asynchronous I/O

● libtuv - Stripped down version of Libuv

● Runs on NuttX and Linux

● Experimental support for mbed OS

Samsung Open Source Group 22 Memory consumption/ Performance

Samsung Open Source Group 23 IoT.js - Memory consumption

Test name IoT.js Node.js require('http') 40KB 6.70MB test_httpserver.js 80KB 8.63MB

Measured on a Raspberry Pi 2

Samsung Open Source Group 24 SunSpider 1.0.2 - Memory consumption

172 string-fasta 44 324 string-base64 152 164 math-spectral-norm 32 160 math-partial-sums 28 164 math-cordic 32 220 date-format-xparb 52 572 date-format-tofte 60 304 crypto-sha1 112 420 crypto-md5 152 268 crypto-aes 104 JerryScript 204 controlflow-recursive 136 Duktape 1.4 384 bitops-nsieve-bits 148 160 bitops-bitwise-and 24 160 bitops-bits-in-byte 24 160 bitops-3bit-bits-in-byte 24 172 access-nbody 36 160 access-fannkuch 32 240 access-binary-trees 76 216 3d-cube 76 0 100 200 300 400 500 600 700 Max RSS in KB (lower is better) Measured on a Raspberry Pi 2

Samsung Open Source Group 25 SunSpider 1.0.2 - Performance

6.46 string-fasta 3.21 5.95 string-base64 5.08 1.13 math-spectral-norm 1.46 3.73 math-partial-sums 1.57 3.62 math-cordic 2.83 2.23 date-format-xparb 0.83 4.64 date-format-tofte 2.09 1.93 crypto-sha1 2.07 2.08 crypto-md5 3.29 2.58 crypto-aes 3.36 JerryScript 1.51 controlflow-recursive 0.95 Duktape 1.4 4.52 bitops-nsieve-bits 17.88 10.75 bitops-bitwise-and 3.27 2.73 bitops-bits-in-byte 2.39 2.42 bitops-3bit-bits-in-byte 1.62 2.23 access-nbody 2.81 3.4 access-fannkuch 7.38 1.78 access-binary-trees 1.36 1.44 3d-cube 2.54 0 2 4 6 8 10 12 14 16 18 20 Execution time in seconds (lower is better) Measured on a Raspberry Pi 2

Samsung Open Source Group 26 ubench - Memory consumption

496 function-closure 32

156 function-correct-args 24

156 function-emtpy 24

156 function-excess-args 24

156 function-missing-args 24 JerryScript Duktape 1.4 156 function-sum 24

152 loop-empty 24

152 loop-empty-resolve 24

152 loop-sum 24

0 100 200 300 400 500 600 Max RSS in KB (lower is better) Measured on a Raspberry Pi 2

Samsung Open Source Group 27 ubench - Performance

18.87 function-closure 2.62

27.95 function-correct-args 22.18

38.89 function-emtpy 19.66

27.93 function-excess-args 20.85

27.35 function-missing-args 16.09 JerryScript Duktape 1.4 23.93 function-sum 18.05

55.23 loop-empty 30.14

6.41 loop-empty-resolve 3.42

76.65 loop-sum 35.48

0 10 20 30 40 50 60 70 80 90 Execution time in seconds (lower is better) Measured on a Raspberry Pi 2

Samsung Open Source Group 28 Demo

Samsung Open Source Group 29 Tetris Demo

● Implementation of the classic Tetris game

● Each device drives one LED matrix as display

● Implemented as a Node.js module

● Both devices are running exactly the same JavaScript code

Samsung Open Source Group 30 Demo

LED Matrix LED Matrix

I2C I2C

Raspberry Pi 2 STM32F4 board (1GB RAM, 8GB Flash) (192KB RAM, 1MB Flash)

Tetris Tetris Node.js IoT.js V8 JerryScript Linux NuttX

USB Keypad Switches via GPIO

Samsung Open Source Group 31 Tetris Demo

https://youtu.be/q4iBeOAC7XI

Samsung Open Source Group 32 Pong Demo

● Implementation of the classic Pong game

● Display shared across two devices

● Each device drives one LED matrix

● Implemented as a Node.js module

● "AI" oppenent running on the microcontroller

Samsung Open Source Group 33 Demo

LED Matrix LED Matrix

I2C I2C

Raspberry Pi 2 STM32F4 board (1GB RAM, 8GB Flash) (192KB RAM, 1MB Flash)

Pong Client Pong Server Node.js IoT.js V8 Ethernet JerryScript Linux NuttX

USB Keypad

Samsung Open Source Group 34 Pong Demo

Samsung Open Source Group 35 Future work

Samsung Open Source Group 36 Future work

● Close performance gap between JerryScript and Duktape

● Further performance and memory optimizations

● Enhance JerryScript C API

● Add more IoT.js modules

● Package manager for IoT.js

● Support more devices

Samsung Open Source Group 37 Summary

Samsung Open Source Group 38 Summary

● Significantly lowers barrier of entry for JavaScript development targeting heavily constrained embedded devices

● Speeds up development

● Active community

● More information on http://www.jerryscript.net and http://www.iotjs.net

● Looking for bug reports and feedback

Samsung Open Source Group 39 Thank you.

Samsung Open Source Group 40 Contact Information:

Tilmann Scheller [email protected]

Samsung Open Source Group Samsung Research UK

Samsung Open Source Group 41