summaryrefslogtreecommitdiffstats
path: root/src/utils_progress.c
blob: 76b181891256836bbf4a19c877a6c85f112da48b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
 * cryptsetup - progress output utilities
 *
 * Copyright (C) 2009-2023 Red Hat, Inc. All rights reserved.
 * Copyright (C) 2009-2023 Milan Broz
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <assert.h>
#include "cryptsetup.h"

#define MINUTES_90 UINT64_C(5400000000)   /* 90 minutes in microseconds */
#define HOURS_36   UINT64_C(129600000000) /* 36 hours in microseconds */

#define MINUTES(A) (A) / UINT64_C(60000000)    /* microseconds to minutes */
#define SECONDS(A) (A) / UINT64_C(1000000)     /* microseconds to seconds */
#define HOURS(A)   (A) / UINT64_C(3600000000)  /* microseconds to hours */
#define DAYS(A)    (A) / UINT64_C(86400000000) /* microseconds to days */

#define REMAIN_SECONDS(A) (SECONDS((A))) % 60
#define REMAIN_MINUTES(A) (MINUTES((A))) % 60

/* The difference in microseconds between two times in "timeval" format. */
static uint64_t time_diff(struct timeval *start, struct timeval *end)
{
	return (end->tv_sec - start->tv_sec) * UINT64_C(1000000)
		+ (end->tv_usec - start->tv_usec);
}

static void tools_clear_line(void)
{
	/* vt100 code clear line */
	log_std("\33[2K\r");
}

static void bytes_to_units(uint64_t *bytes, const char **units)
{
	if (*bytes < (UINT64_C(1) << 32)) { /* less than 4 GiBs */
		*units = "MiB";
		*bytes >>= 20;
	} else if (*bytes < (UINT64_C(1) << 42)) { /* less than 4 TiBs */
		*units = "GiB";
		*bytes >>= 30;
	} else if (*bytes < (UINT64_C(1) << 52)) { /* less than 4 PiBs */
		*units = "TiB";
		*bytes >>= 40;
	} else if (*bytes < (UINT64_C(1) << 62)) { /* less than 4 EiBs */
		*units = "PiB";
		*bytes >>= 50;
	} else {
		*units = "EiB";
		*bytes >>= 60;
	}
}

static bool time_to_human_string(uint64_t usecs, char *buf, size_t buf_len)
{
	ssize_t r;

	if (usecs < MINUTES_90)
		r = snprintf(buf, buf_len, _("%02" PRIu64 "m%02" PRIu64 "s"), MINUTES(usecs), REMAIN_SECONDS(usecs));
	else if (usecs < HOURS_36)
		r = snprintf(buf, buf_len, _("%02" PRIu64 "h%02" PRIu64 "m%02" PRIu64 "s"), HOURS(usecs), REMAIN_MINUTES(usecs), REMAIN_SECONDS(usecs));
	else
		r = snprintf(buf, buf_len, _("%02" PRIu64 " days"), DAYS(usecs));

	if (r < 0 || (size_t)r >= buf_len)
		return false;

	return true;
}

static void log_progress(uint64_t bytes, uint64_t device_size, uint64_t eta, double uib, const char *ustr, const char *eol)
{
	double progress;
	int r;
	const char *units;
	char time[128], written[128], speed[128];

	/*
	 * TRANSLATORS: 'time' string with examples:
	 * "12m44s"    : meaning 12 minutes 44 seconds
	 * "26h12m44s" : meaning 26 hours 12 minutes 44 seconds
	 * "3 days"
	 */
	if (!time_to_human_string(eta, time, sizeof(time)))
		return;

	progress = (double)bytes / device_size * 100.0;

	bytes_to_units(&bytes, &units);
	r = snprintf(written, sizeof(written), _("%4" PRIu64 " %s written"), bytes, units);
	if (r < 0 || (size_t)r >= sizeof(written))
		return;

	r = snprintf(speed, sizeof(speed), _("speed %5.1f %s/s"), uib, ustr);
	if (r < 0 || (size_t)r >= sizeof(speed))
		return;

	/*
	 * TRANSLATORS: 'time', 'written' and 'speed' string are supposed
	 * to get translated as well. 'eol' is always new-line or empty.
	 * See above.
	 */
	log_std(_("Progress: %5.1f%%, ETA %s, %s, %s%s"),
		progress, time, written, speed, eol);
}

static void log_progress_final(uint64_t time_spent, uint64_t bytes, double uib, const char *ustr)
{
	int r;
	const char *units;
	char time[128], written[128], speed[128];

	/*
	 * TRANSLATORS: 'time' string with examples:
	 * "12m44s"    : meaning 12 minutes 44 seconds
	 * "26h12m44s" : meaning 26 hours 12 minutes 44 seconds
	 * "3 days"
	 */
	if (!time_to_human_string(time_spent, time, sizeof(time)))
		return;

	bytes_to_units(&bytes, &units);
	r = snprintf(written, sizeof(written) - 1, _("%4" PRIu64 " %s written"), bytes, units);
	if (r < 0 || (size_t)r >= sizeof(written))
		return;

	r = snprintf(speed, sizeof(speed) - 1, _("speed %5.1f %s/s"), uib, ustr);
	if (r < 0 || (size_t)r >= sizeof(speed))
		return;

	/*
	 * TRANSLATORS: 'time', 'written' and 'speed' string are supposed
	 * to get translated as well. See above
	 */
	log_std(_("Finished, time %s, %s, %s\n"), time, written, speed);
}

