xref: /titanic_41/usr/src/lib/libumem/common/umem_update_thread.c (revision 9404882939d18ddd3c94a5bd3da7a0449c195a5d)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include "umem_base.h"
29 #include "vmem_base.h"
30 
31 #include <signal.h>
32 
33 /*
34  * we use the _ version, since we don't want to be cancelled.
35  */
36 extern int _cond_timedwait(cond_t *cv, mutex_t *mutex, const timespec_t *delay);
37 
38 /*ARGSUSED*/
39 static void *
40 umem_update_thread(void *arg)
41 {
42 	struct timeval now;
43 	int in_update = 0;
44 
45 	(void) mutex_lock(&umem_update_lock);
46 
47 	ASSERT(umem_update_thr == thr_self());
48 	ASSERT(umem_st_update_thr == 0);
49 
50 	for (;;) {
51 		umem_process_updates();
52 
53 		if (in_update) {
54 			in_update = 0;
55 			/*
56 			 * we wait until now to set the next update time
57 			 * so that the updates are self-throttling
58 			 */
59 			(void) gettimeofday(&umem_update_next, NULL);
60 			umem_update_next.tv_sec += umem_reap_interval;
61 		}
62 
63 		switch (umem_reaping) {
64 		case UMEM_REAP_DONE:
65 		case UMEM_REAP_ADDING:
66 			break;
67 
68 		case UMEM_REAP_ACTIVE:
69 			umem_reap_next = gethrtime() +
70 			    (hrtime_t)umem_reap_interval * NANOSEC;
71 			umem_reaping = UMEM_REAP_DONE;
72 			break;
73 
74 		default:
75 			ASSERT(umem_reaping == UMEM_REAP_DONE ||
76 			    umem_reaping == UMEM_REAP_ADDING ||
77 			    umem_reaping == UMEM_REAP_ACTIVE);
78 			break;
79 		}
80 
81 		(void) gettimeofday(&now, NULL);
82 		if (now.tv_sec > umem_update_next.tv_sec ||
83 		    (now.tv_sec == umem_update_next.tv_sec &&
84 		    now.tv_usec >= umem_update_next.tv_usec)) {
85 			/*
86 			 * Time to run an update
87 			 */
88 			(void) mutex_unlock(&umem_update_lock);
89 
90 			vmem_update(NULL);
91 			/*
92 			 * umem_cache_update can use umem_add_update to
93 			 * request further work.  The update is not complete
94 			 * until all such work is finished.
95 			 */
96 			umem_cache_applyall(umem_cache_update);
97 
98 			(void) mutex_lock(&umem_update_lock);
99 			in_update = 1;
100 			continue;	/* start processing immediately */
101 		}
102 
103 		/*
104 		 * if there is no work to do, we wait until it is time for
105 		 * next update, or someone wakes us.
106 		 */
107 		if (umem_null_cache.cache_unext == &umem_null_cache) {
108 			timespec_t abs_time;
109 			abs_time.tv_sec = umem_update_next.tv_sec;
110 			abs_time.tv_nsec = umem_update_next.tv_usec * 1000;
111 
112 			(void) _cond_timedwait(&umem_update_cv,
113 			    &umem_update_lock, &abs_time);
114 		}
115 	}
116 	/* LINTED no return statement */
117 }
118 
119 int
120 umem_create_update_thread(void)
121 {
122 	sigset_t sigmask, oldmask;
123 	thread_t newthread;
124 
125 	ASSERT(MUTEX_HELD(&umem_update_lock));
126 	ASSERT(umem_update_thr == 0);
127 
128 	/*
129 	 * The update thread handles no signals
130 	 */
131 	(void) sigfillset(&sigmask);
132 	(void) thr_sigsetmask(SIG_BLOCK, &sigmask, &oldmask);
133 
134 	/*
135 	 * drop the umem_update_lock; we cannot hold locks acquired in
136 	 * pre-fork handler while calling thr_create or thr_continue().
137 	 */
138 
139 	(void) mutex_unlock(&umem_update_lock);
140 
141 	if (thr_create(NULL, NULL, umem_update_thread, NULL,
142 	    THR_BOUND | THR_DAEMON | THR_DETACHED | THR_SUSPENDED,
143 	    &newthread) == 0) {
144 		(void) thr_sigsetmask(SIG_SETMASK, &oldmask, NULL);
145 
146 		(void) mutex_lock(&umem_update_lock);
147 		/*
148 		 * due to the locking in umem_reap(), only one thread can
149 		 * ever call umem_create_update_thread() at a time.  This
150 		 * must be the case for this code to work.
151 		 */
152 
153 		ASSERT(umem_update_thr == 0);
154 		umem_update_thr = newthread;
155 		(void) mutex_unlock(&umem_update_lock);
156 		(void) thr_continue(newthread);
157 		(void) mutex_lock(&umem_update_lock);
158 
159 		return (1);
160 	} else { /* thr_create failed */
161 		(void) thr_sigsetmask(SIG_SETMASK, &oldmask, NULL);
162 		(void) mutex_lock(&umem_update_lock);
163 	}
164 	return (0);
165 }
166