xref: /freebsd-src/cddl/usr.sbin/zfsd/callout.cc (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
17a0c41d5SAlan Somers /*-
27a0c41d5SAlan Somers  * Copyright (c) 2011, 2012, 2013 Spectra Logic Corporation
37a0c41d5SAlan Somers  * All rights reserved.
47a0c41d5SAlan Somers  *
57a0c41d5SAlan Somers  * Redistribution and use in source and binary forms, with or without
67a0c41d5SAlan Somers  * modification, are permitted provided that the following conditions
77a0c41d5SAlan Somers  * are met:
87a0c41d5SAlan Somers  * 1. Redistributions of source code must retain the above copyright
97a0c41d5SAlan Somers  *    notice, this list of conditions, and the following disclaimer,
107a0c41d5SAlan Somers  *    without modification.
117a0c41d5SAlan Somers  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
127a0c41d5SAlan Somers  *    substantially similar to the "NO WARRANTY" disclaimer below
137a0c41d5SAlan Somers  *    ("Disclaimer") and any redistribution must be conditioned upon
147a0c41d5SAlan Somers  *    including a substantially similar Disclaimer requirement for further
157a0c41d5SAlan Somers  *    binary redistribution.
167a0c41d5SAlan Somers  *
177a0c41d5SAlan Somers  * NO WARRANTY
187a0c41d5SAlan Somers  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
197a0c41d5SAlan Somers  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
207a0c41d5SAlan Somers  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
217a0c41d5SAlan Somers  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
227a0c41d5SAlan Somers  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
237a0c41d5SAlan Somers  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
247a0c41d5SAlan Somers  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
257a0c41d5SAlan Somers  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
267a0c41d5SAlan Somers  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
277a0c41d5SAlan Somers  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
287a0c41d5SAlan Somers  * POSSIBILITY OF SUCH DAMAGES.
297a0c41d5SAlan Somers  *
307a0c41d5SAlan Somers  * Authors: Justin T. Gibbs     (Spectra Logic Corporation)
317a0c41d5SAlan Somers  */
327a0c41d5SAlan Somers 
337a0c41d5SAlan Somers /**
347a0c41d5SAlan Somers  * \file callout.cc
357a0c41d5SAlan Somers  *
367a0c41d5SAlan Somers  * \brief Implementation of the Callout class - multi-client
377a0c41d5SAlan Somers  *        timer services built on top of the POSIX interval timer.
387a0c41d5SAlan Somers  */
397a0c41d5SAlan Somers 
40*9e5787d2SMatt Macy #include <sys/byteorder.h>
417a0c41d5SAlan Somers #include <sys/time.h>
427a0c41d5SAlan Somers 
437a0c41d5SAlan Somers #include <signal.h>
447a0c41d5SAlan Somers #include <syslog.h>
457a0c41d5SAlan Somers 
467a0c41d5SAlan Somers #include <climits>
477a0c41d5SAlan Somers #include <list>
487a0c41d5SAlan Somers #include <map>
497a0c41d5SAlan Somers #include <string>
507a0c41d5SAlan Somers 
517a0c41d5SAlan Somers #include <devdctl/guid.h>
527a0c41d5SAlan Somers #include <devdctl/event.h>
537a0c41d5SAlan Somers #include <devdctl/event_factory.h>
547a0c41d5SAlan Somers #include <devdctl/consumer.h>
557a0c41d5SAlan Somers #include <devdctl/exception.h>
567a0c41d5SAlan Somers 
577a0c41d5SAlan Somers #include "callout.h"
587a0c41d5SAlan Somers #include "vdev_iterator.h"
597a0c41d5SAlan Somers #include "zfsd.h"
607a0c41d5SAlan Somers #include "zfsd_exception.h"
617a0c41d5SAlan Somers 
627a0c41d5SAlan Somers std::list<Callout *> Callout::s_activeCallouts;
637a0c41d5SAlan Somers bool		     Callout::s_alarmFired(false);
647a0c41d5SAlan Somers 
657a0c41d5SAlan Somers void
Init()667a0c41d5SAlan Somers Callout::Init()
677a0c41d5SAlan Somers {
687a0c41d5SAlan Somers 	signal(SIGALRM,  Callout::AlarmSignalHandler);
697a0c41d5SAlan Somers }
707a0c41d5SAlan Somers 
717a0c41d5SAlan Somers bool
Stop()727a0c41d5SAlan Somers Callout::Stop()
737a0c41d5SAlan Somers {
747a0c41d5SAlan Somers 	if (!IsPending())
757a0c41d5SAlan Somers 		return (false);
767a0c41d5SAlan Somers 
777a0c41d5SAlan Somers 	for (std::list<Callout *>::iterator it(s_activeCallouts.begin());
787a0c41d5SAlan Somers 	     it != s_activeCallouts.end(); it++) {
797a0c41d5SAlan Somers 		if (*it != this)
807a0c41d5SAlan Somers 			continue;
817a0c41d5SAlan Somers 
827a0c41d5SAlan Somers 		it = s_activeCallouts.erase(it);
837a0c41d5SAlan Somers 		if (it != s_activeCallouts.end()) {
847a0c41d5SAlan Somers 
857a0c41d5SAlan Somers 			/*
867a0c41d5SAlan Somers 			 * Maintain correct interval for the
877a0c41d5SAlan Somers 			 * callouts that follow the just removed
887a0c41d5SAlan Somers 			 * entry.
897a0c41d5SAlan Somers 			 */
907a0c41d5SAlan Somers 			timeradd(&(*it)->m_interval, &m_interval,
917a0c41d5SAlan Somers 				 &(*it)->m_interval);
927a0c41d5SAlan Somers 		}
937a0c41d5SAlan Somers 		break;
947a0c41d5SAlan Somers 	}
957a0c41d5SAlan Somers 	m_pending = false;
967a0c41d5SAlan Somers 	return (true);
977a0c41d5SAlan Somers }
987a0c41d5SAlan Somers 
997a0c41d5SAlan Somers bool
Reset(const timeval & interval,CalloutFunc_t * func,void * arg)1007a0c41d5SAlan Somers Callout::Reset(const timeval &interval, CalloutFunc_t *func, void *arg)
1017a0c41d5SAlan Somers {
1027a0c41d5SAlan Somers 	bool cancelled(false);
1037a0c41d5SAlan Somers 
1047a0c41d5SAlan Somers 	if (!timerisset(&interval))
1057a0c41d5SAlan Somers 		throw ZfsdException("Callout::Reset: interval of 0");
1067a0c41d5SAlan Somers 
1077a0c41d5SAlan Somers 	cancelled = Stop();
1087a0c41d5SAlan Somers 
1097a0c41d5SAlan Somers 	m_interval = interval;
1107a0c41d5SAlan Somers 	m_func     = func;
1117a0c41d5SAlan Somers 	m_arg      = arg;
1127a0c41d5SAlan Somers 	m_pending  = true;
1137a0c41d5SAlan Somers 
1147a0c41d5SAlan Somers 	std::list<Callout *>::iterator it(s_activeCallouts.begin());
1157a0c41d5SAlan Somers 	for (; it != s_activeCallouts.end(); it++) {
1167a0c41d5SAlan Somers 
1177a0c41d5SAlan Somers 		if (timercmp(&(*it)->m_interval, &m_interval, <=)) {
1187a0c41d5SAlan Somers 			/*
1197a0c41d5SAlan Somers 			 * Decrease our interval by those that come
1207a0c41d5SAlan Somers 			 * before us.
1217a0c41d5SAlan Somers 			 */
1227a0c41d5SAlan Somers 			timersub(&m_interval, &(*it)->m_interval, &m_interval);
1237a0c41d5SAlan Somers 		} else {
1247a0c41d5SAlan Somers 			/*
1257a0c41d5SAlan Somers 			 * Account for the time between the newly
1267a0c41d5SAlan Somers 			 * inserted event and those that follow.
1277a0c41d5SAlan Somers 			 */
1287a0c41d5SAlan Somers 			timersub(&(*it)->m_interval, &m_interval,
1297a0c41d5SAlan Somers 				 &(*it)->m_interval);
1307a0c41d5SAlan Somers 			break;
1317a0c41d5SAlan Somers 		}
1327a0c41d5SAlan Somers 	}
1337a0c41d5SAlan Somers 	s_activeCallouts.insert(it, this);
1347a0c41d5SAlan Somers 
1357a0c41d5SAlan Somers 
1367a0c41d5SAlan Somers 	if (s_activeCallouts.front() == this) {
1377a0c41d5SAlan Somers 		itimerval timerval = { {0, 0}, m_interval };
1387a0c41d5SAlan Somers 
1397a0c41d5SAlan Somers 		setitimer(ITIMER_REAL, &timerval, NULL);
1407a0c41d5SAlan Somers 	}
1417a0c41d5SAlan Somers 
1427a0c41d5SAlan Somers 	return (cancelled);
1437a0c41d5SAlan Somers }
1447a0c41d5SAlan Somers 
1457a0c41d5SAlan Somers void
AlarmSignalHandler(int)1467a0c41d5SAlan Somers Callout::AlarmSignalHandler(int)
1477a0c41d5SAlan Somers {
1487a0c41d5SAlan Somers 	s_alarmFired = true;
1497a0c41d5SAlan Somers 	ZfsDaemon::WakeEventLoop();
1507a0c41d5SAlan Somers }
1517a0c41d5SAlan Somers 
1527a0c41d5SAlan Somers void
ExpireCallouts()1537a0c41d5SAlan Somers Callout::ExpireCallouts()
1547a0c41d5SAlan Somers {
1557a0c41d5SAlan Somers 	if (!s_alarmFired)
1567a0c41d5SAlan Somers 		return;
1577a0c41d5SAlan Somers 
1587a0c41d5SAlan Somers 	s_alarmFired = false;
1597a0c41d5SAlan Somers 	if (s_activeCallouts.empty()) {
1607a0c41d5SAlan Somers 		/* Callout removal/SIGALRM race was lost. */
1617a0c41d5SAlan Somers 		return;
1627a0c41d5SAlan Somers 	}
1637a0c41d5SAlan Somers 
1647a0c41d5SAlan Somers 	/*
1657a0c41d5SAlan Somers 	 * Expire the first callout (the one we used to set the
1667a0c41d5SAlan Somers 	 * interval timer) as well as any callouts following that
1677a0c41d5SAlan Somers 	 * expire at the same time (have a zero interval from
1687a0c41d5SAlan Somers 	 * the callout before it).
1697a0c41d5SAlan Somers 	 */
1707a0c41d5SAlan Somers 	do {
1717a0c41d5SAlan Somers 		Callout *cur(s_activeCallouts.front());
1727a0c41d5SAlan Somers 		s_activeCallouts.pop_front();
1737a0c41d5SAlan Somers 		cur->m_pending = false;
1747a0c41d5SAlan Somers 		cur->m_func(cur->m_arg);
1757a0c41d5SAlan Somers 	} while (!s_activeCallouts.empty()
1767a0c41d5SAlan Somers 	      && timerisset(&s_activeCallouts.front()->m_interval) == 0);
1777a0c41d5SAlan Somers 
1787a0c41d5SAlan Somers 	if (!s_activeCallouts.empty()) {
1797a0c41d5SAlan Somers 		Callout *next(s_activeCallouts.front());
1807a0c41d5SAlan Somers 		itimerval timerval = { { 0, 0 }, next->m_interval };
1817a0c41d5SAlan Somers 
1827a0c41d5SAlan Somers 		setitimer(ITIMER_REAL, &timerval, NULL);
1837a0c41d5SAlan Somers 	}
1847a0c41d5SAlan Somers }
1857a0c41d5SAlan Somers 
1867a0c41d5SAlan Somers timeval
TimeRemaining() const1877a0c41d5SAlan Somers Callout::TimeRemaining() const
1887a0c41d5SAlan Somers {
1897a0c41d5SAlan Somers 	/*
1907a0c41d5SAlan Somers 	 * Outline: Add the m_interval for each callout in s_activeCallouts
1917a0c41d5SAlan Somers 	 * ahead of this, except for the first callout.  Add to that the result
1927a0c41d5SAlan Somers 	 * of getitimer (That's because the first callout stores its original
1937a0c41d5SAlan Somers 	 * interval setting while the timer is ticking).
1947a0c41d5SAlan Somers 	 */
1957a0c41d5SAlan Somers 	itimerval timervalToAlarm;
1967a0c41d5SAlan Somers 	timeval timeToExpiry;
1977a0c41d5SAlan Somers 	std::list<Callout *>::iterator it;
1987a0c41d5SAlan Somers 
1997a0c41d5SAlan Somers 	if (!IsPending()) {
2007a0c41d5SAlan Somers 		timeToExpiry.tv_sec = INT_MAX;
2017a0c41d5SAlan Somers 		timeToExpiry.tv_usec = 999999;	/*maximum normalized value*/
2027a0c41d5SAlan Somers 		return (timeToExpiry);
2037a0c41d5SAlan Somers 	}
2047a0c41d5SAlan Somers 
2057a0c41d5SAlan Somers 	timerclear(&timeToExpiry);
2067a0c41d5SAlan Somers 	getitimer(ITIMER_REAL, &timervalToAlarm);
2077a0c41d5SAlan Somers 	timeval& timeToAlarm = timervalToAlarm.it_value;
2087a0c41d5SAlan Somers 	timeradd(&timeToExpiry, &timeToAlarm, &timeToExpiry);
2097a0c41d5SAlan Somers 
2107a0c41d5SAlan Somers 	it =s_activeCallouts.begin();
2117a0c41d5SAlan Somers 	it++;	/*skip the first callout in the list*/
2127a0c41d5SAlan Somers 	for (; it != s_activeCallouts.end(); it++) {
2137a0c41d5SAlan Somers 		timeradd(&timeToExpiry, &(*it)->m_interval, &timeToExpiry);
2147a0c41d5SAlan Somers 		if ((*it) == this)
2157a0c41d5SAlan Somers 			break;
2167a0c41d5SAlan Somers 	}
2177a0c41d5SAlan Somers 	return (timeToExpiry);
2187a0c41d5SAlan Somers }
219