xref: /minix3/minix/lib/libsys/sys_hz.c (revision 433d6423c39e34ec4b79c950597bb2d236f886be)
1*433d6423SLionel Sambuc 
2*433d6423SLionel Sambuc #include <stdio.h>
3*433d6423SLionel Sambuc #include <sys/time.h>
4*433d6423SLionel Sambuc #include <sys/types.h>
5*433d6423SLionel Sambuc #include <minix/u64.h>
6*433d6423SLionel Sambuc #include <minix/config.h>
7*433d6423SLionel Sambuc #include <minix/const.h>
8*433d6423SLionel Sambuc 
9*433d6423SLionel Sambuc #include "sysutil.h"
10*433d6423SLionel Sambuc 
11*433d6423SLionel Sambuc static u32_t Hz;
12*433d6423SLionel Sambuc 
13*433d6423SLionel Sambuc u32_t
sys_hz(void)14*433d6423SLionel Sambuc sys_hz(void)
15*433d6423SLionel Sambuc {
16*433d6423SLionel Sambuc 	if(Hz <= 0) {
17*433d6423SLionel Sambuc 		int r;
18*433d6423SLionel Sambuc 		/* Get HZ. */
19*433d6423SLionel Sambuc 		if((r=sys_getinfo(GET_HZ, &Hz, sizeof(Hz), 0, 0)) != OK) {
20*433d6423SLionel Sambuc 			Hz = DEFAULT_HZ;
21*433d6423SLionel Sambuc 			printf("sys_hz: can not get HZ: error %d.\nUsing default HZ = %u\n",
22*433d6423SLionel Sambuc 			    r, (unsigned int) Hz);
23*433d6423SLionel Sambuc 		}
24*433d6423SLionel Sambuc 	}
25*433d6423SLionel Sambuc 
26*433d6423SLionel Sambuc 	return Hz;
27*433d6423SLionel Sambuc }
28*433d6423SLionel Sambuc 
29*433d6423SLionel Sambuc u32_t
micros_to_ticks(u32_t micros)30*433d6423SLionel Sambuc micros_to_ticks(u32_t micros)
31*433d6423SLionel Sambuc {
32*433d6423SLionel Sambuc         u32_t ticks;
33*433d6423SLionel Sambuc 
34*433d6423SLionel Sambuc         ticks = (u32_t)(((u64_t)micros * sys_hz()) / 1000000);
35*433d6423SLionel Sambuc         if(ticks < 1) ticks = 1;
36*433d6423SLionel Sambuc 
37*433d6423SLionel Sambuc         return ticks;
38*433d6423SLionel Sambuc }
39*433d6423SLionel Sambuc 
40