Commit 211cfef0b26adf6eade801e8beae44526d31d1ca

Stefan Sperling 2022-01-05T19:57:10

use time-based rate-limiting for gotadmin progress output Suggested by naddy some time ago. ok tracey

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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
diff --git a/got/Makefile b/got/Makefile
index 7e44f91..8b43193 100644
--- a/got/Makefile
+++ b/got/Makefile
@@ -13,7 +13,7 @@ SRCS=		got.c blame.c commit_graph.c delta.c diff.c \
 		diff_myers.c diff_output.c diff_output_plain.c \
 		diff_output_unidiff.c diff_output_edscript.c \
 		diff_patience.c send.c deltify.c pack_create.c dial.c \
-		bloom.c murmurhash2.c
+		bloom.c murmurhash2.c ratelimit.c
 
 MAN =		${PROG}.1 got-worktree.5 git-repository.5 got.conf.5
 
diff --git a/gotadmin/Makefile b/gotadmin/Makefile
index 47d9d10..781133b 100644
--- a/gotadmin/Makefile
+++ b/gotadmin/Makefile
@@ -8,7 +8,7 @@ SRCS=		gotadmin.c \
 		inflate.c lockfile.c object.c object_cache.c object_create.c \
 		object_idset.c object_parse.c opentemp.c pack.c pack_create.c \
 		path.c privsep.c reference.c repository.c repository_admin.c \
-		worktree_open.c sha1.c bloom.c murmurhash2.c
+		worktree_open.c sha1.c bloom.c murmurhash2.c ratelimit.c
 MAN =		${PROG}.1
 
 CPPFLAGS = -I${.CURDIR}/../include -I${.CURDIR}/../lib
diff --git a/lib/got_lib_ratelimit.h b/lib/got_lib_ratelimit.h
new file mode 100644
index 0000000..9b95dcd
--- /dev/null
+++ b/lib/got_lib_ratelimit.h
@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+struct got_ratelimit {
+	struct timespec last;
+	struct timespec interval;
+};
+
+void got_ratelimit_init(struct got_ratelimit *, time_t, unsigned int);
+const struct got_error *got_ratelimit_check(int *, struct got_ratelimit *);
diff --git a/lib/pack_create.c b/lib/pack_create.c
index 5e229ab..2e421bd 100644
--- a/lib/pack_create.c
+++ b/lib/pack_create.c
@@ -20,6 +20,7 @@
 #include <sys/tree.h>
 #include <sys/uio.h>
 #include <sys/stat.h>
+#include <sys/time.h>
 
 #include <stdint.h>
 #include <imsg.h>
@@ -27,6 +28,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sha1.h>
+#include <time.h>
 #include <limits.h>
 #include <zlib.h>
 
@@ -47,6 +49,7 @@
 #include "got_lib_pack.h"
 #include "got_lib_privsep.h"
 #include "got_lib_repository.h"
+#include "got_lib_ratelimit.h"
 
 #ifndef MIN
 #define	MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
@@ -249,12 +252,30 @@ encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
 	return NULL;
 }
 
