glibc malloc hooks and TLSF

Recently, I was asked to constrain the memory usage of an application on Linux. Glibc provides hooks for [malloc, free, etc](http://www.gnu.org/software/libc/manual/html_node/Hooks-for-Malloc.html). By the way, the hook functions are responsible to guarantee thread safety — glibc doesn’t do it automatically. I used the malloc hooks in combination with a memory manager that a colleague found: TLSF. There are two implementations:

* [http://tlsf.baisoku.org/](http://tlsf.baisoku.org/) (public domain)
* [http://rtportal.upv.es/rtmalloc/](http://rtportal.upv.es/rtmalloc/) (GPL)

There are benefits and caveats when using a custom memory allocator. TLSF was meant to shine for real-time use, because the overhead of malloc and free are O(1) constant-time operations. On the other hand, TLSF isn’t thread-safe.

mtnwestruby: Meta Notes

Mountain West Ruby Conference: Meta Notes
17 March 2007

Setup. When I arrived, the conference organizers were setting up the auditorium
with power extension cables, network cabling, etc. Having a wired network
connection was very nice, although Wi-Fi was available.

Attendance was better on Friday than on Saturday.

Laptops. I’d estimate that nearly half of attendees had Apple laptops. Nearly
half of the presenters used Apple laptops, and of the remainder, half used
Windows and half used Linux.

Editors. Of the presenters that edited code on-the-fly, one used emacs, one used Textmate and the rest used VIM. None used an IDE.

JRuby or Ruby.NET. It seems like my app would become tied to the platform if I
use the libraries from that platform. This would make it difficult to go from
JRuby to Ruby.NET, or visa-versa. Or difficult to move from JRuby or Ruby.NET to
traditional Ruby.

mtnwestruby: JRuby

Mountain West Ruby Conference: JRuby by Charles Nutter and Ted Enebo
16 March 2007

Most of the developers in the auditorium have been Java programmers, and don’t
want to go back. Charles said that they have a hard time getting the message
out that JRuby isn’t about Java, it’s about Ruby, and they’ve aimed to make
JRuby as compatible as possible with Ruby.

Background: They’ve both been Java developers for the past ten years, and
their goal is to make Ruby a first-class language on the Java platform. They
didn’t start the JRuby project — they adopted it.

Ruby 1.8 Design Issues:

  • Green Threading doesn’t scale across multiple processors/cores. The
    one-size-fits-all scheduler doesn’t fit all platforms where it runs. Although
    Ruby 1.9 will use native threads, there’s much work left to make it work on
    various platforms. Java/JRuby already uses native threading and scales across multiple processors and cores.

  • Partial Unicode support. Ruby 1.9 will have Unicode support, but will bring with it other difficult issues (e.g. what happens when you concatenate two strings, each being in a different encoding?). In JRuby, one can use Java Unicode strings, or the Rails Unicode library.
  • Slower than most other dynamic languages. Makes it difficult to sell to management. It’s a long term perception problem for the language. JRuby will allow compiling to bytecode, which allows HotSpot to do JIT optimization.
  • Garbage collection is simplistic. JRuby uses Java’s best-in-the-world memory management and GC. Scales well to enormous applications and loads, and is battle-tested in deployments worldwide.
  • C language extensions can crash the Ruby runtime, don’t necessarily interact well with garbage collection or with threading. JRuby lets you use Java-based extensions, which aren’t going to crash the VM.

Politically, it’s easier to get JRuby into an organization than Ruby, because
organizations have already accepted and deployed Java. JRuby is just a library for Java.

Most “pure” Ruby code runs on JRuby, and Rails mostly runs on it, although
only 90% of ActiveRecord passes unit tests. It’s easier to write JRuby code
than Java code. Perhaps Java is good for implementing libraries, and JRuby is
good for using those libraries for implementing applications.

JRBuilder/Cheri project by Bil Dortch lets you build Swing apps in JRuby much
more easily than writing Java code. Demo shown. Project is still in development.

NetBeans Ruby support is a one-man developer effort by Tor Norbye. His
progress has been impressive. Demo shown. Code completion, syntax
highlighting, built-in ruby documentations, go-to-declaration, auto-indent,
rename support, built-in interactive ruby shell (irb). By the way, Charles
uses vim, and Ted uses emacs. NetBeans Ruby uses the JRuby AST to do its work.

Speed. Interpreted JRuby is currently generally slower than Ruby 1.8.5, although some things are faster. They’re working on making it faster, and should be able to achieve comparable performance to Ruby 1.8.5. Compiled Ruby (bytecode) runs faster on the JVM than it does on Ruby 1.8.5.

Q: How can you make a JRuby app deployable so that people don’t know it’s a Ruby app or a Java app?
A: Make your ruby app into a jar file, and just double-click it.

Q: How easy or hard is it to deploy JRuby apps to servers?
A: Use Sun’s glassfish deployment framework.

mtnwestruby: Review of a Rails App

mtnwestruby: Review of a Rails App by Marcello and Jamis of 37signals.com
17 March 2007

Almost everyone in the audience has used Rails. A little less than have write Rails apps for a living. I haven’t used Rails, and even if I had, it would be difficult to take notes on this presentation. I recommend viewing the video when it becomes available.

Marcello and Jamis recommend Kent Beck’s book on Smalltalk best practices and how to decide what code belongs where. They also recommend Domain Driven Design.

Why do you prefer the operator ‘&&’ over ‘and’? The use of ‘&&’ leads to fewer bugs. Consider the following code:

  • return foo and bar # will not return what you expect
  • return (foo and bar) # this will work
  • return foo && bar # use ‘&&’ and ‘||’ because they’re more predictable in behavior than ‘and’ and ‘or’

Convention: A bad smell in code is seeing a chain of if..elsif
statements to check for error conditions. In this case, you probably want to
handle error cases with exceptions. For example, in Rails, myobj.save! will
validate data, and raise an exception when there’s a problem, whereas
myobj.save will not raise an exception.

mtnwestruby: Ruby USB

mtnwestruby: Ruby USB by Michael Hewner
16 March 2007

Nerds love to customize their software — their shell prompt, adding vim plugins, emacs, etc. After a while, they run out of things to customize. What do they do now? Customize USB hardware!

Ruby USB is about fun, about controlling USB devices using Ruby. No
one paid him to work on it. USB devices are self describing. You can even build
your own USB devices, which has nothing to do with Ruby. The most interesting
USB spec is the one for Human Interface Devices. For HID, the device sends a
description of the format of the data that it’s going to send and what it means
— before it actually sends the data. No reverse engineering necessary!

Ruby USB simplifies the interpretation of the meta-data coming from a USB
device.

mouse_interface.all_input_usages => [Button (9)::Button ...]
keyboard_interface.all_input_usages # will list ever key available on the keyboard

Michael did not write libUSB; his Ruby library merely uses it. It supposedly
supports all UNIX operating systems, although he’s only tested it on Linux.

Lessons learned:

  • Don’t write the USB HID parsing library in C++ and patch it into Ruby — it’s too much work. He should have written it directly in Ruby.
  • Write unit tests.

AVRUSB: Build your own custom USB device — requires that you have a soldering iron and an AVR microcontroller.

Evdev uses the linux-level USB device layer to let you talk to USB devices. It’s easier to use, more rock solid, but doesn’t let you do as many cool things.

Q: Is Ruby USB ruby-thread safe?
A: Haven’t tested it. There are probably some bugs.

Q: Do you have the ability to send output to USB devices?
A: He’s working on it. It’s almost ready.

Q: What’s the craziest thing you’ve done with Ruby USB?
A: Glued two keyboards together. Made one work for Vi insert mode, and one for Vi command mode. It didn’t work out well because he would frequently type on the wrong keyboard, and because it was too large to fit on his lap comfortably.

mtnwestruby: RubyCLR and Ruby.NET by John Lam

mtnwestruby: RubyCLR and Ruby.NET by John Lam
17 March 2007

John works for Microsoft. Before coming to Microsoft, he built the bridge for running Ruby on the CLR (common language runtime).

Why would anyone want to run Ruby on someone else’s virtual machine? To answer
that, we look at the overlap between what we want to do and what we actually
do at work. It would be nice to maximize that overlap. Many of us want to use
dynamic languages, by many of us have to use .NET or Java. Running dynamic
languages on the one of these VMs helps maximize the overlap.

The interop layer for Python is going to be fundamentally different than that
for Ruby so that when you write a Python program using .NET libraries, it still
feels like Python code, and when you write Ruby code, using .NET libraries, it
still feels like writing Ruby code.

Lutz is an MS employee that worked on Intentional Programming, which never went
anywhere at MS. He created an editor that would parse the AST (abstract syntax
tree) and display it in different ways. This idea lives on in the .NET
Reflector
, which will decompile .NET assemblies into IL, Visual Basic, C#,
Delphi, etc. It’s interesting to use Iron Python to compile code, and use the
.NET Reflector to look at the IL.

Dynamic methods are an interesting feature of the CLR in version 2.0. If they’re not going to be used, they get garbage collected. Before 2.0, once you load code, it stays loaded for the lifetime of the VM.

Interesting: The CLR supports polymorphic methods based on the return type, not jus the arguments. This isn’t a feature that C# supports.

The Ruby to .NET interop is extremely fast — millions of operations per
second.

John used Vim on Windows when he edited code.

On April 30th at the MS mixed conference, we will see the interesting things
that John Lam has been working on since he joined Microsoft, which he can’t
talk about right now. However, the really interesting conference will be Microsoft PDC because the technology will be more mature.

Q: Can you run Rails on the Ruby CLR?
A: No idea.

Q: Can I run Ruby.NET on mono or on my PowerPC?
A: There’s nothing that would preclude it from running on Mono, but Microsoft is not working on it.

mtnwestruby: Chad Fowler Keynote

mtnwestruby: Chad Fowler Keynote
16 March 2007

Background: Musician. Dropped out of music school to be a programmer.
Programming experience: Perl. Java Enterprise software. Now Ruby.
He’s been organizing international Ruby conferences since 2000, even before he could write a decent Ruby program.

Ruby is at a dangerous crossroads. We need to act, as a community,
to avoid failure. How do we need to behave to remain relevant in the industry —
as programmers, and as a Ruby community? For so long, Ruby has been perceived
as a niche player. The community has been in the habit of fighting, and
defending against FUD. Once we start “winning”, we continue those habits, and
new “converts” to Ruby pickup the same habits. We hear “Ruby doesn’t perform
well”, and we should say “not it doesn’t”. We hear “Ruby doesn’t support
internationalization”, and we should say “no, it doesn’t”.

If you’re at this conference, you’re a pioneer in the dynamic languages
community. In 2001, Matz was the only professional Ruby developer. In 2005,
that had changed because more people were employed to write Ruby code. We’re
“winning” in the sense that we can use Ruby in the workplace. Sun and Microsoft
are hiring people to work on Ruby.

Ruby 2.0 has been vaporware for longer than Perl 6.

The Ruby community of 2006 and 2007 is a melting pot of backgrounds — PHP,
.NET, ASP, 4th Dimension, J2EE, Rails, system admins, the agile community. The
tendency is to take a big community and to run it like a big company. The germs
(PHP programmers) are going to start coming into the community, and
the antibodies are going to start smashing them. There are some germs in the
community that do need to be cleansed. We need to figure out what those germs
are, although some germs we will never get rid of.

There are germs we must be exposed to in order to keep the community strong.

Metaphors influence the way we think about things. Americans think “arguing is war”, and that shapes the way we respond to people in the community. However, not all cultures have that metaphor.

The book “Leading Revolution” by Gary Hamel is worth reading. It has nothing to do with programming. Things designed in Ivory Towers don’t respond well to small evolutionary changes.

If we’re trying to grow as a community, creating a new framework like Rails is a waste of time — there are more interesting problems to solve.

We build systems to support people, and sometimes, geeks forget this fact, and become jerks.

Monkeys in India are brave and annoying when they’re trying to steal food.
In Southern India, they’re controlled by creating holes in the ground, with a larger space at the
bottom. The hole is just big enough for the monkey to slip his hand into, with
food at the bottom in the bigger space. The monkey wants the food, and puts his
hand in. Once he makes a fist by grabbing the food, he can’t pull it out.
Someone comes along and clubs the monkey. The monkey doesn’t let go of the food
because of value rigidity.

In the Ruby community, Chad hopes we are not like the monkey. We need to at
least be cognizant of the rigid values we have, and whether they’re worth
holding onto. Do you identify yourself as a Ruby developer? Hopefully, you are
always reaching for something beyond what you are comfortable with — perhaps
it’s Erlang, Haskell, ocaml, etc. If you’re comfortable, if you know the
answers, you aren’t learning. Try to lead, but also try to be learning
something new.

Chad has done well in the Ruby community by doing the things that no one else
wants to do — organizing Ruby conferences, bringing publicity to Ruby gems.

mtnwestruby: Lightning Sessions

Mountain West Ruby Conference: Lightning Sessions — Five minutes each.

16 March 2007

Mike. BinaryLottery uses ‘figlet’ to translate strings to ASCII art.

WAX by dan@eparklabs.com. WAX – Web Application X — A similar thing to Rails, but a totally different concept. Strongly tied to the database. Extremely fast development for enterprise database applications. Used by 10% of Fortune 500 companies, although they don’t know that. Self-contained applications. Designed with CSS. Website/CMS and Applications are integrated. REST architecture. Start with “wax on”. Stop with “wax off”. Allows you to GET and POST Excel spreadsheet files, among other things.

Managing SSH keys with Capistrano by Jade Meskill from Pheonix, AZ. Capistrano takes all developer’s SSH public keys, and puts them into an authorized keys file on the remote boxes they need to deploy software to. http://iamruious.com

Goldberg presented by Coby Rhenquist. He found that Rails leaves developers to themselves after getting their application framework stetup. Then he found the Goldberg generator, which gives you a more complete site, including better looks, a login page, users, roles, permissions and built-in CMS (textile), etc. Gives the administrator the capability to wire things up using menus in the web UI front end. Goldberg.rubyforge.org. Disadvantage: When rails 1.2 comes out, you have to re-do your app.

CruiseControl.rb 1.0 was just released. http://cruiseControlrb.thoughtworks.com It should take about 10 minutes to download and install. No XML or XSLT as with Java CruiseControl. Not released as a gem.

JRuby deployment. Charles Nutter. Just released JRuby Rails Integration.

$ rake war:standalone:create

JRuby has better database support than Rails because of JDBC.

LogWatchR by Pat Eyler (uses Suse Linux and vim). Concepts: Simple, Extensible, Low Support costs. Uses YAML to specify good and bad patterns. LogWatchR saves statistics to that YAML file as well. Written in 250 lines of Ruby. Handles 2250 log entries/sec. Likes Ruby because it’s easy to write, easy to read, easy to maintain.

mtnwestruby: Black-boxing with Ruby

Mountain West Ruby Conference: SBN
Tag: mtnwestruby
16 March 2007

James Britt – Black-boxing with Ruby

Reinventing the wheel is overrated. Why not reuse what other people have already implemented, even from other languages?

Trac: A good project tracking system. Requires too much use of the mouse.
What if: I could use Trac from the command line? Tracula.

Tracula is written in Ruby and uses Hpricot & Mechanize to pretend to be a web browser. Unfortunately, tracula is brittle because the web page UI tends to change — it’s not an API they’re publishing. Many features are missing from Tracula, but that’s ok — he writes the functionality as he needs it, and stubs out missing behavior with hardcoded values.

Tracula acts as a proxy, so the code using tracula doesn’t break just because trac changes.

WordPress: A best of breed web log system.
What if: I could have the great comment system for the Django Book?
www.djangobook.com. Uses Yahoo UI and Yahoo.EXT.
Comet is written in Ruby. Comments made by users are redirected via Apache modprox through comet, which takes the comment, and posts it to WordPress.

Lessons learned:

  • WordPress plugins are the way to go.
  • Repurposing other people’s code can bite your hard.
  • Most web sites offer APIs, including TRAC. Use them, but still use a proxy layer so your software is insulated from changes in the API.
  • Proxies can add exception handling that is missing from APIs.

Techniques:

  • Screen scraping
  • DOM munging
  • Proxies, proxies, proxies!
  • Save off a copy of web pages while you are developing your HTML screen scraper, and hit those saved pages so that you don’t run up someone’s bandwidth bill.