#!/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
Tuesday, June 23, 2009
Debug bash function interactively
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.
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>
Saturday, June 13, 2009
Reading notes: optimizing Linux performance
Chapter 3
- 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.
- slabtop is "top" for slab.
Chapter 4
- strace can show system call performance statistics too (strace -c).
- ltrace: performance statistics for library (like strace, but library function instead of system call).
- ld.so: dynamic load/link statistics for libraries. Example:
env LD_DEBUG=statistics LD_DEBUG_OUTPUT=lddebug gcalctool
Chapter 6
- iostate: show disk IO statistics
Chapter 7
- netstat -c: period update
- netstat -t: choose tcp protocol
- netstat -l, -a: listening sockets or listening + conntected.
Chapter 8
- script: record what you have done and the output, seems useful.
- gcc -g3: provide more debug information then gcc -g (default to
- g2), such as the macro definitions present in the source.
Chapter 11
- 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
- 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.
- 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
- 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
- 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
- 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.
- 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.
- 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).
Fast boot
How to make Linux boot faster?
- Keep CPU and disk I/O usage rate to or near 100%, that is, take full advantage of CPU speed and disk I/O throughput.
- Reduce works needed in boot.
Previous, I think the method 2 is more important, such as substituting boot with resuming from hibernation. But I think Moblin team achieve a great result with mainly method 1.
Audio and real-time
Only audio playback and audio recording do not need much real-time capability of system. Because you can just need to prepare a buffer big enough until next refill (or re-consume).
The real real-time demanding work is something like VOIP, a small buffer should be used to keep speak-to-hear lag low.
SNES9x for Zaurus Akita with rotate
The resolution of zaurus akita is 480x640 (or 240x320) instead of 640x480 (or 320x240). While SNES9x uses resolution 640x480 or 320x240, so rotation should be implemented in some layer of the graphic stack. The possible graphic stacks are:
- SNES9x + X Window + frame buffer
- SNES9x + SDL + X Window + frame buffer
- SNES9x + SDL + frame buffer
Rotation has been implemented in X window so for 1 and 2, there is no need to implement rotation in SDL or SNES9x. But the performance of 1 and 2 is worse than 3. So I implement the rotation in SNES9x. The patch can be downloaded here.
Some software for tablet PC
- Stroke (gesture recognition)
- I find easystroke is the best so far. To make it easier to enable/disable easytroke, (because several gesture recognition programs in system may conflict, for example, there is gesture recognition in firefox too.) I write a small patch to disable/enable easystroke with left button, which can be downloaded from here.
- Note taking with pen
- I think xournal is very good. It can be used to add annotations to PDF files.
Firefox addons for tablet PC
- Grab and Drag
Enables Adobe Acrobat-style grab and drag scrolling in Mozilla applications.
- All-in-One Gestures
This extension allows you to execute common commands using mouse (pen) gestures.
Several mouse gestures programs may conflict each other, so I write a simple AIOGControl (All-in-One Gestures) control addon to enable/disable All-in-One Gestures via a Firefox toolbar button. This is easier for pen to operate.
Finger friendly scrollbar in GTK+
Standard GTK+ scrollbar is too small for finger, to make it bigger, you can put following code in your .gtkrc-2.0 or gtkrc file of theme.
style "big-scrolls" {
GtkRange::stepper-size = 50
GtkRange::slider-width = 50
}
class "GtkScrollbar" style "big-scrolls"
Please refer to http://www.roosmaa.net/index.php/2009/02/09/making-gtk-scrollbars-more-touchscreen-friendly/ too.
Comparison against overflow
Time variables are subject to overflow. So, the comparison statement as follow will return incorrect result if t1 has overflowed and t2 has not overflowed.
unsigned long t1, t2; if (t1 > t2) printf("t1 > t2");
The code as follow can be used to deal with the above issue.
unsigned long t1, t2; if ((long)(t1 - t2) > 0) printf("t1 > t2");
Suppose sizeof(unsigned long) = 4, and t1 < t2, the result of t1 - t2 under the unsigned semantics is (0x100000000 + t1 - t2), because of borrow, if the result < 0x80000000, in signed semantics and complemental code, the result is positive.
Screen with title in status bar
screen is a nice terminal tool.
- cp example screenrc from doc to home directory
- Add following lines to screenrc, window titles of current screen session will be shown in status bar with current window title hilighted.
hardstatus string "%h"
caption always "%{=r}%?%{b}%-Lw%?%{+bu r}%n*%f %t%{R}%{-bu}%?%{b}%+Lw%: %?%{+b B}%=%C:%s"
Mer: Comparison with desktop distributions
Comes from a presentation of mer community.
Is Mer just yet another linux distribution? Many people would disagree since we break quite a lot of things (Maemo GTK is a big one!), and I'll tell you some of the ways we differ.
Like Maemo we focus on power saving, some things that make sense on desktop/laptop distributions simply doesn't make sense on mobile devices that's supposed to last on the same charge several days being alwaysconnected.
A tablet still comes with limited storage space, so there might be a need to shrink packages or dependancies (why do we need a weather library for NetworkManager?); but not hard as on an embedded system; there's miles difference between 256m flash and 64mb flash.
Yes, you can't run Crysis on a tablet, it has CPU, memory and bandwidth constraints and apps should be suited towards that. But, what it can do is to be more powerful because of the environment and services surrounding it.
Hard disk image in qemu
create/update image
Install a mini linux system in </sub-linux>. It is very easy to install debian inside another Linux.
Update image quickly
quick_upimg.sh
qemu-img create -b <hd.img> -f qcow2 <hd.qcow2>
Update image
upimg.sh
sudo genext2fs -d </sub-linux> -b 344800 <hd.img> chown <yourself>. <hd.img> quick_upimg.sh
If you use <hd.qcow2> in QEMU, <hd.img> will not changed for ordinary usage.
commit changes
You can explicitely commit changes to <hd.img> by qemu monitor command "commit".
access qcow2 image
$ qemu-nbd [-p <port>] hd.qcow2 $ sudo modprobe nbd nbds_max=<2> $ sudo nbd-client localhost <port> /dev/nbd0 $ sudo mount /dev/nbd0 /mnt
after using:
$ sudo umount /mnt $ sudo nbd-client -d /dev/nbd0
Blog with muse
Recently I decide to use muse as my primary composing tool. Because:
- Easy to use.
- Mark based instead of WYSIWYG. Full control of output and document structure.
- Can convert to many other format, including docbook and latex. So if formating capability of muse is not enough, we can use other format instead later.
To use muse to write blog (mainly for blogspot):
- Instal muse-el
- Add "(require 'muse-blosxom)" into your .emacs
- Write your blog in emacs with muse mode.
- "C-c C-t" to generate output in blosxom-html format.
- Open generated blosxom-html, "C-x h" to mark whole buffer, "M-!" to run a external shell script on your current buffer to do some post-processing. The shell script can be as follow.
sed -e '1d' -e '/^#postdate/d' | tr '\r\n' ' '
New process group in shell script
To create a new process group for a command (or another shell script) in bash script, the following method can be used.
b=$(bash -i -c "<command> & echo \$!")
This can be put in a shell script. String after -c will be executed by the (sub-)bash, while -i means the (sub-)bash will behave like a interactive bash. So "<command> &" in string will execute <command> in a new process group, just like "<command> &" is typed in a login shell. The "echo \$!" in string will print the process ID, that is, the new process group ID, so you can operate on the new created process group, such as send signal to process group via "kill -SIGNAL -$b".