2015年10月19日 星期一

qsort in c library

qsort shouldn't use a compare function like
int cmp(const void* a, const void* b){
    return *(int*)a - *(int*)b;
}
it would easily cause a overflow, like 2000000000-(-2000000000), it would  overflow and become a negative number.

instead, we should use a compare function like
int cmp(const void* a, const void* b){
    if(*(int*)a > *(int*)b)return 1;
    else if(*(int*)a < *(int*)b)return -1;
    return 0;
}

2015年10月7日 星期三

SSD optimizing in linux

If your are using ext4 as file system in SSD, you may consider add discard and noatime parameter in /etc/fstab.
discard makes it trim. And noatime makes it not modify the last read time information for file.

And you can change CFS i/o scheduler to noop scheduler.
The approach is to modify the /etc/default/grub, add elevator=noop in kernel parameter.
And do
update-grub
Or if you have many storage devices not only SSD but also motion driven devices, you can  edit or create a file in
/etc/udev/rules.d/60-schedulers.rules
contains
# set noop scheduler for non-rotating disks 
ACTION=="add|change", KERNEL=="sd[a-z]", ATTR{queue/rotational}=="0", ATTR{queue/scheduler}="noop" 
# set cfq scheduler for rotating disks 
ACTION=="add|change", KERNEL=="sd[a-z]", ATTR{queue/rotational}=="1", ATTR{queue/scheduler}="cfq"
And use
cat /sys/block/sda/queue/scheduler
to see the scheduler status.

2015年10月3日 星期六

test disk speed

hdparm -Tt /dev/sda

mount /dev/sda at /test
dd if=/dev/zero of=/test bs=1M count=1024

iotop
iometer
iozone