Skip to content

Commit

Permalink
Update bcachefs sources to 3e93567c51 bcachefs: Switch to local_clock…
Browse files Browse the repository at this point in the history
…() for fastpath time source
  • Loading branch information
Kent Overstreet committed Oct 15, 2022
1 parent 3165f53 commit e0a51cc
Show file tree
Hide file tree
Showing 27 changed files with 1,058 additions and 274 deletions.
2 changes: 1 addition & 1 deletion .bcachefs_revision
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6ee8a33cee5dfb74a1fb6ff348578fd43aae3a14
3e93567c5196ef0c80e2ac3c08295130d858dfd6
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@ update-bcachefs-sources:
git add include/linux/printbuf.h
cp $(LINUX_DIR)/lib/printbuf.c linux/
git add linux/printbuf.c
cp $(LINUX_DIR)/lib/math/mean_and_variance.c linux/
git add linux/mean_and_variance.c
cp $(LINUX_DIR)/include/linux/mean_and_variance.h include/linux/
git add include/linux/mean_and_variance.h
cp $(LINUX_DIR)/lib/math/int_sqrt.c linux/
git add linux/int_sqrt.c
cp $(LINUX_DIR)/scripts/Makefile.compiler ./
git add Makefile.compiler
$(RM) libbcachefs/*.mod.c
Expand Down
170 changes: 170 additions & 0 deletions include/linux/mean_and_variance.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef MEAN_AND_VARIANCE_H_
#define MEAN_AND_VARIANCE_H_

#include <linux/types.h>
#include <linux/limits.h>
#include <linux/math64.h>
#include <linux/printbuf.h>

#define SQRT_U64_MAX 4294967295ULL


#if defined(CONFIG_ARCH_SUPPORTS_INT128) && defined(__SIZEOF_INT128__)

typedef unsigned __int128 u128;

static inline u128 u64_to_u128(u64 a)
{
return (u128)a;
}

static inline u64 u128_to_u64(u128 a)
{
return (u64)a;
}

static inline u64 u128_shr64_to_u64(u128 a)
{
return (u64)(a >> 64);
}

static inline u128 u128_add(u128 a, u128 b)
{
return a + b;
}

static inline u128 u128_sub(u128 a, u128 b)
{
return a - b;
}

static inline u128 u128_shl(u128 i, s8 shift)
{
return i << shift;
}

static inline u128 u128_shl64_add(u64 a, u64 b)
{
return ((u128)a << 64) + b;
}

static inline u128 u128_square(u64 i)
{
return i*i;
}

#else

typedef struct {
u64 hi, lo;
} u128;

static inline u128 u64_to_u128(u64 a)
{
return (u128){ .lo = a };
}

static inline u64 u128_to_u64(u128 a)
{
return a.lo;
}

static inline u64 u128_shr64_to_u64(u128 a)
{
return a.hi;
}

static inline u128 u128_add(u128 a, u128 b)
{
u128 c;

c.lo = a.lo + b.lo;
c.hi = a.hi + b.hi + (c.lo < a.lo);
return c;
}

static inline u128 u128_sub(u128 a, u128 b)
{
u128 c;

c.lo = a.lo - b.lo;
c.hi = a.hi - b.hi - (c.lo > a.lo);
return c;
}

static inline u128 u128_shl(u128 i, s8 shift)
{
u128 r;

r.lo = i.lo << shift;
if (shift < 64)
r.hi = (i.hi << shift) | (i.lo >> (64 - shift));
else {
r.hi = i.lo << (shift - 64);
r.lo = 0;
}
return r;
}

static inline u128 u128_shl64_add(u64 a, u64 b)
{
return u128_add(u128_shl(u64_to_u128(a), 64), u64_to_u128(b));
}

static inline u128 u128_square(u64 i)
{
u128 r;
u64 h = i >> 32, l = i & (u64)U32_MAX;

r = u128_shl(u64_to_u128(h*h), 64);
r = u128_add(r, u128_shl(u64_to_u128(h*l), 32));
r = u128_add(r, u128_shl(u64_to_u128(l*h), 32));
r = u128_add(r, u64_to_u128(l*l));
return r;
}

#endif

static inline u128 u128_div(u128 n, u64 d)
{
u128 r;
u64 rem;
u64 hi = u128_shr64_to_u64(n);
u64 lo = u128_to_u64(n);
u64 h = hi & ((u64)U32_MAX << 32);
u64 l = (hi & (u64)U32_MAX) << 32;

r = u128_shl(u64_to_u128(div64_u64_rem(h, d, &rem)), 64);
r = u128_add(r, u128_shl(u64_to_u128(div64_u64_rem(l + (rem << 32), d, &rem)), 32));
r = u128_add(r, u64_to_u128(div64_u64_rem(lo + (rem << 32), d, &rem)));
return r;
}

struct mean_and_variance {
s64 n;
s64 sum;
u128 sum_squares;
};

/* expontentially weighted variant */
struct mean_and_variance_weighted {
bool init;
u8 w;
s64 mean;
u64 variance;
};

inline s64 fast_divpow2(s64 n, u8 d);

struct mean_and_variance mean_and_variance_update(struct mean_and_variance s1, s64 v1);
s64 mean_and_variance_get_mean(struct mean_and_variance s);
u64 mean_and_variance_get_variance(struct mean_and_variance s1);
u32 mean_and_variance_get_stddev(struct mean_and_variance s);

