Coverage Report

Created: 2026-04-10 07:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/rocksdb/monitoring/instrumented_mutex.cc
Line
Count
Source
1
//  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2
//  This source code is licensed under both the GPLv2 (found in the
3
//  COPYING file in the root directory) and Apache 2.0 License
4
//  (found in the LICENSE.Apache file in the root directory).
5
6
#include "monitoring/instrumented_mutex.h"
7
8
#include "monitoring/perf_context_imp.h"
9
#include "monitoring/thread_status_util.h"
10
#include "rocksdb/system_clock.h"
11
#include "test_util/sync_point.h"
12
13
namespace ROCKSDB_NAMESPACE {
14
namespace {
15
#ifndef NPERF_CONTEXT
16
3.46M
Statistics* stats_for_report(SystemClock* clock, Statistics* stats) {
17
3.46M
  if (clock != nullptr && stats != nullptr &&
18
0
      stats->get_stats_level() > kExceptTimeForMutex) {
19
0
    return stats;
20
3.46M
  } else {
21
3.46M
    return nullptr;
22
3.46M
  }
23
3.46M
}
24
#endif  // NPERF_CONTEXT
25
}  // namespace
26
27
3.41M
void InstrumentedMutex::Lock() {
28
3.41M
  PERF_CONDITIONAL_TIMER_FOR_MUTEX_GUARD(
29
3.41M
      db_mutex_lock_nanos, stats_code_ == DB_MUTEX_WAIT_MICROS,
30
3.41M
      stats_for_report(clock_, stats_), stats_code_);
31
3.41M
  LockInternal();
32
3.41M
}
33
34
3.41M
void InstrumentedMutex::LockInternal() {
35
#ifndef NDEBUG
36
  ThreadStatusUtil::TEST_StateDelay(ThreadStatus::STATE_MUTEX_WAIT);
37
#endif
38
#ifdef COERCE_CONTEXT_SWITCH
39
  if (stats_code_ == DB_MUTEX_WAIT_MICROS) {
40
    thread_local Random rnd(301);
41
    if (rnd.OneIn(2)) {
42
      if (bg_cv_) {
43
        bg_cv_->SignalAll();
44
      }
45
      sched_yield();
46
    } else {
47
      uint32_t sleep_us = rnd.Uniform(11) * 1000;
48
      if (bg_cv_) {
49
        bg_cv_->SignalAll();
50
      }
51
      SystemClock::Default()->SleepForMicroseconds(sleep_us);
52
    }
53
  }
54
#endif
55
3.41M
  mutex_.Lock();
56
3.41M
}
57
58
8.31k
void InstrumentedCondVar::Wait() {
59
8.31k
  PERF_CONDITIONAL_TIMER_FOR_MUTEX_GUARD(
60
8.31k
      db_condition_wait_nanos, stats_code_ == DB_MUTEX_WAIT_MICROS,
61
8.31k
      stats_for_report(clock_, stats_), stats_code_);
62
8.31k
  WaitInternal();
63
8.31k
}
64
65
8.31k
void InstrumentedCondVar::WaitInternal() {
66
#ifndef NDEBUG
67
  ThreadStatusUtil::TEST_StateDelay(ThreadStatus::STATE_MUTEX_WAIT);
68
#endif
69
8.31k
  cond_.Wait();
70
8.31k
}
71
72
35.6k
bool InstrumentedCondVar::TimedWait(uint64_t abs_time_us) {
73
35.6k
  PERF_CONDITIONAL_TIMER_FOR_MUTEX_GUARD(
74
35.6k
      db_condition_wait_nanos, stats_code_ == DB_MUTEX_WAIT_MICROS,
75
35.6k
      stats_for_report(clock_, stats_), stats_code_);
76
35.6k
  return TimedWaitInternal(abs_time_us);
77
35.6k
}
78
79
35.6k
bool InstrumentedCondVar::TimedWaitInternal(uint64_t abs_time_us) {
80
#ifndef NDEBUG
81
  ThreadStatusUtil::TEST_StateDelay(ThreadStatus::STATE_MUTEX_WAIT);
82
#endif
83
84
35.6k
  TEST_SYNC_POINT_CALLBACK("InstrumentedCondVar::TimedWaitInternal",
85
35.6k
                           &abs_time_us);
86
87
35.6k
  return cond_.TimedWait(abs_time_us);
88
35.6k
}
89
90
}  // namespace ROCKSDB_NAMESPACE