]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - scrub/progress.c
xfsprogs: Release v6.7.0
[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 "path.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 #define NSEC_PER_SEC (1000000000)
114 static void *
115 progress_report_thread(void *arg)
116 {
117 struct timespec abstime;
118 int ret;
119
120 pthread_mutex_lock(&pt.lock);
121 while (1) {
122 /* Every half second. */
123 ret = clock_gettime(CLOCK_REALTIME, &abstime);
124 if (ret)
125 break;
126 abstime.tv_nsec += NSEC_PER_SEC / 2;
127 if (abstime.tv_nsec > NSEC_PER_SEC) {
128 abstime.tv_sec++;
129 abstime.tv_nsec -= NSEC_PER_SEC;
130 }
131 pthread_cond_timedwait(&pt.wakeup, &pt.lock, &abstime);
132 if (pt.terminate)
133 break;
134 progress_report(ptcounter_value(pt.ptc));
135 }
136 pthread_mutex_unlock(&pt.lock);
137 return NULL;
138 }
139
140 /* End a phase of progress reporting. */
141 void
142 progress_end_phase(void)
143 {
144 if (!pt.fp)
145 return;
146
147 pthread_mutex_lock(&pt.lock);
148 pt.terminate = true;
149 pthread_mutex_unlock(&pt.lock);
150 pthread_cond_broadcast(&pt.wakeup);
151 pthread_join(pt.thread, NULL);
152
153 progress_report(pt.max);
154 ptcounter_free(pt.ptc);
155 pt.max = 0;
156 pt.ptc = NULL;
157 if (pt.fp) {
158 fprintf(pt.fp, CLEAR_EOL);
159 fflush(pt.fp);
160 }
161 pt.fp = NULL;
162 }
163
164 /* Set ourselves up to report progress. */
165 bool
166 progress_init_phase(
167 struct scrub_ctx *ctx,
168 FILE *fp,
169 unsigned int phase,
170 uint64_t max,
171 int rshift,
172 unsigned int nr_threads)
173 {
174 int ret;
175
176 assert(pt.fp == NULL);
177 if (fp == NULL || max == 0) {
178 pt.fp = NULL;
179 return true;
180 }
181 pt.fp = fp;
182 pt.isatty = isatty(fileno(fp));
183 pt.tag = ctx->mntpoint;
184 pt.max = max;
185 pt.phase = phase;
186 pt.rshift = rshift;
187 pt.twiddle = 0;
188 pt.terminate = false;
189
190 pt.ptc = ptcounter_init(nr_threads);
191 if (!pt.ptc)
192 goto out_max;
193
194 ret = pthread_create(&pt.thread, NULL, progress_report_thread, NULL);
195 if (ret)
196 goto out_ptcounter;
197
198 return true;
199
200 out_ptcounter:
201 ptcounter_free(pt.ptc);
202 pt.ptc = NULL;
203 out_max:
204 pt.max = 0;
205 pt.fp = NULL;
206 return false;
207 }