]> git.proxmox.com Git - mirror_spl-debian.git/blame - module/spl/spl-time.c
Imported Upstream version 0.6.1
[mirror_spl-debian.git] / module / spl / spl-time.c
CommitLineData
716154c5
BB
1/*****************************************************************************\
2 * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3 * Copyright (C) 2007 The Regents of the University of California.
4 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
5 * Written by Brian Behlendorf <behlendorf1@llnl.gov>.
715f6251 6 * UCRL-CODE-235197
7 *
716154c5 8 * This file is part of the SPL, Solaris Porting Layer.
d956cfac 9 * For details, see <http://zfsonlinux.org/>.
716154c5
BB
10 *
11 * The SPL is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
715f6251 15 *
716154c5 16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
715f6251 17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
716154c5
BB
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 *****************************************************************************
24 * Solaris Porting Layer (SPL) Time Implementation.
25\*****************************************************************************/
715f6251 26
ee476682 27#include <sys/sysmacros.h>
28#include <sys/time.h>
ee476682 29
3d061e9d 30#ifdef HAVE_MONOTONIC_CLOCK
31extern unsigned long long monotonic_clock(void);
32#endif
33
937879f1 34#ifdef DEBUG_SUBSYSTEM
35#undef DEBUG_SUBSYSTEM
36#endif
37
38#define DEBUG_SUBSYSTEM S_TIME
39
ee476682 40void
41__gethrestime(timestruc_t *ts)
42{
3d061e9d 43 struct timeval tv;
79b31f36 44
3d061e9d 45 do_gettimeofday(&tv);
46 ts->tv_sec = tv.tv_sec;
47 ts->tv_nsec = tv.tv_usec * NSEC_PER_USEC;
79b31f36 48}
3d061e9d 49EXPORT_SYMBOL(__gethrestime);
79b31f36 50
3d061e9d 51/* Use monotonic_clock() by default. It's faster and is available on older
52 * kernels, but few architectures have them, so we must fallback to
53 * do_posix_clock_monotonic_gettime().
79b31f36 54 */
55hrtime_t
56__gethrtime(void) {
3d061e9d 57#ifdef HAVE_MONOTONIC_CLOCK
b07335c1 58 unsigned long long res = monotonic_clock();
3d061e9d 59
b07335c1 60 /* Deal with signed/unsigned mismatch */
61 return (hrtime_t)(res & ~(1ULL << 63));
3d061e9d 62#else
6020190e 63 struct timespec ts;
79b31f36 64
6020190e 65 do_posix_clock_monotonic_gettime(&ts);
8a1c9a02 66 return (((hrtime_t)ts.tv_sec * NSEC_PER_SEC) + ts.tv_nsec);
3d061e9d 67#endif
79b31f36 68}
69EXPORT_SYMBOL(__gethrtime);
ff449ac4 70
6a6cafbe 71/* set_normalized_timespec() API changes
72 * 2.6.0 - 2.6.15: Inline function provided by linux/time.h
3d061e9d 73 * 2.6.16 - 2.6.25: Function prototype defined but not exported
6a6cafbe 74 * 2.6.26 - 2.6.x: Function defined and exported
ff449ac4 75 */
6a6cafbe 76#if !defined(HAVE_SET_NORMALIZED_TIMESPEC_INLINE) && \
77 !defined(HAVE_SET_NORMALIZED_TIMESPEC_EXPORT)
ff449ac4 78void
79set_normalized_timespec(struct timespec *ts, time_t sec, long nsec)
80{
81 while (nsec >= NSEC_PER_SEC) {
82 nsec -= NSEC_PER_SEC;
83 ++sec;
84 }
85 while (nsec < 0) {
86 nsec += NSEC_PER_SEC;
87 --sec;
88 }
89 ts->tv_sec = sec;
90 ts->tv_nsec = nsec;
91}
92EXPORT_SYMBOL(set_normalized_timespec);
6a6cafbe 93#endif