December 10, 2011

Bash shortcuts - Maximum Productivity

Bash is my favorite shell, I use Bash every day. Using shortcuts help you to use Bash more efficiently and hence increase your productivity.



Here are some shortcuts:

Movement:
  • Ctrl - a: jump to the beginning of the line
  • Ctrl - e: jump to the end of the line
  • Alt - b: move backward a word
  • Alt - f: move forward a word
  • Ctrl - f: move forward a character
  • Ctrl - b: move backward a character


Editing:
  • Ctrl - k: delete the text from the cursor to the end of the line
  • Ctrl - u: delete the text backward from the cursor to the beginning of the line
  • Ctrl - w: delete the text backward from the cursor to the previous space
  • Ctrl - y: paste the kill ring before the cursor
  • Ctrl - d: delete the character under the cursor
  • Ctrl - h: delete the character before the cursor
  • Alt - d: delete from the cursor to the end of the word
  • Alt - c: uppercase the current character and move to the end of the word
  • Alt - u: uppercase from the cursor to the end of word
  • Alt - l: lowercase from the cursor to the end of word
  • Alt - t: swap the current word and previous word 
  • TABTAB: double TAB, auto complete
  • $$: process number of the current process 
  • $?: return code of last finished program


Command Recall:
  • Ctrl - r: search command history
  • Ctrl - g: exit Ctrl + r without history command, ESC exits with history command in the current line
  • Ctrl - p: fetch previous command in the history
  • Ctrl - n: fetch next command in the history
  • !!: refers to the most recent command
  • !string: refer to the most recent command starting with string.


Command Control:
  • Ctrl - l: Clear the screen, similar to "clear" command, but preserve the argument if there is any. 
  • Ctrl - s: Stop screen output
  • Ctrl - q: re-enable screen output stopped by Ctrl-s
  • Ctrl - c: generate SIGINT signal, usually kills the foreground process
  • Ctrl - z: generate SIGSTP signal, usually suspends the foreground process






November 17, 2011

Upgrading G1 from CM4.2 to CM7.1

It is not difficult to compile a CM7.1 rom by yourself. It is not fun to follow instructions to compile and flash the rom, what is really fun is the process of finding out why other people's rom works well but your rom make your G1 brick.

Initially, I just checkout the code from github, compile the code, pack my own rom and then flash it to my phone. Then the phone "brick"(It is not brick actually because it still boot into bootoloader mode and use fastboot), stuck at splash screen and ADB not yet started(no debug message) !!! Now it becomes fun.

After googling, I find that HTC released new bootloader and radio that give my G1 15MB extra RAM, I need a patched kernel to work with the new firmware and ezterry published his rom GINGERBREAD-DS-Gamma-20111107 which works properly on g1.

I boot into bootloader mode and flash the following images using fastboot:
  • New bootloader: Hboot 1.33.0013d 
  • New radio: 2.22.27.08 
  • New recovery: RA-dream-v1.7.0 cyan 
and then reboot into recovery mode and flash my CM7.1 rom and ez-nightly271-cm-2708port_S kernel.

This time, much better since I can view debug message using logcat although it still stuck at splash screen.

The logcat shows that something wrong with the propriety libcamera.so(the file is copied from cm4.2), it cannot locate the symbol "_ZN7android16CameraParametersC1Ev".

Let's find out what happen to libcamera.so and compare with libraries in GINGERBREAD-DS-Gamma-20111107 rom.
The readelf command show that libcamera.so linked to the following libraris:

[jason@jasonpc tmp]$ readelf -a old-libcamera.so|grep "Shared library"
0x00000001 (NEEDED) Shared library: [libutils.so]
0x00000001 (NEEDED) Shared library: [libui.so]
0x00000001 (NEEDED) Shared library: [liblog.so]
0x00000001 (NEEDED) Shared library: [libcamera_client.so]
0x00000001 (NEEDED) Shared library: [libbinder.so]
0x00000001 (NEEDED) Shared library: [libdl.so]
0x00000001 (NEEDED) Shared library: [libc.so]
0x00000001 (NEEDED) Shared library: [libstdc++.so]
0x00000001 (NEEDED) Shared library: [libm.so]


[jason@jasonpc tmp]$ readelf -a ezgingerbread-libcamera.so|grep "Shared library"
0x00000001 (NEEDED) Shared library: [libutils.so]
0x00000001 (NEEDED) Shared library: [libui.so]
0x00000001 (NEEDED) Shared library: [liblog.so]
0x00000001 (NEEDED) Shared library: [libcamera_client.so]
0x00000001 (NEEDED) Shared library: [libbinder.so]
0x00000001 (NEEDED) Shared library: [libdl.so]
0x00000001 (NEEDED) Shared library: [libc.so]
0x00000001 (NEEDED) Shared library: [libstdc++.so]
0x00000001 (NEEDED) Shared library: [libm.so]

ezgingerbread's libcamera.so needs libcamera_client.so while my old-libcamera.so does not.
Let's look into libui.so and libcamera_client.so.

The nm command shows the symbol in the dynamic library.
[jason@jasonpc tmp]$ nm -D old-libui.so |grep _ZN7android16CameraParametersC1Ev
000177e9 T _ZN7android16CameraParametersC1Ev

