simple examples of how to

Thursday, October 20, 2011

[Linux Kernel] What is the main function in Linux kernel ?

In x86 architecture, Linux booting starts with reading MBR (master boot record) into main memory.
The instructions in MBR look up an active partition table and executes a boot loader such as GRUB, LILO, etc.
After users select one of OS, the boot loader loads the selected OS and calls start_kernel() function in "init/main.c" in the Linux kernel source tree.
The terms after the OS name in GRUB (such as ro root=/dev/hda1) are passed to start_kernel() function similar to argc and argv[] in c programs.
That is argv_init variable.

[GNUPLOT] gnuplot 3D plot example

GNUPLOT source (plot.gnuplot)

set surface
set contour surface
set view 60, 30, 1, 1
set clabel '%8.2f'
set key right
set title 'Graph Title'
set xlabel 'vss'
set ylabel 'sbb'
set zlabel 'closs'
set term png
set output 'jf.png'
splot 'gnuplot-closs-3d.data' using 2:1:3 notitle with pm3d


data source (gnuplot-closs-3d.data)

400 0 25200
400 1000 33036
400 2000 21044
400 3000 19764
400 4000 5344
400 5000 92
400 6000 0
400 7000 0
400 8000 0

300 0 20344
300 1000 24204
300 2000 19476
300 3000 15992
300 4000 11020
300 5000 116
300 6000 4
300 7000 4
300 8000 0

200 0 23352
200 1000 19084
200 2000 11072
200 3000 28472
200 4000 22552
200 5000 32
200 6000 8
200 7000 0
200 8000 16

180 0 30648
180 1000 31264
180 2000 18396
180 3000 23688
180 4000 15956
180 5000 10352
180 6000 4
180 7000 16
180 8000 0

160 0 24008
160 1000 28172
160 2000 32704
160 3000 21632
160 4000 9664
160 5000 20
160 6000 12
160 7000 0
160 8000 16

140 0 21824
140 1000 29428
140 2000 22972
140 3000 23176
140 4000 17588
140 5000 144
140 6000 8
140 7000 20
140 8000 0

120 0 34584
120 1000 33220
120 2000 23788
120 3000 22912
120 4000 11976
120 5000 1004
120 6000 4
120 7000 0
120 8000 0

100 0 20768
100 1000 26288
100 2000 23484
100 3000 19332
100 4000 28984
100 5000 9760
100 6000 140
100 7000 4
100 8000 0

80 0 34156
80 1000 29908
80 2000 29596
80 3000 21380
80 4000 12008
80 5000 27508
80 6000 332
80 7000 0
80 8000 0

60 0 23300
60 1000 21892
60 2000 26108
60 3000 18168
60 4000 23992
60 5000 20780
60 6000 1116
60 7000 8
60 8000 0

40 0 26176
40 1000 26428
40 2000 24232
40 3000 22684
40 4000 25632
40 5000 21244
40 6000 9292
40 7000 4652
40 8000 0

20 0 26864
20 1000 30564
20 2000 36524
20 3000 29248
20 4000 32988
20 5000 18144
20 6000 700
20 7000 676
20 8000 0

0 0 27300
0 1000 27656
0 2000 29148
0 3000 29216
0 4000 29548
0 5000 13992
0 6000 3432
0 7000 1200
0 8000 0

Wednesday, October 19, 2011

[PYTHON] struct.pack example

struct.pack("!LHL", 1, 0, int(str, 16));

Make a packed network-byte order (!) binary that has type unsigned long (L), unsigned short (H), unsigned long (L),
The values are given as:
  • First unsigned long is 1
  • Second unsigned short is 0
  • Last unsigned long is int(str, 16), where str contains hexadecimal (16) presentation.

Monday, October 17, 2011

[Linux] How to show the device info in ubuntu?

in ubuntu, try
$ udevadm info -q all -n /dev/sda

it is the same as
$ udevinfo -a -p /dev/sda


[Linux Device Driver] The difference between interrupt, softirq, and tasklet

All three are for handling interrupts. The difference is the speed. There is also a way to postpone the execution of interrupt: that is work_queue

interrupt: interrupt-context (top-half), most fast but should be very light. no sleep, no mutex-locking (only spin-lock)

softirq: interrupt-context (bottom-half), medium fast, no re-enterant, no preemption, not reasonably generally for device drivers.

tasklet: interrupt-context (bottom-half), not fast, no re-enterant, no preemption,

work_queue: process-context, slow, allow sleep and locking, re-enterant, preemption

[Linux Device Driver] tasklet control functions

tasklet is used for implementing bottom-half interrupt handler to handle heavy and slow interrupts

