Skip to content

Commit 617c936

Browse files
committed
blk/KernelDevice: Unify discard thread management
Instead of having _discard_start() and _discard_stop() partially or completely duplicate functionality in handle_conf_change(), have a single _discard_update_threads() that can handle all three. Loops are tidied slightly, the unnecessary target_discard_threads class variable has been removed, and now handle_conf_change() will respect support_discard. Signed-off-by: Joshua Baergen <[email protected]>
1 parent 3d4a899 commit 617c936

File tree

2 files changed

+36
-70
lines changed

2 files changed

+36
-70
lines changed

src/blk/kernel/KernelDevice.cc

Lines changed: 34 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ int KernelDevice::open(const string& p)
134134
{
135135
path = p;
136136
int r = 0, i = 0;
137-
uint64_t num_discard_threads = 0;
138137
dout(1) << __func__ << " path " << path << dendl;
139138

140139
struct stat statbuf;
@@ -286,10 +285,7 @@ int KernelDevice::open(const string& p)
286285
goto out_fail;
287286
}
288287

289-
num_discard_threads = cct->_conf.get_val<uint64_t>("bdev_async_discard_threads");
290-
if (support_discard && cct->_conf->bdev_enable_discard && num_discard_threads > 0) {
291-
_discard_start();
292-
}
288+
_discard_update_threads();
293289

294290
// round size down to an even block
295291
size &= ~(block_size - 1);
@@ -536,42 +532,48 @@ void KernelDevice::_aio_stop()
536532
}
537533
}
538534

539-
void KernelDevice::_discard_start()
535+
void KernelDevice::_discard_update_threads()
540536
{
541-
uint64_t num = cct->_conf.get_val<uint64_t>("bdev_async_discard_threads");
542-
dout(10) << __func__ << " starting " << num << " threads" << dendl;
543-
544537
std::unique_lock l(discard_lock);
545538

546-
target_discard_threads = num;
547-
discard_threads.reserve(num);
548-
for(uint64_t i = 0; i < num; i++)
549-
{
550-
// All threads created with the same name
551-
discard_threads.emplace_back(new DiscardThread(this, i));
552-
discard_threads.back()->create("bstore_discard");
553-
}
539+
uint64_t oldcount = discard_threads.size();
540+
uint64_t newcount = cct->_conf.get_val<uint64_t>("bdev_async_discard_threads");
541+
if (!cct->_conf.get_val<bool>("bdev_enable_discard") || !support_discard || discard_stop) {
542+
newcount = 0;
543+
}
544+
545+
// Increase? Spawn now, it's quick
546+
if (newcount > oldcount) {
547+
dout(10) << __func__ << " starting " << (newcount - oldcount) << " additional discard threads" << dendl;
548+
discard_threads.reserve(newcount);
549+
for(uint64_t i = oldcount; i < newcount; i++)
550+
{
551+
// All threads created with the same name
552+
discard_threads.emplace_back(new DiscardThread(this, i));
553+
discard_threads.back()->create("bstore_discard");
554+
}
555+
// Decrease? Signal threads after telling them to stop
556+
} else if (newcount < oldcount) {
557+
dout(10) << __func__ << " stopping " << (oldcount - newcount) << " existing discard threads" << dendl;
558+
559+
// Signal the last threads to quit, and stop tracking them
560+
for(uint64_t i = oldcount; i > newcount; i--)
561+
{
562+
discard_threads[i-1]->stop = true;
563+
discard_threads[i-1]->detach();
564+
}
565+
discard_threads.resize(newcount);
554566

555-
dout(10) << __func__ << " started " << num << " threads" << dendl;
567+
discard_cond.notify_all();
568+
}
556569
}
557570

558571
void KernelDevice::_discard_stop()
559572
{
560573
dout(10) << __func__ << dendl;
561574

562-
// Signal threads to stop, then wait for them to join
563-
{
564-
std::unique_lock l(discard_lock);
565-
566-
for(auto &t : discard_threads) {
567-
t->stop = true;
568-
t->detach();
569-
}
570-
discard_threads.clear();
571-
572-
discard_cond.notify_all();
573-
}
574-
575+
discard_stop = true;
576+
_discard_update_threads();
575577
discard_drain();
576578

577579
dout(10) << __func__ << " stopped" << dendl;
@@ -1524,42 +1526,6 @@ void KernelDevice::handle_conf_change(const ConfigProxy& conf,
15241526
const std::set <std::string> &changed)
15251527
{
15261528
if (changed.count("bdev_async_discard_threads") || changed.count("bdev_enable_discard")) {
1527-
std::unique_lock l(discard_lock);
1528-
1529-
uint64_t oldval = target_discard_threads;
1530-
uint64_t newval = cct->_conf.get_val<uint64_t>("bdev_async_discard_threads");
1531-
if (!cct->_conf.get_val<bool>("bdev_enable_discard")) {
1532-
// We don't want these threads running if discard has been disabled (this is consistent with
1533-
// KernelDevice::open())
1534-
newval = 0;
1535-
}
1536-
1537-
target_discard_threads = newval;
1538-
1539-
// Increase? Spawn now, it's quick
1540-
if (newval > oldval) {
1541-
dout(10) << __func__ << " starting " << (newval - oldval) << " additional discard threads" << dendl;
1542-
discard_threads.reserve(target_discard_threads);
1543-
for(uint64_t i = oldval; i < newval; i++)
1544-
{
1545-
// All threads created with the same name
1546-
discard_threads.emplace_back(new DiscardThread(this, i));
1547-
discard_threads.back()->create("bstore_discard");
1548-
}
1549-
// Decrease? Signal threads after telling them to stop
1550-
} else if (newval < oldval) {
1551-
dout(10) << __func__ << " stopping " << (oldval - newval) << " existing discard threads" << dendl;
1552-
1553-
// Signal the last threads to quit, and stop tracking them
1554-
for(uint64_t i = oldval - 1; i >= newval && i != UINT64_MAX; i--)
1555-
{
1556-
// Also detach the thread so we no longer need to join
1557-
discard_threads[i]->stop = true;
1558-
discard_threads[i]->detach();
1559-
discard_threads.erase(discard_threads.begin() + i);
1560-
}
1561-
1562-
discard_cond.notify_all();
1563-
}
1529+
_discard_update_threads();
15641530
}
15651531
}

src/blk/kernel/KernelDevice.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class KernelDevice : public BlockDevice,
5252
aio_callback_t discard_callback;
5353
void *discard_callback_priv;
5454
bool aio_stop;
55+
bool discard_stop;
5556

5657
ceph::mutex discard_lock = ceph::make_mutex("KernelDevice::discard_lock");
5758
ceph::condition_variable discard_cond;
@@ -78,7 +79,6 @@ class KernelDevice : public BlockDevice,
7879
}
7980
};
8081
std::vector<std::shared_ptr<DiscardThread>> discard_threads;
81-
uint64_t target_discard_threads = 0;
8282

8383
std::atomic_int injecting_crash;
8484

@@ -93,7 +93,7 @@ class KernelDevice : public BlockDevice,
9393
int _aio_start();
9494
void _aio_stop();
9595

96-
void _discard_start();
96+
void _discard_update_threads();
9797
void _discard_stop();
9898
bool _discard_started();
9999

0 commit comments

Comments
 (0)