30 June 2011

spinlock

volatile modifier tells compiler that value may change in a way which is not predictable by the compiler. it guaranties that the compiler won't re-order volatile read and write.
- useful for memory-mapped I/O and shared memory.

volatile guaranties that the compiler will generate ordered volatile read and write, but CPU may reorder the execution.

memory barriar is used to avoid the CPUs employ performance optimizations that can result in out-of-order execution.
Compiler memory barrier - prevent a compiler from reordering instructions, they do not prevent reordering by CPU. ex:- asm volatile ("" : : : "memory");

why spinlock required???

1. protects data from multiple thread of execution
==> spin + ena/dis preemption

2. protects data from interruct handler
==> ena/dis interrupt + spin + ena/dis preemption

SMP + PREEMPT ==> spin + disable preemption
SMP + NO PREEMPT ==> spin
NO SMP + PREEMPT ==> disable kernel preemption
NO SMP + NO PREEMPT ==> NULL

==> spinlock protects critical region
==> spinlock provides mutual exclusion
==> spinlock marks the non-preemptible regions

; Intel syntax
lock:
dd 0 ;

spin_lock:
mov eax, 1 ;
xchg eax, [lock] ;
test eax, eax ;
jnz spin_lock ;
ret ;

spin_unlock:
mov eax, 0 ;
xchg eax, [lock] ;
ret ;



Reference:
http://en.wikipedia.org/wiki/Spinlock#Example_implementation
http://en.wikipedia.org/wiki/Memory_barrier
http://en.wikipedia.org/wiki/Memory_ordering

21 June 2011

Hang a linux box with relatime process?

A runnable SCHED_FIFO task continues to run until it blocks or explicitly yields the processor; it has no timeslice and can run indefinitely.
A SCHED_RR is identical to SCHED_FIFO except that each process can run only until it exhausts a predetermined timeslice.

/proc/sys/kernel/sched_rt_runtime_us: A global limit on how much time realtime scheduling may use.

Since 2.6.25 kernel, the default values for sched_rt_period_us (1000000 or 1s) and sched_rt_runtime_us (950000 or 0.95s). This gives 0.05s to be used by SCHED_OTHER (non-RT tasks). In other words, the group scheduler is configured, by default, to reserve 5% of the CPU for non-SCHED_FIFO tasks.

Download testrt.c
root permission is required to execute this!!!

# gcc -o testrt testrt.c -lrt -Wall
# ./testrt
using realtime, priority: 20

# echo 1000000 > /proc/sys/kernel/sched_rt_runtime_us

system is hang!!!
Download simple-module.c
Download Makefile.mod
# mv Makefile.mod Makefile
# make
# insmod ./simple-module.ko

# cat /proc/getprio
==> [120:120:120:0] [1000] [0x00402100] systemd [1][0]
==> [120:120:120:0] [1000] [0x8020A040] kthreadd [2][0]
==> [120:120:120:0] [1000] [0x8420A040] ksoftirqd/0 [3][2]
==> [120:120:120:0] [1000] [0x8420A060] kworker/u:0 [5][2]
==> [100:100:100:0] [1000] [0x8420A040] khelper [6][2]
==> [120:120:120:0] [1000] [0x8020A040] sync_supers [95][2]
==> [120:120:120:0] [1000] [0x80A0A040] bdi-default [97][2]
==> [100:100:100:0] [1000] [0x8420A040] kblockd [99][2]
==> [100:100:100:0] [1000] [0x8420A040] ata_sff [255][2]
==> [100:100:100:0] [1000] [0x8420A040] md [261][2]
==> [120:120:120:0] [1000] [0x8420A060] kworker/0:1 [359][2]
==> [120:120:120:0] [1000] [0x80A4A840] kswapd0 [370][2]
...
==> [79:120:79:20] [1000] [0x00400100] testrt [3264][2463]
...

Reference:
http://lwn.net/Articles/296419/
http://book.chinaunix.net/special/ebook/Linux_Kernel_Development/0672327201/ch04lev1sec4.html
http://www.kernel.org/doc/Documentation/scheduler/sched-rt-group.txt
https://rt.wiki.kernel.org/index.php/Squarewave-example

20 June 2011

sample c programs

Find first bit in word - ffb.c

# ./ffb
first bit(0xDEADBEAF): 0
first bit(0xDEADBEA0): 5
first bit(0xDEADBE00): 9
first bit(0xDEADB000): 12
first bit(0xDEAD0000): 16
create a daemon and redirect output to logfile - pcreate.c

cat > daemon.sh << EOF
#!/bin/sh
while [ 1 ]; do date; sleep 1; done
EOF

# chmod +x daemon.sh
# gcc -o pcreate pcreate.c
# ./pcreate ./daemon.sh logfile.log
# tail -f logfile.log
program to print U-Boot environment variables - ubootenv.c
hexdump of U-Boot env partition - uboot.env

# gcc -o ubootenv ubootenv.c
# ./ubootenv uboot.env
CRC32: 0xE2A77069
Active Flag: 0x01

bootdelay=0
baudrate=115200
download_baudrate=115200
....
Euclid's Algorithm to find GCD (gcd.c)

# gcc -o gcd gcd.c
# ./gcd 12345 56789
gcd of 12345 and 56789 is 1
sieve of Eratosthenes to find prime numbers (prime.c)

# gcc -o prime prime.c
# ./prime 100
2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97
Tottal number: 25
Simple client/Server communication (server1.c, client1.c)
Simple client/Server communication (server2.c, client2.c)
Simple client/Server communication (server3.c, client3.c)



# gcc -o server serverX.c
# gcc -o client clientX.c

# ./server 5555
Here is the message: hello server

# ./client localhost 5555
Please enter the message: hello server
from server time: Tue Nov 23 05:06:08 2010


Reference:

19 June 2011

linux 2.6 simple module

Download simple-proc.c
Download Makefile.sim
# mv Makefile.sim Makefile
# make
# insmod ./simple-proc.ko

# echo "1" > /proc/arith/sum
# echo "11" > /proc/arith/sum
# echo "111" > /proc/arith/sum

# cat /proc/arith/sum
123

Download simple-module.c
Download Makefile.mod
# mv Makefile.mod Makefile
# make
# insmod ./simple-module.ko

# cat /proc/getprio
==> [120:120:120:0] [1000] [0x00402100] systemd [1][0]
==> [120:120:120:0] [1000] [0x8020A040] kthreadd [2][0]
==> [120:120:120:0] [1000] [0x8420A040] ksoftirqd/0 [3][2]
==> [120:120:120:0] [1000] [0x8420A060] kworker/u:0 [5][2]
==> [100:100:100:0] [1000] [0x8420A040] khelper [6][2]
==> [120:120:120:0] [1000] [0x8020A040] sync_supers [95][2]
==> [120:120:120:0] [1000] [0x80A0A040] bdi-default [97][2]
==> [100:100:100:0] [1000] [0x8420A040] kblockd [99][2]
==> [100:100:100:0] [1000] [0x8420A040] ata_sff [255][2]
==> [100:100:100:0] [1000] [0x8420A040] md [261][2]
==> [120:120:120:0] [1000] [0x8420A060] kworker/0:1 [359][2]
==> [120:120:120:0] [1000] [0x80A4A840] kswapd0 [370][2]
...
==> [79:120:79:20] [1000] [0x00400100] testrt [3264][2463]
...


Reference:
http://www.captain.at/programming/kernel-2.6
http://tldp.org/LDP/lkmpg/2.6/html/lkmpg.html