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 FormatDateString(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     static uint64_t globalTimeStamp = std::chrono::duration_cast<std::chrono::microseconds>(
68         std::chrono::steady_clock::now().time_since_epoch()).count();
69     static auto globalTp = std::chrono::system_clock::now();
70 
71     std::chrono::microseconds us((int64_t)(steadyClockTimeStamp - globalTimeStamp));
72     return FormatDateString(globalTp + us, timeUnit);
73 }
74 
Arm64CntFrq(void)75 static inline uint64_t Arm64CntFrq(void)
76 {
77     uint64_t freq = 1;
78     asm volatile("mrs %0, cntfrq_el0" : "=r" (freq));
79     return freq;
80 }
81 
Arm64CntCt(void)82 static inline uint64_t Arm64CntCt(void)
83 {
84     uint64_t tsc = 1;
85     asm volatile("mrs %0, cntvct_el0" : "=r" (tsc));
86     return tsc;
87 }
88 
89 static std::string FormatDateString4CntCt(uint64_t cntCtTimeStamp, time_unit_t timeUnit = millisecond)
90 {
91     constexpr int Ratio = 1000 * 1000;
92 
93     static int64_t globalFreq = Arm64CntFrq();
94     if (globalFreq == 0) {
95         return "";
96     }
97     static uint64_t globalCntCt = Arm64CntCt();
98     static auto globalTp = std::chrono::system_clock::now();
99     std::chrono::microseconds us((int64_t)(cntCtTimeStamp - globalCntCt) * Ratio / globalFreq);
100     return FormatDateString(globalTp + us, timeUnit);
101 }
102 }
103 #endif // UTIL_TIME_FORAMT_H
104