clock_impl.h revision 579a68952787d75370b20af99861b7dcc781c6be
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni/*
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni * CDDL HEADER START
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni *
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni * The contents of this file are subject to the terms of the
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni * Common Development and Distribution License (the "License").
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni * You may not use this file except in compliance with the License.
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni *
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni * or http://www.opensolaris.org/os/licensing.
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni * See the License for the specific language governing permissions
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni * and limitations under the License.
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni *
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni * When distributing Covered Code, include this CDDL HEADER in each
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni * If applicable, add the following below this CDDL HEADER, with the
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni * fields enclosed by brackets "[]" replaced with your own identifying
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni * information: Portions Copyright [yyyy] [name of copyright owner]
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni *
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni * CDDL HEADER END
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni */
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni/*
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni * Use is subject to license terms.
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni */
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni#ifndef _SYS_CLOCK_IMPL_H
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni#define _SYS_CLOCK_IMPL_H
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni#ifdef __cplusplus
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoniextern "C" {
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni#endif
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni#if (defined(_KERNEL) || defined(_KMEMUSER))
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni#include <sys/types.h>
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni#include <sys/cpuvar.h>
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni#include <sys/cyclic.h>
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni#include <sys/time.h>
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni/*
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni * Default clock rate in Hz.
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni */
579a68952787d75370b20af99861b7dcc781c6beRafael Vanoni#define HZ_DEFAULT (100)
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni/*
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni * Thresholds over which we switch between event and cyclic driven lbolt. The
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni * current default values were derived experimentally and will keep the
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni * system on event driven mode when idle and respond to activity around the
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni * lbolt DDI functions by switching to cyclic mode.
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni */
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni#define LBOLT_THRESH_CALLS (75)
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni#define LBOLT_THRESH_INTERVAL (1)
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni/*
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni * Both lbolt_cpu_t and lbolt_info_t are cache line sized and aligned,
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni * please take that in consideration if modifying these.
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni */
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanonitypedef struct lbolt_cpu {
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni int64_t lbc_counter; /* number of calls to the DDI lbolt routines */
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni int64_t lbc_cnt_start; /* beggining of the cnt interval (in ticks) */
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni char lbc_pad[CPU_CACHE_COHERENCE_SIZE - (2 * sizeof (int64_t))];
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni} lbolt_cpu_t;
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanonitypedef struct lbolt_info {
579a68952787d75370b20af99861b7dcc781c6beRafael Vanoni union {
579a68952787d75370b20af99861b7dcc781c6beRafael Vanoni cyclic_id_t lbi_cyclic_id; /* lbolt's cyclic id */
579a68952787d75370b20af99861b7dcc781c6beRafael Vanoni int64_t lbi_id_pad; /* 64bit padding */
579a68952787d75370b20af99861b7dcc781c6beRafael Vanoni } id;
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni int64_t lbi_internal; /* lbolt source when on cyclic mode */
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni int64_t lbi_debug_time; /* time spent in the debugger */
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni int64_t lbi_debug_ts; /* last time we dropped into kmdb */
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni int64_t lbi_thresh_calls; /* max calls per interval */
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni int64_t lbi_thresh_interval; /* interval window for the # of calls */
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni uint32_t lbi_token; /* synchronize cyclic mode switch */
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni boolean_t lbi_cyc_deactivate; /* lbolt_cyclic self deactivation */
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni int64_t lbi_cyc_deac_start; /* deactivation interval */
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni} lbolt_info_t;
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoniextern int64_t lbolt_event_driven(void);
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoniextern int64_t lbolt_cyclic_driven(void);
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoniextern int64_t (*lbolt_hybrid)(void);
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoniextern uint_t lbolt_ev_to_cyclic(caddr_t, caddr_t);
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoniextern void lbolt_softint_add(void);
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoniextern void lbolt_softint_post(void);
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoniextern void lbolt_debug_entry(void);
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoniextern void lbolt_debug_return(void);
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoniextern lbolt_info_t *lb_info;
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni/*
1b7f7204f8aa806a57b84cdeba100d819be85a61Rafael Vanoni * LBOLT_WAITFREE{,64} provide a non-waiting version of lbolt.
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni */
1b7f7204f8aa806a57b84cdeba100d819be85a61Rafael Vanoni#define LBOLT_WAITFREE64 \
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni (lbolt_hybrid == lbolt_event_driven ? \
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni ((gethrtime_waitfree()/nsec_per_tick) - \
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni lb_info->lbi_debug_time) : \
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni (lb_info->lbi_internal - lb_info->lbi_debug_time))
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni
1b7f7204f8aa806a57b84cdeba100d819be85a61Rafael Vanoni#define LBOLT_WAITFREE (clock_t)LBOLT_WAITFREE64
1b7f7204f8aa806a57b84cdeba100d819be85a61Rafael Vanoni
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni/*
1b7f7204f8aa806a57b84cdeba100d819be85a61Rafael Vanoni * LBOLT_FASTPATH{,64} should *only* be used where the cost of calling the
1b7f7204f8aa806a57b84cdeba100d819be85a61Rafael Vanoni * DDI lbolt routines affects performance. This is currently only used by
1b7f7204f8aa806a57b84cdeba100d819be85a61Rafael Vanoni * the TCP/IP code and will be removed once it's no longer required.
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni */
1b7f7204f8aa806a57b84cdeba100d819be85a61Rafael Vanoni#define LBOLT_FASTPATH64 \
1b7f7204f8aa806a57b84cdeba100d819be85a61Rafael Vanoni (lbolt_hybrid == lbolt_event_driven ? ddi_get_lbolt64() : \
1b7f7204f8aa806a57b84cdeba100d819be85a61Rafael Vanoni (lb_info->lbi_internal - lb_info->lbi_debug_time))
1b7f7204f8aa806a57b84cdeba100d819be85a61Rafael Vanoni
1b7f7204f8aa806a57b84cdeba100d819be85a61Rafael Vanoni#define LBOLT_FASTPATH (clock_t)LBOLT_FASTPATH64
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni/*
1b7f7204f8aa806a57b84cdeba100d819be85a61Rafael Vanoni * LBOLT_NO_ACCOUNT{,64} is used by lbolt consumers who fire at a periodic
1b7f7204f8aa806a57b84cdeba100d819be85a61Rafael Vanoni * rate, such as clock(), for which the lbolt usage statistics are not updated.
1b7f7204f8aa806a57b84cdeba100d819be85a61Rafael Vanoni * This is especially important for consumers whose rate may be modified by
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni * the user, resulting in an unaccounted for increase in activity around the
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni * lbolt routines that could cause a mode switch.
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni */
1b7f7204f8aa806a57b84cdeba100d819be85a61Rafael Vanoni#define LBOLT_NO_ACCOUNT64 \
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni (lbolt_hybrid == lbolt_event_driven ? \
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni ((gethrtime()/nsec_per_tick) - lb_info->lbi_debug_time) : \
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni (lb_info->lbi_internal - lb_info->lbi_debug_time))
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni
1b7f7204f8aa806a57b84cdeba100d819be85a61Rafael Vanoni#define LBOLT_NO_ACCOUNT (clock_t)LBOLT_NO_ACCOUNT64
1b7f7204f8aa806a57b84cdeba100d819be85a61Rafael Vanoni
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni#endif /* _KERNEL || _KMEMUSER */
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni#ifdef __cplusplus
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni}
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni#endif
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni#endif /* _SYS_CLOCK_IMPL_H */