1 of 26

on small devices

~ shashank daté ~

stdate@gmail.com

twitter: @shanko

github.com/shanko

1

2 of 26

minimalistic Ruby (mruby.org)

  • lightweight Ruby
  • complying with part of the Ruby ISO standard
  • 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)

2

3 of 26

why?

  • embedding (ruby code in c)
  • linking (libmruby.a)
  • distribution (of compiled code)
  • age of “small” devices ( # IoT)

3

4 of 26

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,

4

5 of 26

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

Storage: microSD

GPIO: 40-pin header, populated

Ports: HDMI, 4 x USB 2.0,

5

6 of 26

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)

6

7 of 26

lightweight

  • size of the executable is small
  • has minimal dependencies on external libraries
  • uses less memory at runtime
  • is fast (enough)

7

8 of 26

size of the executable

  • $ size binary-file-name
  • $ strip or sstrip << to reduce the size
  • $ mruby-strip

8

9 of 26

minimal dependencies

  • $ ldd /path/to/binary
  • $ lsof -P -T -p Application_PID
  • $ objdump -p /path/to/binary | grep
  • ... many more

9

10 of 26

mem @ runtime

  • /usr/bin/time
  • pmap -x PID
  • cat /proc/PID/statm

10

11 of 26

install

pre-requisites:

  • git
  • gcc: build essentials
  • bison: parser generator
  • ruby: > 1.9.x

11

12 of 26

installation

  • git clone https://github.com/mruby/mruby.git
  • cd mruby
  • ./minirake
  • optionally modify PATH env variable

12

13 of 26

generates

binaries:

  • mruby = interpreter
  • mirb = interactive shell (REPL)
  • mrbc = compiler (byte code, symbol-table)
  • mrdb = debugger
  • mruby-strip = irep dump debug section remover (???)

13

14 of 26

TIMTOWDI

  • mirb (REPL)
  • mruby hello.rb (Interpreted)
  • compile using gcc
  • bytecode (.mrb)
  • bytecode (.c)

14

15 of 26

embedding (ruby in c)

/* hello.c */

#include "mruby.h"

#include "mruby/compile.h"intmain(void)�{� mrb_state *mrb = mrb_open();� if (!mrb) { printf(“handle error \n”); }� mrb_load_string(mrb, "puts 'hello world'");� mrb_close(mrb);�}

15

16 of 26

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

16

17 of 26

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

17

18 of 26

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);

}

18

19 of 26

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

19

20 of 26

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

20

21 of 26

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 (???)

21

22 of 26

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;�}

22

23 of 26

mruby vs. crystal

Crystal:

  • Ruby like syntax
  • Static typing
  • Type inference
  • Compiles to native binary
  • No REPL
  • No VM
  • Smaller / faster
  • Came out in Sept 2012
  • Current version 0.20.4 (version 1.0 in 2017 ??)

mRuby:

  • Ruby syntax
  • Dynamic typing
  • Duck typing
  • Compiles to bytecode
  • Has REPL (mirb)
  • VM is stack based
  • Bigger / slower
  • Came out in 2014
  • Current version 1.2

23

24 of 26

crystal example

require "http/client"

channel = Channel(String).new

spawn do

channel.send(HTTP::Client.get(

"https://crystal-lang.org/").body)

end

spawn do

channel.send(HTTP::Client.get(

"http://mruby.org").body)

end

2.times do

channel.receive

end

24

25 of 26

resources

mruby.org / lua.org / crystal-lang.org /

presentations by matz:

presentations by masayoshi takahashi & yurie yamane:

25

26 of 26

hack my pi

$ ssh pi@.....

Password: rubyconfindia

26