blob: a02c9d64e2758f66009a39b4d123e07c6b8af941 [file] [log] [blame]
Bruce Dawson2af6ef72018-05-01 15:28:39 +01001/*
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 Seckler83dcc8c2019-08-21 12:18:43 +010017#include "perfetto/base/time.h"
Primiano Tuccib730b112020-12-01 14:56:11 +010018
Bruce Dawson2af6ef72018-05-01 15:28:39 +010019#include "perfetto/base/build_config.h"
Primiano Tuccib730b112020-12-01 14:56:11 +010020#include "perfetto/base/logging.h"
Bruce Dawson2af6ef72018-05-01 15:28:39 +010021
22#if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
23#include <Windows.h>
Bruce Dawson21c1a0d2018-05-18 12:01:48 +010024#else
25#include <unistd.h>
26#endif
Bruce Dawson2af6ef72018-05-01 15:28:39 +010027
28namespace perfetto {
29namespace base {
30
Bruce Dawson21c1a0d2018-05-18 12:01:48 +010031#if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
32
Bruce Dawson2af6ef72018-05-01 15:28:39 +010033TimeNanos GetWallTimeNs() {
34 LARGE_INTEGER freq;
35 ::QueryPerformanceFrequency(&freq);
36 LARGE_INTEGER counter;
37 ::QueryPerformanceCounter(&counter);
Primiano Tucci42433ab2020-11-30 18:42:01 +010038 double elapsed_nanoseconds = (1e9 * static_cast<double>(counter.QuadPart)) /
39 static_cast<double>(freq.QuadPart);
Bruce Dawson2af6ef72018-05-01 15:28:39 +010040 return TimeNanos(static_cast<uint64_t>(elapsed_nanoseconds));
41}
42
43TimeNanos GetThreadCPUTimeNs() {
44 FILETIME dummy, kernel_ftime, user_ftime;
45 ::GetThreadTimes(GetCurrentThread(), &dummy, &dummy, &kernel_ftime,
46 &user_ftime);
Hector Dearman859d8f82022-05-10 15:13:45 +010047 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 Dawson2af6ef72018-05-01 15:28:39 +010051
52 return TimeNanos((kernel_time + user_time) * 100);
53}
54
Bruce Dawson21c1a0d2018-05-18 12:01:48 +010055void 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
65void SleepMicroseconds(unsigned interval_us) {
66 ::usleep(static_cast<useconds_t>(interval_us));
67}
Bruce Dawson2af6ef72018-05-01 15:28:39 +010068
69#endif // PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
Bruce Dawson21c1a0d2018-05-18 12:01:48 +010070
Primiano Tuccib730b112020-12-01 14:56:11 +010071std::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 Dawson21c1a0d2018-05-18 12:01:48 +010081} // namespace base
82} // namespace perfetto