Compiling the Linux kernel

This is actually a very old writing. I wrote this back on 2007 in my old blog (which is now deleted). I was building my own Linux From Scratch and needed to compile the kernel as part of that. Later during our OS course we were given an assignment to add a system call in Linux kernel and as a result compiling the kernel was needed. Some of my friends were having trouble with the process. It was not a straight forward task, given the fact that most were unfamiliar with Linux itself. So I decided to write this. I know that now this may not even work for the latest kernel. Still I am keeping this here … just as a sweet memory. Building a Linux From Scratch is something that personally I consider “crazy”.

Frankly speaking, compiling Linux kernel may vary from architecture to architecture and from kernel version to version. Here is one way that you can build on i386.

  1. Download the latest stable kernel from kernel.org. I’m using 2.6.22.1 as an example. 2.6.22.1 can be downloaded from here.
  2. Extract that.
    $ tar xfj linux-2.6.22.1.tar.bz2
  3. Move to the source directory.
    $ cd linux-2.6.22.1
  4. Remove any *.o files from source tree. It is recommended to do this before every compilation, even when the files are just extracted.
    $ make mrproper
  5. Configure the kernel.
    $ make menuconfig
    Well, this is a long process. You can do lots of things. You can optimize for specific processor, for desktop or server, build the kernel with debugging symbols, even you can add your name with kernel version :-). Explore the options as much as possible.
  6. Compile the kernel.
    $ make
  7. If you have configured some parts of the kernel as loadable module (you should do that), install those modules.
    # make modules_install
    Modules are installed in /lib/modules. Here is a caution: if you are building the same version as your running version (or any existing version), modules will be overwritten and you may damage your running system. So be careful before doing this. It’s better to append a custom string after kernel release (local version) during configuring.
  8. arch/i386/boot/bzImage is the built kernel image. Copy that to your boot partition.
    # cp arch/i386/boot/bzImage /boot/bzImage-2.6.22.1
  9. Copy the symbol map file.
    # cp System.map /boot/System.map-2.6.22.1
  10. Your configuration is stored in .config file. Save that for future reference.
    # cp .config /boot/config-2.6.22.1
  11. Create an initial ram disk for your kernel.
    # mkinitrd /boot/initrd-2.6.22.1 2.6.22.1
  12. Modify your boot loader’s configuration file. Most of the distros use grub now. /boot/grub/grub.conf or /boot/grub/menu.lst is the grub configuration file. Modify whichever is present in your distro. On some distros both are present and one is a link to another. In that case you can work with anyone, no doubt. Add the following lines:

    title 2.6.22.1
    root (hd0,7)
    kernel /bzImage-2.6.22.1 ro root=LABEL=/1 rhgb quiet
    module /initrd-2.6.22.1


    What’s the meaning of all these? First line is the title which will be shown in the menu during boot time. Second line means that my boot partition is /dev/hda8. hd0 means primary master (hda) and 7 means hda8. (Yes, grub starts from zero). Third line shows the path of kernel image. ro root=LABEL=/1 rhgb quiet are the booting options. LABEL is specified in my /etc/fstab file. Fourth line is the path of initial ram disk. Both kernel and initrd paths are relative to the partition which is specified in the second line. In my system it is hda8.

    Obviously these will vary from system to system. Now here is a big question: what will happen if you don’t know what is your boot partition, what is your root partition, what lies in /etc/fstab and what are the options to the kernel?? You will get all these information from grub configuration file. How? Just see the entry of your existing running kernel :-).

    And finally, if you see a panic something like “policy checking”, add enforcing=0 in the kernel option list.
  13. Now reboot and enjoy 🙂

Special Thanks to:

  • Sayeed Hyder Sir, who was our OS course teacher and tried lots of interesting things.
  • Linux from scratch project. Here is their kernel building page.
Advertisements
Posted in Linux | Tagged | 1 Comment

AS3: Load assets dynamically for better performance

Asset management is a crucial point of every game. These assets may include bitmap images, audio and video files, animations, 3D models etc. etc. Depending on the size of the game there might be hundreds of such assets. How these assets and handled can have severe impact on the performance and user experience. One way to handle the assets is to embed all the assets with the main swf file. The obvious result is a big main swf file which will take more time to load and user will need to wait more. This leads to a very bad user experience for medium size games and just impossible for big size games. Better way is to embed only the required assets to start the game and load the remaining things after the game starts or load them only when that is needed. This requires dynamic loading of assets, the thing that I am going to discuss here.

