Skip to content

Commit 6c5106d

Browse files
committed
crimson/common/tri_mutex: add debug logs
to be used only for testing Signed-off-by: Matan Breizman <mbreizma@redhat.com>
1 parent 583cb17 commit 6c5106d

File tree

3 files changed

+74
-4
lines changed

3 files changed

+74
-4
lines changed

src/crimson/common/tri_mutex.cc

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
#include <seastar/util/later.hh>
77

8+
SET_SUBSYS(osd);
9+
//TODO: SET_SUBSYS(crimson_tri_mutex);
10+
811
seastar::future<> read_lock::lock()
912
{
1013
return static_cast<tri_mutex*>(this)->lock_for_read();
@@ -57,20 +60,28 @@ void excl_lock_from_write::unlock()
5760

5861
tri_mutex::~tri_mutex()
5962
{
63+
LOG_PREFIX(tri_mutex::~tri_mutex());
64+
DEBUGDPP("", *this);
6065
assert(!is_acquired());
6166
}
6267

6368
seastar::future<> tri_mutex::lock_for_read()
6469
{
70+
LOG_PREFIX(tri_mutex::lock_for_read());
71+
DEBUGDPP("", *this);
6572
if (try_lock_for_read()) {
73+
DEBUGDPP("lock_for_read successfully", *this);
6674
return seastar::now();
6775
}
76+
DEBUGDPP("can't lock_for_read, adding to waiters", *this);
6877
waiters.emplace_back(seastar::promise<>(), type_t::read);
6978
return waiters.back().pr.get_future();
7079
}
7180

7281
bool tri_mutex::try_lock_for_read() noexcept
7382
{
83+
LOG_PREFIX(tri_mutex::try_lock_for_read());
84+
DEBUGDPP("", *this);
7485
if (!writers && !exclusively_used && waiters.empty()) {
7586
++readers;
7687
return true;
@@ -81,6 +92,8 @@ bool tri_mutex::try_lock_for_read() noexcept
8192

8293
void tri_mutex::unlock_for_read()
8394
{
95+
LOG_PREFIX(tri_mutex::unlock_for_read());
96+
DEBUGDPP("", *this);
8497
assert(readers > 0);
8598
if (--readers == 0) {
8699
wake();
@@ -89,29 +102,39 @@ void tri_mutex::unlock_for_read()
89102

90103
void tri_mutex::promote_from_read()
91104
{
105+
LOG_PREFIX(tri_mutex::promote_from_read());
106+
DEBUGDPP("", *this);
92107
assert(readers == 1);
93108
--readers;
94109
exclusively_used = true;
95110
}
96111

97112
void tri_mutex::demote_to_read()
98113
{
114+
LOG_PREFIX(tri_mutex::demote_to_read());
115+
DEBUGDPP("", *this);
99116
assert(exclusively_used);
100117
exclusively_used = false;
101118
++readers;
102119
}
103120

104121
seastar::future<> tri_mutex::lock_for_write()
105122
{
123+
LOG_PREFIX(tri_mutex::lock_for_write());
124+
DEBUGDPP("", *this);
106125
if (try_lock_for_write()) {
126+
DEBUGDPP("lock_for_write successfully", *this);
107127
return seastar::now();
108128
}
129+
DEBUGDPP("can't lock_for_write, adding to waiters", *this);
109130
waiters.emplace_back(seastar::promise<>(), type_t::write);
110131
return waiters.back().pr.get_future();
111132
}
112133

113134
bool tri_mutex::try_lock_for_write() noexcept
114135
{
136+
LOG_PREFIX(tri_mutex::try_lock_for_write());
137+
DEBUGDPP("", *this);
115138
if (!readers && !exclusively_used) {
116139
if (waiters.empty()) {
117140
++writers;
@@ -123,6 +146,8 @@ bool tri_mutex::try_lock_for_write() noexcept
123146

124147
void tri_mutex::unlock_for_write()
125148
{
149+
LOG_PREFIX(tri_mutex::unlock_for_write());
150+
DEBUGDPP("", *this);
126151
assert(writers > 0);
127152
if (--writers == 0) {
128153
wake();
@@ -131,13 +156,17 @@ void tri_mutex::unlock_for_write()
131156

132157
void tri_mutex::promote_from_write()
133158
{
159+
LOG_PREFIX(tri_mutex::promote_from_write());
160+
DEBUGDPP("", *this);
134161
assert(writers == 1);
135162
--writers;
136163
exclusively_used = true;
137164
}
138165

139166
void tri_mutex::demote_to_write()
140167
{
168+
LOG_PREFIX(tri_mutex::demote_to_write());
169+
DEBUGDPP("", *this);
141170
assert(exclusively_used);
142171
exclusively_used = false;
143172
++writers;
@@ -146,15 +175,21 @@ void tri_mutex::demote_to_write()
146175
// for exclusive users
147176
seastar::future<> tri_mutex::lock_for_excl()
148177
{
178+
LOG_PREFIX(tri_mutex::lock_for_excl());
179+
DEBUGDPP("", *this);
149180
if (try_lock_for_excl()) {
181+
DEBUGDPP("lock_for_excl, successfully", *this);
150182
return seastar::now();
151183
}
184+
DEBUGDPP("can't lock_for_excl, adding to waiters", *this);
152185
waiters.emplace_back(seastar::promise<>(), type_t::exclusive);
153186
return waiters.back().pr.get_future();
154187
}
155188

156189
bool tri_mutex::try_lock_for_excl() noexcept
157190
{
191+
LOG_PREFIX(tri_mutex::try_lock_for_excl());
192+
DEBUGDPP("", *this);
158193
if (readers == 0u && writers == 0u && !exclusively_used) {
159194
exclusively_used = true;
160195
return true;
@@ -165,13 +200,17 @@ bool tri_mutex::try_lock_for_excl() noexcept
165200

166201
void tri_mutex::unlock_for_excl()
167202
{
203+
LOG_PREFIX(tri_mutex::unlock_for_excl());
204+
DEBUGDPP("", *this);
168205
assert(exclusively_used);
169206
exclusively_used = false;
170207
wake();
171208
}
172209

173210
bool tri_mutex::is_acquired() const
174211
{
212+
LOG_PREFIX(tri_mutex::is_acquired());
213+
DEBUGDPP("", *this);
175214
if (readers != 0u) {
176215
return true;
177216
} else if (writers != 0u) {
@@ -185,6 +224,8 @@ bool tri_mutex::is_acquired() const
185224

186225
void tri_mutex::wake()
187226
{
227+
LOG_PREFIX(tri_mutex::wake());
228+
DEBUGDPP("", *this);
188229
assert(!readers && !writers && !exclusively_used);
189230
type_t type = type_t::none;
190231
while (!waiters.empty()) {
@@ -210,7 +251,9 @@ void tri_mutex::wake()
210251
default:
211252
assert(0);
212253
}
254+
// TODO: DEBUGDPP("waking up {} ", *this);
213255
waiter.pr.set_value();
214256
waiters.pop_front();
215257
}
258+
DEBUGDPP("no waiters", *this);
216259
}

src/crimson/common/tri_mutex.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <seastar/core/future.hh>
77
#include <seastar/core/circular_buffer.hh>
8+
#include "crimson/common/log.h"
89

910
class read_lock {
1011
public:
@@ -62,6 +63,11 @@ class tri_mutex : private read_lock,
6263
{
6364
public:
6465
tri_mutex() = default;
66+
#ifdef NDEBUG
67+
tri_mutex(const std::string obj_name) : name() {}
68+
#else
69+
tri_mutex(const std::string obj_name) : name(obj_name) {}
70+
#endif
6571
~tri_mutex();
6672

6773
read_lock& for_read() {
@@ -120,6 +126,10 @@ class tri_mutex : private read_lock,
120126
}
121127
}
122128

129+
std::string_view get_name() const{
130+
return name;
131+
}
132+
123133
private:
124134
void wake();
125135
unsigned readers = 0;
@@ -139,10 +149,25 @@ class tri_mutex : private read_lock,
139149
type_t type;
140150
};
141151
seastar::circular_buffer<waiter_t> waiters;
152+
const std::string name;
142153
friend class read_lock;
143154
friend class write_lock;
144155
friend class excl_lock;
145156
friend class excl_lock_from_read;
146157
friend class excl_lock_from_write;
147158
friend class excl_lock_from_excl;
159+
friend std::ostream& operator<<(std::ostream &lhs, const tri_mutex &rhs);
148160
};
161+
162+
inline std::ostream& operator<<(std::ostream& os, const tri_mutex& tm)
163+
{
164+
os << fmt::format("tri_mutex {} writers {} readers {}"
165+
" exclusively_used {} waiters: {}",
166+
tm.get_name(), tm.get_writers(), tm.get_readers(),
167+
tm.exclusively_used, tm.waiters.size());
168+
return os;
169+
}
170+
171+
#if FMT_VERSION >= 90000
172+
template <> struct fmt::formatter<tri_mutex> : fmt::ostream_formatter {};
173+
#endif

src/crimson/osd/object_context.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ class ObjectContext : public ceph::common::intrusive_lru_base<
6161
ceph::common::intrusive_lru_config<
6262
hobject_t, ObjectContext, obc_to_hoid<ObjectContext>>>
6363
{
64+
private:
65+
tri_mutex lock;
66+
bool recovery_read_marker = false;
67+
6468
public:
6569
ObjectState obs;
6670
SnapSetContextRef ssc;
@@ -74,7 +78,8 @@ class ObjectContext : public ceph::common::intrusive_lru_base<
7478
// make other users of this obc to await for the loading to complete.
7579
seastar::shared_mutex loading_mutex;
7680

77-
ObjectContext(hobject_t hoid) : obs(std::move(hoid)) {}
81+
ObjectContext(hobject_t hoid) : lock(hoid.oid.name),
82+
obs(std::move(hoid)) {}
7883

7984
const hobject_t &get_oid() const {
8085
return obs.oi.soid;
@@ -130,9 +135,6 @@ class ObjectContext : public ceph::common::intrusive_lru_base<
130135
}
131136

132137
private:
133-
tri_mutex lock;
134-
bool recovery_read_marker = false;
135-
136138
template <typename Lock, typename Func>
137139
auto _with_lock(Lock& lock, Func&& func) {
138140
Ref obc = this;

0 commit comments

Comments
 (0)