pthread

A very good one-shot tutorial to the POSIX thread libraries (pthread). Talks all about threading, synchronization and scheduling with example code.

Deadline scheduling for Linux kernel

A new scheduler class has been proposed for including deadline based scheduling algorithms like EDF
The public git repository is available on gitorious
git clone git://gitorious.org/sched_deadline/linux-deadline.git

Generating patches without cleaning code

Instead of generating a patch from two pristine copies of the code (one with your changes and both not compiled), it is useful to create a exclude-list of all the files that you want to ignore.

 .*
 *.lds
 *.o
 *.o.*
 *.a
 *.s
 *.ko
 *.so
 *.so.dbg
 *.mod.c
 *.i
 *.lst
 *.symtypes
 *.order
 *.elf
 *.bin
 *.gz
 *.lzma
 *.patch
 *.gcno
 tags
 TAGS
 bzImage
 vmlinux
 System.map
 Module.markers
 Module.symvers
 !.gitignore
 !.mailmap
 patches-*
 patches
 series
 exclude-list
 filefortag
 filelist
 cscope.*
 ncscope.*
 GPATH
 GRTAGS
 GSYMS
 GTAGS
 *.orig
 *~


Then create the patch

diff --exclude-from /path/to/exclude-list -urNd linux-2.6.24-hunterwala linux-2.6.24 > patch_file

Each line in exclude-list is a pattern which diff would match and ignore the files. I am having trouble excluding following directories.

include/asm
include/asm-*/asm-offsets.h
include/config
include/linux/autoconf.h
include/linux/compile.h
include/linux/version.h
include/linux/utsrelease.h
include/linux/bounds.h
include/generated


So, the -N passed to diff would treat these as new files and include them in the patch. :(

TODO: Update post once I find a better way to handle directories.

[The exclude list above has been taken from .gitignore]

Threading and parallelism

Another good series of articles found on www.embedded.com on threading and parallelism

Part 1: Parallelism and threading primer
Part 2: The threading development cycle
Part 3: Debuging and tuning multi-threaded code

Linux performance analysis for embedded system

Nice series of articles found on www.embedded.com

Part 1: Available tools
Part 2: Profiling/analysis methods and techniques

fscanf

Did not know this until yesterday.

fscanf (fptr, "%*s");
               ^^^
This reads the line from the file but does not store it in any local variable, as compared to this,

fscanf (fptr, "%s", buffer);

Handy '*' :)

Find executables

To find all the executables files

find . -type f -executable -name "whatever*"

Kernel debugging tools

While working inside the kernel you are prone to crashes, oops and complete kernel freeze. Some of these tools\methods help

1. lockdep
http://www.mjmwired.net/kernel/Documentation/lockdep-design.txt

2. netconsole
http://www.mjmwired.net/kernel/Documentation/networking/netconsole.txt

Using netconsole inside Ubuntu
https://wiki.ubuntu.com/KernelTeam/Netconsole

Another netconsole tutorial
http://www.cyberciti.biz/tips/linux-netconsole-log-management-tutorial.html

3. Debugging kernel Oops
https://wiki.ubuntu.com/DebuggingKernelOops

4. Using the built-in kernel debugger
http://oss.sgi.com/projects/kdb/
IBM tutorial (http://www.ibm.com/developerworks/linux/library/l-kdbug/) .. looks dated .. not sure if this works with 2.6.24+ kernels

5. Nice article on Kernel_Debugging_Tips

6. Another brilliant article on Kernel oops

Update: I recently posted a piece on how to use KGDB over the serial null modem cable

Print the IP of the machine at login

Wanted to know the IP address of the test machine the moment I log in. include the following in your .bashrc file.

echo "Welcome to" `hostname` "("`ifconfig eth0 | grep "inet addr:" | awk -F: '{print($2)}' | sed "s/ /:/g" | awk -F: '{print($1)}' `")"

Not very efficient use of sed and awk :(

Script to install a new kernel

#!/bin/sh
#
# (c) Hunterwala

if [ $(/usr/bin/id -u) -ne 0 ]; then
     echo "run this script with sudo"
     exit 2
fi

if [ -z $1 ]; then
     echo "kernel version not found"
     echo "usage: ./install_kernel [kernel_version]"
     exit 2
else
     VERSION=$1
     echo "using $VERSION"
fi

if [ ! -d /lib/modules/$VERSION ]; then
     echo "kernel modules not installed"
     echo "run make install_modules from the kernel directory"
     exit 2
fi

BOOTDIR=/boot

echo "Installing kernel ........................"
cp $PWD/.config $BOOTDIR/config-$VERSION
cp $PWD/System.map $BOOTDIR/System.map-$VERSION
cp $PWD/arch/i386/boot/bzImage $BOOTDIR/vmlinuz-$VERSION
echo "done"

echo "Creating initrd image ...................."
update-initramfs -c -k $VERSION
echo "done"

echo "Updating grub ............................"
update-grub
echo "done"

echo "Linux kernel $VERSION successfully installed"
echo "Please restart and select $VERSION in grub menu"


Kernel module programming

Found a nice article on TLDP on Linux kernel module programming in the 2.6 kernel. (link)

Using VLC player to play Internet Radio

Agent M sent me a link to a very good online western classical radio site. In order to play the radio inside Firefox on Ubuntu the gstreamer plugins are required. The other option is to play it through VLC player.

Install VLC player
%% sudo aptitude install vlc

Open the link in VLC, Media->Open Network, enter the URL and you are good to go.

To avoid this step every time, create a launcher.

1. Right click on the desktop, "Create launcher"
2. Give it a name, "My Radio"
3. In the command use the following /usr/bin/vlc "URL"
4. You can specify a tool-tip comment as well, "ABCD Radio"
5. Click on the icon and select the icon of your choice form /usr/share/icons/hicolor/scalable

Enjoy the music

Battery monitoring from console

If you prefer to work on a console based machine (without gdm\kdm), the following script is useful if you are running your laptop on battery. It shows the current battery charge and can be used for monitoring the battery discharge.

#!/bin/sh
# While working in console mode (without gdm) this script
# tells you the remaining charge of your laptop battery
#
# Run it along with watch on a separate tty
# $ watch -n 10 ./batmon
#
# (c) Hunterwala, 2009
#
# TODO: check for error levels and buzz using pcspkr mod
# when the charge level decreases beyond that level.

PROC_PATH="/proc/acpi/battery/BAT0"

org_cap=`cat $PROC_PATH/info | grep "last full capacity" | sed "s/ //g" | awk -F: '{print($2)}' | sed "s/mAh//g"`
rem_cap=`cat $PROC_PATH/state | grep "remaining capacity" | sed "s/ //g" | awk -F: '{print($2)}'| sed "s/mAh//g"`
message=`cat $PROC_PATH/state | grep "charging state" | sed "s/ //g" | awk -F: '{print($2)}'| sed "s/mAh//g"`

diff=$(echo "($org_cap-$rem_cap)" | bc)
total_per=$(echo "100-($diff*100/$org_cap)" | bc)

echo "Battery is" $message ":" $total_per"%"


Save this script as batmon.sh

Change the permissions chmod a+x batmon.sh

Run it on a separate tty watch -n 10 ./batmon.sh