[jason@jasonpc tmp]$ nm -D ezgingerbread-libui.so |grep _ZN7android16CameraParametersC1Ev
[jason@jasonpc tmp]$

[jason@jasonpc tmp]$ nm -D ezgingerbread/system/lib/libcamera_client.so |grep _ZN7android16CameraParametersC1Ev
0000d155 T _ZN7android16CameraParametersC1Ev

git log shows that the CameraParameter class has been moved from libui.so to libcamera_client.so since Froyo. That is why the old libcamera.so cannot find the symbol and the new library linked to libui.so.

Since I dont have a workable libcamera.so, I just copied the one from GINGERBREAD-DS-Gamma-20111107 rom. After using the new libcamera.so, the phone boot up successfully though there still few other errors.



References:
ezterry 's post on xda-developers

November 12, 2011

Parallel programming Optimization tricks

Comparision of 2 __m128i:
  • SSE 4.1:
#define iszero(a) _mm_testz_si128(a, a)
#define isequal(a, b) _mm_testc_si128(_mm_cmpeq_epi32(a, b), gAllOnes)
  • Others:
#define iszero(a) (_mm_movemask_epi8(_mm_cmpeq_epi32(a, _mm_setzero_si128())) == 0xffff)
#define isequal(a, b) (_mm_movemask_epi8(_mm_cmpeq_epi32(a, b)) == 0xffff)


Number of trailing zeros of an int:
  • intrinsic: _bit_scan_forward()
  • corresponding asm instruction: bsfl

Nehalem

Some features of Intel's Nehalem CPU architecture.


Turbo Boost:
  • allow processors to operate above the rated frequency
  • control by continual measurement of process temperature, current draw and power consumption
  • upper limit of frequency controlled by:
  • number of active(in “C0″ or “C1″ state) cores(dictates the upper limit)
  • estimated current consumption
  • estimated power consumption
  • process temperature
  • all active cores operate at the same frequency and voltage
  • BIOS can enable/disable Turbo Boost
  • ACPI awared OS need no change to support Turbo Boost
  • OS should request the highest performance state(P0), should enable ACPI ?
  • Software shows correct core frequency: i7z

Intelligent Power:
  • adjust processor frequency when process is idle

Hyper-Threading:
  • 2 threads per core

Quick Path:
  • replace FSB
  • up to 25.6 GB/s
  • each processor has its own dedicated memory that it accesses directly through an integrated memory controller
  • access the dedicated memory of another processor should go through QPI

SSE4:
  • XML, text processing

Multi-level cache:
  • Level 1: 32KB instruction cache, 32 KB data cache
  • Level 2: 256KB per core, for data and instruction
  • Level 3: cache shared across all cores

Benchmark Tool

Disk IO / File System performance: Bonnie++ , IOzone

Memory Bandwidth:  STREAM

FLOPS: HPL

Extract a RPM package without installing it

Sometimes you want to use the programs, config files provided by a RPM package, but you cannot install the package since you are not root. You can extract the files you want from the package:
 


Command:
 
rpm2cpio myrpmfile.rpm | cpio -idmv


Explanation:

rpm2cpio: converts the .rpm file specified to a cpio archive on standard out.
cpio: copy files to and from archives.
cpio options:
  • -i: extracts the archive
  • -v: shows the file names as they are extracted
  • -d: create directories as necessary
  • -m: retain previous file modification times when creating files.

Reference:
cpio manpage
rpm2cpop manpage
How To Extract an RPM Package Without Installing It

RPM - Redhat Package Manager

RPM(Redhat Package Manager) is a powerful package management tools widely used by many Linux distributions as well as Unix .


Documentation:

Maximum RPM, by Edward C. Bailey. Covers RPM 2
Distribution RPM Macro comparison


Useful sites:

rpm.org Contains documentation:
rpm5.org RPM version 5

LVM-Logical Volume Manager

LVM is a Logical Volume Manager for the Linux operating system.

LVM 2 uses the device mapper kernel driver and supports snapshots which allow the administrator to create a new block device which presents an exact copy of a logical volume, frozen at some point in time.



Anatomy of LVM:

Volume Group, the highest level abstraction used within the LVM. It gathers together a collection of Logical Volumes and Physical Volumes into one administrative unit.

A physical volume is typically a hard disk, though it may well just be a device that ‘looks’ like a hard disk.

Logical volume, the equivalent of a disk partition in a non-LVM system. The LV is visible as a standard block device; as such the LV can contain a file system.

Each physical volume is divided chunks of data, known as physical extents, these extents have the same size as the logical extents for the volume group.

Each logical volume is split into chunks of data, known as logical extents. The extent size is the same for all logical volumes in the volume group.


Overview of a LVM system:
      hda1      hdc1      (PV: on partitions or whole disks)
             \     /
               \ /
           diskvg        (VG)
             /  |  \
           /    |    \
usrlv  rootlv  varlv (LV)
   |            |           |
ext2  reiserfs  xfs (filesystems)
 
 
 
Steps to create a LVM system:
1. create lvm partition, fdisk, partition system id is 0x8e
2. create pv, pvcreate
3. create vg, vgcreate
4. create lv, lvcreate
5. format lv, mkfs
6. mount lv, mount

Steps to extend a Logical volume:
1. extend the logical volume, lvextend
2. resize file system, for ext2/ext3, resize2fs


Reference:
LVM HOWTO