10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
54258Sbarts * Common Development and Distribution License (the "License").
64258Sbarts * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21*5891Sraf
220Sstevel@tonic-gate /*
23*5891Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
280Sstevel@tonic-gate
290Sstevel@tonic-gate #include "umem_base.h"
300Sstevel@tonic-gate #include "vmem_base.h"
310Sstevel@tonic-gate
320Sstevel@tonic-gate #include <signal.h>
330Sstevel@tonic-gate
340Sstevel@tonic-gate /*ARGSUSED*/
350Sstevel@tonic-gate static void *
umem_update_thread(void * arg)360Sstevel@tonic-gate umem_update_thread(void *arg)
370Sstevel@tonic-gate {
380Sstevel@tonic-gate struct timeval now;
390Sstevel@tonic-gate int in_update = 0;
400Sstevel@tonic-gate
410Sstevel@tonic-gate (void) mutex_lock(&umem_update_lock);
420Sstevel@tonic-gate
430Sstevel@tonic-gate ASSERT(umem_update_thr == thr_self());
440Sstevel@tonic-gate ASSERT(umem_st_update_thr == 0);
450Sstevel@tonic-gate
460Sstevel@tonic-gate for (;;) {
470Sstevel@tonic-gate umem_process_updates();
480Sstevel@tonic-gate
490Sstevel@tonic-gate if (in_update) {
500Sstevel@tonic-gate in_update = 0;
510Sstevel@tonic-gate /*
520Sstevel@tonic-gate * we wait until now to set the next update time
530Sstevel@tonic-gate * so that the updates are self-throttling
540Sstevel@tonic-gate */
550Sstevel@tonic-gate (void) gettimeofday(&umem_update_next, NULL);
560Sstevel@tonic-gate umem_update_next.tv_sec += umem_reap_interval;
570Sstevel@tonic-gate }
580Sstevel@tonic-gate
590Sstevel@tonic-gate switch (umem_reaping) {
600Sstevel@tonic-gate case UMEM_REAP_DONE:
610Sstevel@tonic-gate case UMEM_REAP_ADDING:
620Sstevel@tonic-gate break;
630Sstevel@tonic-gate
640Sstevel@tonic-gate case UMEM_REAP_ACTIVE:
650Sstevel@tonic-gate umem_reap_next = gethrtime() +
660Sstevel@tonic-gate (hrtime_t)umem_reap_interval * NANOSEC;
670Sstevel@tonic-gate umem_reaping = UMEM_REAP_DONE;
680Sstevel@tonic-gate break;
690Sstevel@tonic-gate
700Sstevel@tonic-gate default:
710Sstevel@tonic-gate ASSERT(umem_reaping == UMEM_REAP_DONE ||
720Sstevel@tonic-gate umem_reaping == UMEM_REAP_ADDING ||
730Sstevel@tonic-gate umem_reaping == UMEM_REAP_ACTIVE);
740Sstevel@tonic-gate break;
750Sstevel@tonic-gate }
760Sstevel@tonic-gate
770Sstevel@tonic-gate (void) gettimeofday(&now, NULL);
780Sstevel@tonic-gate if (now.tv_sec > umem_update_next.tv_sec ||
790Sstevel@tonic-gate (now.tv_sec == umem_update_next.tv_sec &&
800Sstevel@tonic-gate now.tv_usec >= umem_update_next.tv_usec)) {
810Sstevel@tonic-gate /*
820Sstevel@tonic-gate * Time to run an update
830Sstevel@tonic-gate */
840Sstevel@tonic-gate (void) mutex_unlock(&umem_update_lock);
850Sstevel@tonic-gate
860Sstevel@tonic-gate vmem_update(NULL);
870Sstevel@tonic-gate /*
880Sstevel@tonic-gate * umem_cache_update can use umem_add_update to
890Sstevel@tonic-gate * request further work. The update is not complete
900Sstevel@tonic-gate * until all such work is finished.
910Sstevel@tonic-gate */
920Sstevel@tonic-gate umem_cache_applyall(umem_cache_update);
930Sstevel@tonic-gate
940Sstevel@tonic-gate (void) mutex_lock(&umem_update_lock);
950Sstevel@tonic-gate in_update = 1;
960Sstevel@tonic-gate continue; /* start processing immediately */
970Sstevel@tonic-gate }
980Sstevel@tonic-gate
990Sstevel@tonic-gate /*
1000Sstevel@tonic-gate * if there is no work to do, we wait until it is time for
1010Sstevel@tonic-gate * next update, or someone wakes us.
1020Sstevel@tonic-gate */
1030Sstevel@tonic-gate if (umem_null_cache.cache_unext == &umem_null_cache) {
104*5891Sraf int cancel_state;
1050Sstevel@tonic-gate timespec_t abs_time;
1060Sstevel@tonic-gate abs_time.tv_sec = umem_update_next.tv_sec;
1070Sstevel@tonic-gate abs_time.tv_nsec = umem_update_next.tv_usec * 1000;
1080Sstevel@tonic-gate
109*5891Sraf (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,
110*5891Sraf &cancel_state);
111*5891Sraf (void) cond_timedwait(&umem_update_cv,
1120Sstevel@tonic-gate &umem_update_lock, &abs_time);
113*5891Sraf (void) pthread_setcancelstate(cancel_state, NULL);
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate }
1164261Sbarts /* LINTED no return statement */
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate int
umem_create_update_thread(void)1200Sstevel@tonic-gate umem_create_update_thread(void)
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate sigset_t sigmask, oldmask;
1234258Sbarts thread_t newthread;
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate ASSERT(MUTEX_HELD(&umem_update_lock));
1260Sstevel@tonic-gate ASSERT(umem_update_thr == 0);
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate /*
1290Sstevel@tonic-gate * The update thread handles no signals
1300Sstevel@tonic-gate */
1310Sstevel@tonic-gate (void) sigfillset(&sigmask);
1320Sstevel@tonic-gate (void) thr_sigsetmask(SIG_BLOCK, &sigmask, &oldmask);
1334258Sbarts
1344258Sbarts /*
1354258Sbarts * drop the umem_update_lock; we cannot hold locks acquired in
1364258Sbarts * pre-fork handler while calling thr_create or thr_continue().
1374258Sbarts */
1384258Sbarts
1394258Sbarts (void) mutex_unlock(&umem_update_lock);
1404258Sbarts
1410Sstevel@tonic-gate if (thr_create(NULL, NULL, umem_update_thread, NULL,
1424258Sbarts THR_BOUND | THR_DAEMON | THR_DETACHED | THR_SUSPENDED,
1434258Sbarts &newthread) == 0) {
1440Sstevel@tonic-gate (void) thr_sigsetmask(SIG_SETMASK, &oldmask, NULL);
1454258Sbarts
1464258Sbarts (void) mutex_lock(&umem_update_lock);
1474258Sbarts /*
1484258Sbarts * due to the locking in umem_reap(), only one thread can
1494258Sbarts * ever call umem_create_update_thread() at a time. This
1504258Sbarts * must be the case for this code to work.
1514258Sbarts */
1524258Sbarts
1534258Sbarts ASSERT(umem_update_thr == 0);
1544258Sbarts umem_update_thr = newthread;
1554258Sbarts (void) mutex_unlock(&umem_update_lock);
1564258Sbarts (void) thr_continue(newthread);
1574258Sbarts (void) mutex_lock(&umem_update_lock);
1584258Sbarts
1590Sstevel@tonic-gate return (1);
1604258Sbarts } else { /* thr_create failed */
1614258Sbarts (void) thr_sigsetmask(SIG_SETMASK, &oldmask, NULL);
1624258Sbarts (void) mutex_lock(&umem_update_lock);
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate return (0);
1650Sstevel@tonic-gate }
166