xref: /titanic_41/usr/src/lib/libumem/common/umem_update_thread.c (revision ea394cb00fd96864e34d2841b4a22357b621c78f)
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 "umem_base.h"
30 #include "vmem_base.h"
31 
32 #include <signal.h>
33 
34 /*ARGSUSED*/
35 static void *
36 umem_update_thread(void *arg)
37 {
38 	struct timeval now;
39 	int in_update = 0;
40 
41 	(void) mutex_lock(&umem_update_lock);
42 
43 	ASSERT(umem_update_thr == thr_self());
44 	ASSERT(umem_st_update_thr == 0);
45 
46 	for (;;) {
47 		umem_process_updates();
48 
49 		if (in_update) {
50 			in_update = 0;
51 			/*
52 			 * we wait until now to set the next update time
53 			 * so that the updates are self-throttling
54 			 */
55 			(void) gettimeofday(&umem_update_next, NULL);
56 			umem_update_next.tv_sec += umem_reap_interval;
57 		}
58 
59 		switch (umem_reaping) {
60 		case UMEM_REAP_DONE:
61 		case UMEM_REAP_ADDING:
62 			break;
63 
64 		case UMEM_REAP_ACTIVE:
65 			umem_reap_next = gethrtime() +
66 			    (hrtime_t)umem_reap_interval * NANOSEC;
67 			umem_reaping = UMEM_REAP_DONE;
68 			break;
69 
70 		default:
71 			ASSERT(umem_reaping == UMEM_REAP_DONE ||
72 			    umem_reaping == UMEM_REAP_ADDING ||
73 			    umem_reaping == UMEM_REAP_ACTIVE);
74 			break;
75 		}
76 
77 		(void) gettimeofday(&now, NULL);
78 		if (now.tv_sec > umem_update_next.tv_sec ||
79 		    (now.tv_sec == umem_update_next.tv_sec &&
80 		    now.tv_usec >= umem_update_next.tv_usec)) {
81 			/*
82 			 * Time to run an update
83 			 */
84 			(void) mutex_unlock(&umem_update_lock);
85 
86 			vmem_update(NULL);
87 			/*
88 			 * umem_cache_update can use umem_add_update to
89 			 * request further work.  The update is not complete
90 			 * until all such work is finished.
91 			 */
92 			umem_cache_applyall(umem_cache_update);
93 
94 			(void) mutex_lock(&umem_update_lock);
95 			in_update = 1;
96 			continue;	/* start processing immediately */
97 		}
98 
99 		/*
100 		 * if there is no work to do, we wait until it is time for
101 		 * next update, or someone wakes us.
102 		 */
103 		if (umem_null_cache.cache_unext == &umem_null_cache) {
104 			int cancel_state;
105 			timespec_t abs_time;
106 			abs_time.tv_sec = umem_update_next.tv_sec;
107 			abs_time.tv_nsec = umem_update_next.tv_usec * 1000;
108 
109 			(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,
110 			    &cancel_state);
111 			(void) cond_timedwait(&umem_update_cv,
112 			    &umem_update_lock, &abs_time);
113 			(void) pthread_setcancelstate(cancel_state, NULL);
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