• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
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 at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * 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 and
13  * limitations under the License.
14  */
15 #ifndef UTIL_TIME_FORMAT_H
16 #define UTIL_TIME_FORMAT_H
17 
18 #include <chrono>
19 #include <string>
20 #include <securec.h>
21 
22 namespace ffrt {
23 typedef enum {
24     millisecond,
25     microsecond,
26 } time_unit_t;
27 
28 static std::string FormatDateString4SystemClock(const std::chrono::system_clock::time_point& timePoint,
29     time_unit_t timeUnit = millisecond)
30 {
31     constexpr int MaxMsLength = 3;
32     constexpr int MsPerSecond = 1000;
33     constexpr int DatetimeStringLength = 80;
34     constexpr int MaxUsLength = 6;
35     constexpr int UsPerSecond = 1000 * 1000;
36 
37     std::string remainder;
38     if (microsecond == timeUnit) {
39         auto tp = std::chrono::time_point_cast<std::chrono::microseconds>(timePoint);
40         auto us = tp.time_since_epoch().count() % UsPerSecond;
41         remainder = std::to_string(us);
42         if (remainder.length() < MaxUsLength) {
43             remainder = std::string(MaxUsLength - remainder.length(), '0') + remainder;
44         }
45     } else {
46         auto tp = std::chrono::time_point_cast<std::chrono::milliseconds>(timePoint);
47         auto ms = tp.time_since_epoch().count() % MsPerSecond;
48         remainder = std::to_string(ms);
49         if (remainder.length() < MaxMsLength) {
50             remainder = std::string(MaxMsLength - remainder.length(), '0') + remainder;
51         }
52     }
53     auto tt = std::chrono::system_clock::to_time_t(timePoint);
54     struct tm curTime;
55     if (memset_s(&curTime, sizeof(curTime), 0, sizeof(curTime)) != EOK) {
56         FFRT_LOGE("Fail to memset");
57         return "";
58     }
59     localtime_r(&tt, &curTime);
60     char sysTime[DatetimeStringLength];
61     std::strftime(sysTime, sizeof(char) * DatetimeStringLength, "%Y-%m-%d %H:%M:%S.", &curTime);
62     return std::string(sysTime) + remainder;
63 }
64 
65 static std::string FormatDateString4SteadyClock(uint64_t steadyClockTimeStamp, time_unit_t timeUnit = millisecond)
66 {
67     auto referenceTimeStamp = std::chrono::duration_cast<std::chrono::microseconds>(
68         std::chrono::steady_clock::now().time_since_epoch()).count();
69     auto referenceTp = std::chrono::system_clock::now();
70 
71     std::chrono::microseconds us(static_cast<int64_t>(steadyClockTimeStamp - referenceTimeStamp));
72     return FormatDateString4SystemClock(referenceTp + us, timeUnit);
73 }
74 
75 #if defined(__aarch64__)
76 
Arm64CntFrq(void)77 static inline uint64_t Arm64CntFrq(void)
78 {
79     uint64_t freq = 1;
80     asm volatile("mrs %0, cntfrq_el0" : "=r" (freq));
81     return freq;
82 }
83 
Arm64CntCt(void)84 static inline uint64_t Arm64CntCt(void)
85 {
86     uint64_t tsc = 1;
87     asm volatile("mrs %0, cntvct_el0" : "=r" (tsc));
88     return tsc;
89 }
90 
91 static std::string FormatDateString4CntCt(uint64_t cntCtTimeStamp, time_unit_t timeUnit = millisecond)
92 {
93     constexpr int Ratio = 1000 * 1000;
94 
95     auto referenceFreq = Arm64CntFrq();
96     if (referenceFreq == 0) {
97         return "";
98     }
99     uint64_t referenceCntCt = Arm64CntCt();
100     auto globalTp = std::chrono::system_clock::now();
101     std::chrono::microseconds us(static_cast<int64_t>(cntCtTimeStamp - referenceCntCt) * Ratio / referenceFreq);
102     return FormatDateString4SystemClock(globalTp + us, timeUnit);
103 }
104 #endif //  __aarch64__
105 }
106 #endif // UTIL_TIME_FORAMT_H
107