1/** 2 * \file 3 * \brief Systime 4 */ 5 6/* 7 * Copyright (c) 2016, ETH Zurich. 8 * All rights reserved. 9 * 10 * This file is distributed under the terms in the attached LICENSE file. 11 * If you do not find this file, copies can be found by writing to: 12 * ETH Zurich D-INFK, Universitaetstrasse 6, CH-8092 Zurich. Attn: Systems Group. 13 */ 14 15#include <barrelfish/systime.h> 16 17/// Number of system ticks per one millisecond 18systime_t systime_frequency = 1; 19 20/// Convert nanoseconds to system ticks 21systime_t ns_to_systime(uint64_t nanoseconds) 22{ 23 uint64_t q, r; 24 25 q = nanoseconds / 1000000000; 26 r = nanoseconds % 1000000000; 27 28 // Adding half a tick to round properly 29 return q * systime_frequency + (r * systime_frequency + 500000000) / 1000000000; 30} 31 32/// Convert microseconds to system ticks 33systime_t us_to_systime(uint64_t microseconds) 34{ 35 uint64_t q, r; 36 37 q = microseconds / 1000000; 38 r = microseconds % 1000000; 39 40 // Adding half a tick to round properly 41 return q * systime_frequency + (r * systime_frequency + 500000) / 1000000; 42} 43 44/// Convert system ticks to nanoseconds 45uint64_t systime_to_ns(systime_t time) 46{ 47 systime_t q, r; 48 49 q = time / systime_frequency; 50 r = time % systime_frequency; 51 52 // Adding half a nanosecond to round properly 53 return q * 1000000000 + (r * 1000000000 + systime_frequency / 2) / systime_frequency; 54} 55 56/// Convert system ticks to microseconds 57uint64_t systime_to_us(systime_t time) 58{ 59 systime_t q, r; 60 61 q = time / systime_frequency; 62 r = time % systime_frequency; 63 64 // Adding half a microsecond to round properly 65 return q * 1000000 + (r * 1000000 + systime_frequency / 2) / systime_frequency; 66} 67