bcachefs: Don't call local_clock() twice in trans_begin()
authorKent Overstreet <kent.overstreet@linux.dev>
Fri, 16 Jun 2023 22:55:07 +0000 (18:55 -0400)
committerKent Overstreet <kent.overstreet@linux.dev>
Sun, 22 Oct 2023 21:10:01 +0000 (17:10 -0400)
local_clock() is not as cheap as we'd like it to be, alas

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/btree_iter.c

index 0a62f55a3aa8a13e1062ccac256eb74ff5438ee4..d906bfb6754debc3387a08951a4d331d812d7986 100644 (file)
@@ -2850,6 +2850,7 @@ static noinline void bch2_trans_reset_srcu_lock(struct btree_trans *trans)
 u32 bch2_trans_begin(struct btree_trans *trans)
 {
        struct btree_path *path;
+       u64 now;
 
        bch2_trans_reset_updates(trans);
 
@@ -2878,13 +2879,16 @@ u32 bch2_trans_begin(struct btree_trans *trans)
                        path->preserve = false;
        }
 
+       now = local_clock();
        if (!trans->restarted &&
            (need_resched() ||
-            local_clock() - trans->last_begin_time > BTREE_TRANS_MAX_LOCK_HOLD_TIME_NS)) {
+            now - trans->last_begin_time > BTREE_TRANS_MAX_LOCK_HOLD_TIME_NS)) {
                bch2_trans_unlock(trans);
                cond_resched();
                bch2_trans_relock(trans);
+               now = local_clock();
        }
+       trans->last_begin_time = now;
 
        if (unlikely(time_after(jiffies, trans->srcu_lock_time + msecs_to_jiffies(10))))
                bch2_trans_reset_srcu_lock(trans);
@@ -2895,7 +2899,6 @@ u32 bch2_trans_begin(struct btree_trans *trans)
                trans->notrace_relock_fail = false;
        }
 
-       trans->last_begin_time = local_clock();
        return trans->restart_count;
 }