http://qs321.pair.com?node_id=11118270

bbs2web has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I found the following script many years ago and it's been absolutely invaluable in providing for local or network based block device replication. Part of it's neat beauty is that it's trivial to simply copy & paste the lines on a Linux host's shell prompt. It essentially reads data from both source and either local or remote destination block devices (could be entire drive, partition, md device or in our case a Ceph RBD image) in 4 MiB chunks. If the chunk's md5 checksum matches it skips ahead, otherwise it overwrites that chunk.

We are however increasingly being bottle necked by the MD5 hashing function and were wondering if anyone had recommendations on replacing the calls with something that could perhaps offload the hashing to an Intel CPU's AES instructions.


Local block devices:
export srcdev=`rbd map rbd_ssd/vm-277-disk-0`; [ -z $srcdev ] && exi +t 1 # eg: /dev/rbd47 export dstdev=`rbd map rbd_ssd/vm-472-disk-0`; [ -z $dstdev ] && exi +t 1 # eg: /dev/rbd48 perl -'MDigest::MD5 md5' -ne 'BEGIN{$/=\4194304};print md5($_)' $dst +dev | perl -'MDigest::MD5 md5' -ne 'BEGIN{$/=\4194304};$b=md5($_); read STDIN,$a,16;if ($a eq $b) {print "s"} else {print "c" . $_} +' $srcdev | perl -ne 'BEGIN{$/=\1} if ($_ eq"s") {$s++} else {if ($s) { seek STDOUT,$s*4194304,1; $s=0}; read ARGV,$buf,4194304; print $ +buf}' 1<> $dstdev;

Local source to remote block devices:

On source system:
export remote='root@zajnb01-kvm5f.mirror.ad.company.co.za'; export dstdev=`ssh -i /root/.ssh/rsync_rsa $remote "rbd map rbd_ssd/ +vm-277-disk-0`; [ -z $srcdev ] && exit 1"; # or map the image on the destination host and program the variabl +e manually on the source system, for example: # export dstdev='/dev/rbd320'; ssh -i /root/.ssh/rsync_rsa $remote " perl -'MDigest::MD5 md5' -ne 'BEGIN{\$/=\4194304};print md5(\$_)' $d +stdev | lzop -c" | lzop -dc | perl -'MDigest::MD5 md5' -ne 'BEGIN{$/=\4194304};$b=md5($ +_); read STDIN,$a,16;if ($a eq $b) {print "s"} else {print "c" . $_}' +$srcdev | lzop -c | ssh -i /root/.ssh/rsync_rsa $remote "lzop -dc | perl -ne 'BEGIN{\$/=\1} if (\$_ eq\"s\") {\$s++} else {if (\$s) { seek STDOUT,\$s*4194304,1; \$s=0}; read ARGV,\$buf,4194304; print +\$buf}' 1<> $dstdev";

Unallocated, discarded or trimmed blocks read as zeros so read speeds accelerate to GiB/s levels where Perl's MD5 hashing function then becomes the bottleneck. Another thread (860184) referencing possibly skipping the MD5 check if buffers matched. I presume that wouldn't be easy to do on the network based iteration above although it would still save time one some operations...


Regards
David Herselman