tasklet_enable: enable tasklet
tasklet_disable: disable tasklet
tasklet_disable_nosync: similar to disable_irq_nosync. disables tasklet without waiting the completion of currently running tasklets

tasklet is not re-enterant similar to interrupt service routine

[Linux] Interesting files in /proc/

Original link: http://www.centos.org/docs/5/html/Deployment_Guide-en-US/s1-proc-topfiles.html


3.2. Top-level Files within the proc File System

3.2.1. /proc/apm

This file provides information about the state of the Advanced Power Management (APM) system and is used by the apm command. If a system with no battery is connected to an AC power source, this virtual file would look similar to the following:

1.16 1.2 0x07 0x01 0xff 0x80 -1% -1 ?
Running the apm -v command on such a system results in output similar to the following:

APM BIOS 1.2 (kernel driver 1.16ac) AC on-line, no system battery
For systems which do not use a battery as a power source, apm is able do little more than put the machine in standby mode. The apm command is much more useful on laptops. For example, the following output is from the command cat /proc/apm on a laptop while plugged into a power outlet:

1.16 1.2 0x03 0x01 0x03 0x09 100% -1 ?
When the same laptop is unplugged from its power source for a few minutes, the content of the apm file changes to something like the following:

1.16 1.2 0x03 0x00 0x00 0x01 99% 1792 min
The apm -v command now yields more useful data, such as the following:

APM BIOS 1.2 (kernel driver 1.16) AC off-line, battery status high: 99% (1 day, 5:52)

3.2.3. /proc/cmdline

This file shows the parameters passed to the kernel at the time it is started. A sample /proc/cmdline file looks like the following:

ro root=/dev/VolGroup00/LogVol00 rhgb quiet 3
This tells us that the kernel is mounted read-only (signified by (ro)), located on the first logical volume (LogVol00) of the first volume group (/dev/VolGroup00). LogVol00 is the equivalent of a disk partition in a non-LVM system (Logical Volume Management), just as /dev/VolGroup00 is similar in concept to /dev/hda1, but much more extensible.

For more information on LVM used in Red Hat Enterprise Linux, refer to http://www.tldp.org/HOWTO/LVM-HOWTO/index.html.

Next, rhgb signals that the rhgb package has been installed, and graphical booting is supported, assuming /etc/inittab shows a default runlevel set to id:5:initdefault:.

Finally, quiet indicates all verbose kernel messages are suppressed at boot time.

3.2.6. /proc/devices

This file displays the various character and block devices currently configured (not including devices whose modules are not loaded). Below is a sample output from this file:

Character devices:
1 mem
4 /dev/vc/0
4 tty
4 ttyS
5 /dev/tty
5 /dev/console
5 /dev/ptmx
7 vcs
10 misc
13 input
29 fb
36 netlink
128 ptm
136 pts
180 usb

Block devices:
1 ramdisk
3 ide0
9 md
22 ide1
253 device-mapper
254 mdp
The output from /proc/devices includes the major number and name of the device, and is broken into two major sections: Character devices and Block devices.

Character devices are similar to block devices, except for two basic differences:

Character devices do not require buffering. Block devices have a buffer available, allowing them to order requests before addressing them. This is important for devices designed to store information — such as hard drives — because the ability to order the information before writing it to the device allows it to be placed in a more efficient order.

Character devices send data with no preconfigured size. Block devices can send and receive information in blocks of a size configured per device.

For more information about devices refer to the following installed documentation:

/usr/share/doc/kernel-doc-/Documentation/devices.txt

3.2.7. /proc/dma

This file contains a list of the registered ISA DMA channels in use. A sample /proc/dma files looks like the following:

4: cascade

3.2.11. /proc/interrupts

This file records the number of interrupts per IRQ on the x86 architecture. A standard /proc/interrupts looks similar to the following:

CPU0
0: 80448940 XT-PIC timer
1: 174412 XT-PIC keyboard
2: 0 XT-PIC cascade
8: 1 XT-PIC rtc
10: 410964 XT-PIC eth0
12: 60330 XT-PIC PS/2 Mouse
14: 1314121 XT-PIC ide0
15: 5195422 XT-PIC ide1
NMI: 0
ERR: 0
For a multi-processor machine, this file may look slightly different:

CPU0 CPU1
0: 1366814704 0 XT-PIC timer
1: 128 340 IO-APIC-edge keyboard
2: 0 0 XT-PIC cascade
8: 0 1 IO-APIC-edge rtc
12: 5323 5793 IO-APIC-edge PS/2 Mouse
13: 1 0 XT-PIC fpu
16: 11184294 15940594 IO-APIC-level Intel EtherExpress Pro 10/100 Ethernet
20: 8450043 11120093 IO-APIC-level megaraid
30: 10432 10722 IO-APIC-level aic7xxx
31: 23 22 IO-APIC-level aic7xxx
NMI: 0
ERR: 0
The first column refers to the IRQ number. Each CPU in the system has its own column and its own number of interrupts per IRQ. The next column reports the type of interrupt, and the last column contains the name of the device that is located at that IRQ.

