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.
Using Erlang in an Embedded and
Cross-Compiled World
Frank Hunleth
Twitter: @fhunleth
ErlangDC R13B 12/7/13
I'm an embedded C/C++ programmer and Erlang/OTP
has been this nice shiny apple that's been tempting me
for a long time.
Why?
1. Software reliability (supervision and “let it
crash philosophy”)
2. Message passing
3. Simple and robust
interface...
I wanted to start small...
●

●

●

●

http://www.youtube.com/watch?v=QpP1hfEOO00

But Erlang/OTP didn't work
that way for...
Agenda
●
●

●
●

●

Baseline embedded systems of interest
Demo of a simple cross-compiled Erlang/OTP
release
Breakdown of ...
What Kind of Embedded?
Limited resource/
Specific purpose

General
purpose
Microcontrollers

RTOS

Embedded Linux Android,...
Demo Demo Demo
The Big Picture

Erlang/OTP
applications

rebar

BEAMs and crosscompiled binaries

Erlang/OTP release
relx

fwtool + scrip...
End Product - The Firmware Image
Master Boot Record
Bootloaders
Root Filesystem A

Linux kernel
Erlang
Libraries
Main appl...
Quick View of the Base Root FS
/
├──
├──
├──
├──
├──
├──
├──
│
├──
├──
│
│
│
│
│
│
│
│
│
│
│
│
└──

bin
boot
dev
etc
lib
s...
Building the Base Root FS
●

●

●

●

●

●

Use Buildroot
Cross-compiler built
for free
Huge selection of recipes
for cros...
RootFS (16.8 MB)

erlangdc_demo release

Non-Erlang libraries

ext4 overhead

/usr/lib/erlang

Linux kernel

Non-Erlang bi...
Installed Applications (5.5MB)

stdlib-1.19.2

kernel-2.16.2

erlangdc_demo-1

cowboy-0.9.0

gproc-0.2.17-1-g45a17ac

jsx-...
System Initialization - erlinit
●

Observations
–

–

Erlang/OTP is pretty good at supervision

–
●

Main responsibilities...
Example Release Directory
/srv/erlang
├── lib
│
├── my_app-0.0.1
│
│
├── ebin
│
│
└── priv
│
├── kernel-2.16.2
│
│
└── ebi...
Boot Time (7.4 seconds)

SPL 0.33

u-boot (1s user timeout)

2.83

Uncompress Linux kernel

0.95

Boot kernel

1.52

erlin...
The Big Picture

Erlang/OTP
applications

rebar

BEAMs and crosscompiled binaries

Erlang/OTP release
relx

