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