root/sys/cddl/dev/dtrace/dtrace_vtime.c
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

void
dtrace_vtime_enable(void)
{
        dtrace_vtime_state_t state, nstate = 0;

        do {
                state = dtrace_vtime_active;

                switch (state) {
                case DTRACE_VTIME_INACTIVE:
                        nstate = DTRACE_VTIME_ACTIVE;
                        break;

                case DTRACE_VTIME_INACTIVE_TNF:
                        nstate = DTRACE_VTIME_ACTIVE_TNF;
                        break;

                case DTRACE_VTIME_ACTIVE:
                case DTRACE_VTIME_ACTIVE_TNF:
                        panic("DTrace virtual time already enabled");
                        /*NOTREACHED*/
                }

        } while (dtrace_cas32((uint32_t *)&dtrace_vtime_active,
            state, nstate) != state);
}

void
dtrace_vtime_disable(void)
{
        dtrace_vtime_state_t state, nstate = 0;

        do {
                state = dtrace_vtime_active;

                switch (state) {
                case DTRACE_VTIME_ACTIVE:
                        nstate = DTRACE_VTIME_INACTIVE;
                        break;

                case DTRACE_VTIME_ACTIVE_TNF:
                        nstate = DTRACE_VTIME_INACTIVE_TNF;
                        break;

                case DTRACE_VTIME_INACTIVE:
                case DTRACE_VTIME_INACTIVE_TNF:
                        panic("DTrace virtual time already disabled");
                        /*NOTREACHED*/
                }

        } while (dtrace_cas32((uint32_t *)&dtrace_vtime_active,
            state, nstate) != state);
}

void
dtrace_vtime_switch(kthread_t *next)
{
        dtrace_icookie_t cookie;
        hrtime_t ts;

        cookie = dtrace_interrupt_disable();
        ts = dtrace_gethrtime();

        if (curthread->t_dtrace_start != 0) {
                curthread->t_dtrace_vtime += ts - curthread->t_dtrace_start;
                curthread->t_dtrace_start = 0;
        }

        if (next != NULL)
                next->t_dtrace_start = ts;

        dtrace_interrupt_enable(cookie);
}