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
17 #include "perfetto/base/time.h"
18
19 #include "perfetto/base/build_config.h"
20 #include "perfetto/base/logging.h"
21
22 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
23 #include <Windows.h>
24 #else
25 #include <unistd.h>
26 #endif
27
28 namespace perfetto {
29 namespace base {
30
31 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
32
GetWallTimeNs()33 TimeNanos GetWallTimeNs() {
34 LARGE_INTEGER freq;
35 ::QueryPerformanceFrequency(&freq);
36 LARGE_INTEGER counter;
37 ::QueryPerformanceCounter(&counter);
38 double elapsed_nanoseconds = (1e9 * static_cast<double>(counter.QuadPart)) /
39 static_cast<double>(freq.QuadPart);
40 return TimeNanos(static_cast<uint64_t>(elapsed_nanoseconds));
41 }
42
GetThreadCPUTimeNs()43 TimeNanos GetThreadCPUTimeNs() {
44 FILETIME dummy, kernel_ftime, user_ftime;
45 ::GetThreadTimes(GetCurrentThread(), &dummy, &dummy, &kernel_ftime,
46 &user_ftime);
47 uint64_t kernel_time = kernel_ftime.dwHighDateTime * 0x100000000 +
48 kernel_ftime.dwLowDateTime;
49 uint64_t user_time = user_ftime.dwHighDateTime * 0x100000000 +
50 user_ftime.dwLowDateTime;
51
52 return TimeNanos((kernel_time + user_time) * 100);
53 }
54
SleepMicroseconds(unsigned interval_us)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 }
68
69 #endif // PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
70
GetTimeFmt(const std::string & fmt)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
81 } // namespace base
82 } // namespace perfetto
83