Erlang

From Openmoko

NOTE: This article is written while I am doing this stuff myself on my Linux Ubunty 8.1 system. If you are on another OS brand or version you will probably have to adjust some parts to make it work. To help understand what commands to type on a terminal on the phone and what to type on a terminal on the host, all phone-terminal text is in green

Introduction

Erlang is a programming language which has many features more commonly associated with an operating system than with a programming language: concurrent processes, scheduling, memory management, distribution, networking, etc.

Concurrency

- Erlang has extremely lightweight processes whose memory requirements can vary dynamically. Processes have no shared memory and communicate by asynchronous message passing. Erlang supports applications with very large numbers of concurrent processes. No requirements for concurrency are placed on the host operating system.

Distribution

- Erlang is designed to be run in a distributed environment. An Erlang virtual machine is called an Erlang node. A distributed Erlang system is a network of Erlang nodes (typically one per processor). An Erlang node can create parallel processes running on other nodes, which perhaps use other operating systems. Processes residing on different nodes communicate in exactly the same was as processes residing on the same node.

Robustness

- Erlang has various error detection primitives which can be used to structure fault-tolerant systems. For example, processes can monitor the status and activities of other processes, even if these processes are executing on other nodes. Processes in a distributed system can be configured to fail-over to other nodes in case of failures and automatically migrate back to recovered nodes.

Soft real-time

- Erlang supports programming "soft" real-time systems, which require response times in the order of milliseconds. Long garbage collection delays in such systems are unacceptable, so Erlang uses incremental garbage collection techniques.

- Many systems cannot be stopped for software maintenance. Erlang allows program code to be changed in a running system. Old code can be phased out and replaced by new code. During the transition, both old code and new code can coexist. It is thus possible to install bug fixes and upgrades in a running system without disturbing its operation.

Incremental code loading

- Users can control in detail how code is loaded. In embedded systems, all code is usually loaded at boot time. In development systems, code is loaded when it is needed, even when the system is running. If testing uncovers bugs, only the buggy code need be replaced.

External interfaces

- Erlang processes communicate with the outside world using the same message passing mechanism as used between Erlang processes. This mechanism is used for communication with the host operating system and for interaction with programs written in other languages. If required for reasons of efficiency, a special version of this concept allows e.g. C programs to be directly linked into the Erlang runtime system.

Fast and Lean

- Erlang is fast and lean. A general erlang application has lots of processes. If you are used to OOP you can compare a process with an object. One process per object instance. To make sure the neo could cope with this I watched top while I was busy dialing a phone number on the Aphasia dialer. Each of the twelve buttons are a separate process. Each has its own little animation, and if you look carefully you can see they run in parallell and are acting independent of each other. The top statistics show a really low CPU utilization, I have to dial quite fast to exceed 5% CPU. Next test I made was to measure the time it takes to start these 12 processes and have them display themselves on the screen (aka start the dialer app). It takes approximately 0.5 sec!

Installation on FSO distribution

The minimal erlang system is only 4 MB. It fits nicely into whats left in the Freerunner and neo1973 after openmoko-fso-image is installed. However, as I am building a vertical application on Openmoko I would prefere to use openmoko-fso-console-image as a baseline and add Xorg and erlang to make a clean environment. Until I have found out how to do that I run the fso-image-glibc and disable e-wm et al.

Install erlang

Download cean_installer.bin from CEAN http://cean.process-one.net/downloads/ At the CEAN Downloads page, select "Installer" and "Production" and "Linux ARMEL" and Download. Copy the file cean_installer.bin to your neo. To save keystrokes I have added "192.168.0.202 phone" to my /etc/hosts file.

scp cean_installer.bin phone:/home/root

Open a terminal window to the phone. Password is blank. Unless you changed it to something you just have to press enter at the prompt.

ssh phone

Before you can install erlang you have to get a copy of the file libsctp.so.1. I grabbed one from my Debian installation on the cf card at /media/mmcblk0p2/usr/lib/

Note that it is ok to create and compile the file on your host and copy the compiled file "test.beam" to the phone. If you want to compile on the phone you have to install the compiler. Try this in the erlang shell.

cean:install(compiler).

I must admit that for some reason it doesn't work for me. I don't compile on the phone anyway so it's no big deal.

Installation on SHR-testing distribution

Get a version from CEAN, as described above, but get the "Archive", and not the "Installer". You will need the "libsctp" lib, too, but it's not in SHR anywhere. You can get a compatible .so out of the .deb package from this link [2]. Find the file libsctp.so.1.0.9, and save it to /usr/lib/ on your SHR freerunner. You should now be able to start the erlang shell.

