]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - scrub/counter.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2018-2024 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <djwong@kernel.org>
12 #include "libfrog/ptvar.h"
18 * This is a global counter object that uses per-thread counters to
19 * count things without having to content for a single shared lock.
20 * Provided we know the number of threads that will be accessing the
21 * counter, each thread gets its own thread-specific counter variable.
22 * Changing the value is fast, though retrieving the value is expensive
29 /* Allocate per-thread counter. */
33 struct ptcounter
**pp
)
38 p
= malloc(sizeof(struct ptcounter
));
41 ret
= -ptvar_alloc(nr
, sizeof(uint64_t), &p
->var
);
50 /* Free per-thread counter. */
53 struct ptcounter
*ptc
)
59 /* Add a quantity to the counter. */
62 struct ptcounter
*ptc
,
68 p
= ptvar_get(ptc
->var
, &ret
);
81 uint64_t *sum
= foreach_arg
;
82 uint64_t *count
= data
;
88 /* Return the approximate value of this counter. */
91 struct ptcounter
*ptc
,
95 return -ptvar_foreach(ptc
->var
, ptcounter_val_helper
, sum
);