xref: /freebsd/sys/cddl/dev/dtrace/dtrace_vtime.c (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
191eaf3e1SJohn Birrell /*
291eaf3e1SJohn Birrell  * CDDL HEADER START
391eaf3e1SJohn Birrell  *
491eaf3e1SJohn Birrell  * The contents of this file are subject to the terms of the
591eaf3e1SJohn Birrell  * Common Development and Distribution License (the "License").
691eaf3e1SJohn Birrell  * You may not use this file except in compliance with the License.
791eaf3e1SJohn Birrell  *
891eaf3e1SJohn Birrell  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
991eaf3e1SJohn Birrell  * or http://www.opensolaris.org/os/licensing.
1091eaf3e1SJohn Birrell  * See the License for the specific language governing permissions
1191eaf3e1SJohn Birrell  * and limitations under the License.
1291eaf3e1SJohn Birrell  *
1391eaf3e1SJohn Birrell  * When distributing Covered Code, include this CDDL HEADER in each
1491eaf3e1SJohn Birrell  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1591eaf3e1SJohn Birrell  * If applicable, add the following below this CDDL HEADER, with the
1691eaf3e1SJohn Birrell  * fields enclosed by brackets "[]" replaced with your own identifying
1791eaf3e1SJohn Birrell  * information: Portions Copyright [yyyy] [name of copyright owner]
1891eaf3e1SJohn Birrell  *
1991eaf3e1SJohn Birrell  * CDDL HEADER END
2091eaf3e1SJohn Birrell  */
2191eaf3e1SJohn Birrell 
2291eaf3e1SJohn Birrell /*
2391eaf3e1SJohn Birrell  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
2491eaf3e1SJohn Birrell  * Use is subject to license terms.
2591eaf3e1SJohn Birrell  */
2691eaf3e1SJohn Birrell 
2791eaf3e1SJohn Birrell void
dtrace_vtime_enable(void)2891eaf3e1SJohn Birrell dtrace_vtime_enable(void)
2991eaf3e1SJohn Birrell {
3091eaf3e1SJohn Birrell 	dtrace_vtime_state_t state, nstate = 0;
3191eaf3e1SJohn Birrell 
3291eaf3e1SJohn Birrell 	do {
3391eaf3e1SJohn Birrell 		state = dtrace_vtime_active;
3491eaf3e1SJohn Birrell 
3591eaf3e1SJohn Birrell 		switch (state) {
3691eaf3e1SJohn Birrell 		case DTRACE_VTIME_INACTIVE:
3791eaf3e1SJohn Birrell 			nstate = DTRACE_VTIME_ACTIVE;
3891eaf3e1SJohn Birrell 			break;
3991eaf3e1SJohn Birrell 
4091eaf3e1SJohn Birrell 		case DTRACE_VTIME_INACTIVE_TNF:
4191eaf3e1SJohn Birrell 			nstate = DTRACE_VTIME_ACTIVE_TNF;
4291eaf3e1SJohn Birrell 			break;
4391eaf3e1SJohn Birrell 
4491eaf3e1SJohn Birrell 		case DTRACE_VTIME_ACTIVE:
4591eaf3e1SJohn Birrell 		case DTRACE_VTIME_ACTIVE_TNF:
4691eaf3e1SJohn Birrell 			panic("DTrace virtual time already enabled");
4791eaf3e1SJohn Birrell 			/*NOTREACHED*/
4891eaf3e1SJohn Birrell 		}
4991eaf3e1SJohn Birrell 
5091eaf3e1SJohn Birrell 	} while	(dtrace_cas32((uint32_t *)&dtrace_vtime_active,
5191eaf3e1SJohn Birrell 	    state, nstate) != state);
5291eaf3e1SJohn Birrell }
5391eaf3e1SJohn Birrell 
5491eaf3e1SJohn Birrell void
dtrace_vtime_disable(void)5591eaf3e1SJohn Birrell dtrace_vtime_disable(void)
5691eaf3e1SJohn Birrell {
5791eaf3e1SJohn Birrell 	dtrace_vtime_state_t state, nstate = 0;
5891eaf3e1SJohn Birrell 
5991eaf3e1SJohn Birrell 	do {
6091eaf3e1SJohn Birrell 		state = dtrace_vtime_active;
6191eaf3e1SJohn Birrell 
6291eaf3e1SJohn Birrell 		switch (state) {
6391eaf3e1SJohn Birrell 		case DTRACE_VTIME_ACTIVE:
6491eaf3e1SJohn Birrell 			nstate = DTRACE_VTIME_INACTIVE;
6591eaf3e1SJohn Birrell 			break;
6691eaf3e1SJohn Birrell 
6791eaf3e1SJohn Birrell 		case DTRACE_VTIME_ACTIVE_TNF:
6891eaf3e1SJohn Birrell 			nstate = DTRACE_VTIME_INACTIVE_TNF;
6991eaf3e1SJohn Birrell 			break;
7091eaf3e1SJohn Birrell 
7191eaf3e1SJohn Birrell 		case DTRACE_VTIME_INACTIVE:
7291eaf3e1SJohn Birrell 		case DTRACE_VTIME_INACTIVE_TNF:
7391eaf3e1SJohn Birrell 			panic("DTrace virtual time already disabled");
7491eaf3e1SJohn Birrell 			/*NOTREACHED*/
7591eaf3e1SJohn Birrell 		}
7691eaf3e1SJohn Birrell 
7791eaf3e1SJohn Birrell 	} while	(dtrace_cas32((uint32_t *)&dtrace_vtime_active,
7891eaf3e1SJohn Birrell 	    state, nstate) != state);
7991eaf3e1SJohn Birrell }
8091eaf3e1SJohn Birrell 
8191eaf3e1SJohn Birrell void
dtrace_vtime_switch(kthread_t * next)8291eaf3e1SJohn Birrell dtrace_vtime_switch(kthread_t *next)
8391eaf3e1SJohn Birrell {
8491eaf3e1SJohn Birrell 	dtrace_icookie_t cookie;
8591eaf3e1SJohn Birrell 	hrtime_t ts;
8691eaf3e1SJohn Birrell 
8791eaf3e1SJohn Birrell 	cookie = dtrace_interrupt_disable();
8891eaf3e1SJohn Birrell 	ts = dtrace_gethrtime();
8991eaf3e1SJohn Birrell 
9091eaf3e1SJohn Birrell 	if (curthread->t_dtrace_start != 0) {
9191eaf3e1SJohn Birrell 		curthread->t_dtrace_vtime += ts - curthread->t_dtrace_start;
9291eaf3e1SJohn Birrell 		curthread->t_dtrace_start = 0;
9391eaf3e1SJohn Birrell 	}
9491eaf3e1SJohn Birrell 
9591eaf3e1SJohn Birrell 	if (next != NULL)
9691eaf3e1SJohn Birrell 		next->t_dtrace_start = ts;
9791eaf3e1SJohn Birrell 
9891eaf3e1SJohn Birrell 	dtrace_interrupt_enable(cookie);
9991eaf3e1SJohn Birrell }
100