2014年4月1日火曜日

How to use zram

By default, Raspbian uses swap file in sdcard for its easy setup.
Swap is generally helpful when system memory is getting less.
But, swap in sdcard partition is a problem since it shortens life of sdcard hardware by frequent data write for swap out. Also, it has slow performance problem.

Therefore, 'zram' is one solution to solve the issues.
It is a kernel feature to create compressible swap partition on ram.

Following instruction explains how to enable it for your raspberry pi.

Disable default swap setting

# sudo apt-get install chkconfig
# sudo chkconfig dphys-swapfile off


Enable swap on zram

Load zram module.

# modprobe zram

Set 128MB zram.

# echo $((128*1024*1024)) > /sys/block/zram0/disksize

Create swap on the zram.

# mkswap /dev/block/zram0

Enable swap.

# swapon -p 10 /dev/block/zram0

How to move rootfs to external usb storage

This instruction is valid for linux OS.

Prepare raspbian image
Download wheezy-raspbian image from official site.

http://www.raspberrypi.org/downloads

Unzip downloaded file and put img file under working directory.
Also create following directory for mount target.

  • mnt_src_rootfs
  • mnt_dist_rootfs

Flash image to sdcard
We don't need rootfs on sdcard finally. But it is useful to check device entry for your external usb storage in this instruction.

# sudo dd bs=1M if=2014-01-07-wheezy-raspbian.img of=/dev/sdc


Mount partitions in image
Check wheezy-raspbian image file with fdisk.

# fdisk -l -u 2014-01-07-wheezy-raspbian.img

Disk 2014-01-07-wheezy-raspbian.img: 2962 MB, 2962227200 bytes
255 heads, 63 sectors/track, 360 cylinders, total 5785600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000981cb

 
Device Boot Start End Blocks Id System
2014-01-07-wheezy-raspbian.img1 8192 122879 57344 c W95 FAT32 (LBA)
2014-01-07-wheezy-raspbian.img2 122880 5785599 2831360 83 Linux

 

Mount 2nd partition with offset.

# sudo mount -o loop,offset=$((122880*512)) 2014-01-07-wheezy-raspbian.img mnt_src_rootfs


Prepare external usb storage

Create ext4 partitions on your extarnal usb storage by fdisk and mkfs.
And mount it as 'mnt_dist_rootfs'.

Copy rootfs

Copy all rootfs files from img file to extarnal usb storage.

# sudo cp -ax mnt_src_rootfs/. mnt_dist_rootfs

Boot Raspberry pi

Insert sdcard and bootup. (Here, rootfs on sdcard is used.)
On raspbian,
  1. login
  2. connect usb extarnal usb storage.
  3. execute dmesg
  4. Get device entry for the ext4 partition on extarnal usb storage
I will assume your device entry is '/dev/sdc1'

Modify cmdline.txt


On raspbian,

# nano /boot/cmdline.txt

Modify following path to device entry of rootfs partition on extarnal usb storage.

root=/dev/sdc1


Sync and reboot rasbian.


# sync
# reboot 0

That's it.

2014年3月27日木曜日

CPU frequency scaling

Raspbian fixes cpu frequency to 700MHz by default.
It would not be reasonable to keep max cpu freq for the usage which doesn't requires full cpu power at all times.

Following instruction explains how to custermize your raspberry pi to be lower cpu freq at idle.
@The effect seems to be less. So, please read this article with some technical interest:)
 

GPU setting
 

/boot/config.txt is read by GPU for hardware configuration before CPU is up. That includes the setting of cpu freq.
Please add following lines in the text.

# Meaningful word.. This means 'enable cpu freq scaling'
force_turbo=0
# Set lowest cpu freq. By default, it's 700.
arm_freq_min=100


Reboot is required to reflect them.

@Refer following page for more detailed information about config.txt.
http://elinux.org/RPiconfig

Linux kernel configuration
 

By GPU setting, cpu gets capability for freq scaling.
But, Linux kernel has its own mechanism to control it and default would fix cpu freq to 100MHz.

To check current/min/max cpu freq please use following commands on shell.

- Current cpu freq
# cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq
- Max cpu freq
# cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_man_freq
- Min cpu freq
# cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_mix_freq

Linux kernel has driver which manages cpu freq policy.
Following option is available for that. Rasbian set powersave by default.

performance  - always use max cpu freq
powersave    - always use min cpu freq
ondemand     - change cpu freq depending on cpu load (On rasbian, it just switches min and max)
conservative - smoothly change cpu freq depending on cpu load
uesrspace    - allow user space daemon to control cpufreq

We can change the setting from shell at runtime via sysfs.

# echo ondemand > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor

To keep this setting over reboot, There are 2 options.

1st option - Put command in /etc/rc.local.
The command execution is after kernel boot. So, it slows bootup down.

2nd option - Enable kernel configration.
Enable either kernel config.

CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE

Though 2nd way requires kernel build, we can use full cpu power during boot.


After all, you can see cpu freq change depending on cpu load.
Enjoy:)