Wednesday, August 29, 2007

unlikely() & likely()

This week wasn't really productive. No preparations for sessionals yet. Stayed up last night so missed the college today. And some idiot classmates must be thinking now that I was preparing hard for sessionals. Poor kids, ;-)

Actually, I tried my hands with Linux Kernel Development. And its such an amazing thing!!

For starting out, I downloaded the source of latest kernel as linux-2.6.22.5.tar.bz2.
I unpacked it, and started reading code. And as some of you must have guessed it, it was beyond my limits of understanding.

Its quite difficult to start actually. Including the comments, the whole kernel code (latest) is about 7.3 million lines. Too much, na? The first version, 0.0.1 had about 10000 lines. I read somewhere that lately they've not been adding much to this code, but refining it and fixing bugs.

The Linux kernel is one of the best (or the best as they say) kernels around. It has got a great mix of good algorithms, and it has refrained from bad policies and poor design. Its monolithic and still quite modular.

Its well commented, and the code gives me a feeling that it can be understood, given time & patience. Well I've got some books too, notably Linux Kernel Development 2nd Edition, which will help me as I go.

There are some interesting things when writing kernel code. First, you can do everything and anything. Nobody will trap you or accuse you or kill you. No memory protection (as userspace has got), no faults and error trapping. You need to behave responsibly.

Second, you don't have the standard C library. No printf (though a printk is there). No great time saving functions. But fortunately, most useful ones are there in kernel headers (like string comparison etc.)

Third, you have a small, fixed-size stack. Unlike userspace applications, you just cannot allocate tons of variables! So every byte counts here.

Fourth, no floats in kernel code. No floating point arithmetic here. Because kernel has a do-it-yourself overhead. So, just integers and chars.

Finally, the one I found most interesting is about predictability. Conditional branches can be optimized as either very likely or very unlikely by GCC. The macros likely() and unlikely() do this work.

/* file is almost never NULL */
if (unlikely(file == NULL)) { .. }

/* the two sizes are almost always equal */
if (likely(new_size == old_size)) { .. }

Compiler will optimize these branches as the programmer has predicted. If prediction was good, it will improve performance. Otherwise it can degrade performance, too. You'll find unlikely() a lot in kernel code.

Its an intelligent piece of code. And so it requires a lot of time to study. Well, I am prepared for anything! Let's tame the beast!

Saturday, August 25, 2007

Get organised!

This one goes out to all my friends and foes who are a part of FLOSS community. Now is the right moment for all of us to get more organised. We don't have much time. Its now or its never. The FLOSS community is calling upon us. Get involved!

You are a free programmer or designer or developer. You are free to do whatever you want. You are free to develop software for yourself. You are free to make it big and better. But not for very long. Because as soon as you get a job, your freedom is lost. The fun for programming is over. Your spirit dies when you do it for money and position.

Imagine, you are a software engineer. Working for 9 hours a day. You just got a 100 page specification from your boss. You got a deadline. And now, painful days of working like a machine. 'Machine' because you are to do what you are told. You cannot innovate. You are not allowed to think. You ain't free. Your freedom sold in dollars.

Again, imagine you are an open source developer. The scenario is different now. You are working on a project you chose for yourself. You are enthusiastic about your work. You've new and great ideas and you are gonna implement them. The fun of programming is alive and everyday is a holiday. You innovate new things, and you find appreciation. You find greatness.

As Niyam (at ILUGD) puts it, "You are born unknown. You'll die unknown. You won't be great if you don't do great things. Join the free software community and get known. Then only you'll be called great. Find that greatness!"

As a student, you've taken the freedom you enjoy for granted. But once you've a boss, you are no longer free. You are a slave of your boss, and your boss is a slave of his boss.

You don't need to be a Linux geek to join us. All you need is a belief in software freedom. There's no second to waste. Rise against ignorance. Make everyone aware of free software. Participate in development! Get known. Get your friends involved.

Freed.in is near. Fasten your seat belts! We've to make it bigger than ever. Free your friends from proprietary software. Involve as many people as you can! Tell everyone what is free software. Tell them to use it. Invite them to Freed.

Gear up! The world is yours!

And don't hesitate to ask me if you don't know what to do.

Big Button - 120x60

Friday, August 24, 2007

To blog or not to blog

But that's not the question, since I've decided finally, after so many requests from so many people (read exaggeration), to blog. This is intended to be my technical blog, about all the funny and stupid and tragic things that I do with technology.

Why Tux Playground? Tux is the name of the penguin that first chased Linus Torvalds, and since then, the whole world has been chasing him, including me. Playground because its all about hacks and tweaks! I think thats all about it. See you again :-)