+static const struct got_error *
+report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
+    struct got_ratelimit *rl, off_t packfile_size, int ncommits,
+    int nobj_total, int obj_deltify, int nobj_written)
+{
+	const struct got_error *err;
+	int elapsed;
+
+	if (progress_cb == NULL)
+		return NULL;
+
+	err = got_ratelimit_check(&elapsed, rl);
+	if (err || !elapsed)
+		return err;
+
+	return progress_cb(progress_arg, packfile_size, ncommits,
+	    nobj_total, obj_deltify, nobj_written);
+}
 
 static const struct got_error *
 pick_deltas(struct got_pack_meta **meta, int nmeta, int nours,
     FILE *delta_cache, struct got_repository *repo,
     got_pack_progress_cb progress_cb, void *progress_arg,
-    got_cancel_cb cancel_cb, void *cancel_arg)
+    struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
 {
 	const struct got_error *err = NULL;
 	struct got_pack_meta *m = NULL, *base = NULL;
@@ -271,11 +292,10 @@ pick_deltas(struct got_pack_meta **meta, int nmeta, int nours,
 			if (err)
 				break;
 		}
-		if (progress_cb) {
-			err = progress_cb(progress_arg, 0L, nours, nmeta, i, 0);
-			if (err)
-				goto done;
-		}
+		err = report_progress(progress_cb, progress_arg, rl,
+		    0L, nours, nmeta, i, 0);
+		if (err)
+			goto done;
 		m = meta[i];
 
 		if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
@@ -923,7 +943,7 @@ read_meta(struct got_pack_meta ***meta, int *nmeta,
     struct got_object_id **theirs, int ntheirs,
     struct got_object_id **ours, int nours, struct got_repository *repo,
     int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
-    got_cancel_cb cancel_cb, void *cancel_arg)
+    struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
 {
 	const struct got_error *err = NULL;
 	struct got_object_id **ids = NULL;
@@ -964,12 +984,10 @@ read_meta(struct got_pack_meta ***meta, int *nmeta,
 		    loose_obj_only, cancel_cb, cancel_arg);
 		if (err)
 			goto done;
-		if (progress_cb) {
-			err = progress_cb(progress_arg, 0L, nours,
-			    v.nmeta, 0, 0);
-			if (err)
-				goto done;
-		}
+		err = report_progress(progress_cb, progress_arg, rl,
+		    0L, nours, v.nmeta, 0, 0);
+		if (err)
+			goto done;
 	}
 
 	for (i = 0; i < ntheirs; i++) {
@@ -990,12 +1008,10 @@ read_meta(struct got_pack_meta ***meta, int *nmeta,
 		    loose_obj_only, cancel_cb, cancel_arg);
 		if (err)
 			goto done;
-		if (progress_cb) {
-			err = progress_cb(progress_arg, 0L, nours,
-			    v.nmeta, 0, 0);
-			if (err)
-				goto done;
-		}
+		err = report_progress(progress_cb, progress_arg, rl,
+		    0L, nours, v.nmeta, 0, 0);
+		if (err)
+			goto done;
 	}
 
 	for (i = 0; i < nobj; i++) {
@@ -1003,12 +1019,12 @@ read_meta(struct got_pack_meta ***meta, int *nmeta,
 		    loose_obj_only, cancel_cb, cancel_arg);
 		if (err)
 			goto done;
-		if (progress_cb) {
-			err = progress_cb(progress_arg, 0L, nours,
-			    v.nmeta, 0, 0);
-			if (err)
-				goto done;
-		}
+		if (err)
+			goto done;
+		err = report_progress(progress_cb, progress_arg, rl,
+		    0L, nours, v.nmeta, 0, 0);
+		if (err)
+			goto done;
 	}
 
 	for (i = 0; i < nours; i++) {
@@ -1029,14 +1045,17 @@ read_meta(struct got_pack_meta ***meta, int *nmeta,
 		    loose_obj_only, cancel_cb, cancel_arg);
 		if (err)
 			goto done;
-		if (progress_cb) {
-			err = progress_cb(progress_arg, 0L, nours,
-			    v.nmeta, 0, 0);
-			if (err)
-				goto done;
-		}
+		err = report_progress(progress_cb, progress_arg, rl,
+		    0L, nours, v.nmeta, 0, 0);
+		if (err)
+			goto done;
 	}
 
+	if (progress_cb) {
+		err = progress_cb(progress_arg, 0L, nours, v.nmeta, 0, 0);
+		if (err)
+			goto done;
+	}
 done:
 	for (i = 0; i < nobj; i++) {
 		free(ids[i]);
@@ -1136,6 +1155,7 @@ genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
     struct got_pack_meta **meta, int nmeta, int nours,
     int use_offset_deltas, struct got_repository *repo,
     got_pack_progress_cb progress_cb, void *progress_arg,
+    struct got_ratelimit *rl,
     got_cancel_cb cancel_cb, void *cancel_arg)
 {
 	const struct got_error *err = NULL;
@@ -1167,12 +1187,10 @@ genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
 		goto done;
 	qsort(meta, nmeta, sizeof(struct got_pack_meta *), write_order_cmp);
 	for (i = 0; i < nmeta; i++) {
-		if (progress_cb) {
-			err = progress_cb(progress_arg, packfile_size, nours,
-			    nmeta, nmeta, i);
-			if (err)
-				goto done;
-		}
+		err = report_progress(progress_cb, progress_arg, rl,
+		    packfile_size, nours, nmeta, nmeta, i);
+		if (err)
+			goto done;
 		m = meta[i];
 		m->off = ftello(packfile);
 		err = got_object_raw_open(&raw, &outfd, repo, &m->id);
@@ -1281,10 +1299,12 @@ genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
 		err = got_ferror(packfile, GOT_ERR_IO);
 	packfile_size += SHA1_DIGEST_LENGTH;
 	packfile_size += sizeof(struct got_packfile_hdr);
-	err = progress_cb(progress_arg, packfile_size, nours,
-	    nmeta, nmeta, nmeta);
-	if (err)
-		goto done;
+	if (progress_cb) {
+		err = progress_cb(progress_arg, packfile_size, nours,
+		    nmeta, nmeta, nmeta);
+		if (err)
+			goto done;
+	}
 done:
 	if (delta_file && fclose(delta_file) == EOF && err == NULL)
 		err = got_error_from_errno("fclose");
@@ -1307,9 +1327,13 @@ got_pack_create(uint8_t *packsha1, FILE *packfile,
 	struct got_pack_meta **meta;
 	int nmeta;
 	FILE *delta_cache = NULL;
+	struct got_ratelimit rl;
+
+	got_ratelimit_init(&rl, 0, 500);
 
 	err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
-	    loose_obj_only, progress_cb, progress_arg, cancel_cb, cancel_arg);
+	    loose_obj_only, progress_cb, progress_arg, &rl,
+	    cancel_cb, cancel_arg);
 	if (err)
 		return err;
 
@@ -1326,7 +1350,7 @@ got_pack_create(uint8_t *packsha1, FILE *packfile,
 
 	if (nmeta > 0) {
 		err = pick_deltas(meta, nmeta, nours, delta_cache, repo,
-		    progress_cb, progress_arg, cancel_cb, cancel_arg);
+		    progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
 		if (err)
 			goto done;
 		if (fseeko(delta_cache, 0L, SEEK_SET) == -1) {
@@ -1336,7 +1360,7 @@ got_pack_create(uint8_t *packsha1, FILE *packfile,
 	}
 
 	err = genpack(packsha1, packfile, delta_cache, meta, nmeta, nours, 1,
-	    repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
+	    repo, progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
 	if (err)
 		goto done;
 done:
diff --git a/lib/ratelimit.c b/lib/ratelimit.c
new file mode 100644
index 0000000..219e790
--- /dev/null
+++ b/lib/ratelimit.c
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/time.h>
+
+#include <stdio.h>
+#include <limits.h>
+#include <string.h>
+#include <time.h>
+
+#include "got_lib_ratelimit.h"
+
+#include "got_error.h"
+
+void
+got_ratelimit_init(struct got_ratelimit *rl, time_t interval_sec,
+    unsigned int interval_msec)
+{
+	memset(rl, 0, sizeof(*rl));
+	rl->interval.tv_sec = interval_sec;
+	rl->interval.tv_nsec = interval_msec * 1000000UL;
+}
+
+const struct got_error *
+got_ratelimit_check(int *elapsed, struct got_ratelimit *rl)
+{
+	struct timespec now, delta;
+
+	if (clock_gettime(CLOCK_MONOTONIC, &now) == -1)
+		return got_error_from_errno("clock_gettime");
+
+	if (timespecisset(&rl->last)) {
+		timespecsub(&now, &rl->last, &delta);
+		*elapsed = timespeccmp(&delta, &rl->interval, >=) ? 1 : 0;
+	} else
+		*elapsed = 1;
+
+	if (*elapsed)
+		rl->last = now;
+
+	return NULL;
+}
diff --git a/lib/repository_admin.c b/lib/repository_admin.c
index 4e1253b..40af887 100644
--- a/lib/repository_admin.c
+++ b/lib/repository_admin.c
@@ -54,6 +54,7 @@
 #include "got_lib_pack_create.h"
 #include "got_lib_sha1.h"
 #include "got_lib_lockfile.h"
+#include "got_lib_ratelimit.h"
 
 #ifndef nitems
 #define nitems(_a)	(sizeof((_a)) / sizeof((_a)[0]))
@@ -598,9 +599,27 @@ done:
 }
 
 static const struct got_error *
+report_cleanup_progress(got_cleanup_progress_cb progress_cb,
+    void *progress_arg, struct got_ratelimit *rl,
+    int nloose, int ncommits, int npurged)
+{
+	const struct got_error *err;
+	int elapsed;
+
+	if (progress_cb == NULL)
+		return NULL;
+
+	err = got_ratelimit_check(&elapsed, rl);
+	if (err || !elapsed)
+		return err;
+
+	return progress_cb(progress_arg, nloose, ncommits, npurged);
+}
+
+static const struct got_error *
 get_loose_object_ids(struct got_object_idset **loose_ids, off_t *ondisk_size,
     got_cleanup_progress_cb progress_cb, void *progress_arg,
-    struct got_repository *repo)
+    struct got_ratelimit *rl, struct got_repository *repo)
 {
 	const struct got_error *err = NULL;
 	char *path_objects = NULL, *path = NULL;
@@ -688,13 +707,11 @@ get_loose_object_ids(struct got_object_idset **loose_ids, off_t *ondisk_size,
 			err = got_object_idset_add(*loose_ids, &id, NULL);
 			if (err)
 				goto done;
-			if (progress_cb) {
-				err = progress_cb(progress_arg,
-				    got_object_idset_num_elements(*loose_ids),
-				    -1, -1);
-				if (err)
-					goto done;
-			}
+			err = report_cleanup_progress(progress_cb,
+			    progress_arg, rl,
+			    got_object_idset_num_elements(*loose_ids), -1, -1);
+			if (err)
+				goto done;
 		}
 
 		if (closedir(dir) != 0) {
@@ -878,7 +895,8 @@ static const struct got_error *
 load_commit_or_tag(struct got_object_idset *loose_ids, int *ncommits,
     int *npacked, struct got_object_idset *traversed_ids,
     struct got_object_id *id, struct got_repository *repo,
-    got_cleanup_progress_cb progress_cb, void *progress_arg, int nloose,
+    got_cleanup_progress_cb progress_cb, void *progress_arg,
+    struct got_ratelimit *rl, int nloose,
     got_cancel_cb cancel_cb, void *cancel_arg)
 {
 	const struct got_error *err;
@@ -986,11 +1004,10 @@ load_commit_or_tag(struct got_object_idset *loose_ids, int *ncommits,
 		if (commit || tag)
 			(*ncommits)++; /* scanned tags are counted as commits */
 
-		if (progress_cb) {
-			err = progress_cb(progress_arg, nloose, *ncommits, -1);
-			if (err)
-				break;
-		}
+		err = report_cleanup_progress(progress_cb, progress_arg, rl,
+		    nloose, *ncommits, -1);
+		if (err)
+			break;
  
 		if (commit) {
 			/* Find parent commits to scan. */
@@ -1024,6 +1041,7 @@ struct purge_loose_object_arg {
 	struct got_repository *repo;
 	got_cleanup_progress_cb progress_cb;
 	void *progress_arg;
+	struct got_ratelimit *rl;
 	int nloose;
 	int ncommits;
 	int npurged;
@@ -1075,10 +1093,10 @@ purge_loose_object(struct got_object_id *id, void *data, void *arg)
 
 		a->npurged++;
 		a->size_purged += sb.st_size;
-		if (a->progress_cb) {
-			err = a->progress_cb(a->progress_arg, a->nloose,
-			    a->ncommits, a->npurged);
-		}
+		err = report_cleanup_progress(a->progress_cb, a->progress_arg,
+		    a->rl, a->nloose, a->ncommits, a->npurged);
+		if (err)
+			goto done;
 	}
 done:
 	if (fd != -1 && close(fd) == -1 && err == NULL)
@@ -1104,15 +1122,17 @@ got_repo_purge_unreferenced_loose_objects(struct got_repository *repo,
 	struct got_reflist_entry *re;
 	struct purge_loose_object_arg arg;
 	time_t max_mtime = 0;
+	struct got_ratelimit rl;
 
 	TAILQ_INIT(&refs);
+	got_ratelimit_init(&rl, 0, 500);
 
 	*size_before = 0;
 	*size_after = 0;
 	*npacked = 0;
 
 	err = get_loose_object_ids(&loose_ids, size_before,
-	    progress_cb, progress_arg, repo);
+	    progress_cb, progress_arg, &rl, repo);
 	if (err)
 		return err;
 	nloose = got_object_idset_num_elements(loose_ids);
@@ -1153,15 +1173,8 @@ got_repo_purge_unreferenced_loose_objects(struct got_repository *repo,
 	for (i = 0; i < nreferenced; i++) {
 		struct got_object_id *id = referenced_ids[i];
 		err = load_commit_or_tag(loose_ids, &ncommits, npacked,
-		    traversed_ids, id, repo, progress_cb, progress_arg, nloose, 
-		    cancel_cb, cancel_arg);
-		if (err)
-			goto done;
-	}
-
-	/* Produce a final progress report in case no objects can be purged. */
-	if (got_object_idset_num_elements(loose_ids) == 0 && progress_cb) {
-		err = progress_cb(progress_arg, nloose, ncommits, 0);
+		    traversed_ids, id, repo, progress_cb, progress_arg, &rl,
+		    nloose, cancel_cb, cancel_arg);
 		if (err)
 			goto done;
 	}
@@ -1170,6 +1183,7 @@ got_repo_purge_unreferenced_loose_objects(struct got_repository *repo,
 	arg.repo = repo;
 	arg.progress_arg = progress_arg;
 	arg.progress_cb = progress_cb;
+	arg.rl = &rl;
 	arg.nloose = nloose;
 	arg.npurged = 0;
 	arg.size_purged = 0;
@@ -1181,6 +1195,14 @@ got_repo_purge_unreferenced_loose_objects(struct got_repository *repo,
 	if (err)
 		goto done;
 	*size_after = *size_before - arg.size_purged;
+
+	/* Produce a final progress report. */
+	if (progress_cb) {
+		err = progress_cb(progress_arg, nloose, ncommits,
+		    got_object_idset_num_elements(loose_ids));
+		if (err)
+			goto done;
+	}
 done:
 	got_object_idset_free(loose_ids);
 	got_object_idset_free(traversed_ids);