Upgrade to Pro — share decks privately, control downloads, hide ads and more …

mRuby on small devices

Shashank
November 16, 2016

mRuby on small devices

Presentation given at Ruby Conf 2016 on Friday 11/11/2016. Watch video: http://confreaks.tv/videos/rubyconf2016-m-ruby-on-small-devices

Shashank

November 16, 2016
Tweet

More Decks by Shashank

Other Decks in Programming

Transcript

  1. minimalistic Ruby (mruby.org) • lightweight Ruby • complying with part

    of the Ruby ISO standard • sponsored by the Regional Innovation Creation R&D Programs of the Ministry of Economy, Trade and Industry of Japan. • mRuby the language, mruby the interpreter 2
  2. mRuby • ISO/IEC 30170:2012 - Ruby • mRuby 1.2 (36

    classes, 7 modules) < Ruby 2.3 (105 classes, 19 modules) • syntax is 1.9 compatible (conceived before Ruby 2.0, in ~ 2010) • MIT license 3
  3. why? • for embedding and linking • age of “small”

    devices ( # IoT) • because Matz is interested (MINSWAN :) 4
  4. lightweight • uses less memory at runtime • size of

    the executable is small • consumes less CPU cycles (i.e. fast) • has minimal dependencies on external libraries 5
  5. raspberry pi (fun!) Single Board Computer - Raspberry Pi Zero:

    ($5) CPU: ARM 11, 1GHz - 32-bit single core GPU: Broadcom VideoCore IV RAM: 512MB LPDDR2 (900 MHz) Networking: none Bluetooth: none Storage: microSD GPIO: 40-pin header, unpopulated Ports: mini HDMI, micro USB 2.0, 6
  6. raspberry pi (fun!) Single Board Computer - Raspberry Pi 3:

    ($35) CPU: ARM Cortex A53, 1.2 GHz, 64-bit quadcore GPU: Broadcom VideoCore IV RAM: 1 GB SDRAM (900 MHz) Networking: 10/100 Mbits Ethernet, 802.11n Wifi Bluetooth: 4.1 Storage: microSD GPIO: 40-pin header, populated Ports: HDMI, 4 x USB 2.0, 7
  7. verifone (profit!) Vx570 Processor • 200 MHz ARM9 32-bit RISC

    microprocessor Memory • 6 MB (4 MB of Flash, 2 MB of SRAM) Display • 128 x 64 pixel graphical LCD with backlighting; Supports 8 lines x 21characters Magnetic Card Reader • Triple-track (tracks 1, 2, 3), high coercivity , bi-directional Keypad • 3 x 4 numeric keypad, plus 8 soft-function keys and 4 screen addressable keys; • PCI approved I/O Modules • 14.4k modem module; Ethernet and 14.4k modem combination module Peripheral Ports • One USB 1.1 port supports flash memory devices; • Two RS-232 ports support peripherals including PIN pads and check readers; • One telecom port and one Ethernet (with optional Ethernet/14.4 I/O module) support communications Security • SSL v3.0, 3DES encryption, Master/Session and DUKPT key management; VeriShield file authentication) 8
  8. embedding (ruby in c) /* hello.c */ #include "mruby.h" #include

    "mruby/compile.h" int main(void) { mrb_state *mrb = mrb_open(); if (!mrb) { printf(“handle error \n”); } mrb_load_string(mrb, "puts 'hello world'"); mrb_close(mrb); } 10
  9. linking $ gcc -std=c99 \ <<< compiler options -I /path/to/include/mruby

    \ <<< include headers hello.c \ <<< program file -lm /path/to/build/host/lib/libmruby.a \ <<< linking -o hello <<< executable 11
  10. install pre-requisites: • git • gcc: build essentials • bison:

    parser generator • ruby • gperf: GNU perfect hash function generator 12
  11. generates binaries: • mruby = interpreter • mirb = interactive

    shell (repl) • mrbc = compiler (byte code, symbol-table) • mrdb = debugger • mruby-strip = irep dump debug section remover (???) 14
  12. embedding (bytecode in c) $ cat hello.rb puts "hello world"

    $ mrbc hello.rb # <<<< produces byte-coded hello.mrb $ hexdump -c hello.mrb 0000000 R I T E 0 0 0 3 � � \0 \0 \0 e M A 0000010 T Z 0 0 0 0 I R E P \0 \0 \0 G 0 0 $ mruby -b hello.mrb hello world 15
  13. embedding (symbol table) $ cat hello.rb puts "hello world" $

    mrbc -B hw_symbol hello.rb ^^^^ produces symbol table in hello.c $ cmp hello_world ^^^^ magic shell script to compile $ ./hello_world hello world /* hello_world.c */ #include "mruby.h" #include "mruby/irep.h" #include "hello.c" int main(void) { mrb_state *mrb = mrb_open(); if (!mrb) {/* handle error */} mrb_load_irep(mrb, hw_symbol); mrb_close(mrb); } 16
  14. magic shell script (cmp) $ cat ~/bin/cmp #!/bin/bash -x gcc

    -std=c99 \ -I /$HOME/mruby/include $1.c \ $HOME/mruby/build/host/lib/libmruby.a \ -lm \ -o $1 17
  15. customize mruby mrbgems: library manager to integrate C and Ruby

    extensions ###### build_config.rb conf.gem '/path/to/your/gem/dir' conf.gem :git => 'https://github.com/masuidrive/mrbgems-example.git', :branch => 'master' conf.gem :github => 'masuidrive/mrbgems-example', :branch => 'master' conf.gem :bitbucket => 'mruby/mrbgems-example', :branch => 'master' ## from mgem-list use :mgem option conf.gem :mgem => 'mruby-yaml' conf.gem :mgem => 'json' # 'mruby-' prefix could be omitted 18
  16. mruby vs. lua Lua: - prototype based (like Javascript) -

    no classes or inheritance - simple (nil, boolean, number, string, function, thread, table) - compiles to bytecode - has stack-based C API - VM is register based - smaller / faster - came out in 1993 - has luajit mRuby: - class based - single inheritance - complex ( ~ 36 classes ) - compiles to bytecode - has stack-based C API - VM is stack based - bigger / slower - came out in 2014 - mruby jit (???) 19
  17. lua example #include <stdio.h> #include "lua.h" #include "lualib.h" #include "lauxlib.h"

    /* the Lua interpreter */ lua_State* L; int main ( int argc, char *argv[] ) { /* initialize Lua */ L = lua_open(); /* load Lua base libraries */ lua_baselibopen(L); /* run the script */ lua_dofile(L, "hello.lua"); /* cleanup Lua */ lua_close(L); return 0; } 20
  18. resources mruby.org / lua.org presentations by matz: • https://www.youtube.com/watch?v=n7XRYWclYDY •

    https://www.youtube.com/watch?v=sB-IifjyeLI • https://www.youtube.com/watch?v=5FLrKg-b6o8 presentations by masayoshi takahashi & yurie yamane: • mruby RTOS: https://www.youtube.com/watch?v=HuPOizo6pSE • mruby robots: https://www.youtube.com/watch?v=7qWZ5w5v7Eg • mruby debugger: https://www.youtube.com/watch?v=VXQYSa2jjLw 21