In this tutorial I am going to create a simple asset file which contains 3 bitmap images, load that asset file dynamically and then load the bitmaps from the asset file. I have assumed that the reader is already familiar with the AS3 language and flash itself. That means I won’t explain what is a class, method or what is a swf or fla file.

Prepare the project and asset file

  • Create a new AS project named LoaderDemo. I am using Adobe Flash Builder 4 for this. Make sure that Copy non-embed files to output folder is checked under project Properties/ActionScript Compiler (by default it is checked).
  • Create a folder named Asset under the project directory. The fla file will be stored here. Note that this is not under src directory. Only the published swf file is needed in the output directory, so there is no need to store fla file under src directory.
  • Create a fla file under Asset folder. Create three rectangle images of size 100×50 and fill them with color red, green and blue by using any image manipulation software like photoshop or paint. These are the bitmap images that will be loaded from the asset file. Drag and drop the files to fla’s Library panel. The bitmaps will be copied to the fla.
  • Now the images are stored in the fla. An way to access them after the file is loaded is required. Right click on the blue.png and select Properties. Check Export for ActionScript and edit the Class to BoxBlue. The base class is BitmapData.
  • Same thing is done for the other two images. Now the linkage for all bitmaps are established.
  • Publish the file. If there is a warning something like new classes will be generated then select Yes. This will create the asset.swf file which contains the bitmaps.
  • Create asset folder under src and copy the swf to src/asset. After building the project this swf will be available in output folder.
  • And we are done with the asset preparation.

Load the swf dynamically

  • Now comes the coding part. We want to load the asset after the main swf file is loaded. Add a listener for the ADDED_TO_STAGE event in the constructor of main LoaderDemo class. The swf will be loaded from this listener function.
    package
    {
        import flash.display.Sprite;
        import flash.events.Event;
        
        public class LoaderDemo extends Sprite
        {
            public function LoaderDemo()
            {
                addEventListener(Event.ADDED_TO_STAGE, mainSWFLoaded);
            }
            
            private function mainSWFLoaded(evt:Event):void {            
            }
        }
    }
    
  • Create a class named AssetManager. This will handle the loading of the asset. This class contains an instance of AS Loader class which is used to load asset files, a callback function which will be fired after the loading is completed and the relative path to asset file. The loader is instantiated in the constructor.
    package
    {
        import flash.display.Loader;
    
        public class AssetManager
        {
            private var assetLoader:Loader = null;
            private var loadingCompletedListener:Function = null;
            
            private const ASSET_FILE_PATH:String = "asset/asset.swf";
            
            public function AssetManager()
            {
                assetLoader = new Loader();
            }
        }
    }
    
  • Create a method loadAsset in AssetManager. Loading of the asset swf file will be started when this method is called. It takes a parameter listener which is the callback function that should be fired when the loading is complete. The client of the AssetManager (main class in this example) supplies this function. The Loader class have a property contentLoaderInfo which is an instance of AS LoaderInfo class which can provide various information about the loaded file and also handles the loading related events. We are particularly interested in COMPLETE event. So an event listener loadingComplete for this event is added. From the event listener the callback which was supplied by the client is simply fired so the client is informed that loading is complete. After registering the event actual loading of the asset is started by calling load method of the assetLoader. The parameter of the load is a URLRequest object.
    public function loadAsset(listener:Function):void {
        this.loadingCompletedListener = listener;
        
        assetLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadingComplete);
        assetLoader.load(new URLRequest(ASSET_FILE_PATH));
    }
    
    private function loadingComplete(evt:Event):void {
        loadingCompletedListener();
    }
    
  • Add an instance of AssetManager in LoaderDemo. Instantiate this in mainSWFLoaded and call loadAsset to start the loading.
    private var assetManager:AssetManager = null;
    
    private function mainSWFLoaded(evt:Event):void {
        assetManager = new AssetManager();
        assetManager.loadAsset(assetLoaded);
    }
    
    private function assetLoaded():void {
        trace("asset loading complete");
    }
    

    The trace is printed when the loading is complete.

  • Now the swf containing the bitmaps is loaded. We need to access the bitmaps from it. During the asset preparation we linked the red, green and blue bitmap files against the class definition BoxRed, BoxGreen and BoxBlue respectively, which all are subclass of BitmapData. The first thing that is required is to get the class definition from the loaded swf. After getting the class definition we can instantiate them.

    The contentLoaderInfo property (which is an instance of LoaderInfo) of assetLoader contains a property named applicationDomain, an instance of AS class ApplicationDomain which is a container of class definitions. All the classes that are defined in the swf file can be accessed by using methods of applicationDomain. So, define a method getClassDefinition in AssetManager which takes a definition string as parameter and check whether the class is present in the swf. If present then that class definition is returned as Class object.

    public function getClassDefinition(definition:String):Class {
        if (assetLoader.contentLoaderInfo.applicationDomain.hasDefinition(definition)) {
            return assetLoader.contentLoaderInfo.applicationDomain.getDefinition(definition) as Class;
        }
        
        return null;
    }
    
  • Define a method createBitmap in LoaderDemo which takes a class name as string and returns the Bitmap if the class is present in the swf. It first gets the Class object from assetManager by calling getClassDefinition. The returned class object is a subclass of BitmapData. Then a new object is instantiated from the returned class object. The constructor of BitmapData takes two parameter, width and height. In this case that is set correctly from the asset, even then we need to pass 0, 0 for them. And then a Bitmap is created from the data and returned.
    private function createBitmap(bmpClassName:String):Bitmap {
        var bmpClass:Class = assetManager.getClassDefinition(bmpClassName);
        
        if (bmpClass) {
            var bmpData:BitmapData = new bmpClass(0, 0) as BitmapData;
            return new Bitmap(bmpData);
        }
        
        return null;
    }
    
  • We are done. The only remaining thing is to test the code. So we create three bitmaps in assetLoaded and add them as the child.
    private function assetLoaded():void {
        trace("asset loading complete");
        
        var bmp1:Bitmap = createBitmap("BoxRed");
        addChild(bmp1);
        
        var bmp2:Bitmap = createBitmap("BoxGreen");
        bmp2.y = 100;
        addChild(bmp2);
        
        var bmp3:Bitmap = createBitmap("BoxBlue");
        bmp3.y = 200;
        addChild(bmp3);
    }
    
  • After running the project, here is the output.

