1e5c31af7Sopenharmony_ci// Copyright 2019 The Amber Authors. 2e5c31af7Sopenharmony_ci// 3e5c31af7Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License"); 4e5c31af7Sopenharmony_ci// you may not use this file except in compliance with the License. 5e5c31af7Sopenharmony_ci// You may obtain a copy of the License at 6e5c31af7Sopenharmony_ci// 7e5c31af7Sopenharmony_ci// http://www.apache.org/licenses/LICENSE-2.0 8e5c31af7Sopenharmony_ci// 9e5c31af7Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software 10e5c31af7Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS, 11e5c31af7Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12e5c31af7Sopenharmony_ci// See the License for the specific language governing permissions and 13e5c31af7Sopenharmony_ci// limitations under the License. 14e5c31af7Sopenharmony_ci 15e5c31af7Sopenharmony_ci#include "samples/timestamp.h" 16e5c31af7Sopenharmony_ci 17e5c31af7Sopenharmony_ci#include <cassert> 18e5c31af7Sopenharmony_ci 19e5c31af7Sopenharmony_ci#if defined(_WIN32) || defined(_WIN64) 20e5c31af7Sopenharmony_ci#define SAMPLE_PLATFORM_WINDOWS 1 21e5c31af7Sopenharmony_ci#define SAMPLE_PLATFORM_POSIX 0 22e5c31af7Sopenharmony_ci#elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) 23e5c31af7Sopenharmony_ci#define SAMPLE_PLATFORM_POSIX 1 24e5c31af7Sopenharmony_ci#define SAMPLE_PLATFORM_WINDOWS 0 25e5c31af7Sopenharmony_ci#endif 26e5c31af7Sopenharmony_ci 27e5c31af7Sopenharmony_ci#if SAMPLE_PLATFORM_WINDOWS 28e5c31af7Sopenharmony_ci#include <windows.h> 29e5c31af7Sopenharmony_ci#elif SAMPLE_PLATFORM_POSIX 30e5c31af7Sopenharmony_ci#include <time.h> 31e5c31af7Sopenharmony_ci#else 32e5c31af7Sopenharmony_ci#error "Unknown platform" 33e5c31af7Sopenharmony_ci#endif 34e5c31af7Sopenharmony_ci 35e5c31af7Sopenharmony_cinamespace timestamp { 36e5c31af7Sopenharmony_ci 37e5c31af7Sopenharmony_ciuint64_t SampleGetTimestampNs() { 38e5c31af7Sopenharmony_ci uint64_t timestamp = 0; 39e5c31af7Sopenharmony_ci 40e5c31af7Sopenharmony_ci#if SAMPLE_PLATFORM_WINDOWS 41e5c31af7Sopenharmony_ci 42e5c31af7Sopenharmony_ci LARGE_INTEGER tick_per_seconds; 43e5c31af7Sopenharmony_ci if (!QueryPerformanceFrequency(&tick_per_seconds)) { 44e5c31af7Sopenharmony_ci return 0; 45e5c31af7Sopenharmony_ci } 46e5c31af7Sopenharmony_ci LARGE_INTEGER ticks; 47e5c31af7Sopenharmony_ci if (!QueryPerformanceCounter(&ticks)) { 48e5c31af7Sopenharmony_ci return 0; 49e5c31af7Sopenharmony_ci } 50e5c31af7Sopenharmony_ci double tick_duration_ns = static_cast<double>(1.0e9) / 51e5c31af7Sopenharmony_ci static_cast<double>(tick_per_seconds.QuadPart); 52e5c31af7Sopenharmony_ci timestamp = uint64_t(static_cast<double>(ticks.QuadPart) * tick_duration_ns); 53e5c31af7Sopenharmony_ci 54e5c31af7Sopenharmony_ci#elif SAMPLE_PLATFORM_POSIX 55e5c31af7Sopenharmony_ci 56e5c31af7Sopenharmony_ci struct timespec time; 57e5c31af7Sopenharmony_ci if (clock_gettime(CLOCK_MONOTONIC, &time)) { 58e5c31af7Sopenharmony_ci return 0; 59e5c31af7Sopenharmony_ci } 60e5c31af7Sopenharmony_ci timestamp = static_cast<uint64_t>((time.tv_sec * 1000000000) + time.tv_nsec); 61e5c31af7Sopenharmony_ci 62e5c31af7Sopenharmony_ci#else 63e5c31af7Sopenharmony_ci#error "Implement timestamp::SampleGetTimestampNs" 64e5c31af7Sopenharmony_ci#endif 65e5c31af7Sopenharmony_ci 66e5c31af7Sopenharmony_ci return timestamp; 67e5c31af7Sopenharmony_ci} 68e5c31af7Sopenharmony_ci 69e5c31af7Sopenharmony_ci} // namespace timestamp 70