1 /*
2 * Copyright (c) 2022-2022 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 #include "time_sysevent.h"
17
18 #include <cinttypes>
19
20 #include "hisysevent.h"
21 #include "time_file_utils.h"
22 #include "ipc_skeleton.h"
23
24 namespace OHOS {
25 namespace MiscServices {
26 namespace {
27 using HiSysEventNameSpace = OHOS::HiviewDFX::HiSysEvent;
28 } // namespace
29
GetBundleOrProcessName()30 std::string GetBundleOrProcessName()
31 {
32 std::string bundleOrProcessName = TimeFileUtils::GetBundleNameByTokenID(IPCSkeleton::GetCallingTokenID());
33 if (bundleOrProcessName.empty()) {
34 bundleOrProcessName = TimeFileUtils::GetNameByPid(IPCSkeleton::GetCallingPid());
35 }
36 return bundleOrProcessName;
37 }
38
StatisticReporter(int32_t size,std::shared_ptr<TimerInfo> timer)39 void StatisticReporter(int32_t size, std::shared_ptr<TimerInfo> timer)
40 {
41 if (timer == nullptr) {
42 return;
43 }
44 int32_t callerUid = timer->uid;
45 int32_t callerPid = timer->pid;
46 std::string bundleOrProcessName = timer->bundleName;
47 std::string timerName = timer->name;
48 int32_t type = timer->type;
49 int64_t triggerTime = timer->whenElapsed.time_since_epoch().count();
50 auto interval = static_cast<uint64_t>(timer->repeatInterval.count());
51 struct HiSysEventParam params[] = {
52 {"CALLER_PID", HISYSEVENT_INT32, {.i32 = callerPid}, 0},
53 {"CALLER_UID", HISYSEVENT_INT32, {.i32 = callerUid}, 0},
54 {"BUNDLE_OR_PROCESS_NAME", HISYSEVENT_STRING, {.s = const_cast<char*>(bundleOrProcessName.c_str())}, 0},
55 {"TIMER_NAME", HISYSEVENT_STRING, {.s = const_cast<char*>(timerName.c_str())}, 0},
56 {"TIMER_SIZE", HISYSEVENT_INT32, {.i32 = size}, 0},
57 {"TIMER_TYPE", HISYSEVENT_INT32, {.i32 = type}, 0},
58 {"TRIGGER_TIME", HISYSEVENT_INT64, {.i64 = triggerTime}, 0},
59 {"INTERVAL", HISYSEVENT_UINT64, {.ui64 = interval}, 0}
60 };
61 int ret = OH_HiSysEvent_Write("TIME", "MISC_TIME_STATISTIC_REPORT", HISYSEVENT_STATISTIC, params,
62 sizeof(params)/sizeof(params[0]));
63 if (ret != 0) {
64 TIME_HILOGE(TIME_MODULE_SERVICE,
65 "hisysevent Statistic failed! pid %{public}d,uid %{public}d,timer type %{public}d", callerPid, callerUid,
66 type);
67 }
68 }
69
TimeBehaviorReport(ReportEventCode eventCode,const std::string & originTime,const std::string & newTime,int64_t ntpTime)70 void TimeBehaviorReport(ReportEventCode eventCode, const std::string &originTime, const std::string &newTime,
71 int64_t ntpTime)
72 {
73 std::string bundleOrProcessName = GetBundleOrProcessName();
74 struct HiSysEventParam params[] = {
75 {"EVENT_CODE", HISYSEVENT_INT32, {.i32 = eventCode}, 0},
76 {"CALLER_UID", HISYSEVENT_INT32, {.i32 = IPCSkeleton::GetCallingUid()}, 0},
77 {"CALLER_NAME", HISYSEVENT_STRING, {.s = const_cast<char*>(bundleOrProcessName.c_str())}, 0},
78 {"ORIGINAL_TIME", HISYSEVENT_STRING, {.s = const_cast<char*>(originTime.c_str())}, 0},
79 {"SET_TIME", HISYSEVENT_STRING, {.s = const_cast<char*>(newTime.c_str())}, 0},
80 {"NTP_TIME", HISYSEVENT_INT64, {.i64 = ntpTime}, 0}
81 };
82 int ret = OH_HiSysEvent_Write("TIME", "BEHAVIOR_TIME", HISYSEVENT_BEHAVIOR, params,
83 sizeof(params)/sizeof(params[0]));
84 if (ret != 0) {
85 TIME_HILOGE(TIME_MODULE_SERVICE, "TimeBehaviorReport failed! eventCode %{public}d, name:%{public}s,"
86 "ret:%{public}d", eventCode, bundleOrProcessName.c_str(), ret);
87 }
88 }
89
TimerBehaviorReport(std::shared_ptr<TimerInfo> timer,bool isStart)90 void TimerBehaviorReport(std::shared_ptr<TimerInfo> timer, bool isStart)
91 {
92 if (timer == nullptr) {
93 return;
94 }
95 int triggerOffset = isStart ? RTC_WAKEUP_EXACT_TIMER_START : RTC_WAKEUP_EXACT_TIMER_TRIGGER;
96 int exactOffset = (timer->windowLength == std::chrono::milliseconds::zero()) ? 0 : EXACT_OFFSET;
97 ReportEventCode eventCode = static_cast<ReportEventCode>(triggerOffset + timer->type + exactOffset);
98 auto bundleOrProcessName = timer->bundleName;
99 auto interval = static_cast<uint32_t>(timer->repeatInterval.count());
100 struct HiSysEventParam params[] = {
101 {"EVENT_CODE", HISYSEVENT_INT32, {.i32 = eventCode}, 0},
102 {"TIMER_ID", HISYSEVENT_UINT32, {.ui32 = timer->id}, 0},
103 {"TRIGGER_TIME", HISYSEVENT_INT64, {.i64 = timer->when.count()}, 0},
104 {"CALLER_UID", HISYSEVENT_INT32, {.i32 = timer->uid}, 0},
105 {"CALLER_NAME", HISYSEVENT_STRING, {.s = const_cast<char*>(bundleOrProcessName.c_str())}, 0},
106 {"INTERVAL", HISYSEVENT_UINT32, {.ui32 =interval}, 0}
107 };
108 int ret = OH_HiSysEvent_Write("TIME", "BEHAVIOR_TIMER", HISYSEVENT_BEHAVIOR, params,
109 sizeof(params)/sizeof(params[0]));
110 if (ret != 0) {
111 TIME_HILOGE(TIME_MODULE_SERVICE, "TimerBehaviorReport failed! eventCode:%{public}d,"
112 "id:%{public}" PRIu64 " name:%{public}s", static_cast<int32_t>(eventCode), timer->id,
113 bundleOrProcessName.c_str());
114 }
115 }
116
TimerCountStaticReporter(int count,int (& uidArr)[COUNT_REPORT_ARRAY_LENGTH],int (& createTimerCountArr)[COUNT_REPORT_ARRAY_LENGTH],int (& startTimerCountArr)[COUNT_REPORT_ARRAY_LENGTH])117 void TimerCountStaticReporter(int count, int (&uidArr)[COUNT_REPORT_ARRAY_LENGTH],
118 int (&createTimerCountArr)[COUNT_REPORT_ARRAY_LENGTH], int (&startTimerCountArr)[COUNT_REPORT_ARRAY_LENGTH])
119 {
120 struct HiSysEventParam params[] = {
121 {"TIMER_NUM", HISYSEVENT_INT32, {.i32 = count}, 0},
122 {"TOP_UID", HISYSEVENT_INT32_ARRAY, {.array = uidArr}, COUNT_REPORT_ARRAY_LENGTH},
123 {"TOP_NUM", HISYSEVENT_INT32_ARRAY, {.array = createTimerCountArr}, COUNT_REPORT_ARRAY_LENGTH},
124 {"TOP_STRAT_NUM", HISYSEVENT_INT32_ARRAY, {.array = startTimerCountArr}, COUNT_REPORT_ARRAY_LENGTH}
125 };
126 int ret = OH_HiSysEvent_Write("TIME", "ALARM_COUNT", HISYSEVENT_STATISTIC,
127 params, sizeof(params)/sizeof(params[0]));
128 if (ret != 0) {
129 std::string uidStr = "";
130 std::string createCountStr = "";
131 std::string startCountStr = "";
132 for (int i = 0; i < COUNT_REPORT_ARRAY_LENGTH; i++) {
133 uidStr = uidStr + std::to_string(uidArr[i]) + " ";
134 createCountStr = createCountStr + std::to_string(createTimerCountArr[i]) + " ";
135 startCountStr = startCountStr + std::to_string(startTimerCountArr[i]) + " ";
136 }
137 TIME_HILOGE(TIME_MODULE_SERVICE, "TimerCountStaticReporter failed! count:%{public}d, uid:[%{public}s],"
138 "create count:[%{public}s], startcount:[%{public}s]", count, uidStr.c_str(), createCountStr.c_str(),
139 startCountStr.c_str());
140 }
141 }
142
TimeServiceFaultReporter(ReportEventCode eventCode,int errCode,int uid,const std::string & bundleOrProcessName,const std::string & extraInfo)143 void TimeServiceFaultReporter(ReportEventCode eventCode, int errCode, int uid, const std::string &bundleOrProcessName,
144 const std::string &extraInfo)
145 {
146 struct HiSysEventParam params[] = {
147 {"EVENT_CODE", HISYSEVENT_INT32, {.i32 = eventCode}, 0},
148 {"ERR_CODE", HISYSEVENT_INT32, {.i32 = errCode}, 0},
149 {"CALLER_UID", HISYSEVENT_INT32, {.i32 = uid}, 0},
150 {"CALLER_NAME", HISYSEVENT_STRING, {.s = const_cast<char*>(bundleOrProcessName.c_str())}, 0},
151 {"EXTRA", HISYSEVENT_STRING, {.s = const_cast<char*>(extraInfo.c_str())}, 0}
152 };
153 int ret = OH_HiSysEvent_Write("TIME", "FUNC_FAULT", HISYSEVENT_FAULT,
154 params, sizeof(params)/sizeof(params[0]));
155 if (ret != 0) {
156 TIME_HILOGE(TIME_MODULE_SERVICE, "TimeServiceFaultReporter failed! eventCode:%{public}d errorcode:%{public}d"
157 "callname:%{public}s", eventCode, errCode, bundleOrProcessName.c_str());
158 }
159 }
160 } // namespace MiscServices
161 } // namespace OHOS