Skip to content

Commit 111c94e

Browse files
committed
tool/ceph_dedup: refactor globals to put them into automatic variable
Signed-off-by: Myoungwon Oh <[email protected]>
1 parent 540c4dc commit 111c94e

File tree

1 file changed

+31
-27
lines changed

1 file changed

+31
-27
lines changed

src/tools/ceph_dedup/ceph_dedup_daemon.cc

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
#define dout_prefix *_dout << "ceph_dedup_daemon: " \
55
<< __func__ << ": "
66

7-
ceph::shared_mutex glock = ceph::make_shared_mutex("glock");
8-
class SampleDedupWorkerThread;
9-
bool all_stop = false; // Accessed in the main thread and in other worker threads under glock
10-
117
po::options_description make_usage() {
128
po::options_description desc("Usage");
139
desc.add_options()
@@ -235,15 +231,29 @@ class SampleDedupWorkerThread : public Thread
235231
};
236232

237233
struct SampleDedupGlobal {
238-
FpStore fp_store;
239-
const double sampling_ratio = -1;
234+
public:
240235
SampleDedupGlobal(
241236
size_t chunk_threshold,
242237
int sampling_ratio,
243238
uint32_t report_period,
244239
size_t fpstore_threshold) :
245240
fp_store(chunk_threshold, report_period, fpstore_threshold),
246241
sampling_ratio(static_cast<double>(sampling_ratio) / 100) { }
242+
243+
bool is_all_stop() {
244+
std::shared_lock l{glock};
245+
return all_stop;
246+
}
247+
void set_all_stop() {
248+
std::unique_lock l{glock};
249+
all_stop = true;
250+
}
251+
friend class SampleDedupWorkerThread;
252+
private:
253+
FpStore fp_store;
254+
const double sampling_ratio = -1;
255+
ceph::shared_mutex glock = ceph::make_shared_mutex("glock");
256+
bool all_stop = false; // Accessed in the main thread and in other worker threads under glock
247257
};
248258

249259
SampleDedupWorkerThread(
@@ -317,9 +327,7 @@ class SampleDedupWorkerThread : public Thread
317327
void SampleDedupWorkerThread::crawl()
318328
{
319329
ObjectCursor current_object = begin;
320-
std::shared_lock l{glock};
321-
while (!all_stop && current_object < end) {
322-
l.unlock();
330+
while (!sample_dedup_global.is_all_stop() && current_object < end) {
323331
std::vector<ObjectItem> objects;
324332
// Get the list of object IDs to deduplicate
325333
std::tie(objects, current_object) = get_objects(current_object, end, 100);
@@ -347,16 +355,12 @@ void SampleDedupWorkerThread::crawl()
347355
} else {
348356
try_dedup_and_accumulate_result(target);
349357
}
350-
l.lock();
351-
if (all_stop) {
358+
if (sample_dedup_global.is_all_stop()) {
352359
oid_for_evict.clear();
353360
break;
354361
}
355-
l.unlock();
356362
}
357-
l.lock();
358363
}
359-
l.unlock();
360364

361365
vector<AioCompRef> evict_completions(oid_for_evict.size());
362366
int i = 0;
@@ -576,6 +580,8 @@ int SampleDedupWorkerThread::do_chunk_dedup(chunk_t &chunk, snap_t snap)
576580
return ret;
577581
}
578582

583+
unique_ptr<SampleDedupWorkerThread::SampleDedupGlobal> state;
584+
579585
int make_crawling_daemon(const po::variables_map &opts)
580586
{
581587
string base_pool_name = get_opts_pool_name(opts);
@@ -662,16 +668,14 @@ int make_crawling_daemon(const po::variables_map &opts)
662668
<< ")"
663669
<< dendl;
664670

665-
std::shared_lock l(glock);
671+
state = std::make_unique<SampleDedupWorkerThread::SampleDedupGlobal>(
672+
chunk_dedup_threshold, sampling_ratio, report_period, fp_threshold);
673+
ret = 0;
666674

667-
while (!all_stop) {
668-
l.unlock();
675+
while (!state->is_all_stop()) {
669676
ObjectCursor begin = io_ctx.object_list_begin();
670677
ObjectCursor end = io_ctx.object_list_end();
671678

672-
SampleDedupWorkerThread::SampleDedupGlobal sample_dedup_global(
673-
chunk_dedup_threshold, sampling_ratio, report_period, fp_threshold);
674-
675679
std::list<SampleDedupWorkerThread> threads;
676680
size_t total_size = 0;
677681
size_t total_duplicate_size = 0;
@@ -695,7 +699,7 @@ int make_crawling_daemon(const po::variables_map &opts)
695699
chunk_size,
696700
fp_algo,
697701
chunk_algo,
698-
sample_dedup_global,
702+
*state,
699703
snap);
700704
threads.back().create("sample_dedup");
701705
}
@@ -724,25 +728,25 @@ int make_crawling_daemon(const po::variables_map &opts)
724728
return -EINVAL;
725729
}
726730

727-
l.lock();
728731
if (run_once) {
729-
all_stop = true;
732+
assert(state);
733+
state->set_all_stop();
730734
break;
731735
}
732736
}
733-
l.unlock();
734737

735738
dout(0) << "done" << dendl;
736-
return 0;
739+
return ret;
737740
}
738741

739742
static void handle_signal(int signum)
740743
{
741-
std::unique_lock l{glock};
742744
switch (signum) {
743745
case SIGINT:
744746
case SIGTERM:
745-
all_stop = true;
747+
if (state) {
748+
state->set_all_stop();
749+
}
746750
dout(0) << "got a signal(" << signum << "), daemon wil be terminiated" << dendl;
747751
break;
748752

0 commit comments

Comments
 (0)