Installation on Debian distribution

Make sure you have an uboot that is compatible, I used Uboot u-boot-gta01bv4-1.2.0+svnnow-r13_0_2632_0.bin

Erlang development environment

You can in fact choose any text editor, and use a terminal window to compile and copy compiled erlang beam-files to the phone. However, I have found SciTE to be a nice choice. It has erlang colour coding, bracket matching, code completion and lots of more stuff you are accustomed to find in a programmers editor. And it's easily configurable to handle compilation and error tracking, click on an error message to go to the line where the error is located.

In addition to all this you can set it up to let you run your erlang app on the host, and to copy the compiled app to the phone and run it there. All from within SciTE. This gives a very fast turnaround time, I can compile my app - run on host - copy to neo - run on neo in less than a minute. Part of this fast development cycle is of course due to the fact that erlang is platform agnostic - the same compiled code runs on Arm and Intel Linux and Windows.

NOTE: Other options for erlang developers are Eclipse and NetBeans. Add erlIDE to Eclipse or use ErlyBird

You don't need an emulator

All you need to run a phone application on your host is erlang and an X-server. Are you running linux it's just to install erlang and you are done. On a Mac you also have to install the Apple X-server app. It works right out of the box, and is a free download from www.apple.com. Textmate is the recommended erlang ide for mac-users. Windows users have to take another path. You develop and compile erlang source on your windows box, and use erlang's built in hot code loading to run your code right on the phone without any restart or reboot.

All I have to do now is to re-start X whenever I want to run an uploaded app. This is what the last two menu buttons do in SciTe - stop X on the phone, and start X on the phone. As an alternative for stopping and starting X you can use Erlang's built-in hot code loading.

Graphics

I'm using vector graphics to produce new widgets. Vector graphics is lightweight and programmable, easy to scale and rotate, and the Byte-Size is small. Use any vector-capable drawing application. I happen to use AutoCAD LT on Wine, but please suggest an open source alternative and I will be happy to create another small conversion program to translate the new drawing file to erlang source. If you have access to AutoCAD, take dxf2erl.erl from here

Convert drawing to erlang source

Views

Personal tools

NOTE: This article is written while I am doing this stuff myself on my Linux Ubunty 8.1 system. If you are on another OS brand or version you will probably have to adjust some parts to make it work. To help understand what commands to type on a terminal on the phone and what to type on a terminal on the host, all phone-terminal text is in green

Introduction

Erlang is a programming language which has many features more commonly associated with an operating system than with a programming language: concurrent processes, scheduling, memory management, distribution, networking, etc.

Concurrency

- Erlang has extremely lightweight processes whose memory requirements can vary dynamically. Processes have no shared memory and communicate by asynchronous message passing. Erlang supports applications with very large numbers of concurrent processes. No requirements for concurrency are placed on the host operating system.

Distribution

- Erlang is designed to be run in a distributed environment. An Erlang virtual machine is called an Erlang node. A distributed Erlang system is a network of Erlang nodes (typically one per processor). An Erlang node can create parallel processes running on other nodes, which perhaps use other operating systems. Processes residing on different nodes communicate in exactly the same was as processes residing on the same node.

Robustness

- Erlang has various error detection primitives which can be used to structure fault-tolerant systems. For example, processes can monitor the status and activities of other processes, even if these processes are executing on other nodes. Processes in a distributed system can be configured to fail-over to other nodes in case of failures and automatically migrate back to recovered nodes.

Soft real-time

- Erlang supports programming "soft" real-time systems, which require response times in the order of milliseconds. Long garbage collection delays in such systems are unacceptable, so Erlang uses incremental garbage collection techniques.

- Many systems cannot be stopped for software maintenance. Erlang allows program code to be changed in a running system. Old code can be phased out and replaced by new code. During the transition, both old code and new code can coexist. It is thus possible to install bug fixes and upgrades in a running system without disturbing its operation.

Incremental code loading

- Users can control in detail how code is loaded. In embedded systems, all code is usually loaded at boot time. In development systems, code is loaded when it is needed, even when the system is running. If testing uncovers bugs, only the buggy code need be replaced.

External interfaces

- Erlang processes communicate with the outside world using the same message passing mechanism as used between Erlang processes. This mechanism is used for communication with the host operating system and for interaction with programs written in other languages. If required for reasons of efficiency, a special version of this concept allows e.g. C programs to be directly linked into the Erlang runtime system.

