xref: /openbsd-src/usr.bin/gprof/hertz.c (revision b9fc9a728fce9c4289b7e9a992665e28d5629a54)
1*b9fc9a72Sderaadt /*	$OpenBSD: hertz.c,v 1.7 2015/01/16 06:40:08 deraadt Exp $	*/
2df930be7Sderaadt 
3df930be7Sderaadt /*
445901bb4Sart  * Copyright (c) 2005 Artur Grabowski <art@openbsd.org>
5df930be7Sderaadt  *
645901bb4Sart  * Permission to use, copy, modify, and distribute this software for any
745901bb4Sart  * purpose with or without fee is hereby granted, provided that the above
845901bb4Sart  * copyright notice and this permission notice appear in all copies.
9df930be7Sderaadt  *
1045901bb4Sart  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1145901bb4Sart  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1245901bb4Sart  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1345901bb4Sart  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1445901bb4Sart  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1545901bb4Sart  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1645901bb4Sart  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17df930be7Sderaadt  */
18df930be7Sderaadt 
19df930be7Sderaadt 
20df930be7Sderaadt #include <sys/time.h>
2145901bb4Sart #include <sys/sysctl.h>
22df930be7Sderaadt 
239bd74cdeSespie #include "gprof.h"
249bd74cdeSespie 
25df930be7Sderaadt /*
2645901bb4Sart  * Return the tick frequency on the machine or 0 if we can't find out.
27df930be7Sderaadt  */
28df930be7Sderaadt 
29d2559c65Smickey int
hertz(void)3045901bb4Sart hertz(void)
31df930be7Sderaadt {
3245901bb4Sart 	struct clockinfo cinfo;
3345901bb4Sart 	int mib[2];
3445901bb4Sart 	size_t len;
35df930be7Sderaadt 
3645901bb4Sart 	mib[0] = CTL_KERN;
3745901bb4Sart 	mib[1] = KERN_CLOCKRATE;
3845901bb4Sart 	len = sizeof(cinfo);
3945901bb4Sart 	if (sysctl(mib, 2, &cinfo, &len, NULL, 0) == -1)
4045901bb4Sart 		return (0);
4145901bb4Sart 
4245901bb4Sart 	return (cinfo.hz);
43df930be7Sderaadt }
44