Each of the types of interrupts seen in this file, which are architecture-specific, mean something different. For x86 machines, the following values are common:

XT-PIC — This is the old AT computer interrupts.

IO-APIC-edge — The voltage signal on this interrupt transitions from low to high, creating an edge, where the interrupt occurs and is only signaled once. This kind of interrupt, as well as the IO-APIC-level interrupt, are only seen on systems with processors from the 586 family and higher.

IO-APIC-level — Generates interrupts when its voltage signal is high until the signal is low again.

3.2.19. /proc/meminfo

This is one of the more commonly used files in the /proc/ directory, as it reports a large amount of valuable information about the systems RAM usage.

The following sample /proc/meminfo virtual file is from a system with 256 MB of RAM and 512 MB of swap space:

MemTotal: 255908 kB
MemFree: 69936 kB
Buffers: 15812 kB
Cached: 115124 kB
SwapCached: 0 kB
Active: 92700 kB
Inactive: 63792 kB
HighTotal: 0 kB
HighFree: 0 kB
LowTotal: 255908 kB
LowFree: 69936 kB
SwapTotal: 524280 kB
SwapFree: 524280 kB
Dirty: 4 kB
Writeback: 0 kB
Mapped: 42236 kB
Slab: 25912 kB
Committed_AS: 118680 kB
PageTables: 1236 kB
VmallocTotal: 3874808 kB
VmallocUsed: 1416 kB
VmallocChunk: 3872908 kB
HugePages_Total: 0
HugePages_Free: 0
Hugepagesize: 4096 kB
Much of the information here is used by the free, top, and ps commands. In fact, the output of the free command is similar in appearance to the contents and structure of /proc/meminfo. But by looking directly at /proc/meminfo, more details are revealed:

MemTotal — Total amount of physical RAM, in kilobytes.

MemFree — The amount of physical RAM, in kilobytes, left unused by the system.

Buffers — The amount of physical RAM, in kilobytes, used for file buffers.

Cached — The amount of physical RAM, in kilobytes, used as cache memory.

SwapCached — The amount of swap, in kilobytes, used as cache memory.

Active — The total amount of buffer or page cache memory, in kilobytes, that is in active use. This is memory that has been recently used and is usually not reclaimed for other purposes.

Inactive — The total amount of buffer or page cache memory, in kilobytes, that are free and available. This is memory that has not been recently used and can be reclaimed for other purposes.

HighTotal and HighFree — The total and free amount of memory, in kilobytes, that is not directly mapped into kernel space. The HighTotal value can vary based on the type of kernel used.

LowTotal and LowFree — The total and free amount of memory, in kilobytes, that is directly mapped into kernel space. The LowTotal value can vary based on the type of kernel used.

SwapTotal — The total amount of swap available, in kilobytes.

SwapFree — The total amount of swap free, in kilobytes.

Dirty — The total amount of memory, in kilobytes, waiting to be written back to the disk.

Writeback — The total amount of memory, in kilobytes, actively being written back to the disk.

Mapped — The total amount of memory, in kilobytes, which have been used to map devices, files, or libraries using the mmap command.

Slab — The total amount of memory, in kilobytes, used by the kernel to cache data structures for its own use.

Committed_AS — The total amount of memory, in kilobytes, estimated to complete the workload. This value represents the worst case scenario value, and also includes swap memory.

PageTables — The total amount of memory, in kilobytes, dedicated to the lowest page table level.

VMallocTotal — The total amount of memory, in kilobytes, of total allocated virtual address space.

VMallocUsed — The total amount of memory, in kilobytes, of used virtual address space.

VMallocChunk — The largest contiguous block of memory, in kilobytes, of available virtual address space.

HugePages_Total — The total number of hugepages for the system. The number is derived by dividing Hugepagesize by the megabytes set aside for hugepages specified in /proc/sys/vm/hugetlb_pool. This statistic only appears on the x86, Itanium, and AMD64 architectures.

HugePages_Free — The total number of hugepages available for the system. This statistic only appears on the x86, Itanium, and AMD64 architectures.

Hugepagesize — The size for each hugepages unit in kilobytes. By default, the value is 4096 KB on uniprocessor kernels for 32 bit architectures. For SMP, hugemem kernels, and AMD64, the default is 2048 KB. For Itanium architectures, the default is 262144 KB. This statistic only appears on the x86, Itanium, and AMD64 architectures.