Fast and Lean

- Erlang is fast and lean. A general erlang application has lots of processes. If you are used to OOP you can compare a process with an object. One process per object instance. To make sure the neo could cope with this I watched top while I was busy dialing a phone number on the Aphasia dialer. Each of the twelve buttons are a separate process. Each has its own little animation, and if you look carefully you can see they run in parallell and are acting independent of each other. The top statistics show a really low CPU utilization, I have to dial quite fast to exceed 5% CPU. Next test I made was to measure the time it takes to start these 12 processes and have them display themselves on the screen (aka start the dialer app). It takes approximately 0.5 sec!

Installation on FSO distribution

The minimal erlang system is only 4 MB. It fits nicely into whats left in the Freerunner and neo1973 after openmoko-fso-image is installed. However, as I am building a vertical application on Openmoko I would prefere to use openmoko-fso-console-image as a baseline and add Xorg and erlang to make a clean environment. Until I have found out how to do that I run the fso-image-glibc and disable e-wm et al.

Install erlang

Download cean_installer.bin from CEAN http://cean.process-one.net/downloads/ At the CEAN Downloads page, select "Installer" and "Production" and "Linux ARMEL" and Download. Copy the file cean_installer.bin to your neo. To save keystrokes I have added "192.168.0.202 phone" to my /etc/hosts file.

scp cean_installer.bin phone:/home/root

Open a terminal window to the phone. Password is blank. Unless you changed it to something you just have to press enter at the prompt.

ssh phone

Before you can install erlang you have to get a copy of the file libsctp.so.1. I grabbed one from my Debian installation on the cf card at /media/mmcblk0p2/usr/lib/

Note that it is ok to create and compile the file on your host and copy the compiled file "test.beam" to the phone. If you want to compile on the phone you have to install the compiler. Try this in the erlang shell.

cean:install(compiler).

I must admit that for some reason it doesn't work for me. I don't compile on the phone anyway so it's no big deal.

Installation on SHR-testing distribution

Get a version from CEAN, as described above, but get the "Archive", and not the "Installer". You will need the "libsctp" lib, too, but it's not in SHR anywhere. You can get a compatible .so out of the .deb package from this link [2]. Find the file libsctp.so.1.0.9, and save it to /usr/lib/ on your SHR freerunner. You should now be able to start the erlang shell.

Installation on Debian distribution

Make sure you have an uboot that is compatible, I used Uboot u-boot-gta01bv4-1.2.0+svnnow-r13_0_2632_0.bin

Erlang development environment

You can in fact choose any text editor, and use a terminal window to compile and copy compiled erlang beam-files to the phone. However, I have found SciTE to be a nice choice. It has erlang colour coding, bracket matching, code completion and lots of more stuff you are accustomed to find in a programmers editor. And it's easily configurable to handle compilation and error tracking, click on an error message to go to the line where the error is located.

In addition to all this you can set it up to let you run your erlang app on the host, and to copy the compiled app to the phone and run it there. All from within SciTE. This gives a very fast turnaround time, I can compile my app - run on host - copy to neo - run on neo in less than a minute. Part of this fast development cycle is of course due to the fact that erlang is platform agnostic - the same compiled code runs on Arm and Intel Linux and Windows.

NOTE: Other options for erlang developers are Eclipse and NetBeans. Add erlIDE to Eclipse or use ErlyBird

You don't need an emulator

All you need to run a phone application on your host is erlang and an X-server. Are you running linux it's just to install erlang and you are done. On a Mac you also have to install the Apple X-server app. It works right out of the box, and is a free download from www.apple.com. Textmate is the recommended erlang ide for mac-users. Windows users have to take another path. You develop and compile erlang source on your windows box, and use erlang's built in hot code loading to run your code right on the phone without any restart or reboot.

All I have to do now is to re-start X whenever I want to run an uploaded app. This is what the last two menu buttons do in SciTe - stop X on the phone, and start X on the phone. As an alternative for stopping and starting X you can use Erlang's built-in hot code loading.

Graphics

I'm using vector graphics to produce new widgets. Vector graphics is lightweight and programmable, easy to scale and rotate, and the Byte-Size is small. Use any vector-capable drawing application. I happen to use AutoCAD LT on Wine, but please suggest an open source alternative and I will be happy to create another small conversion program to translate the new drawing file to erlang source. If you have access to AutoCAD, take dxf2erl.erl from here