Bruce Dawson | 2af6ef7 | 2018-05-01 15:28:39 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Eric Seckler | 83dcc8c | 2019-08-21 12:18:43 +0100 | [diff] [blame] | 17 | #include "perfetto/base/time.h" |
Primiano Tucci | b730b11 | 2020-12-01 14:56:11 +0100 | [diff] [blame] | 18 | |
Bruce Dawson | 2af6ef7 | 2018-05-01 15:28:39 +0100 | [diff] [blame] | 19 | #include "perfetto/base/build_config.h" |
Primiano Tucci | b730b11 | 2020-12-01 14:56:11 +0100 | [diff] [blame] | 20 | #include "perfetto/base/logging.h" |
Bruce Dawson | 2af6ef7 | 2018-05-01 15:28:39 +0100 | [diff] [blame] | 21 | |
| 22 | #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN) |
| 23 | #include <Windows.h> |
Bruce Dawson | 21c1a0d | 2018-05-18 12:01:48 +0100 | [diff] [blame] | 24 | #else |
| 25 | #include <unistd.h> |
| 26 | #endif |
Bruce Dawson | 2af6ef7 | 2018-05-01 15:28:39 +0100 | [diff] [blame] | 27 | |
| 28 | namespace perfetto { |
| 29 | namespace base { |
| 30 | |
Bruce Dawson | 21c1a0d | 2018-05-18 12:01:48 +0100 | [diff] [blame] | 31 | #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN) |
| 32 | |
Bruce Dawson | 2af6ef7 | 2018-05-01 15:28:39 +0100 | [diff] [blame] | 33 | TimeNanos GetWallTimeNs() { |
| 34 | LARGE_INTEGER freq; |
| 35 | ::QueryPerformanceFrequency(&freq); |
| 36 | LARGE_INTEGER counter; |
| 37 | ::QueryPerformanceCounter(&counter); |
Primiano Tucci | 42433ab | 2020-11-30 18:42:01 +0100 | [diff] [blame] | 38 | double elapsed_nanoseconds = (1e9 * static_cast<double>(counter.QuadPart)) / |
| 39 | static_cast<double>(freq.QuadPart); |
Bruce Dawson | 2af6ef7 | 2018-05-01 15:28:39 +0100 | [diff] [blame] | 40 | return TimeNanos(static_cast<uint64_t>(elapsed_nanoseconds)); |
| 41 | } |
| 42 | |
| 43 | TimeNanos GetThreadCPUTimeNs() { |
| 44 | FILETIME dummy, kernel_ftime, user_ftime; |
| 45 | ::GetThreadTimes(GetCurrentThread(), &dummy, &dummy, &kernel_ftime, |
| 46 | &user_ftime); |
Hector Dearman | 859d8f8 | 2022-05-10 15:13:45 +0100 | [diff] [blame] | 47 | uint64_t kernel_time = |
| 48 | kernel_ftime.dwHighDateTime * 0x100000000 + kernel_ftime.dwLowDateTime; |
| 49 | uint64_t user_time = |
| 50 | user_ftime.dwHighDateTime * 0x100000000 + user_ftime.dwLowDateTime; |
Bruce Dawson | 2af6ef7 | 2018-05-01 15:28:39 +0100 | [diff] [blame] | 51 | |
| 52 | return TimeNanos((kernel_time + user_time) * 100); |
| 53 | } |
| 54 | |
Bruce Dawson | 21c1a0d | 2018-05-18 12:01:48 +0100 | [diff] [blame] | 55 | void SleepMicroseconds(unsigned interval_us) { |
| 56 | // The Windows Sleep function takes a millisecond count. Round up so that |
| 57 | // short sleeps don't turn into a busy wait. Note that the sleep granularity |
| 58 | // on Windows can dynamically vary from 1 ms to ~16 ms, so don't count on this |
| 59 | // being a short sleep. |
| 60 | ::Sleep(static_cast<DWORD>((interval_us + 999) / 1000)); |
| 61 | } |
| 62 | |
| 63 | #else // PERFETTO_BUILDFLAG(PERFETTO_OS_WIN) |
| 64 | |
| 65 | void SleepMicroseconds(unsigned interval_us) { |
| 66 | ::usleep(static_cast<useconds_t>(interval_us)); |
| 67 | } |
Bruce Dawson | 2af6ef7 | 2018-05-01 15:28:39 +0100 | [diff] [blame] | 68 | |
| 69 | #endif // PERFETTO_BUILDFLAG(PERFETTO_OS_WIN) |
Bruce Dawson | 21c1a0d | 2018-05-18 12:01:48 +0100 | [diff] [blame] | 70 | |
Primiano Tucci | b730b11 | 2020-12-01 14:56:11 +0100 | [diff] [blame] | 71 | std::string GetTimeFmt(const std::string& fmt) { |
| 72 | time_t raw_time; |
| 73 | time(&raw_time); |
| 74 | struct tm* local_tm; |
| 75 | local_tm = localtime(&raw_time); |
| 76 | char buf[128]; |
| 77 | PERFETTO_CHECK(strftime(buf, 80, fmt.c_str(), local_tm) > 0); |
| 78 | return buf; |
| 79 | } |
| 80 | |
Bruce Dawson | 21c1a0d | 2018-05-18 12:01:48 +0100 | [diff] [blame] | 81 | } // namespace base |
| 82 | } // namespace perfetto |