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 #include "umem_base.h"
28 #include "vmem_base.h"
29 
30 #include <sys/ccompile.h>
31 
32 #include <signal.h>
33 
34 /*ARGSUSED*/
35 static void * __NORETURN
umem_update_thread(void * arg)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
umem_create_update_thread(void)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, 0, 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 		(void) thr_setname(newthread, "umem_update");
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