0

Because there is a situation in my project where files cannot be saved to disk after being written, these two scripts have been added in the background. Will they have any impact on system performance?

My device may frequently experience abnormal power outages instead of rebooting or shutting down. I hope that data written to a file 1 second before the abnormal power outage can also be saved to the disk, but the actual situation is that it often fails to be saved to the disk.

My device has UPS(Uninterruptible Power System). I am using C++programming.

autoSync.sh:

#!/bin/sh

while true; do
    sync
    sleep 1
done

autoCacheFree.sh:

#!/bin/sh

drop_caches() {
    echo "Drop caches."
    sync
    echo 1 > /proc/sys/vm/drop_caches &
    return 0
}

while true; do
    sleep 600
    drop_caches
done

/etc/fstab:

#device         mount-point     type    options         dump    fsck order
/dev/mmcblk3p3  /dobot          ext3    defaults                0       0
/dev/mmcblk3p6  /dobot/userdata ext3    defaults                0       0
proc            /proc           proc    defaults                0       0
tmpfs           /tmp            tmpfs   defaults                0       0
sysfs           /sys            sysfs   defaults                0       0
tmpfs           /dev            tmpfs   defaults                0       0
var             /dev            tmpfs   defaults                0       0
ramfs           /dev            ramfs   defaults                0       0
5
  • Can you please provide details about the underlying filesystem together with associated mount options ? Commented Jan 26, 2024 at 9:06
  • @MC68020 Thank you, I have improved my question. Commented Jan 26, 2024 at 9:20
  • Errrr ? Not precisely answering my question though… filesystem (v.g. ext4, zfs, xfs… ?) mount options (possibly readable as part of the /etc/fstab if fs mounted that way) Commented Jan 26, 2024 at 9:28
  • Facing power outages in spite using the UPS ? Your device ? (what device ? an external USB disk ?) Commented Jan 26, 2024 at 9:32
  • @MC68020 The device on which my application runs.I have added information about /etc/fstab in the issue. Commented Jan 26, 2024 at 9:37

1 Answer 1

1

autoSync.sh runs sync every second; this means that every second, all pending disk writes are forced to disk. The impact this will have depends on the underlying storage, but in all cases it will increase the amount of data that gets written: in general operation, many writes end up being unnecessary (because they are replaced by later writes touching the same data, or they are writes to temporary files which are quickly deleted), and running sync periodically reduces the amount of time during which such unnecessary writes can be avoided.

autoCacheFree.sh runs sync and clears the pagecache every ten minutes. The sync effects are the same as discussed previously. Clearing the pagecache means that all subsequent reads have to read from the underlying storage, which will also affect performance.

You’d be better off addressing the underlying problem; it’s not clear what you mean by “files cannot be saved to disk after being written”, but I gather from your edited question that you’re writing a program and want to ensure that the data it writes ends up on disk as quickly as possible. The correct approach for this is to call fsync after every write your care about; this will ensure that those writes and up on disk, with a much smaller performance impact than your scripts.

Note too that while sleep ...; do ...; done is better than while true; do ...; sleep ...; done, because it makes it easier to stop the loop (a cancelled sleep will exit the loop).

4
  • Thank you, I have improved my question. Commented Jan 26, 2024 at 9:20
  • OK, you should use fsync then, see the updated answer. Commented Jan 26, 2024 at 9:26
  • My application has 4 processes, totaling about 40 threads, and there are many places where it may be written. Is there a better way? Commented Jan 26, 2024 at 9:35
  • You could also open the file with O_SYNC. Commented Jan 26, 2024 at 9:48

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.