xref: /freebsd/sys/cddl/dev/dtrace/dtrace_vtime.c (revision 525fe93dc7487a1e63a90f6a2b956abc601963c1)
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 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 void
28 dtrace_vtime_enable(void)
29 {
30 	dtrace_vtime_state_t state, nstate = 0;
31 
32 	do {
33 		state = dtrace_vtime_active;
34 
35 		switch (state) {
36 		case DTRACE_VTIME_INACTIVE:
37 			nstate = DTRACE_VTIME_ACTIVE;
38 			break;
39 
40 		case DTRACE_VTIME_INACTIVE_TNF:
41 			nstate = DTRACE_VTIME_ACTIVE_TNF;
42 			break;
43 
44 		case DTRACE_VTIME_ACTIVE:
45 		case DTRACE_VTIME_ACTIVE_TNF:
46 			panic("DTrace virtual time already enabled");
47 			/*NOTREACHED*/
48 		}
49 
50 	} while	(dtrace_cas32((uint32_t *)&dtrace_vtime_active,
51 	    state, nstate) != state);
52 }
53 
54 void
55 dtrace_vtime_disable(void)
56 {
57 	dtrace_vtime_state_t state, nstate = 0;
58 
59 	do {
60 		state = dtrace_vtime_active;
61 
62 		switch (state) {
63 		case DTRACE_VTIME_ACTIVE:
64 			nstate = DTRACE_VTIME_INACTIVE;
65 			break;
66 
67 		case DTRACE_VTIME_ACTIVE_TNF:
68 			nstate = DTRACE_VTIME_INACTIVE_TNF;
69 			break;
70 
71 		case DTRACE_VTIME_INACTIVE:
72 		case DTRACE_VTIME_INACTIVE_TNF:
73 			panic("DTrace virtual time already disabled");
74 			/*NOTREACHED*/
75 		}
76 
77 	} while	(dtrace_cas32((uint32_t *)&dtrace_vtime_active,
78 	    state, nstate) != state);
79 }
80 
81 void
82 dtrace_vtime_switch(kthread_t *next)
83 {
84 	dtrace_icookie_t cookie;
85 	hrtime_t ts;
86 
87 	cookie = dtrace_interrupt_disable();
88 	ts = dtrace_gethrtime();
89 
90 	if (curthread->t_dtrace_start != 0) {
91 		curthread->t_dtrace_vtime += ts - curthread->t_dtrace_start;
92 		curthread->t_dtrace_start = 0;
93 	}
94 
95 	if (next != NULL)
96 		next->t_dtrace_start = ts;
97 
98 	dtrace_interrupt_enable(cookie);
99 }
100