Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Shrink to grow

2,195 views

Published on

Presentation for RubyKaigi 2013 in Tokyo.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Shrink to grow

  1. 1. Daniel BovensiepenShrink To Grow
  2. 2. mruby
  3. 3. m for ...
  4. 4. m for ...minimal
  5. 5. Binary Size< 100kB
  6. 6. Memory Footprint< 40kB RAM
  7. 7. m for ...
  8. 8. m for ...embedded
  9. 9. https://github.com/matsumoto-r/mod_mrubymod_mruby
  10. 10. LoadModule mruby_module modules/mod_mruby.soAddHandler mruby-script .rbbackends = ["http://192.168.0.101:8888/","http://192.168.0.102:8888/","http://192.168.0.103:8888/","http://192.168.0.104:8888/",]# write balancing algorithm here.r = Apache::Request.new()r.handler = "proxy-server"r.proxyreq = Apache::PROXYREQ_REVERSEr.filename = "proxy:" +backends[rand(backends.length)] +r.uriApache::return(Apache::OK)https://github.com/matsumoto-r/mod_mrubymod_mruby
  11. 11. https://github.com/spiritloose/sqlite3ext-mrubysqlite3ext-mruby
  12. 12. $ cat test.rbdef fib(n)n < 2 ? n : fib(n - 1) + fib(n - 2)endcreate_function :fib do |n|fib(n)end$ LD_LIBRARY_PATH=. sqlite3sqlite> .load libsqlite3ext_mruby.so.1.0.0;sqlite> SELECT mrb_load(test.rb);sqlite> SELECT fib(30);832040sqlite>https://github.com/spiritloose/sqlite3ext-mrubysqlite3ext-mruby
  13. 13. EmbedArangoDB , Redis,mobiruby, Android, SMCArangoDB: http://www.arangodb.orgRedis: https://github.com/antirez/redis/pull/848mobiruby (iOS): http://mobiruby.orgAndroid: http://podtynnyi.com/2012/11/29/build-mruby-for-android/SMC: http://www.secretmaryo.org, https://github.com/Quintus/SMC
  14. 14. m for ...
  15. 15. m for ...modular
  16. 16. Add/Remove Features(i.e. Fiber, stdio, ObjectSpace, etc.)Standard Library
  17. 17. $ ./bin/mirbmirb - Embeddable Interactive Ruby ShellThis is a very early version, please test and reporterrors.Thanks :)> ThreadNameError: uninitialized constant Thread> FiberNameError: uninitialized constant Fiber> eval "puts hello world"NotImplementedError: eval not implemented> print "Test"NotImplementedError: print not available> ObjectSpaceNameError: uninitialized constant ObjectSpace> require fileNoMethodError: undefined method require for mainAdd/Remove Features(i.e. Fiber, stdio, ObjectSpace, etc.)Standard Library
  18. 18. RegExpOniguruma & Henry Spencer EngineOverview: http://blog.mruby.sh/201302190729.htmlOniguruma: http://www.geocities.jp/kosako3/oniguruma/Henry Spencer Engine: http://www.arglist.com/regex
  19. 19. Howis this possible?
  20. 20. Implementationrewritten in C99
  21. 21. ImplementationISO 30170:2012
  22. 22. EXECUTINGRUBY CODEFive Ways to execute mruby: http://blog.mruby.sh/201207020720.htmlmrbc, mruby and mirb
  23. 23. Build SystemMakefile, cmake and ...
  24. 24. RakeBuild System
  25. 25. MRuby::Build.new do |conf|# load specific toolchain settingstoolchain :gcc# include the default GEMsconf.gembox defaultend# Define cross build settingsMRuby::CrossBuild.new(32bit) do |conf|toolchain :gccconf.cc.flags << "-m32"conf.linker.flags << "-m32"endRakeBuild System
  26. 26. mrbgems
  27. 27. mrbgemsmgem is like RubyGem but incompatible
  28. 28. mrbgems
  29. 29. mrbgemsmruby.c CC mruby
  30. 30. mrbgemsmruby.c CC mrubymgem activate+mgem +Fiber(i.e. Fiber)
  31. 31. Whatcan we do now?
  32. 32. mruby on Arduino
  33. 33. C++ CodeArduino
  34. 34. C++ CodeArduino// Pin 13 has an LED connected on most Arduino boards.// give it a name:int led = 13;// the setup routine runs once when you press reset:void setup() {// initialize the digital pin as an output.pinMode(led, OUTPUT);}// the loop routine runs over and over again forever:void loop() {digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)delay(1000); // wait for a seconddigitalWrite(led, LOW); // turn the LED off by making the voltage LOWdelay(1000); // wait for a second}
  35. 35. WorkflowArduino
  36. 36. WorkflowArduinocode.cpp
  37. 37. WorkflowArduinocode.cpp a.binCross-CC
  38. 38. WorkflowArduinocode.cpp a.binCross-CC .Upload
  39. 39. Cross-Compile
  40. 40. Atmel SAM3X8E (ARM Cortex-M3 based)Cross-Compile
  41. 41. Atmel SAM3X8E (ARM Cortex-M3 based)Cross-CompileMRuby::CrossBuild.new("Arduino Due") do |conf|toolchain :gccARDUINO_PATH = /home/daniel/Downloads/arduino-1.5.2BIN_PATH = "#{ARDUINO_PATH}/hardware/tools/g++_arm_none_eabi/bin"SAM_PATH = "#{ARDUINO_PATH}/hardware/arduino/sam"TARGET_PATH = "#{SAM_PATH}/variants/arduino_due_x"conf.cc do |cc|cc.command = "#{BIN_PATH}/arm-none-eabi-gcc"cc.include_paths = ["#{SAM_PATH}/system/libsam -I#{SAM_PATH}/system/CMSIS/CMSIS/Include/","#{SAM_PATH}/system/CMSIS/Device/ATMEL/","#{SAM_PATH}/cores/arduino -I#{TARGET_PATH}","#{MRUBY_ROOT}/include"]cc.flags << -Os -w -ffunction-sections -fdata-sections -nostdlib +--param max-inline-insns-single=500 -Dprintf=iprintf +-mcpu=cortex-m3 -DF_CPU=84000000L -DARDUINO=152 +-D__SAM3X8E__ -mthumb -DUSB_PID=0x003e -DUSB_VID=0x2341 -DUSBCONcc.compile_options = "%{flags} -o %{outfile} -c %{infile}"endconf.archiver do |archiver|archiver.command = "#{BIN_PATH}/arm-none-eabi-ar"archiver.archive_options = rcs %{outfile} %{objs}endconf.bins = []end
  42. 42. Arduino EcosystemEmbed
  43. 43. Arduino EcosystemEmbed#include "Arduino.h"#include "../mruby_src/include/mruby.h"mrb_state *mrb;/* Init phase of Microcontroller */voidsetup() { }/* Process Loop */voidloop() {mrb = mrb_open();/* Use mruby here */mrb_close(mrb);}
  44. 44. Arduino Ecosystem (API)Embed
  45. 45. Arduino Ecosystem (API)Embed/* ... */mrb_valuemrb_ar(mrb_state *mrb, mrb_value self) {mrb_int pin;mrb_get_args(mrb, "i", &pin);return mrb_fixnum_value(analogRead(pin));}/* ... *//* Integrate into mruby */RClass *am = mrb_define_module(mrb, "Arduino");mrb_define_module_function(mrb, am, "analog_read", mrb_ar, ARGS_REQ(1));/* ... */
  46. 46. Shrink
  47. 47. available 96kB SRAM and 512kB FlashShrink
  48. 48. available 96kB SRAM and 512kB FlashShrink0160003200048000640008000096000112000128000144000160000nothing mrb_open wrd box nan box flt seglist heap khash mrblib ALL (-flt -wrd) ALL (-nan)Image (bytes) Free Memory (bytes) Execution Time (µs)
  49. 49. Shrink96kB SRAM available (~31kB used)512kb Flash available (~86kB used)
  50. 50. Workflow (mruby)Arduino
  51. 51. Workflow (mruby)Arduinomruby.c
  52. 52. Workflow (mruby)Arduinomruby.cmruby¹(host)cc
  53. 53. Workflow (mruby)Arduinomruby.cmruby¹(host)ccmruby²(target)cross-cc
  54. 54. Workflow (mruby)Arduinocode.rbmruby.cmruby¹(host)ccmruby²(target)cross-cc
  55. 55. Workflow (mruby)Arduinocode.rbarray.c(Array)mruby.cmruby¹(host)ccmruby²(target)cross-ccmrbc¹
  56. 56. Workflow (mruby)Arduinocode.rbarray.c(Array)code.cppmruby.cmruby¹(host)ccmruby²(target)cross-ccmrbc¹ integrate
  57. 57. Workflow (mruby)Arduinocode.rbarray.c(Array)code.cppmruby.cmruby¹(host)ccmruby²(target)code.cpp+mruby²cross-ccmrbc¹ integrate
  58. 58. Workflow (mruby)Arduinocode.rbarray.c(Array)code.cppmruby.cmruby¹(host)ccmruby²(target)code.cpp+mruby²a.bincross-ccmrbc¹ integratecross-cc
  59. 59. Workflow (mruby)Arduinocode.rbarray.c(Array)code.cppmruby.cmruby¹(host)ccmruby²(target)code.cpp+mruby²a.bin .cross-ccmrbc¹ integratecross-cc upload
  60. 60. Workflow (mruby)Arduinocode.rbarray.c(Array)code.cppmruby.cmruby¹(host)ccmruby²(target)code.cpp+mruby²a.bin .cross-ccmrbc¹ integratecross-cc uploadOMG
  61. 61. Port mirb to Arduino DueArduino
  62. 62. Port mirb to Arduino DueArduino.enter .rb
  63. 63. Interactive Arduino Shell (LED Switch)Arduino
  64. 64. Interactive Arduino Shell (3D Printer)Arduino
  65. 65. Coming Next“Webruby”byXuejie Xiao
  66. 66. THX@bovensiepenhttp://mruby.sh

×