]>
git.proxmox.com Git - ceph.git/blob - ceph/src/common/Finisher.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
15 #ifndef CEPH_FINISHER_H
16 #define CEPH_FINISHER_H
18 #include "include/atomic.h"
19 #include "common/Mutex.h"
20 #include "common/Cond.h"
21 #include "common/Thread.h"
22 #include "common/perf_counters.h"
26 /// Finisher queue length performance counter ID.
28 l_finisher_first
= 997082,
30 l_finisher_complete_lat
,
34 /** @brief Asynchronous cleanup class.
35 * Finisher asynchronously completes Contexts, which are simple classes
36 * representing callbacks, in a dedicated worker thread. Enqueuing
37 * contexts to complete is thread-safe.
41 Mutex finisher_lock
; ///< Protects access to queues and finisher_running.
42 Cond finisher_cond
; ///< Signaled when there is something to process.
43 Cond finisher_empty_cond
; ///< Signaled when the finisher has nothing more to process.
44 bool finisher_stop
; ///< Set when the finisher should stop.
45 bool finisher_running
; ///< True when the finisher is currently executing contexts.
46 bool finisher_empty_wait
; ///< True mean someone wait finisher empty.
47 /// Queue for contexts for which complete(0) will be called.
48 /// NULLs in this queue indicate that an item from finisher_queue_rval
49 /// should be completed in that place instead.
50 vector
<Context
*> finisher_queue
;
54 /// Queue for contexts for which the complete function will be called
55 /// with a parameter other than 0.
56 list
<pair
<Context
*,int> > finisher_queue_rval
;
58 /// Performance counter for the finisher's queue length.
59 /// Only active for named finishers.
62 void *finisher_thread_entry();
64 struct FinisherThread
: public Thread
{
66 explicit FinisherThread(Finisher
*f
) : fin(f
) {}
67 void* entry() override
{ return (void*)fin
->finisher_thread_entry(); }
71 /// Add a context to complete, optionally specifying a parameter for the complete function.
72 void queue(Context
*c
, int r
= 0) {
74 if (finisher_queue
.empty()) {
75 finisher_cond
.Signal();
78 finisher_queue_rval
.push_back(pair
<Context
*, int>(c
, r
));
79 finisher_queue
.push_back(NULL
);
81 finisher_queue
.push_back(c
);
83 logger
->inc(l_finisher_queue_len
);
84 finisher_lock
.Unlock();
86 void queue(vector
<Context
*>& ls
) {
88 if (finisher_queue
.empty()) {
89 finisher_cond
.Signal();
91 finisher_queue
.insert(finisher_queue
.end(), ls
.begin(), ls
.end());
93 logger
->inc(l_finisher_queue_len
, ls
.size());
94 finisher_lock
.Unlock();
97 void queue(deque
<Context
*>& ls
) {
99 if (finisher_queue
.empty()) {
100 finisher_cond
.Signal();
102 finisher_queue
.insert(finisher_queue
.end(), ls
.begin(), ls
.end());
104 logger
->inc(l_finisher_queue_len
, ls
.size());
105 finisher_lock
.Unlock();
108 void queue(list
<Context
*>& ls
) {
109 finisher_lock
.Lock();
110 if (finisher_queue
.empty()) {
111 finisher_cond
.Signal();
113 finisher_queue
.insert(finisher_queue
.end(), ls
.begin(), ls
.end());
115 logger
->inc(l_finisher_queue_len
, ls
.size());
116 finisher_lock
.Unlock();
120 /// Start the worker thread.
123 /** @brief Stop the worker thread.
125 * Does not wait until all outstanding contexts are completed.
126 * To ensure that everything finishes, you should first shut down
127 * all sources that can add contexts to this finisher and call
128 * wait_for_empty() before calling stop(). */
131 /** @brief Blocks until the finisher has nothing left to process.
132 * This function will also return when a concurrent call to stop()
133 * finishes, but this class should never be used in this way. */
134 void wait_for_empty();
136 /// Construct an anonymous Finisher.
137 /// Anonymous finishers do not log their queue length.
138 explicit Finisher(CephContext
*cct_
) :
139 cct(cct_
), finisher_lock("Finisher::finisher_lock"),
140 finisher_stop(false), finisher_running(false), finisher_empty_wait(false),
141 thread_name("fn_anonymous"), logger(0),
142 finisher_thread(this) {}
144 /// Construct a named Finisher that logs its queue length.
145 Finisher(CephContext
*cct_
, string name
, string tn
) :
146 cct(cct_
), finisher_lock("Finisher::" + name
),
147 finisher_stop(false), finisher_running(false), finisher_empty_wait(false),
148 thread_name(tn
), logger(0),
149 finisher_thread(this) {
150 PerfCountersBuilder
b(cct
, string("finisher-") + name
,
151 l_finisher_first
, l_finisher_last
);
152 b
.add_u64(l_finisher_queue_len
, "queue_len");
153 b
.add_time_avg(l_finisher_complete_lat
, "complete_latency");
154 logger
= b
.create_perf_counters();
155 cct
->get_perfcounters_collection()->add(logger
);
156 logger
->set(l_finisher_queue_len
, 0);
157 logger
->set(l_finisher_complete_lat
, 0);
162 cct
->get_perfcounters_collection()->remove(logger
);
168 /// Context that is completed asynchronously on the supplied finisher.
169 class C_OnFinisher
: public Context
{
173 C_OnFinisher(Context
*c
, Finisher
*f
) : con(c
), fin(f
) {
178 ~C_OnFinisher() override
{
179 if (con
!= nullptr) {
185 void finish(int r
) override
{