Home > Linux, 工具介绍 > 关于ramdisk

关于ramdisk

December 5th, 2011

原创文章,转载请注明: 转载自系统技术非业余研究

本文链接地址: 关于ramdisk

ramdisk是Linux内核带的一个以内存为后备的虚拟设备,以块设备方式提供。 具体功能参见Documentation/ramdisk.txt,代码参见drivers/block/brd.c.

内核在启动的时候会预设几个ramdisk, 初始大小通常为128K的块设备,方便有需要的用户。

$ uname -r
2.6.32-131.0.15.el6.x86_64
$ ls /dev/ram0 -al
brw-rw---- 1 root disk 1, 0 2011-12-02 11:41 /dev/ram0
$ sudo blockdev --getsize /dev/ram0
131072

在实践中ramdisk有如下几个用途:
1. ramdisk的代码写的非常简洁,很容易让你明白块设备如何编写。

2. ramdisk基于内存的设备,IO操作速度非常快,所以可以用来测试块设备IOPS的极限,对照现在出现的各种SSD设备,了解IO协议栈的瓶颈。比如我就会用fio工具来测量下操作系统IOPS的天花板。

$ cat ramdisk-test                     
[global]
bs=4k
ioengine=libaio
iodepth=32
direct=1
rw=randrw
rwmixread=90
thread
time_based
runtime=180
[ram0]
filename=/dev/ram0

$ sudo fio ramdisk-test            
ram0: (g=0): rw=randrw, bs=4K-4K/4K-4K, ioengine=libaio, iodepth=32
Starting 1 thread
bs: 1 (f=1): [m] [3.9% done] [812M/88M /s] [203K/22K iops] [eta 02m:53s]
fio: terminating on signal 2

fio: terminating on signal 2

ram0: (groupid=0, jobs=1): err= 0: pid=30172
  read : io=4,852MB, bw=756MB/s, iops=193K, runt=  6420msec
    slat (usec): min=2, max=171, avg= 2.95, stdev= 0.84
    clat (usec): min=3, max=371, avg=138.66, stdev=22.10
  write: io=527MB, bw=83,986KB/s, iops=20,996, runt=  6420msec
    slat (usec): min=3, max=181, avg= 4.84, stdev= 1.18
    clat (usec): min=24, max=353, avg=138.61, stdev=22.39
  cpu          : usr=28.48%, sys=71.54%, ctx=701, majf=0, minf=39
  IO depths    : 1=0.1%, 2=0.1%, 4=0.1%, 8=0.2%, 16=0.4%, 32=99.2%, >=64=0.0%
     submit    : 0=0.0%, 4=100.0%, 8=0.0%, 16=0.0%, 32=0.0%, 64=0.0%, >=64=0.0%
     complete  : 0=0.0%, 4=100.0%, 8=0.0%, 16=0.0%, 32=0.1%, 64=0.0%, >=64=0.0%
     issued r/w: total=1242005/134798, short=0/0
     lat (usec): 4=0.01%, 10=0.03%, 20=0.07%, 50=0.14%, 100=0.26%
     lat (usec): 250=99.48%, 500=0.03%


Run status group 0 (all jobs):
   READ: io=4,852MB, aggrb=756MB/s, minb=774MB/s, maxb=774MB/s, mint=6420msec, maxt=6420msec
  WRITE: io=527MB, aggrb=83,986KB/s, minb=86,001KB/s, maxb=86,001KB/s, mint=6420msec, maxt=6420msec

Disk stats (read/write):
  ram1: ios=0/0, merge=0/0, ticks=0/0, in_queue=0, util=0.00%

3. ramdisk是个虚拟的块设备,可以任意操作,不用当心把盘写坏了。比如我就用它来了解Ext4文件系统是如果工作的。

$ sudo mkfs.ext4 /dev/ram0 
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
4096 inodes, 16384 blocks
819 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=16777216
2 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks: 
        8193

Writing inode tables: done                            
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 27 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

$ sudo mkdir /test
$ sudo mount /dev/ram0 /test
$ sudo perf stat -e 'ext4:*' -e 'jbd2:*' dd if=/dev/zero of=/test/data bs=4096 count=1024
1024+0 records in
1024+0 records out
4194304 bytes (4.2 MB) copied, 0.014986 s, 280 MB/s

 Performance counter stats for 'dd if=/dev/zero of=/test/data bs=4096 count=1024':

                 0 ext4:ext4_free_inode    
                 0 ext4:ext4_request_inode 
                 0 ext4:ext4_allocate_inode
                 0 ext4:ext4_write_begin   
             1,024 ext4:ext4_da_write_begin
                 0 ext4:ext4_ordered_write_end
                 0 ext4:ext4_writeback_write_end
                 0 ext4:ext4_journalled_write_end
             1,024 ext4:ext4_da_write_end  
             1,536 ext4:ext4_writepage     
                 1 ext4:ext4_da_writepages 
                 3 ext4:ext4_da_write_pages
                 1 ext4:ext4_da_writepages_result
                 0 ext4:ext4_discard_blocks
                 0 ext4:ext4_mb_new_inode_pa
                 0 ext4:ext4_mb_new_group_pa
                 0 ext4:ext4_mb_release_inode_pa
                 0 ext4:ext4_mb_release_group_pa
                 2 ext4:ext4_discard_preallocations
                 0 ext4:ext4_mb_discard_preallocations
                 2 ext4:ext4_request_blocks
                 2 ext4:ext4_allocate_blocks
                 0 ext4:ext4_free_blocks   
                 0 ext4:ext4_sync_file     
                 0 ext4:ext4_sync_fs       
                 1 ext4:ext4_alloc_da_blocks
                 2 ext4:ext4_mballoc_alloc 
                 0 ext4:ext4_mballoc_prealloc
                 0 ext4:ext4_mballoc_discard
                 0 ext4:ext4_mballoc_free  
                 0 jbd2:jbd2_checkpoint    
                 0 jbd2:jbd2_start_commit  
                 0 jbd2:jbd2_commit_locking
                 0 jbd2:jbd2_commit_flushing
                 0 jbd2:jbd2_commit_logging
                 0 jbd2:jbd2_end_commit    
                 0 jbd2:jbd2_submit_inode_data
                 0 jbd2:jbd2_run_stats     
                 0 jbd2:jbd2_checkpoint_stats

        0.018253602  seconds time elapsed

有它的帮忙,做各种实验的时候很爽!

祝玩得开心!

Post Footer automatically generated by wp-posturl plugin for wordpress.

Categories: Linux, 工具介绍 Tags:
Comments are closed.