xref: /dragonfly/lib/libc/upmap/ukp_gettimeofday.c (revision b227f3f5)
1*b227f3f5SMatthew Dillon /*
2*b227f3f5SMatthew Dillon  * Copyright (c) 2018 The DragonFly Project.  All rights reserved.
3*b227f3f5SMatthew Dillon  *
4*b227f3f5SMatthew Dillon  * This code is derived from software contributed to The DragonFly Project
5*b227f3f5SMatthew Dillon  * by Matthew Dillon <dillon@backplane.com>
6*b227f3f5SMatthew Dillon  *
7*b227f3f5SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
8*b227f3f5SMatthew Dillon  * modification, are permitted provided that the following conditions
9*b227f3f5SMatthew Dillon  * are met:
10*b227f3f5SMatthew Dillon  *
11*b227f3f5SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
12*b227f3f5SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
13*b227f3f5SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
14*b227f3f5SMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
15*b227f3f5SMatthew Dillon  *    the documentation and/or other materials provided with the
16*b227f3f5SMatthew Dillon  *    distribution.
17*b227f3f5SMatthew Dillon  * 3. Neither the name of The DragonFly Project nor the names of its
18*b227f3f5SMatthew Dillon  *    contributors may be used to endorse or promote products derived
19*b227f3f5SMatthew Dillon  *    from this software without specific, prior written permission.
20*b227f3f5SMatthew Dillon  *
21*b227f3f5SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*b227f3f5SMatthew Dillon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*b227f3f5SMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24*b227f3f5SMatthew Dillon  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25*b227f3f5SMatthew Dillon  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26*b227f3f5SMatthew Dillon  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27*b227f3f5SMatthew Dillon  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28*b227f3f5SMatthew Dillon  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29*b227f3f5SMatthew Dillon  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30*b227f3f5SMatthew Dillon  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31*b227f3f5SMatthew Dillon  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*b227f3f5SMatthew Dillon  * SUCH DAMAGE.
33*b227f3f5SMatthew Dillon  */
34*b227f3f5SMatthew Dillon 
35*b227f3f5SMatthew Dillon /*
36*b227f3f5SMatthew Dillon  * UKP-Optimized gettimeofday().  Use the kpmap after the 10th call.
37*b227f3f5SMatthew Dillon  */
38*b227f3f5SMatthew Dillon #include <sys/cdefs.h>
39*b227f3f5SMatthew Dillon #include <sys/types.h>
40*b227f3f5SMatthew Dillon #include <sys/syscall.h>
41*b227f3f5SMatthew Dillon #include <sys/upmap.h>
42*b227f3f5SMatthew Dillon #include <sys/time.h>
43*b227f3f5SMatthew Dillon #include <machine/cpufunc.h>
44*b227f3f5SMatthew Dillon #include <errno.h>
45*b227f3f5SMatthew Dillon #include <time.h>
46*b227f3f5SMatthew Dillon #include <stdio.h>
47*b227f3f5SMatthew Dillon #include <unistd.h>
48*b227f3f5SMatthew Dillon /*#include "un-namespace.h"*/
49*b227f3f5SMatthew Dillon #include "libc_private.h"
50*b227f3f5SMatthew Dillon #include "upmap.h"
51*b227f3f5SMatthew Dillon 
52*b227f3f5SMatthew Dillon extern int __sys_gettimeofday(struct timeval *, struct timezone *);
53*b227f3f5SMatthew Dillon int __gettimeofday(struct timeval *, struct timezone *);
54*b227f3f5SMatthew Dillon 
55*b227f3f5SMatthew Dillon static int fast_clock;
56*b227f3f5SMatthew Dillon static int fast_count;
57*b227f3f5SMatthew Dillon static int *fast_gtod;
58*b227f3f5SMatthew Dillon static int *upticksp;
59*b227f3f5SMatthew Dillon static struct timespec *ts_realtime;
60*b227f3f5SMatthew Dillon 
61*b227f3f5SMatthew Dillon #ifdef KPTYPE_FAST_GTOD
62*b227f3f5SMatthew Dillon int
__gettimeofday(struct timeval * tv,struct timezone * tz)63*b227f3f5SMatthew Dillon __gettimeofday(struct timeval *tv, struct timezone *tz)
64*b227f3f5SMatthew Dillon {
65*b227f3f5SMatthew Dillon 	struct timespec ts;
66*b227f3f5SMatthew Dillon 	int res;
67*b227f3f5SMatthew Dillon 	int w;
68*b227f3f5SMatthew Dillon 
69*b227f3f5SMatthew Dillon 	if (fast_clock == 0 && fast_count++ >= 10) {
70*b227f3f5SMatthew Dillon                 __kpmap_map(&upticksp, &fast_clock, KPTYPE_UPTICKS);
71*b227f3f5SMatthew Dillon 		__kpmap_map(&ts_realtime, &fast_clock, KPTYPE_TS_REALTIME);
72*b227f3f5SMatthew Dillon 		__kpmap_map(&fast_gtod, &fast_clock, KPTYPE_FAST_GTOD);
73*b227f3f5SMatthew Dillon 		__kpmap_map(NULL, &fast_clock, 0);
74*b227f3f5SMatthew Dillon 	}
75*b227f3f5SMatthew Dillon 	if (fast_clock > 0 && *fast_gtod && tz == NULL) {
76*b227f3f5SMatthew Dillon 		do {
77*b227f3f5SMatthew Dillon 			w = *upticksp;
78*b227f3f5SMatthew Dillon 			cpu_lfence();
79*b227f3f5SMatthew Dillon 			ts = ts_realtime[w & 1];
80*b227f3f5SMatthew Dillon 			cpu_lfence();
81*b227f3f5SMatthew Dillon 			w = *upticksp - w;
82*b227f3f5SMatthew Dillon 		} while (w > 1);
83*b227f3f5SMatthew Dillon 		res = 0;
84*b227f3f5SMatthew Dillon 		if (tv) {
85*b227f3f5SMatthew Dillon 			tv->tv_sec = ts.tv_sec;
86*b227f3f5SMatthew Dillon 			tv->tv_usec = ts.tv_nsec / 1000;
87*b227f3f5SMatthew Dillon 		}
88*b227f3f5SMatthew Dillon 	} else {
89*b227f3f5SMatthew Dillon 		res = __sys_gettimeofday(tv, tz);
90*b227f3f5SMatthew Dillon 	}
91*b227f3f5SMatthew Dillon 	return res;
92*b227f3f5SMatthew Dillon }
93*b227f3f5SMatthew Dillon 
94*b227f3f5SMatthew Dillon #else
95*b227f3f5SMatthew Dillon 
96*b227f3f5SMatthew Dillon int
__gettimeofday(struct timeval * tv,struct timezone * tz)97*b227f3f5SMatthew Dillon __gettimeofday(struct timeval *tv, struct timezone *tz)
98*b227f3f5SMatthew Dillon {
99*b227f3f5SMatthew Dillon 	int res;
100*b227f3f5SMatthew Dillon 
101*b227f3f5SMatthew Dillon 	res = __sys_gettimeofday(tv, tz);
102*b227f3f5SMatthew Dillon 	return res;
103*b227f3f5SMatthew Dillon }
104*b227f3f5SMatthew Dillon 
105*b227f3f5SMatthew Dillon #endif
106*b227f3f5SMatthew Dillon 
107*b227f3f5SMatthew Dillon __weak_reference(__gettimeofday, gettimeofday);
108