Few final notes

  • This example handles only one asset file. In real situation more than one asset file might be required to load dynamically.
  • assetManager is a member of LoaderDemo. A Singleton might be a better choice.
  • createBitmap is a method of LoaderDemo. This might be okay for a small tutorial like this. But in reality a Factory should be used.
  • Bitmaps are not handled here efficiently. createBitmap creates a new bitmap every time it is called. However, there should be no reason to load the same bitmap data more than once in memory. Bitmap data can be shared efficiently by using a Flyweight.
  • The asset file path is hardcoded in this example. However, it is better to read them from a config file.
  • Though this example contains only bitmap assets, the same technique can be used for other types of assets.
  • AS3 reference manual contains detail information on Loader, URLRequest, LoaderInfo and ApplicationDomain classes.
  • A nice tutorial on loader can be found here.
  • There are other methods to manage assets. They can be found here.

The full code for this tutorial can be downloaded from here. Any feedback is welcome.

Posted in AS3 | Tagged | 5 Comments

Let Bash make the birthday wish

One of my colleagues birthday was a few days ago (December 22). He is our network/sysadmin and is a big Linux fan (nothing surprising, all sysadmins of the world tend to be Linux fan). So I decided to write a one line bash script for him and posted it to his Facebook wall.

[ "`date | cut -d ' ' -f 2,3`" == "Dec 22" ] && echo "happy birthday to LL bhai"

What it does? Nothing serious at all. If it is run on December 22 then it will print happy birthday to LL bhai (“LL bhai” is his name, nothing related to bash).