fwtool + scrip...
Back to the Demo
erlangdc_demo/
├── config
│
└── vm.args
├── priv
│
├── index.html
│
└── static
│
├── demo.css
│
├── image...
Configuration Files
rebar.config
{deps, [
{cowboy, ".*", {git, "https://github.com/extend/cowboy.git", "master"}},
{jsx, "...
Building
fhunleth@halfmarathon:~/nerves/nerves-sdk$ source ./nerves-env.sh
Nerves environment updated for this session
fhu...
Copying to the SDCard
sdcard.img

●

Traditional Unix way
–
–

●

sudo dd if=sdcard.img of=/dev/sdc bs=1M
Bit for bit copy...
The Big Picture

Erlang/OTP
applications

rebar

BEAMs and crosscompiled binaries

Erlang/OTP release
relx

fwtool + scrip...
Things I Don't Know How to Do Yet
●

Improve the development work flow
–

Need to shorten edit/compile/test time

–

Curre...
Using Erlang in an Embedded and
Cross-Compiled World
Frank Hunleth
fhunleth@troodon-software.com
Twitter: @fhunleth
Erlang...
Using Erlang in an Embedded and Cross-Compiled World
Using Erlang in an Embedded and Cross-Compiled World
Upcoming SlideShare
Loading in …5
×

Using Erlang in an Embedded and Cross-Compiled World

27,247 views

Published on

This presentation gives an overview of turning Erlang/OTP releases into Flash images that are ready to copy to an SDCard and run on a BeagleBone Black. It was presented at the ErlangDC conference on 12/7/2013.

Published in: Technology, News & Politics
  • Be the first to comment

Using Erlang in an Embedded and Cross-Compiled World

  1. 1. Using Erlang in an Embedded and Cross-Compiled World Frank Hunleth Twitter: @fhunleth ErlangDC R13B 12/7/13
  2. 2. I'm an embedded C/C++ programmer and Erlang/OTP has been this nice shiny apple that's been tempting me for a long time.
  3. 3. Why? 1. Software reliability (supervision and “let it crash philosophy”) 2. Message passing 3. Simple and robust interface to C/C++ 4. Binary pattern matching Now, I just need to introduce Erlang into a project...
  4. 4. I wanted to start small... ● ● ● ● http://www.youtube.com/watch?v=QpP1hfEOO00 But Erlang/OTP didn't work that way for my embedded projects It's large enough and wants to be in control Hard to gradually introduce into a project Feels like only way to proceed is to eat the whole apple...
  5. 5. Agenda ● ● ● ● ● Baseline embedded systems of interest Demo of a simple cross-compiled Erlang/OTP release Breakdown of parts involved and the tools Using rebar and relx in the cross-compiled environment Items that I need some help with
  6. 6. What Kind of Embedded? Limited resource/ Specific purpose General purpose Microcontrollers RTOS Embedded Linux Android, iOS, Ubuntu, Windows 32-bit Microprocessors ● ● ● Focus is on purpose-built devices capable of running Linux and Erlang Resource limitations, cost or quantity-made drive need to create self-contained firmware images Developing on target is impractical
  7. 7. Demo Demo Demo
  8. 8. The Big Picture Erlang/OTP applications rebar BEAMs and crosscompiled binaries Erlang/OTP release relx fwtool + scripts Base Firmware Image Firmware image
  9. 9. End Product - The Firmware Image Master Boot Record Bootloaders Root Filesystem A Linux kernel Erlang Libraries Main application Root Filesystem B Ping-pong location used when upgrading the firmware Application Data Partition Majority of nonvolatile memory for use by the application *This is just one of the many ways of organizing a firmware image.
  10. 10. Quick View of the Base Root FS / ├── ├── ├── ├── ├── ├── ├── │ ├── ├── │ │ │ │ │ │ │ │ │ │ │ │ └── bin boot dev etc lib sbin srv └── erlang tmp usr ├── bin ├── lib │ └── erlang │ ├── bin │ ├── erts-5.10.2 │ │ ├── bin │ │ └── lib │ ├── releases │ │ └── R16B01 │ └── usr ├── sbin └── share var Erlang/OTP release will go here Cross-compiled ERTS install
  11. 11. Building the Base Root FS ● ● ● ● ● ● Use Buildroot Cross-compiler built for free Huge selection of recipes for cross-compiling C/C++ libraries Cross-compiles Erlang “Installs” everything to a staging directory and creates an EXT4 file system from it Only minor customization needed for Erlang use – https://github.com/nerves-project/nerves-sdk
  12. 12. RootFS (16.8 MB) erlangdc_demo release Non-Erlang libraries ext4 overhead /usr/lib/erlang Linux kernel Non-Erlang binaries Other files /etc 0 1 2 3 MB 4 5 6
  13. 13. Installed Applications (5.5MB) stdlib-1.19.2 kernel-2.16.2 erlangdc_demo-1 cowboy-0.9.0 gproc-0.2.17-1-g45a17ac jsx-1.4.3 crypto-3.0 cowlib-0.4.0 erlang_ale-0.0.1 (gpio port only) ranch-0.9.0 0 0.5 1 1.5 MB 2 2.5 3
  14. 14. System Initialization - erlinit ● Observations – – Erlang/OTP is pretty good at supervision – ● Main responsibilities for /sbin/init are initialization and process supervision Erlang/OTP releases contain initialization order erlinit – Replacement for /sbin/init that starts an Erlang/OTP release – https://github.com/nerves-project/erlinit
  15. 15. Example Release Directory /srv/erlang ├── lib │ ├── my_app-0.0.1 │ │ ├── ebin │ │ └── priv │ ├── kernel-2.16.2 │ │ └── ebin │ └── stdlib-1.19.2 │ └── ebin └── releases ├── 1 │ ├── my_app.boot │ ├── my_app.rel │ ├── my_app.script │ ├── sys.config │ └── vm.args └── RELEASES Source code, docs, etc. trimmed Application initialization script (binary version) Release specification Application configuration Custom VM command line arguments
  16. 16. Boot Time (7.4 seconds) SPL 0.33 u-boot (1s user timeout) 2.83 Uncompress Linux kernel 0.95 Boot kernel 1.52 erlinit 0.06 Load and start ERTS 1.00 Demo running 0.68 0 1 2 3 4 Seconds 5 6 7 8
  17. 17. The Big Picture Erlang/OTP applications rebar BEAMs and crosscompiled binaries Erlang/OTP release relx fwtool + scripts Base Firmware Image Firmware image
  18. 18. Back to the Demo erlangdc_demo/ ├── config │ └── vm.args ├── priv │ ├── index.html │ └── static │ ├── demo.css │ ├── images │ │ └── ajax-loader.gif │ ├── jquery-1.10.2.min.js │ ├── jquery.mobile-1.3.2.min.css │ └── jquery.mobile-1.3.2.min.js ├── Makefile ├── rebar.config ├── relhelper.sh ├── relx.config └── src ├── erlangdc_demo_app.erl ├── erlangdc_demo.app.src ├── erlangdc_demo_sup.erl ├── hwinterface.erl └── ws_handler.erl https://github.com/fhunleth/erlangdc_demo Configure sname and cookie for remote access (debug/demo use here) Build scripts and configuration Calls to Erlang ALE to access GPIOs Cowboy websockets handler
  19. 19. Configuration Files rebar.config {deps, [ {cowboy, ".*", {git, "https://github.com/extend/cowboy.git", "master"}}, {jsx, ".*", {git, "https://github.com/talentdeficit/jsx.git", "master"}}, {erlang_ale, ".*", {git, "https://github.com/fhunleth/erlang_ale.git", "rebar-bbb"}} ]}. {post_hooks, [{"linux", compile, "./relhelper.sh"}]}. Modified version of Erlang ALE for the BeagleBone (C code in here) relx.config {include_erts, false}. {vm_args, "./config/vm.args"}. {release, {erlangdc_demo, "1"}, [erlangdc_demo]}. Just calls relx. Helper script currently needed to pass path to cross-compiled system libraries.
  20. 20. Building fhunleth@halfmarathon:~/nerves/nerves-sdk$ source ./nerves-env.sh Nerves environment updated for this session fhunleth@halfmarathon:~/nerves/nerves-sdk$ cd ../erlangdc_demo/ fhunleth@halfmarathon:~/nerves/erlangdc_demo$ make rebar get-deps compile ==> cowlib (get-deps) ... ==> erlang_ale (compile) ==> erlangdc_demo (compile) Generating a cross-compiled release ===> Starting relx build process ... ... ===> Resolved erlangdc_demo-1 ===> release successfully created! Updating base firmware image with Erlang release... Building /home/fhunleth/nerves/erlangdc_demo/_images/bbb.fw... Building /home/fhunleth/nerves/erlangdc_demo/_images/sdcard.img... fhunleth@halfmarathon:~/nerves/erlangdc_demo$ Once per session to update $PATH and other variables Unmodified version of rebar Unmodified version of relx Run script from nerves-sdk to put relx output into base Root FS and build images
  21. 21. Copying to the SDCard sdcard.img ● Traditional Unix way – – ● sudo dd if=sdcard.img of=/dev/sdc bs=1M Bit for bit copy from sdcard.img to SDCard (/dev/sdc) IMHO... more convenient – sudo mmccopy -p sdcard.img – Discovers the SDCard, shows progress, auto-unmounts – https://github.com/fhunleth/mmccopy
  22. 22. The Big Picture Erlang/OTP applications rebar BEAMs and crosscompiled binaries Erlang/OTP release relx fwtool + scripts Base Firmware Image Firmware image
  23. 23. Things I Don't Know How to Do Yet ● Improve the development work flow – Need to shorten edit/compile/test time – Currently don't take advantage code replacement – Lots of potential here to improve embedded development ● How to estimate and bound Erlang memory usage ● Handle permissions/security within Erlang – Everything runs as root (typical of many embedded devices, but not necessarily desirable) – Run multiple Erlang instances? Probably a ton more, but I'm looking forward to more embedded Erlang!
  24. 24. Using Erlang in an Embedded and Cross-Compiled World Frank Hunleth fhunleth@troodon-software.com Twitter: @fhunleth ErlangDC R13B 12/7/13

×