xref: /freebsd/cddl/usr.sbin/zfsd/callout.cc (revision 069ac184)
1 /*-
2  * Copyright (c) 2011, 2012, 2013 Spectra Logic Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions, and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    substantially similar to the "NO WARRANTY" disclaimer below
13  *    ("Disclaimer") and any redistribution must be conditioned upon
14  *    including a substantially similar Disclaimer requirement for further
15  *    binary redistribution.
16  *
17  * NO WARRANTY
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGES.
29  *
30  * Authors: Justin T. Gibbs     (Spectra Logic Corporation)
31  */
32 
33 /**
34  * \file callout.cc
35  *
36  * \brief Implementation of the Callout class - multi-client
37  *        timer services built on top of the POSIX interval timer.
38  */
39 
40 #include <sys/byteorder.h>
41 #include <sys/time.h>
42 
43 #include <signal.h>
44 #include <syslog.h>
45 
46 #include <climits>
47 #include <list>
48 #include <map>
49 #include <string>
50 
51 #include <devdctl/guid.h>
52 #include <devdctl/event.h>
53 #include <devdctl/event_factory.h>
54 #include <devdctl/consumer.h>
55 #include <devdctl/exception.h>
56 
57 #include "callout.h"
58 #include "vdev_iterator.h"
59 #include "zfsd.h"
60 #include "zfsd_exception.h"
61 
62 std::list<Callout *> Callout::s_activeCallouts;
63 bool		     Callout::s_alarmFired(false);
64 
65 void
66 Callout::Init()
67 {
68 	signal(SIGALRM,  Callout::AlarmSignalHandler);
69 }
70 
71 bool
72 Callout::Stop()
73 {
74 	if (!IsPending())
75 		return (false);
76 
77 	for (std::list<Callout *>::iterator it(s_activeCallouts.begin());
78 	     it != s_activeCallouts.end(); it++) {
79 		if (*it != this)
80 			continue;
81 
82 		it = s_activeCallouts.erase(it);
83 		if (it != s_activeCallouts.end()) {
84 
85 			/*
86 			 * Maintain correct interval for the
87 			 * callouts that follow the just removed
88 			 * entry.
89 			 */
90 			timeradd(&(*it)->m_interval, &m_interval,
91 				 &(*it)->m_interval);
92 		}
93 		break;
94 	}
95 	m_pending = false;
96 	return (true);
97 }
98 
99 bool
100 Callout::Reset(const timeval &interval, CalloutFunc_t *func, void *arg)
101 {
102 	bool cancelled(false);
103 
104 	if (!timerisset(&interval))
105 		throw ZfsdException("Callout::Reset: interval of 0");
106 
107 	cancelled = Stop();
108 
109 	m_interval = interval;
110 	m_func     = func;
111 	m_arg      = arg;
112 	m_pending  = true;
113 
114 	std::list<Callout *>::iterator it(s_activeCallouts.begin());
115 	for (; it != s_activeCallouts.end(); it++) {
116 
117 		if (timercmp(&(*it)->m_interval, &m_interval, <=)) {
118 			/*
119 			 * Decrease our interval by those that come
120 			 * before us.
121 			 */
122 			timersub(&m_interval, &(*it)->m_interval, &m_interval);
123 		} else {
124 			/*
125 			 * Account for the time between the newly
126 			 * inserted event and those that follow.
127 			 */
128 			timersub(&(*it)->m_interval, &m_interval,
129 				 &(*it)->m_interval);
130 			break;
131 		}
132 	}
133 	s_activeCallouts.insert(it, this);
134 
135 
136 	if (s_activeCallouts.front() == this) {
137 		itimerval timerval = { {0, 0}, m_interval };
138 
139 		setitimer(ITIMER_REAL, &timerval, NULL);
140 	}
141 
142 	return (cancelled);
143 }
144 
145 void
146 Callout::AlarmSignalHandler(int)
147 {
148 	s_alarmFired = true;
149 	ZfsDaemon::WakeEventLoop();
150 }
151 
152 void
153 Callout::ExpireCallouts()
154 {
155 	if (!s_alarmFired)
156 		return;
157 
158 	s_alarmFired = false;
159 	if (s_activeCallouts.empty()) {
160 		/* Callout removal/SIGALRM race was lost. */
161 		return;
162 	}
163 
164 	/*
165 	 * Expire the first callout (the one we used to set the
166 	 * interval timer) as well as any callouts following that
167 	 * expire at the same time (have a zero interval from
168 	 * the callout before it).
169 	 */
170 	do {
171 		Callout *cur(s_activeCallouts.front());
172 		s_activeCallouts.pop_front();
173 		cur->m_pending = false;
174 		cur->m_func(cur->m_arg);
175 	} while (!s_activeCallouts.empty()
176 	      && timerisset(&s_activeCallouts.front()->m_interval) == 0);
177 
178 	if (!s_activeCallouts.empty()) {
179 		Callout *next(s_activeCallouts.front());
180 		itimerval timerval = { { 0, 0 }, next->m_interval };
181 
182 		setitimer(ITIMER_REAL, &timerval, NULL);
183 	}
184 }
185 
186 timeval
187 Callout::TimeRemaining() const
188 {
189 	/*
190 	 * Outline: Add the m_interval for each callout in s_activeCallouts
191 	 * ahead of this, except for the first callout.  Add to that the result
192 	 * of getitimer (That's because the first callout stores its original
193 	 * interval setting while the timer is ticking).
194 	 */
195 	itimerval timervalToAlarm;
196 	timeval timeToExpiry;
197 	std::list<Callout *>::iterator it;
198 
199 	if (!IsPending()) {
200 		timeToExpiry.tv_sec = INT_MAX;
201 		timeToExpiry.tv_usec = 999999;	/*maximum normalized value*/
202 		return (timeToExpiry);
203 	}
204 
205 	timerclear(&timeToExpiry);
206 	getitimer(ITIMER_REAL, &timervalToAlarm);
207 	timeval& timeToAlarm = timervalToAlarm.it_value;
208 	timeradd(&timeToExpiry, &timeToAlarm, &timeToExpiry);
209 
210 	it =s_activeCallouts.begin();
211 	it++;	/*skip the first callout in the list*/
212 	for (; it != s_activeCallouts.end(); it++) {
213 		timeradd(&timeToExpiry, &(*it)->m_interval, &timeToExpiry);
214 		if ((*it) == this)
215 			break;
216 	}
217 	return (timeToExpiry);
218 }
219