1129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel/* Copyright (c) 2014, The Linux Foundation. All rights reserved.
2129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel *
3129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel * Redistribution and use in source and binary forms, with or without
4129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel * modification, are permitted provided that the following conditions are
5129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel * met:
6129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel *     * Redistributions of source code must retain the above copyright
7129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel *       notice, this list of conditions and the following disclaimer.
8129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel *     * Redistributions in binary form must reproduce the above
9129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel *       copyright notice, this list of conditions and the following
10129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel *       disclaimer in the documentation and/or other materials provided
11129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel *       with the distribution.
12129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel *     * Neither the name of The Linux Foundation nor the names of its
13129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel *       contributors may be used to endorse or promote products derived
14129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel *       from this software without specific prior written permission.
15129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel *
16129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel */
28129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel
29129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel#include "loc_stub_time.h"
30129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel#include <stdlib.h>
31129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel#include <sys/time.h>
3263f8eb733a0dd9b2c3a0ae282e5c8b1adde7ef16Thierry Strudel#include <time.h>
33129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel
34129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudelint64_t systemTime(int clock)
35129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel{
36129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel    struct timeval t;
37129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel    t.tv_sec = t.tv_usec = 0;
38129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel    gettimeofday(&t, NULL);
39129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel    return t.tv_sec*1000000LL + t.tv_usec;
40129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel}
41129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel
4263f8eb733a0dd9b2c3a0ae282e5c8b1adde7ef16Thierry Strudelint64_t elapsedMicrosSinceBoot()
4363f8eb733a0dd9b2c3a0ae282e5c8b1adde7ef16Thierry Strudel{
4463f8eb733a0dd9b2c3a0ae282e5c8b1adde7ef16Thierry Strudel    struct timespec ts;
4563f8eb733a0dd9b2c3a0ae282e5c8b1adde7ef16Thierry Strudel    int64_t time_ms = 0;
4663f8eb733a0dd9b2c3a0ae282e5c8b1adde7ef16Thierry Strudel    clock_gettime(CLOCK_BOOTTIME, &ts);
4763f8eb733a0dd9b2c3a0ae282e5c8b1adde7ef16Thierry Strudel    time_ms += (ts.tv_sec * 1000000000LL);     /* Seconds to nanoseconds */
4863f8eb733a0dd9b2c3a0ae282e5c8b1adde7ef16Thierry Strudel    time_ms += ts.tv_nsec;   /* Add Nanoseconds  */
4963f8eb733a0dd9b2c3a0ae282e5c8b1adde7ef16Thierry Strudel    return time_ms;
5063f8eb733a0dd9b2c3a0ae282e5c8b1adde7ef16Thierry Strudel}
51129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel
52129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudelint64_t elapsedMillisSinceBoot()
53129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel{
5463f8eb733a0dd9b2c3a0ae282e5c8b1adde7ef16Thierry Strudel    return (int64_t) (elapsedMicrosSinceBoot() /1000000LL);
55129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel}
56