static bool calculate_tdiff(bool final, uint64_t bytes, struct tools_progress_params *parms, double *r_tdiff)
{
	uint64_t frequency;
	struct timeval now_time;

	assert(r_tdiff);

	gettimeofday(&now_time, NULL);
	if (parms->start_time.tv_sec == 0 && parms->start_time.tv_usec == 0) {
		parms->start_time = now_time;
		parms->end_time = now_time;
		parms->start_offset = bytes;
		return false;
	}

	if (parms->frequency)
		frequency = parms->frequency * UINT64_C(1000000);
	else
		frequency = 500000;

	if (!final && time_diff(&parms->end_time, &now_time) < frequency)
		return false;

	parms->end_time = now_time;

	*r_tdiff = time_diff(&parms->start_time, &parms->end_time) / 1E6;
	if (!*r_tdiff)
		return false;

	return true;
}

static void tools_time_progress(uint64_t device_size, uint64_t bytes, struct tools_progress_params *parms)
{
	uint64_t eta;
	double tdiff, uib;
	const char *eol, *ustr;
	bool final = (bytes == device_size);

	if (!calculate_tdiff(final, bytes, parms, &tdiff))
		return;

	if (parms->frequency)
		eol = "\n";
	else
		eol = "";

	uib = (double)(bytes - parms->start_offset) / tdiff;

	eta = (uint64_t)((device_size / uib - tdiff) * 1E6);

	if (uib > 1073741824.0f) {
		uib /= 1073741824.0f;
		ustr = "GiB";
	} else if (uib > 1048576.0f) {
		uib /= 1048576.0f;
		ustr = "MiB";
	} else if (uib > 1024.0f) {
		uib /= 1024.0f;
		ustr = "KiB";
	} else
		ustr = "B";

	if (!parms->frequency)
		tools_clear_line();

	if (final)
		log_progress_final((uint64_t)(tdiff * 1E6), bytes, uib, ustr);
	else
		log_progress(bytes, device_size, eta, uib, ustr, eol);

	fflush(stdout);
}

static void log_progress_json(const char *device, uint64_t bytes, uint64_t device_size, uint64_t eta, uint64_t uib, uint64_t time_spent)
{
	int r;
	char json[PATH_MAX+256];

	r = snprintf(json, sizeof(json) - 1,
		     "{\"device\":\"%s\","
		     "\"device_bytes\":\"%"	PRIu64 "\","	/* in bytes */
		     "\"device_size\":\"%"	PRIu64 "\","	/* in bytes */
		     "\"speed\":\"%"		PRIu64 "\","	/* in bytes per second */
		     "\"eta_ms\":\"%"		PRIu64 "\","	/* in milliseconds */
		     "\"time_ms\":\"%"		PRIu64 "\"}\n",	/* in milliseconds */
		     device, bytes, device_size, uib, eta, time_spent);

	if (r < 0 || (size_t)r >= sizeof(json) - 1)
		return;

	log_std("%s", json);
}

static void tools_time_progress_json(uint64_t device_size, uint64_t bytes, struct tools_progress_params *parms)
{
	double tdiff, uib;
	bool final = (bytes == device_size);

	if (!calculate_tdiff(final, bytes, parms, &tdiff))
		return;

	uib = (double)(bytes - parms->start_offset) / tdiff;

	log_progress_json(parms->device,
			  bytes,
			  device_size,
			  final ? UINT64_C(0) : (uint64_t)((device_size / uib - tdiff) * 1E3),
			  (uint64_t)uib,
			  (uint64_t)(tdiff * 1E3));

	fflush(stdout);
}

int tools_progress(uint64_t size, uint64_t offset, void *usrptr)
{
	int r = 0;
	struct tools_progress_params *parms = (struct tools_progress_params *)usrptr;

	if (parms && parms->json_output)
		tools_time_progress_json(size, offset, parms);
	else if (parms && !parms->batch_mode)
		tools_time_progress(size, offset, parms);

	check_signal(&r);
	if (r) {
		if (!parms || (!parms->frequency && !parms->json_output))
			tools_clear_line();
		if (parms && parms->interrupt_message)
			log_err("%s", parms->interrupt_message);
	}

	return r;
}

const char *tools_get_device_name(const char *device, char **r_backing_file)
{
	char *bfile;

	assert(r_backing_file);

	bfile = crypt_loop_backing_file(device);
	if (bfile) {
		*r_backing_file = bfile;
		return bfile;
	}

	return device;
}