1 /*
2 * Copyright (c) 2021-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
16 #ifndef BASE_EVENTHANDLER_FRAMEWORKS_EVENTHANDLER_INCLUDE_EVENT_HANDLER_UTILS_H
17 #define BASE_EVENTHANDLER_FRAMEWORKS_EVENTHANDLER_INCLUDE_EVENT_HANDLER_UTILS_H
18
19 #include <cerrno>
20 #include <chrono>
21 #include <cstring>
22 #include <string>
23
24 #include "hitrace/trace.h"
25 #include "inner_event.h"
26
27 namespace OHOS {
28 namespace AppExecFwk {
29 inline const int64_t NANOSECONDS_PER_ONE_MILLISECOND = 1000000;
30 inline const int64_t NANOSECONDS_PER_ONE_SECOND = 1000000000;
31 inline const int32_t INFINITE_TIMEOUT = -1;
32 inline const uint8_t MAX_ERRORMSG_LEN = 128;
33
34 // Help to convert time point into delay time from now.
TimePointToTimeOut(const InnerEvent::TimePoint & when)35 static inline int64_t TimePointToTimeOut(const InnerEvent::TimePoint &when)
36 {
37 InnerEvent::TimePoint now = InnerEvent::Clock::now();
38 if (when <= now) {
39 return 0;
40 }
41
42 auto duration = when - now;
43 return std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count();
44 }
45
NanosecondsToTimeout(int64_t nanoseconds)46 static inline int32_t NanosecondsToTimeout(int64_t nanoseconds)
47 {
48 if (nanoseconds < 0) {
49 return INFINITE_TIMEOUT;
50 }
51
52 int64_t milliseconds = nanoseconds / NANOSECONDS_PER_ONE_MILLISECOND;
53 if ((nanoseconds % NANOSECONDS_PER_ONE_MILLISECOND) > 0) {
54 milliseconds += 1;
55 }
56
57 return (milliseconds > INT32_MAX) ? INT32_MAX : static_cast<int32_t>(milliseconds);
58 }
59
60 using HiTraceChain = OHOS::HiviewDFX::HiTraceChain;
61
AllowHiTraceOutPut(const std::shared_ptr<HiTraceId> & traceId,bool isSyncEvent)62 static inline bool AllowHiTraceOutPut(const std::shared_ptr<HiTraceId>& traceId, bool isSyncEvent)
63 {
64 if ((!traceId) || (!traceId->IsValid())) {
65 return false;
66 }
67 if ((!isSyncEvent) && (!traceId->IsFlagEnabled(HITRACE_FLAG_INCLUDE_ASYNC))) {
68 return false;
69 }
70 return true;
71 }
72
HiTracePointerOutPutEventId(const std::shared_ptr<HiTraceId> & spanId,const char * action,HiTraceTracepointType type,const InnerEvent::EventId & innerEventId)73 static inline void HiTracePointerOutPutEventId(const std::shared_ptr<HiTraceId> &spanId, const char *action,
74 HiTraceTracepointType type, const InnerEvent::EventId &innerEventId)
75 {
76 if (spanId == nullptr || action == nullptr) {
77 return;
78 }
79 if (innerEventId.index() == TYPE_U32_INDEX) {
80 HiTraceChain::Tracepoint(type, *spanId, "%s event, event id: %u", action, std::get<uint32_t>(innerEventId));
81 } else {
82 HiTraceChain::Tracepoint(
83 type, *spanId, "%s event, event id: %s", action, std::get<std::string>(innerEventId).c_str());
84 }
85 }
86
HiTracePointerOutPut(const std::shared_ptr<HiTraceId> & spanId,const InnerEvent::Pointer & event,const char * action,HiTraceTracepointType type)87 static inline void HiTracePointerOutPut(const std::shared_ptr<HiTraceId>& spanId,
88 const InnerEvent::Pointer& event, const char* action, HiTraceTracepointType type)
89 {
90 if (!event->HasTask()) {
91 auto eventId = event->GetInnerEventIdEx();
92 HiTracePointerOutPutEventId(spanId, action, type, eventId);
93 } else if (!event->GetTaskName().empty()) {
94 HiTraceChain::Tracepoint(type, *spanId, "%s task with name, name: %s", action, event->GetTaskName().c_str());
95 } else {
96 HiTraceChain::Tracepoint(type, *spanId, "%s UnNamed Task", action);
97 }
98 }
99
100 static inline void GetLastErr(char *errmsg, size_t size = MAX_ERRORMSG_LEN)
101 {
102 size = size > MAX_ERRORMSG_LEN ? MAX_ERRORMSG_LEN : size;
103 strerror_r(errno, errmsg, size);
104 }
105
GetTimeStamp()106 static inline int64_t GetTimeStamp()
107 {
108 InnerEvent::TimePoint now = InnerEvent::Clock::now();
109 auto epoch = now.time_since_epoch();
110 return std::chrono::duration_cast<std::chrono::nanoseconds>(epoch).count();
111 }
112 } // namespace AppExecFwk
113 } // namespace OHOS
114
115 #endif // #ifndef BASE_EVENTHANDLER_FRAMEWORKS_EVENTHANDLER_INCLUDE_EVENT_HANDLER_UTILS_H
116