186

I've copied a large file to a USB disk mounted on a Linux system with async. This returns to a command prompt relatively quickly, but when I type sync, of course, it all has to go to disk, and that takes a long time.

I understand that it's going to be slow, but is there somewhere where I can watch a counter go down to zero? Watching buffers in top doesn't help.

mattdm
  • 40,245

8 Answers8

309

Looking at /proc/meminfo will show the Dirty number shrinking over time as all the data spools out; some of it may spill into Writeback as well. That will be a summary against all devices, but in the cases where one device on the system is much slower than the rest you'll usually end up where everything in that queue is related to it. You'll probably find the Dirty number large when you start and the sync finishes about the same time it approaches 0. Try this to get an interactive display:

watch -d grep -e Dirty: -e Writeback: /proc/meminfo

With regular disks I can normally ignore Writeback, but I'm not sure if it's involved more often in the USB transfer path. If it just bounces up and down without a clear trend to it, you can probably just look at the Dirty number.

Greg Smith
  • 3,571
  • 6
    On my system, writeback stays at a few megs until right near the end, when Dirty is empty, at which point it starts going down too. – mattdm Oct 08 '12 at 02:02
  • 2
    same, I copied a 6 gig file to a usb formatted with exfat on fedora 36, and "dirty" was like 200 kb while writeback was a lot more, until the very end when it went to 0. – xdavidliu Sep 11 '22 at 16:00
29

You can look at the /sys/block/<device>/stat file for the appropriate device while you're syncing. The 9th column will indicate the number of in-flight requests on the device, which should go down to zero when the sync is done.
Don't know of a way to translate that to a number of bytes, but it should give you a rough idea of how much "stuff" is still pending.

See the stat.txt file in the kernel documentation for a bit more information. (There's also an inflight file in that directory on my system which looks like it could contain read and write in-flight requests, but I can't find docs for that.)

Mat
  • 52,586
  • 3
    Just for handy reference, combining an idea from another answer: watch -t -n1 'awk "{ print \$9 }" /sys/block/sdd/stat' – mattdm Oct 08 '12 at 01:59
  • 7
    For my usb stick, this tends to hover around 150 throughout the duration of the copy operation and the sync afterwards. It does go to 0, but only at the very end. That makes the other answer more useful for impatiently watching progress. – mattdm Oct 08 '12 at 02:01
  • 1
    (Even though in theory I like looking at just the appropriate device rather than the systemwide info.) – mattdm Oct 08 '12 at 02:03
  • Is there a way to get amount of bytes written (synced, flushed not in flight) or cached to be written to drive?

    If I had a write-cache size per drive I could use that with du to calculate the real data that is synced on drive.

    – unfa Apr 26 '17 at 10:06
20

By using Greg's answer, you can simply have sync run in background while displaying the state of the Dirty block in memory.

To achieve this, simply run this command:

sync & watch -n 1 grep -e Dirty: /proc/meminfo

This will call sync in the background while executing watch in the front. When the sync command will have finished (around when the size of the Dirty block has reached 0), you will have an output that looks like this :

1]  + 27260 done        sync

This means that the command has finished and you can kill the watch command with Ctrl+C.

jflemieux
  • 301
  • 2
  • 2
  • 1
    BTW: I know this is an old question, but it is the first one that pops-up on Google when someone looks for it, and I wanted to add the solution I found. – jflemieux May 24 '17 at 14:15
  • 4
    Too bad sync/umount didn't get updates from the kernel and could print this kind of stuff directly if told to. – mcr Oct 07 '17 at 22:16
  • 1
    That's cute. Thanks for improving my surprisingly popular suggestion. – Greg Smith Feb 15 '21 at 21:42
7

Based on the other answers I made this over-bloated one-liner that tracks summary progress as well as device-specific progress:

# Create the command
watchSync() {
  watch -n1 'grep -E "(Dirty|Write)" /proc/meminfo; echo; ls /sys/block/ | while read device; do awk "{ print \"$device: \"  \$9 }" "/sys/block/$device/stat"; done'
}

Run the command

watchSync

The output looks like this:

Dirty:            848956 kB
Writeback:        125948 kB
WritebackTmp:          0 kB

dm-0: 0 dm-1: 0 loop0: 0 loop1: 0 loop2: 0 loop3: 0 loop4: 0 loop5: 0 loop6: 0 loop7: 0 nvme0n1: 0 sda: 0 sdb: 0 sdc: 124

The above tells me that 1) I have a few 100,000 kBytes that need writing, and 2) the device being written to is sdc. Sync is complete when, in this case, sdc and Writeback hit zero (this will happen at the same time).

Donatzsky
  • 103
1

You can watch current block device I/O traffic with nmon, as in:

NMON=ld nmon -s1

(this preselects display of load graph and disk graph with 1 second refresh time.. this can also be set by starting nmon without parameters, than pressing l, d, - to toggle graphs and reduce refresh time from default 2s .. + of course would increase refresh delay.)

eMPee584
  • 327
0

I wrote a script for this based on the answers here:

watchSync

#!/bin/sh

Output the Dirty section of /proc/meminfo

report() { local dirty="$(grep -w Dirty: /proc/meminfo | cut -d ':' -f 2 | tr -d ' ')" echo -n -e "\e[2K\rSyncing ${dirty}... " }

report

Start syncing

sync & SYNC_PID=$!

Give a short sleep in case there's not much

sleep 0.125

While sync is running, report

while ps -p $SYNC_PID > /dev/null 2>&1; do report sleep 1 done

echo "Done!"

Fordi
  • 502
0
repr () {
    if [[ $# -gt 0 ]]; then
        local result="$(printf -- '%q ' "$@")"
        printf -- '%s' "${result::-1}"
    fi
    return $?
}

sync () {
    local args
    if [[ $- = *i* ]]; then
        args="$(repr "$@")"
        bash -sc "$(shopt -p); $(declare -f); ${FUNCNAME[0]} $args"
        return $?
    fi
    local initial_value
    local percentage
    local backspace=''
    local i
    command sync "$@" &
    initial_value="$(grep -e 'Dirty:' /proc/meminfo | awk '{print $2}')"
    if [[ $initial_value = 0 ]]; then initial_value=1; fi
    sleep 0.1
    while ps -p $! > /dev/null; do
        percentage="$(((($initial_value-$(grep -e 'Dirty:' /proc/meminfo | awk '{print $2}'))*100)/$initial_value))"
        percentage="$(( $percentage < 0 ? 0 : $percentage ))%"
        printf -- '%b' "$backspace$percentage"
        backspace=''
        for (( i=1; i<=${#percentage}; i++ )); do backspace+='\b \b'; done
        sleep 1
    done
    printf -- '%b' "$backspace"
    return $(wait $!)
}
Mario Palumbo
  • 233
  • 1
  • 14
0

If anyone is looking for any tools that automate rsync and sync monitoring afterwards. I've made one, based on others great answers, check it out:

https://github.com/satk0/usbdrivetools

Using the tool you can simply run:

usbcp /path/to/src /path/to/dest

To have a source file copied to USB Drive mounted destination.

satk0
  • 141