MD中bitmap源代码分析--入题概述

  在MD模块中,各级raid都使用的一份bitmap的源码,也就是说共用一种bitmap的流程,下面以raid1的使用为例来分析bitmap的工作原理。

  在使用raid1磁盘阵列的时候,对于数据的可靠性有很高的要求。在写的过程中,有可能存在不稳定的因素,比如磁盘损坏、掉电/宕机、网络故障、系统故障等,这样导致写入失败,在系统恢复后,raid也需要进行恢复,如果磁盘比较大,那同步恢复的过程会很长。以raid1来说,在发生故障时,可能盘阵的数据很多都是已经一致的了,其实只有少部分不一致,所以就没必要进行全盘扫描,但是系统并不知道盘阵中哪些数据是一致的,这就需要在某个地方记录哪些是已同步的。为此,就诞生了bitmap,简单来说,bitmap就是记录raid中哪些数据是一致的,哪些是不一致的,这样在raid进行恢复的时候就不用全量同步,而是增量同步了,从而减少了恢复的时间。

  Bitmap的工作原理说来也是直截了当的,文件的内容就是一个位图。Bitmap的一个bit对应盘阵的一个chunk(数据块,默认为4KB),在盘阵数据写入前,设置该chunk对应的bit,盘阵写入完成,则清除该bit。要进行同步时,参照bitmap,只有置位的bit对应的chunk才需要进行同步,这样缩短了同步的时间,提高了效率。

  bitmap原理很明了,按照这个原理直接进行实施也是可以的,但直接这样实施的话,由于一次数据块的写入多了两次磁盘访问(bitmap的设置和清除),写入效率会受到较大影响,所以还需要考虑一些优化。优化的核心体现在两方面,具体的代码都是为了实现这两点思想:

  1bitmap设置后批量写入;

  2bitmap延时清除。

  这两方面的优化,需要在内存中构建和磁盘bitmap文件对应的数据结构,bitmap操作首先在缓存中进行,必要时才进行真正的磁盘操作。

 

  Bitmap分两种,一种是internal,一种是external。internal bitmap是存放在raid设备的成员盘的superblock附近(可以在之前也可以在之后),而external是单独指定一个文件用来存放bitmap。也就是说,Bitmap磁盘文件可以存储在MD设备之外,此时MD结构中的bitmap_file表示这个bitmap文件。Bitmap磁盘文件也可以存储于MD设备自身,此时bitmap相对于MD设备的超级块的位置由bitmap_offset指定。Bitmap_offset可以是负数,表示的是bitmap位于superblock之前。Bitmap相对于superblock的位置是与创建MD设备时的参数metadata相关的。例如,以下的创建MD设备命令:

    Mdadm –CR /dev/md0 –l1 –n2 /dev/sdb /dev/sdc –bitmap=internal –metadata=x –assume-clean

  在metadata=1.0时,bitmap_offset为-8,bitmap在superblock之前;在metadata=1.1时,bitmap_offset为8,bitmap在superblock之后;在metadata=1.2时,bitmap_offset为8,bitmap在superblock之后;在metadata=0.9时,从代码的注释中可知,bitmap是紧跟在superblock后面的。Internal的bitmap是存放在MD设备的superblock附近,而external是单独指定一个文件用来存放bitmap。

  Md的superblock的版本由—metadata参数指定。有四个版本的superblock:

    0.9——限制一个raid中的设备数为28个,限制组件设备大小为2TB;

    1.0——superblock存储在设备的结尾;

    1.1——superblock存储在设备的开头;

    1.2——superblock存储在设备的4K处。

 

  下一篇会详细分析bitmap的数据结构是如何设计,进而实现这两个优化机制的。

 

转载请注明出处:http://www.cnblogs.com/fangpei/

参考资料:

[1] linux内核源码2.6.32

[2] MD中的bitmap 开篇  http://blog.csdn.net/qincp/article/details/4396517

[3] linux软raid的bitmap分析  http://blog.csdn.net/qincp/article/details/4396517

 

