I am using the cmp
command to compare a 1GB files stored on an SD card to a reference 1GB file stored in main memory. The completion times for a single cmp
command vary significantly, ranging from 17 seconds to 3.5 minutes.
The files are expected to be the same, and in all cases so far have been. I run a function (see below) that compares the 100 1GB files on the SD card to the reference 1GB file stored in main memory. Usually, all 100 cmp
's in the loop will tend either fast (<20 sec) or slow for the duration of the script.
Based on the output of top
, I have not observed any processes that would be causing the extended duration when it takes longer.
What could be causing the completion time of the cmp
command to vary?
Also, what can be done to make sure the command is completed in a reasonable amount of time (<25 seconds)?
This is occurring on a Yocto distribution in an embedded application.
function check_files() {
for filename in /mnt/Android/data/File_*;
do
echo "Checking $filename"
result=$(cmp -l /data/1GB_File.bin $filename)
resultlength=${#result}
if [ $resultlength -gt 0 ]; then
date >> /data/errors.txt
echo $filename >> /data/errors.txt
echo $result >> /data/errors.txt
echo "==========" >> /data/errors.txt
fi
done
}
echo 3 > /proc/sys/vm/drop_caches
?cmp (GNU diffutils) 3.6
exits on the first difference unless you use the-l
option (a feature which is not mentioned in the man page, but is ininfo cmp
!). So 17 seconds means the first difference happened about 8% into the data. 3.5 minutes happens when there is no difference, and that is limited by the SD data rate.cmp
is just a linear check. If the files are text, you might first check if they are different lengths --stat %s
on each, and compare. If different, no need to read any data. Then maybe check the ends if that is the most likely place for differences --tail -c 1024
on each file andcmp
those fragments. Ultimately, though, if the difference can be a single bit anywhere in the file, you have to read the whole of each file.cmp
's in the loop will tend either fast (<20 sec) or slow for the duration of the script. I will add this to the original question. I haven't had a second to check ifecho 3 > /proc/sys/vm/drop_caches
fixes the issue. I will update.