1/*	$NetBSD: dtrace_vtime.c,v 1.3 2018/05/28 21:05:03 chs Exp $	*/
2
3/*
4 * CDDL HEADER START
5 *
6 * The contents of this file are subject to the terms of the
7 * Common Development and Distribution License (the "License").
8 * You may not use this file except in compliance with the License.
9 *
10 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11 * or http://www.opensolaris.org/os/licensing.
12 * See the License for the specific language governing permissions
13 * and limitations under the License.
14 *
15 * When distributing Covered Code, include this CDDL HEADER in each
16 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17 * If applicable, add the following below this CDDL HEADER, with the
18 * fields enclosed by brackets "[]" replaced with your own identifying
19 * information: Portions Copyright [yyyy] [name of copyright owner]
20 *
21 * CDDL HEADER END
22 *
23 * $FreeBSD: head/sys/cddl/dev/dtrace/dtrace_vtime.c 179237 2008-05-23 05:59:42Z jb $
24 */
25
26/*
27 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
28 * Use is subject to license terms.
29 */
30
31void
32dtrace_vtime_enable(void)
33{
34	dtrace_vtime_state_t state, nstate = 0;
35
36	do {
37		state = dtrace_vtime_active;
38
39		switch (state) {
40		case DTRACE_VTIME_INACTIVE:
41			nstate = DTRACE_VTIME_ACTIVE;
42			break;
43
44		case DTRACE_VTIME_INACTIVE_TNF:
45			nstate = DTRACE_VTIME_ACTIVE_TNF;
46			break;
47
48		case DTRACE_VTIME_ACTIVE:
49		case DTRACE_VTIME_ACTIVE_TNF:
50			panic("DTrace virtual time already enabled");
51			/*NOTREACHED*/
52		}
53
54	} while	(dtrace_cas32((uint32_t *)&dtrace_vtime_active,
55	    state, nstate) != state);
56}
57
58void
59dtrace_vtime_disable(void)
60{
61	dtrace_vtime_state_t state, nstate = 0;
62
63	do {
64		state = dtrace_vtime_active;
65
66		switch (state) {
67		case DTRACE_VTIME_ACTIVE:
68			nstate = DTRACE_VTIME_INACTIVE;
69			break;
70
71		case DTRACE_VTIME_ACTIVE_TNF:
72			nstate = DTRACE_VTIME_INACTIVE_TNF;
73			break;
74
75		case DTRACE_VTIME_INACTIVE:
76		case DTRACE_VTIME_INACTIVE_TNF:
77			panic("DTrace virtual time already disabled");
78			/*NOTREACHED*/
79		}
80
81	} while	(dtrace_cas32((uint32_t *)&dtrace_vtime_active,
82	    state, nstate) != state);
83}
84
85void
86dtrace_vtime_switch(kthread_t *next)
87{
88	dtrace_icookie_t cookie;
89	hrtime_t ts;
90
91	cookie = dtrace_interrupt_disable();
92	ts = dtrace_gethrtime();
93
94	if (curthread->t_dtrace_start != 0) {
95		curthread->t_dtrace_vtime += ts - curthread->t_dtrace_start;
96		curthread->t_dtrace_start = 0;
97	}
98
99	if (next != NULL)
100		next->t_dtrace_start = ts;
101
102	dtrace_interrupt_enable(cookie);
103}
104