Path: blob/21.2-virgl/include/android_stub/log/log_time.h
4547 views
/*1* Copyright (C) 2005-2017 The Android Open Source Project2*3* Licensed under the Apache License, Version 2.0 (the "License");4* you may not use this file except in compliance with the License.5* You may obtain a copy of the License at6*7* http://www.apache.org/licenses/LICENSE-2.08*9* Unless required by applicable law or agreed to in writing, software10* distributed under the License is distributed on an "AS IS" BASIS,11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12* See the License for the specific language governing permissions and13* limitations under the License.14*/1516#pragma once1718#include <stdint.h>19#include <time.h>2021/* struct log_time is a wire-format variant of struct timespec */22#define NS_PER_SEC 1000000000ULL23#define US_PER_SEC 1000000ULL24#define MS_PER_SEC 1000ULL2526#define LOG_TIME_SEC(t) ((t)->tv_sec)27/* next power of two after NS_PER_SEC */28#define LOG_TIME_NSEC(t) ((t)->tv_nsec & (UINT32_MAX >> 2))2930#ifdef __cplusplus3132extern "C" {3334struct log_time {35public:36uint32_t tv_sec = 0; /* good to Feb 5 2106 */37uint32_t tv_nsec = 0;3839static constexpr timespec EPOCH = {0, 0};4041log_time() {}42explicit log_time(const timespec& T)43: tv_sec(static_cast<uint32_t>(T.tv_sec)), tv_nsec(static_cast<uint32_t>(T.tv_nsec)) {}44explicit log_time(uint32_t sec, uint32_t nsec = 0)45: tv_sec(sec), tv_nsec(nsec) {46}47#ifdef __linux__48explicit log_time(clockid_t id) {49timespec T;50clock_gettime(id, &T);51tv_sec = static_cast<uint32_t>(T.tv_sec);52tv_nsec = static_cast<uint32_t>(T.tv_nsec);53}54#endif55/* timespec */56bool operator==(const timespec& T) const {57return (tv_sec == static_cast<uint32_t>(T.tv_sec)) &&58(tv_nsec == static_cast<uint32_t>(T.tv_nsec));59}60bool operator!=(const timespec& T) const {61return !(*this == T);62}63bool operator<(const timespec& T) const {64return (tv_sec < static_cast<uint32_t>(T.tv_sec)) ||65((tv_sec == static_cast<uint32_t>(T.tv_sec)) &&66(tv_nsec < static_cast<uint32_t>(T.tv_nsec)));67}68bool operator>=(const timespec& T) const {69return !(*this < T);70}71bool operator>(const timespec& T) const {72return (tv_sec > static_cast<uint32_t>(T.tv_sec)) ||73((tv_sec == static_cast<uint32_t>(T.tv_sec)) &&74(tv_nsec > static_cast<uint32_t>(T.tv_nsec)));75}76bool operator<=(const timespec& T) const {77return !(*this > T);78}7980/* log_time */81bool operator==(const log_time& T) const {82return (tv_sec == T.tv_sec) && (tv_nsec == T.tv_nsec);83}84bool operator!=(const log_time& T) const {85return !(*this == T);86}87bool operator<(const log_time& T) const {88return (tv_sec < T.tv_sec) ||89((tv_sec == T.tv_sec) && (tv_nsec < T.tv_nsec));90}91bool operator>=(const log_time& T) const {92return !(*this < T);93}94bool operator>(const log_time& T) const {95return (tv_sec > T.tv_sec) ||96((tv_sec == T.tv_sec) && (tv_nsec > T.tv_nsec));97}98bool operator<=(const log_time& T) const {99return !(*this > T);100}101102log_time operator-=(const log_time& T) {103// No concept of negative time, clamp to EPOCH104if (*this <= T) {105return *this = log_time(EPOCH);106}107108if (this->tv_nsec < T.tv_nsec) {109--this->tv_sec;110this->tv_nsec = NS_PER_SEC + this->tv_nsec - T.tv_nsec;111} else {112this->tv_nsec -= T.tv_nsec;113}114this->tv_sec -= T.tv_sec;115116return *this;117}118log_time operator-(const log_time& T) const {119log_time local(*this);120return local -= T;121}122log_time operator+=(const log_time& T) {123this->tv_nsec += T.tv_nsec;124if (this->tv_nsec >= NS_PER_SEC) {125this->tv_nsec -= NS_PER_SEC;126++this->tv_sec;127}128this->tv_sec += T.tv_sec;129130return *this;131}132log_time operator+(const log_time& T) const {133log_time local(*this);134return local += T;135}136137uint64_t nsec() const {138return static_cast<uint64_t>(tv_sec) * NS_PER_SEC + tv_nsec;139}140uint64_t usec() const {141return static_cast<uint64_t>(tv_sec) * US_PER_SEC +142tv_nsec / (NS_PER_SEC / US_PER_SEC);143}144uint64_t msec() const {145return static_cast<uint64_t>(tv_sec) * MS_PER_SEC +146tv_nsec / (NS_PER_SEC / MS_PER_SEC);147}148149/* Add %#q for the fraction of a second to the standard library functions */150char* strptime(const char* s, const char* format);151} __attribute__((__packed__));152}153154#else /* __cplusplus */155156typedef struct log_time {157uint32_t tv_sec;158uint32_t tv_nsec;159} __attribute__((__packed__)) log_time;160161#endif /* __cplusplus */162163164