Tuesday, June 23, 2009

Debug bash function interactively


#!/bin/bash

# processing command line parameters
if [ $# -eq 2 ]; then
p1=$1
p2=$2
fi

# setup some environment
export A=a
export B=b

tmpfile=/tmp/myshell-$$-$RANDOM

trap "rm $tmpfile" EXIT

cat <<EOF > $tmpfile
if [ -f /etc/bash.bashrc ]; then
source /etc/bash.bashrc
fi

if [ -f $HOME/.bashrc ]; then
source $HOME/.bashrc
fi

source <shell functions in this file>

export PS1="My Shell$ "
EOF

/bin/bash --rcfile $tmpfile

Shell script parameters - a simpler way


#!/bin/sh

for param in "$@"; do
eval "$param"
done

param1=${param1:-param1}
param2=${param2:-param2}
param3=${param3:-param3}

echo $param1 $param2 $param3

Usage:


param.sh param1=<param1> param2=<param2> param3=<param3>

Friday, June 19, 2009

GNOME desktop layout for small screen

Default GNOME desktop layout is for typical workstation screen (> XGA). For smaller screen such as netbook (typically 1024x600), too much vertical space is been occupied by two desktop panel and window title bar. This is worse for wide screen, which has relatively small vertical space.

Desktop screenshot before optimizing

To optimize vertical space utility, the following method can be used:

  • Remove bottom panel (or top panel)
  • Replace expanded menubar in panel with collapsed one.
  • Compile and install window-picker-applet, which provides a compact task icon list and focus task title.

    It can downloaded from here. I write a small patch to add window maximizing toggling support to it, which can be downloaded from here.

  • Remove window title bar for maximized window. For metacity this can be done via theme customization, that is, change the "frame_geometry" with name "normal_maximized" as follow:
 <frame_geometry name="normal_maximized" parent="normal"  rounded_top_left="false"
rounded_top_right="false" rounded_bottom_left="false" rounded_bottom_right="false" has_title="false">
<!-- strip frame spacing off the normal geometry when maximized -->
<distance name="left_width" value="0"/>
<distance name="right_width" value="0"/>
<distance name="bottom_height" value="0"/>
<distance name="left_titlebar_edge" value="0"/>
<distance name="right_titlebar_edge" value="0"/>
<distance name="title_vertical_pad" value="0"/>
<border name="title_border" left="0" right="0" top="0" bottom="0"/>
</frame_geometry>

Desktop screenshot after optimizing

Saturday, June 13, 2009

Reading notes: optimizing Linux performance

Chapter 3

  1. vmstat can show swap in/out pages. A simultaneously high swap-in and swap-out rate could indicate that the system does not have enough memory to handle all the running processes.
  2. slabtop is "top" for slab.

Chapter 4

  1. strace can show system call performance statistics too (strace -c).
  2. ltrace: performance statistics for library (like strace, but library function instead of system call).
  3. ld.so: dynamic load/link statistics for libraries. Example: env LD_DEBUG=statistics LD_DEBUG_OUTPUT=lddebug gcalctool

Chapter 6

  1. iostate: show disk IO statistics

Chapter 7

  1. netstat -c: period update
  2. netstat -t: choose tcp protocol
  3. netstat -l, -a: listening sockets or listening + conntected.

Chapter 8

  1. script: record what you have done and the output, seems useful.
  2. gcc -g3: provide more debug information then gcc -g (default to
    • g2), such as the macro definitions present in the source.

Chapter 11

  1. Use gdb to check call chain:
    • Set a breakpoint at check-point
    • gdb can execute a given set of commands when it hits a breakpoint. By using the command command, we can tell gdb to execute bt; cont every time it hits the breakpoint.
    • Conditional breakpoint can be implemented via gdb script (commands) too. For example, we can enable breakpoint 2, when we reach breakpoint 1.
    • gdb output can be logged to a text file via: set logging on
  2. oprofile reveals which function is hot, but do not show how many times it is called. ltrace or some other traces shows which function (including subfunctions) consumes most CPU time. This is from two perspective, and they can be combined to inspect the performance property.
  3. For Fedora and Enterprise Linux, Red Hat provides a set of debuginfo rpms that contain all the symbol information and sources that were generated by the compiler when the application was complied. Each binary package or library has a corresponding debuginfo rpm that contains the debugging information. This allows Red Hat to ship the binaries without the disk-space–consuming debugging information. However, it allows developers, or those investigating performance problems, to download the appropriate debuginfo packages and use them. In this case, Red Hat's version of oprofile will also recognize the debuginfo packages and pick up the symbols when profiling both an application, such a nautilus, and a library, such as gtk.
    • Debian has similar binary debug symbol packages: -dbg

Chapter 12

  1. strace to find bottleneck of IO intensive application.
    • Oprofile can be used only for CPU intensive application. Because IO is performed in kernel space, strace is the right tool to reveal which kind of IO application is performing.

Chapter 13

  1. This call-tree tool would be useful even if it dramatically slowed down application performance as it runs. A common way of using this would be to run oprofile to figure out which functions in an application are "hot," and then run the call-tree program to figure out why the application called them.

Reading notes of internals of the RT patch

  1. Real-time is something about predictability instead of performance, throughput or latency. That is, real-time system may have worse average throughput or latency, but it will have better largest latency or lowest throughput. Linux kernel have great average performance but the worst one is not so good.
  2. The drawback of Robert Love's kernel preemption patch is: A high priority process must still wait on a lower priority process while it is in a critical section, even if that same high priority process did not need to access that section.
  3. The RT patch is all about determinism and being able to have full control of the kernel. But, like being root, the more power you give the user the more likely they will destroy themselves with it. Such as, a user process can have higher priority than IRQ handler, if the process holding the CPU, system will hang.

Why NMI is not used widely in Linux kernel

"There are some systems where NMIs are broken (e.g. a lot of ThinkPads explode when they get NMI inside SMI). This was one of the reasons the NMI watchdog is not enabled by default anymore." From Andi Kleen.

force_sig_info

Behavior of force_sig_info

  • If SIGXXX is blocked or ignored: unblock SIGXXX, reset action to SIG_DFL

Force_sig_info in some special environment

  • kvm_arch_vcpu_ioctl_run(), SIGXXX is blocked
    • SIGXXX is unblocked, but before returning from kernel:kvm_arch_vcpu_ioctl_run(), it is blocked again (restore host signal mask).
  • In sys_rt_sigtimedwait(), SIGXXX is blocked
    • SIGXXX is unblocked, but before returning from sys_rt_sigtimedwait(), it is blocked again (restore to original signal mask).