How it does its work? Pretty simple. date command is used to get the system date. Output is piped to the cut command which splits the date string by space and retrieves field 2 and 3. Then that is compared against target "Dec 22" by using [ command which is equivalent (other than the syntactic sugar) to test command. If the comparing is true then the second part of logical and (&&) is executed which is a echo command. And that’s all.

The fun part is if this single line is placed in bash startup file (~/.bash_profile if interactive login shell and ~/.bashec if interactive but not login shell) then on every year our LL bhai will get this wish. Every time bash is run on Dec 22, this wish will be printed. Everybody may forget, but Bash will never forget his birthday.

Just a few ending notes and references:

  • Bash Guide for Beginners and Shell Programming chapter on Beginning Linux Programming might be the best places to start for complete beginners to bash scripting.
  • This is not a great script. It is possible to remove the cut command entirely by using some options to date. No doubt, there are more than one ways to do a single task.
  • It requires some changes to work on other date. First, “Dec 22” is hardcoded, and second, if the date is one digit long then the argument of cut requires changing.

Happy Linuxing.

Posted in Linux | Tagged | 2 Comments

Implement a true Singleton in AS3

Singleton is a design pattern that enforces that a class can have only one instance which is shared by all and provides an easy global access point to this. A few examples are:

  • In iOS the UIDevice class is a singleton which provides information about the device like its unique ID. Obviously there is not more than one device.
  • Every application running in iOS must have a single instance of UIApplication class. Naturally that is a Singleton.
  • In a game generally there is one single GameControl object which manages the main game loop. If someone can create more than one game control and can run the main loop more than once, it can damage a lot.
  • Several design patterns like Abstract Factory, Flyweight uses Singleton in their implementation.

Although Singleton provides a global access, it have two benefits over using global variable. First, it does not pollute the global name space and second, it can be lazy initialized i.e. unlike global variables it can be instantiated only when it is required.

Okay, that’s enough talking. Let’s see how we can implement a Singleton in AS3.

package
{
    public class Singleton
    {
        private static var _instance:Singleton = null;

        public function Singleton()
        {
        }

        public static function sharedInstance():Singleton {
            if (_instance == null) {
                _instance = new Singleton();
            }

            return _instance;
        }
    }
}

This is the most straight forward way to implement a Singleton. First we declare a private static variable _instance in Singleton class and then create a public static method sharedInstace(). In sharedInstance() we check whether _instance is null or not. If it is null, then sharedInstance() is called for the first time. So we instantiate the instance of Singleton. Any further call to sharedInstance() will return the _instance created earlier, instead of creating a new one. As a result every call to sharedInstance() returns the same instance.

We can test this easily.

var a:Singleton = Singleton.sharedInstance();
var b:Singleton = Singleton.sharedInstance();

if (a == b) {
    trace("a and b are same");
} else {
    trace("a and b are not same");
}

Output is naturally a and b are same.

Note that Singleton.sharedInstance() is the global access point to the shared instance that the Singleton is supposed to provide. And also the instance is not created unless sharedInstance() is called, unlike a global variable.

But we have a little problem here. Calling to sharedInstance() will return the shared _instance, but Singleton class does not prevent to create new objects of it. In fact it is fairly easy to create a new Singleton object which is not shared, something that should be enforced by the Singleton pattern.

var c:Singleton = new Singleton();
var d:Singleton = new Singleton();

if (c == d) {
    trace("c and d are same");
} else {
    trace("c and d are not same");
}

Naturally c and d are not same.

The solution of this is to make the constructor of Singleton non-public so that new Singleton() is no longer valid.

private function Singleton()
{
}

And opps, the compiler have problem with this. It is saying “A constructor can only be declared public”. Now we have a big problem. Making the constructor non-public is the approach taken by most of the languages like C++ or Java, but AS3 does NOT support non-public constructor. So in AS3 we cannot prevent the creation of Singleton object in this way. This poses the real difficulty of implementing a true Singleton in AS3.

A few approaches, all equally weird, are available to prevent the creation. Personally I like the following.

public class Singleton
{
    private static var _instance:Singleton = null;

    public function Singleton(caller:Function = null)
    {
        if (caller != preventCreation) {
            throw new Error("Creation of Singleton without calling sharedInstance is not valid");
        }
    }

    private static function preventCreation():void {
    }

    public static function sharedInstance():Singleton {
        if (_instance == null) {
            _instance = new Singleton(preventCreation);
        }

        return _instance;
    }
}

A private static method preventCreation (I wanted to name it weirdMethod) is defined and it is passed to the constructor from sharedInstance. The constructor compares the caller and throws a run time error if the caller is not preventCreation. As preventCreation is private, it is not available outside. So trying to do new Singleton() from outside will fail in run time. However, this method does not force compile time prevention.

The whole demo project can be downloaded from here.

And now some final ending notes:

  • Singleton can be generalized to create ‘n’ number of objects instead of creating 1. But that is not as common as the single instance version where n = 1.
  • Before creating a Singleton make sure that a singleton is really needed. There is a huge debate on whether Singleton should be used or not, and there are people who consider Singleton as an anti-pattern. Those discussions are beyond the scope of this current writing, neither they are specific to AS3. May be in future I will write about that too, but for now check Why Singletons are Evil and Singleton: How should it be used.
  • For a better understanding of Singleton check Design Patterns book by GoF and Wikipedia entry for Singleton.
  • There are other ways, possibly better, to implement Singleton in AS3. Check Singleton Pattern in AS3 which explains another way and discusses two common mistakes.

As always, any feedback and/or suggestion is welcome.

Update: Even this process of creating Singleton can be broken by extending Singleton as explained here. Thanks to Krilnon for pointing this out.

Posted in AS3, Design Pattern | Tagged , | 6 Comments