version 1.4, 2003/06/03 02:56:08 |
version 1.5, 2005/05/03 08:08:53 |
|
|
/* $OpenBSD$ */ |
/* $OpenBSD$ */ |
/* $NetBSD: hertz.c,v 1.5 1995/04/19 07:16:03 cgd Exp $ */ |
|
|
|
/* |
/* |
* Copyright (c) 1983, 1993 |
* Copyright (c) 2005 Artur Grabowski <art@openbsd.org> |
* The Regents of the University of California. All rights reserved. |
|
* |
* |
* Redistribution and use in source and binary forms, with or without |
* Permission to use, copy, modify, and distribute this software for any |
* modification, are permitted provided that the following conditions |
* purpose with or without fee is hereby granted, provided that the above |
* are met: |
* copyright notice and this permission notice appear in all copies. |
* 1. Redistributions of source code must retain the above copyright |
|
* notice, this list of conditions and the following disclaimer. |
|
* 2. Redistributions in binary form must reproduce the above copyright |
|
* notice, this list of conditions and the following disclaimer in the |
|
* documentation and/or other materials provided with the distribution. |
|
* 3. Neither the name of the University nor the names of its contributors |
|
* may be used to endorse or promote products derived from this software |
|
* without specific prior written permission. |
|
* |
* |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
|
* SUCH DAMAGE. |
|
*/ |
*/ |
|
|
#ifndef lint |
|
#if 0 |
|
static char sccsid[] = "@(#)hertz.c 8.1 (Berkeley) 6/6/93"; |
|
#else |
|
static char rcsid[] = "$OpenBSD$"; |
|
#endif |
|
#endif /* not lint */ |
|
|
|
|
#include <sys/param.h> |
#include <sys/time.h> |
#include <sys/time.h> |
|
#include <sys/sysctl.h> |
|
|
/* |
/* |
* discover the tick frequency of the machine |
* Return the tick frequency on the machine or 0 if we can't find out. |
* if something goes wrong, we return 0, an impossible hertz. |
*/ |
*/ |
|
#define HZ_WRONG 0 |
|
|
|
int |
int |
hertz() |
hertz(void) |
{ |
{ |
struct itimerval tim; |
struct clockinfo cinfo; |
|
int mib[2]; |
|
size_t len; |
|
|
tim.it_interval.tv_sec = 0; |
mib[0] = CTL_KERN; |
tim.it_interval.tv_usec = 1; |
mib[1] = KERN_CLOCKRATE; |
tim.it_value.tv_sec = 0; |
len = sizeof(cinfo); |
tim.it_value.tv_usec = 0; |
if (sysctl(mib, 2, &cinfo, &len, NULL, 0) == -1) |
setitimer(ITIMER_REAL, &tim, 0); |
return (0); |
setitimer(ITIMER_REAL, 0, &tim); |
|
if (tim.it_interval.tv_usec < 2) |
return (cinfo.hz); |
return(HZ_WRONG); |
|
return (1000000 / tim.it_interval.tv_usec); |
|
} |
} |