Monday, June 7, 2010

Learning C++

C++
Recently I joined a new project at work. It's some application running on an embedded Linux platform. So it was decided that we would write it in C++. C++ is great for these kind of jobs, where you have limited space and processing power.

The only problem was, I had never worked with C++ before. Why was I even assigned to the team you ask. Well, I had previously been working on a related project for the same client. So I had a lot of domain specific knowledge that the other team members didn't have. Also I'm the resident UNIX guy so I would help them when their machines started acting up.

So there I was learning C++ as I went. Reading a lot of books by Scott Meyers and a ton of BOOST documentations. And after a couple of months I'm still a C++ rookie. Frankly, I didn't think it was a Language worth learning. I though it was outmoded and inferior to modern languages.

BOOST
The BOOST library is an amazing piece of software. I can't imagine where we would be without it. From Wikipedia:


"The BOOST libraries are aimed at a wide range of C++ users and application domains. They range from general-purpose libraries like the smart_ptrlibrary, to OS abstractions like Boost FileSystem, to libraries primarily aimed at other library developers and advanced C++ users, like the template metaprogramming (MPL) and DSL creation (Proto).
In order to ensure efficiency and flexibility, Boost makes extensive use of templates. Boost has been a source of extensive work and research into generic programming and metaprogramming in C++."
Most of all I was pleasantly surprised to see how many functional programming features is provides. I mean I use boost::bind, boost::function or boost::signals every single day. boost::bind allows you to create a callable object and bind to an arbitrary member function. It also allows partial function application and the like. There is also the BOOST lambda which allows you to use lambda expression just the way you're used from FP languages.


Valgrind
I've also installed Valgrind on our development machines. We generally use it to detect memory leaks and other memory management issues. Apart from suchmemory checks, it also features several profilers. But the coolest feature IMO is Helgrind, which can detect race conditions in multi-threaded applications and/or improper locking of resources. 

Thursday, May 27, 2010

To Infinity and Beyond

Sigfpe made the effort to collect many of his great blog posts he wrote over the years into a single, structured post. Basically writing an index. He covers Monads, Comonads, Fold, Unfold, the general Category Theory. I've been reading and rereading for weeks and there's still more to discover. Check it out here.

Friday, May 7, 2010

Update: Lucid Lynx on the HP Mini 210

Bye Bye Koala, Hello Lynx
Some time ago, I installed Koala on my HP Mini 210. Shortly after the next Ubuntu version "Lucid Lynx" was released. So I upgraded in hopes that some the hardware would be better supported. So I figured I should write a quick update about how things are.

Improvements
Bluetooth
Bluetooth works consistently now even after many suspend/resume cycles. This is definitely an improvement for people who need Bluetooth enabled peripherals. 

Remaining Problems
No Multitouch
Unfortunately there still doesn't seem to be touch pad drivers which support multi-touch properly. Normally I wouldn't care much. But the way the Mini's touch pad is designed makes this a bit of an annoyance. The left and right mouse buttons are embedded in the touch pad. This means that the cursor jumps around the screen every time I touch the pad with a second finger. Unfortunately this happens all the time when I point and click. 

Short Battery Life
Sadly, the one improvement I was hoping for hasn't happened. Even relying on powerttop to reduce the power consumption, the battery life is still only about two thirds of what it should be. I also figured out that shutting down the wireless controller before suspending, reduces power consumption while the netbook is suspended.

Conclusion
So that's that. Lucid's hardware support has improved compared to Karmic. That and the new look and additional features speak for an upgrade.

Sunday, April 11, 2010

Mixed Locale in Ubuntu

If you're not a native English speaker the odds are that you most likely want to change the default locale. Well, the right thing in that case would be to select the correct language during installation. Or perhaps you come from a multilingual country and a single locale just won't cut it. I'm from Switzerland where we have four official languages (English not included - yet). I like to have my OS talking to me in English, like the makers intended, but I also want the currency, date & time, etc. formatted in the familiar way.

Installing necessary locales
Before you can start to tweak the locale, you must first install all locales you are planning to use. 

The easiest way to install support for additional languages in Ubuntu is through the aptly named 'Language Support' tool. It is located in System -> Administration -> Language Support. This tool will also allow you to specify the language of your user environment as well as the language for the login screen.  I keep both settings at US English. At the bottom you will see a button called "Add/Remove Languages". When you press it, another menu will pop up where you can check all languages that you require. In my case, I added German. When you apply the changes support for all selected languages will be installed.

If you're working on the command line. You can install language-pack packages through your favorite CLI package management tool, like apt or aptitude or whatever you like. There are a couple of language-packs for different languages. 

When I additionally picked support for German, the "Language Support" tool installed the following packages on my system:
  • language-pack-de
  • language-pack-gnome-de
  • language-pack-de-base
  • language-pack-gnome-de-base
  • language-support-writing-de
There are also language-pack-kde-de and language-pack-kde-de-base, respectively for KDE. But you probably won't need them unless you are on Kubuntu.

Mixing locales
As I mentioned in the previous paragraph. You can select your locale in System -> Administration -> Language Support. But there you can only select one locale for everything. For example, I like English GUI & CLI a lot, but I don't like their date & time formats so much - I'm just not used to that. Not to mention the American system of measurements :)

Luckily UNIX/Linux actually allows more fine grained control over the locale settings than the Ubuntu GUI wants to make you believe. The language you select on the GUI will act as the default, but you can also tweak it to your liking.

First create a new file called .custom_locale in your home directory. This will contain the custom locale settings for you alone:

# Custom locale settings:
# use Swissgerman date, time, currency, numbering etc.
export LANG=de_CH.utf8
# keep the CLI/GUI in US english
export LC_MESSAGES=en_US.utf8
# sort in alphabet the Swiss way
export LC_COLLATE=de_CH.utf8
# change currency formatting behavior of strfmon(3)
export LC_MONETARY=de_CH.utf8
# change time formatting behavior of strftime(3)
export LC_TIME=de_CH.utf8
# change number formatting behavior of printf(3) and scanf(3)
export LC_NUMERIC=de_CH.utf8

The LC_* variables are described in more detail in man locale(7). The most important settings are the first two. This file alone won't do much. You have to include it to your environment.

In ~/.profile, add the following lines:

# Include ustom locale settings if they exist
# (gdm parses .profile so these settings will 
# also be active within the Gnome environment).
if [ -f "$HOME/.custom_locale"]; then
   . "$HOME/.custom_locale"
fi
Additionally, you will also have to add the same snippet to your ~/.bashrc or ~/.zprofile (or rc file of whatever shell you use) because unlike gdm, the shells usually don't read .profile. Once you're done, reboot.

After the login, you can verify the locale settings e.g. from the shell with the command locale:

~$ locale
LANG=de_CH.utf8
LANGUAGE=en_US.UTF-8
LC_CTYPE="de_CH.utf8"
LC_NUMERIC=de_CH.utf8
LC_TIME=de_CH.utf8
LC_COLLATE=de_CH.utf8
LC_MONETARY=de_CH.utf8
LC_MESSAGES=en_US.utf8
LC_PAPER="de_CH.utf8"
LC_NAME="de_CH.utf8"
LC_ADDRESS="de_CH.utf8"
LC_TELEPHONE="de_CH.utf8"
LC_MEASUREMENT="de_CH.utf8"
LC_IDENTIFICATION="de_CH.utf8"
LC_ALL=

Systemwide Changes
The above tweak will only work for individual users. If you want to make a system wide change, you should change move the custom_locale file to /etc and then modify /etc/profile rather than ~/.profile. And likewise /etc/bash.bashrc & /etc/zsh/zprofile

Saturday, March 20, 2010

Ubuntu on HP Mini 210 - Taming the fierce Koala

Bad Koala! Nasty Koala!
I got an HP mini 210 last weekend and of course the first thing I had to do, was getting rid of Windows 7 Starter. Initially I wanted to give Easy Peasy a try, but it didn't boot properly. So I switched to the next best thing, which is Ubuntu 9.10 "Karmic Koala" Netbook Remix.

Before kissing windows 7 good-bye for good (HP doesn't provide neither Windows7 nor any driver CDs), I wanted to make sure that Ubuntu would support the hardware. So I created a bootable USB stick with Unetbootin. In the live CD environment almost everything worked nicely. Most importantly wifi ran out of the box with the proprietary Broadcom STA driver. Suspend & resume also worked. The only thing that I noticed was that the multitouch pad didn't work. So I went along and installed Karmic. To my surprise the wireless driver failed to load! Just great. Fortunately for me, I knew that the driver had to be somewhere after all it ran in the live image. After a little searching I found it in /media/usb/pool/restricted/b/bcmwl/.


You can install it directly from Nautilus or in the shell. Whatever you prefer. After that, wireless should work.

The Good - Things that work Out of the Box™
Just like its namesake the Karmic Koala is pretty tame already and many things worked:
  • Wireless
  • Ethernet
  • Suspend/Resume
  • Sound
  • Webcam
  • Microphone
  • Bluetooth
  • SD/MMC/MS/xD card reader
  • USB
  • Touchpad (single touch and no right click)
Moreover, the Mini 210 has SIM card slot for mobile Broadband. I haven't tested that because I'm not using such a service.

The Bad - Stuff I could fix
Touchpad
Solution #1. Initially, presumed that fixing the Touchpad was just a matter of installing the Synaptics driver. But that didn't help. After some googling, I found a quick and dirty fix here. Essentially it suggests adding "options psmouse proto=exps" to psmouse.modprobe like so:

$ sudo echo options psmouse proto=exps > /etc/modprobe.d/psmouse.modprobe

I call this quick and dirty because it will essentially make the OS treat the touchpad like a PS mouse. It also breaks the edge-scrolling. A feature I can't live without.

Solution #2. After some more searching I discovered a touchpad driver patch on the Ubuntu forums. You can download and apply the patch and then hand roll your own driver. This will fix left/right clicks and edge-scrolling. But real multitouch doesn't seem to be currently supported.

The Ugly - Issues I couldn't fix
Bluetooth is gone after suspend
I also noticed that the Bluetooth device is only available after a fresh reboot. This is not really a problem for me since I don't normally use any Bluetooth peripherals. I didn't find any solutions for the problem. As a matter of fact, I'm not even sure what the root cause is...

Short Battery Life
I saved the worst part for last. I noticed that the battery life is not nearly a good as it is supposed to be. I have a 6 cell lithium ion battery which should last around 8+ hours. Effectively, I only get about half that.
I installed a tool called powerttop. It combines various sources of information from the kernel into one convenient screen so that you can see how well your system is doing at saving power, and which components are the biggest problem. Running powertop on my system revealed that the kernel spends half the time (i.e. half the battery) scheduling!


After further digging I discovered that this was a well known regression bug that affects various Atom based netbooks by different manufacturers. Apparently this was no problem in Jaunty. Hopefully the bug will be fixed with the next Ubuntu release which is due April 30th. If it isn't, I likely will revert to Jaunty for better or worse.

Conclusion
All in all, Karmic runs ok on the HP Mini. Although there a couple of hurdles that are very difficult to overcome by the average user.