xref: /titanic_50/usr/src/lib/libumem/common/umem_update_thread.c (revision ba2be53024c0b999e74ba9adcd7d80fec5df8c57)
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 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include "c_synonyms.h"
30 #include "umem_base.h"
31 #include "vmem_base.h"
32 
33 #include <signal.h>
34 
35 /*ARGSUSED*/
36 static void *
37 umem_update_thread(void *arg)
38 {
39 	struct timeval now;
40 	int in_update = 0;
41 
42 	(void) mutex_lock(&umem_update_lock);
43 
44 	ASSERT(umem_update_thr == thr_self());
45 	ASSERT(umem_st_update_thr == 0);
46 
47 	for (;;) {
48 		umem_process_updates();
49 
50 		if (in_update) {
51 			in_update = 0;
52 			/*
53 			 * we wait until now to set the next update time
54 			 * so that the updates are self-throttling
55 			 */
56 			(void) gettimeofday(&umem_update_next, NULL);
57 			umem_update_next.tv_sec += umem_reap_interval;
58 		}
59 
60 		switch (umem_reaping) {
61 		case UMEM_REAP_DONE:
62 		case UMEM_REAP_ADDING:
63 			break;
64 
65 		case UMEM_REAP_ACTIVE:
66 			umem_reap_next = gethrtime() +
67 			    (hrtime_t)umem_reap_interval * NANOSEC;
68 			umem_reaping = UMEM_REAP_DONE;
69 			break;
70 
71 		default:
72 			ASSERT(umem_reaping == UMEM_REAP_DONE ||
73 			    umem_reaping == UMEM_REAP_ADDING ||
74 			    umem_reaping == UMEM_REAP_ACTIVE);
75 			break;
76 		}
77 
78 		(void) gettimeofday(&now, NULL);
79 		if (now.tv_sec > umem_update_next.tv_sec ||
80 		    (now.tv_sec == umem_update_next.tv_sec &&
81 		    now.tv_usec >= umem_update_next.tv_usec)) {
82 			/*
83 			 * Time to run an update
84 			 */
85 			(void) mutex_unlock(&umem_update_lock);
86 
87 			vmem_update(NULL);
88 			/*
89 			 * umem_cache_update can use umem_add_update to
90 			 * request further work.  The update is not complete
91 			 * until all such work is finished.
92 			 */
93 			umem_cache_applyall(umem_cache_update);
94 
95 			(void) mutex_lock(&umem_update_lock);
96 			in_update = 1;
97 			continue;	/* start processing immediately */
98 		}
99 
100 		/*
101 		 * if there is no work to do, we wait until it is time for
102 		 * next update, or someone wakes us.
103 		 */
104 		if (umem_null_cache.cache_unext == &umem_null_cache) {
105 			int cancel_state;
106 			timespec_t abs_time;
107 			abs_time.tv_sec = umem_update_next.tv_sec;
108 			abs_time.tv_nsec = umem_update_next.tv_usec * 1000;
109 
110 			(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,
111 			    &cancel_state);
112 			(void) cond_timedwait(&umem_update_cv,
113 			    &umem_update_lock, &abs_time);
114 			(void) pthread_setcancelstate(cancel_state, NULL);
115 		}
116 	}
117 	/* LINTED no return statement */
118 }
119 
120 int
121 umem_create_update_thread(void)
122 {
123 	sigset_t sigmask, oldmask;
124 	thread_t newthread;
125 
126 	ASSERT(MUTEX_HELD(&umem_update_lock));
127 	ASSERT(umem_update_thr == 0);
128 
129 	/*
130 	 * The update thread handles no signals
131 	 */
132 	(void) sigfillset(&sigmask);
133 	(void) thr_sigsetmask(SIG_BLOCK, &sigmask, &oldmask);
134 
135 	/*
136 	 * drop the umem_update_lock; we cannot hold locks acquired in
137 	 * pre-fork handler while calling thr_create or thr_continue().
138 	 */
139 
140 	(void) mutex_unlock(&umem_update_lock);
141 
142 	if (thr_create(NULL, NULL, umem_update_thread, NULL,
143 	    THR_BOUND | THR_DAEMON | THR_DETACHED | THR_SUSPENDED,
144 	    &newthread) == 0) {
145 		(void) thr_sigsetmask(SIG_SETMASK, &oldmask, NULL);
146 
147 		(void) mutex_lock(&umem_update_lock);
148 		/*
149 		 * due to the locking in umem_reap(), only one thread can
150 		 * ever call umem_create_update_thread() at a time.  This
151 		 * must be the case for this code to work.
152 		 */
153 
154 		ASSERT(umem_update_thr == 0);
155 		umem_update_thr = newthread;
156 		(void) mutex_unlock(&umem_update_lock);
157 		(void) thr_continue(newthread);
158 		(void) mutex_lock(&umem_update_lock);
159 
160 		return (1);
161 	} else { /* thr_create failed */
162 		(void) thr_sigsetmask(SIG_SETMASK, &oldmask, NULL);
163 		(void) mutex_lock(&umem_update_lock);
164 	}
165 	return (0);
166 }
167