转载于:https://www.cnblogs.com/fangpei/p/4652826.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 Delphi-OpenCV 库在 Delphi XE 将 IplImage 对象转换为Bitmap 对象的详细代码: ```delphiuses OpenCV_Core, OpenCV_ImageProc, // Delphi-OpenCV 库单元 Vcl.Graphics; // VCL 图形单元 function IplImageToBitmap(const Image: pIplImage): TBitmap; var Depth, Channels: Integer; LineSize: Integer; ImageData, SrcLine, DestLine: Pointer; Bitmap: TBitmap; Row, Col: Integer; Data: Byte; begin Depth := Image.depth; Channels := Image.nChannels; LineSize := Image.width * Channels; // 分配 Bitmap 对象 Bitmap := TBitmap.Create; Bitmap.PixelFormat := pf24bit; Bitmap.Width := Image.width; Bitmap.Height := Image.height; // 按行遍历 IplImage 数据并转换为 TBitmap 数据 ImageData := Image.imageData; for Row := 0 to Image.height - 1 do begin SrcLine := ImageData + Row * Image.widthStep; DestLine := Bitmap.ScanLine[Row]; case Depth of IPL_DEPTH_8U: begin for Col := 0 to Image.width - 1 do begin Data := PByte(SrcLine + Col * Channels)^; PByte(DestLine + Col * 3)^ := Data; PByte(DestLine + Col * 3 + 1)^ := Data; PByte(DestLine + Col * 3 + 2)^ := Data; end; end; IPL_DEPTH_8S: begin for Col := 0 to Image.width - 1 do begin Data := Byte(PShortInt(SrcLine + Col * Channels)^); PByte(DestLine + Col * 3)^ := Data; PByte(DestLine + Col * 3 + 1)^ := Data; PByte(DestLine + Col * 3 + 2)^ := Data; end; end; IPL_DEPTH_16U: begin for Col := 0 to Image.width - 1 do begin Data := Byte(PWord(SrcLine + Col * Channels)^ shr 8); PByte(DestLine + Col * 3)^ := Data; PByte(DestLine + Col * 3 + 1)^ := Data; PByte(DestLine + Col * 3 + 2)^ := Data; end; end; IPL_DEPTH_16S: begin for Col := 0 to Image.width - 1 do begin Data := Byte(PShortInt(SrcLine + Col * Channels)^ shr 8 + 128); PByte(DestLine + Col * 3)^ := Data; PByte(DestLine + Col * 3 + 1)^ := Data; PByte(DestLine + Col * 3 + 2)^ := Data; end; end; IPL_DEPTH_32S: begin for Col := 0 to Image.width - 1 do begin Data := Byte(PInteger(SrcLine + Col * Channels)^ shr 24); PByte(DestLine + Col * 3)^ := Data; PByte(DestLine + Col * 3 + 1)^ := Data; PByte(DestLine + Col * 3 + 2)^ := Data; end; end; IPL_DEPTH_32F: begin for Col := 0 to Image.width - 1 do begin Data := Byte(PSingle(SrcLine + Col * Channels)^ * 255); PByte(DestLine + Col * 3)^ := Data; PByte(DestLine + Col * 3 + 1)^ := Data; PByte(DestLine + Col * 3 + 2)^ := Data; end; end; IPL_DEPTH_64F: begin for Col := 0 to Image.width - 1 do begin Data := Byte(PDouble(SrcLine + Col * Channels)^ * 255); PByte(DestLine + Col * 3)^ := Data; PByte(DestLine + Col * 3 + 1)^ := Data; PByte(DestLine + Col * 3 + 2)^ := Data; end; end; end; end; Result := Bitmap; end; ``` 使用方法: ```delphi var Image: pIplImage; Bitmap: TBitmap; begin // 加载图像到 Image 变量 Image := cvLoadImage('image.jpg'); // 将 IplImage 对象转换为 TBitmap 对象 Bitmap := IplImageToBitmap(Image); // 将 TBitmap 对象显示在 TImage 组件上 Image1.Picture.Assign(Bitmap); // 释放 IplImage 对象内存 cvReleaseImage(@Image); end; ``` 需要注意的是,由于 Delphi-OpenCV 库的 IplImage 对象是指针类型的,因此需要传入指针的指针作为参数。在使用完毕后,需要手动释放 IplImage 对象的内存。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值