struct mean_and_variance_weighted mean_and_variance_weighted_update(struct mean_and_variance_weighted s1, s64 v1);
s64 mean_and_variance_weighted_get_mean(struct mean_and_variance_weighted s);
u64 mean_and_variance_weighted_get_variance(struct mean_and_variance_weighted s);
u32 mean_and_variance_weighted_get_stddev(struct mean_and_variance_weighted s);

#endif // MEAN_AND_VAIRANCE_H_
5 changes: 3 additions & 2 deletions libbcachefs/backpointers.c
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,8 @@ int bch2_bucket_backpointer_add(struct btree_trans *trans,
int bch2_get_next_backpointer(struct btree_trans *trans,
struct bpos bucket, int gen,
u64 *bp_offset,
struct bch_backpointer *dst)
struct bch_backpointer *dst,
unsigned iter_flags)
{
struct bch_fs *c = trans->c;
struct bpos bp_pos, bp_end_pos;
Expand Down Expand Up @@ -1023,7 +1024,7 @@ static int check_one_backpointer(struct btree_trans *trans,
struct printbuf buf = PRINTBUF;
int ret;

ret = bch2_get_next_backpointer(trans, bucket, -1, bp_offset, &bp);
ret = bch2_get_next_backpointer(trans, bucket, -1, bp_offset, &bp, 0);
if (ret || *bp_offset == U64_MAX)
return ret;

Expand Down
2 changes: 1 addition & 1 deletion libbcachefs/backpointers.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ int bch2_bucket_backpointer_del(struct btree_trans *, struct bkey_i_alloc_v4 *,
int bch2_bucket_backpointer_add(struct btree_trans *, struct bkey_i_alloc_v4 *,
struct bch_backpointer, struct bkey_s_c);
int bch2_get_next_backpointer(struct btree_trans *, struct bpos, int,
u64 *, struct bch_backpointer *);
u64 *, struct bch_backpointer *, unsigned);
struct bkey_s_c bch2_backpointer_get_key(struct btree_trans *, struct btree_iter *,
struct bpos, u64, struct bch_backpointer);
struct btree *bch2_backpointer_get_node(struct btree_trans *, struct btree_iter *,
Expand Down
8 changes: 4 additions & 4 deletions libbcachefs/btree_gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1979,10 +1979,10 @@ int bch2_gc_gens(struct bch_fs *c)
NULL, NULL,
BTREE_INSERT_NOFAIL,
gc_btree_gens_key(&trans, &iter, k));
if (ret) {
if (ret && ret != -EROFS)
bch_err(c, "error recalculating oldest_gen: %s", bch2_err_str(ret));
if (ret)
goto err;
}
}

ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_alloc,
Expand All @@ -1992,10 +1992,10 @@ int bch2_gc_gens(struct bch_fs *c)
NULL, NULL,
BTREE_INSERT_NOFAIL,
bch2_alloc_write_oldest_gen(&trans, &iter, k));
if (ret) {
if (ret && ret != -EROFS)
bch_err(c, "error writing oldest_gen: %s", bch2_err_str(ret));
if (ret)
goto err;
}

c->gc_gens_btree = 0;
c->gc_gens_pos = POS_MIN;
Expand Down
12 changes: 7 additions & 5 deletions libbcachefs/btree_iter.c
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ static int btree_path_prefetch(struct btree_trans *trans, struct btree_path *pat

bch2_bkey_buf_init(&tmp);

while (nr && !ret) {
while (nr-- && !ret) {
if (!bch2_btree_node_relock(trans, path, path->level))
break;

Expand Down Expand Up @@ -807,7 +807,7 @@ static int btree_path_prefetch_j(struct btree_trans *trans, struct btree_path *p

bch2_bkey_buf_init(&tmp);

while (nr && !ret) {
while (nr-- && !ret) {
if (!bch2_btree_node_relock(trans, path, path->level))
break;

Expand Down Expand Up @@ -2386,6 +2386,8 @@ struct bkey_s_c bch2_btree_iter_peek_slot(struct btree_iter *iter)
}

k = bch2_btree_path_peek_slot(iter->path, &iter->k);
if (unlikely(!k.k))
goto out_no_locked;
} else {
struct bpos next;

Expand Down Expand Up @@ -2783,7 +2785,7 @@ u32 bch2_trans_begin(struct btree_trans *trans)

if (!trans->restarted &&
(need_resched() ||
ktime_get_ns() - trans->last_begin_time > BTREE_TRANS_MAX_LOCK_HOLD_TIME_NS)) {
local_clock() - trans->last_begin_time > BTREE_TRANS_MAX_LOCK_HOLD_TIME_NS)) {
bch2_trans_unlock(trans);
cond_resched();
bch2_trans_relock(trans);
Expand All @@ -2793,7 +2795,7 @@ u32 bch2_trans_begin(struct btree_trans *trans)
if (trans->restarted)
bch2_btree_path_traverse_all(trans);

trans->last_begin_time = ktime_get_ns();
trans->last_begin_time = local_clock();
return trans->restart_count;
}

Expand Down Expand Up @@ -2850,7 +2852,7 @@ void __bch2_trans_init(struct btree_trans *trans, struct bch_fs *c, const char *
memset(trans, 0, sizeof(*trans));
trans->c = c;
trans->fn = fn;
trans->last_begin_time = ktime_get_ns();
trans->last_begin_time = local_clock();
trans->fn_idx = bch2_trans_get_fn_idx(trans, c, fn);
trans->locking_wait.task = current;
closure_init_stack(&trans->ref);
Expand Down

0 comments on commit e0a51cc

Please sign in to comment.