]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - scrub/progress.c
xfs_scrub: handle concurrent directory updates during name scan
[thirdparty/xfsprogs-dev.git] / scrub / progress.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2018 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
5 */
6 #include "xfs.h"
7 #include <dirent.h>
8 #include <pthread.h>
9 #include <sys/statvfs.h>
10 #include <time.h>
11 #include "libfrog/paths.h"
12 #include "disk.h"
13 #include "read_verify.h"
14 #include "xfs_scrub.h"
15 #include "common.h"
16 #include "counter.h"
17 #include "progress.h"
18
19 /*
20 * Progress Tracking
21 *
22 * For scrub phases that expect to take a long time, this facility uses
23 * the threaded counter and some phase/state information to report the
24 * progress of a particular phase to stdout. Each phase that wants
25 * progress information needs to set up the tracker with an estimate of
26 * the work to be done and periodic updates when work items finish. In
27 * return, the progress tracker will print a pretty progress bar and
28 * twiddle to a tty, or a raw numeric output compatible with fsck -C.
29 */
30 struct progress_tracker {
31 FILE *fp;
32 const char *tag;
33 struct ptcounter *ptc;
34 uint64_t max;
35 unsigned int phase;
36 int rshift;
37 int twiddle;
38 bool isatty;
39 bool terminate;
40 pthread_t thread;
41
42 /* static state */
43 pthread_mutex_t lock;
44 pthread_cond_t wakeup;
45 };
46
47 static struct progress_tracker pt = {
48 .lock = PTHREAD_MUTEX_INITIALIZER,
49 .wakeup = PTHREAD_COND_INITIALIZER,
50 };
51
52 /* Add some progress. */
53 void
54 progress_add(
55 uint64_t x)
56 {
57 if (pt.fp)
58 ptcounter_add(pt.ptc, x);
59 }
60
61 static const char twiddles[] = "|/-\\";
62
63 static void
64 progress_report(
65 uint64_t sum)
66 {
67 char buf[81];
68 int tag_len;
69 int num_len;
70 int pbar_len;
71 int plen;
72
73 if (!pt.fp)
74 return;
75
76 if (sum > pt.max)
77 sum = pt.max;
78
79 /* Emulate fsck machine-readable output (phase, cur, max, label) */
80 if (!pt.isatty) {
81 snprintf(buf, sizeof(buf), _("%u %"PRIu64" %"PRIu64" %s"),
82 pt.phase, sum, pt.max, pt.tag);
83 fprintf(pt.fp, "%s\n", buf);
84 fflush(pt.fp);
85 return;
86 }
87
88 /* Interactive twiddle progress bar. */
89 if (debug) {
90 num_len = snprintf(buf, sizeof(buf),
91 "%c %"PRIu64"/%"PRIu64" (%.1f%%)",
92 twiddles[pt.twiddle],
93 sum >> pt.rshift,
94 pt.max >> pt.rshift,
95 100.0 * sum / pt.max);
96 } else {
97 num_len = snprintf(buf, sizeof(buf),
98 "%c (%.1f%%)",
99 twiddles[pt.twiddle],
100 100.0 * sum / pt.max);
101 }
102 memmove(buf + sizeof(buf) - (num_len + 1), buf, num_len + 1);
103 tag_len = snprintf(buf, sizeof(buf), _("Phase %u: |"), pt.phase);
104 pbar_len = sizeof(buf) - (num_len + 1 + tag_len);
105 plen = (int)((double)pbar_len * sum / pt.max);
106 memset(buf + tag_len, '=', plen);
107 memset(buf + tag_len + plen, ' ', pbar_len - plen);
108 pt.twiddle = (pt.twiddle + 1) % 4;
109 fprintf(pt.fp, "%c%s\r%c", START_IGNORE, buf, END_IGNORE);
110 fflush(pt.fp);
111 }
112
113 static void *
114 progress_report_thread(void *arg)
115 {
116 struct timespec abstime;
117 int ret;
118
119 pthread_mutex_lock(&pt.lock);
120 while (1) {
121 uint64_t progress_val;
122
123 /* Every half second. */
124 ret = clock_gettime(CLOCK_REALTIME, &abstime);
125 if (ret)
126 break;
127 abstime.tv_nsec += NSEC_PER_SEC / 2;
128 if (abstime.tv_nsec > NSEC_PER_SEC) {
129 abstime.tv_sec++;
130 abstime.tv_nsec -= NSEC_PER_SEC;
131 }
132 ret = pthread_cond_timedwait(&pt.wakeup, &pt.lock, &abstime);
133 if (ret && ret != ETIMEDOUT)
134 break;
135 if (pt.terminate)
136 break;
137 ret = ptcounter_value(pt.ptc, &progress_val);
138 if (!ret)
139 progress_report(progress_val);
140 }
141 pthread_mutex_unlock(&pt.lock);
142 return NULL;
143 }
144
145 /* End a phase of progress reporting. */
146 void
147 progress_end_phase(void)
148 {
149 if (!pt.fp)
150 return;
151
152 pthread_mutex_lock(&pt.lock);
153 pt.terminate = true;
154 pthread_mutex_unlock(&pt.lock);
155 pthread_cond_broadcast(&pt.wakeup);
156 pthread_join(pt.thread, NULL);
157
158 progress_report(pt.max);
159 ptcounter_free(pt.ptc);
160 pt.max = 0;
161 pt.ptc = NULL;
162 if (pt.fp) {
163 fprintf(pt.fp, CLEAR_EOL);
164 fflush(pt.fp);
165 }
166 pt.fp = NULL;
167 }
168
169 /*
170 * Set ourselves up to report progress. If errors are encountered, this
171 * function will log them and return nonzero.
172 */
173 int
174 progress_init_phase(
175 struct scrub_ctx *ctx,
176 FILE *fp,
177 unsigned int phase,
178 uint64_t max,
179 int rshift,
180 unsigned int nr_threads)
181 {
182 int ret;
183
184 assert(pt.fp == NULL);
185 if (fp == NULL || max == 0) {
186 pt.fp = NULL;
187 return 0;
188 }
189 pt.fp = fp;
190 pt.isatty = isatty(fileno(fp));
191 pt.tag = ctx->mntpoint;
192 pt.max = max;
193 pt.phase = phase;
194 pt.rshift = rshift;
195 pt.twiddle = 0;
196 pt.terminate = false;
197
198 ret = ptcounter_alloc(nr_threads, &pt.ptc);
199 if (ret) {
200 str_liberror(ctx, ret, _("allocating progress counter"));
201 goto out_max;
202 }
203
204 ret = pthread_create(&pt.thread, NULL, progress_report_thread, NULL);
205 if (ret) {
206 str_liberror(ctx, ret, _("creating progress reporting thread"));
207 goto out_ptcounter;
208 }
209
210 return 0;
211
212 out_ptcounter:
213 ptcounter_free(pt.ptc);
214 pt.ptc = NULL;
215 out_max:
216 pt.max = 0;
217 pt.fp = NULL;
218 return ret;
219 }