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