1 /*
2  * Copyright (c) 2024 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 "hisysevent_adapter.h"
17 #include "accesstoken_log.h"
18 #include "hisysevent.h"
19 #include "time_util.h"
20 
21 namespace OHOS {
22 namespace Security {
23 namespace AccessToken {
24 namespace {
25 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "AtmDfx"};
26 static const std::string ACCESSTOKEN_PROCESS_NAME = "accesstoken_service";
27 static constexpr char ADD_DOMAIN[] = "PERFORMANCE";
28 }
29 
ReportSysEventPerformance()30 void ReportSysEventPerformance()
31 {
32     // accesstoken_service add CPU_SCENE_ENTRY system event in OnStart, avoid CPU statistics
33     long id = 1 << 0; // first scene
34     int64_t time = AccessToken::TimeUtil::GetCurrentTimestamp();
35 
36     int32_t ret = HiSysEventWrite(ADD_DOMAIN, "CPU_SCENE_ENTRY", HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
37         "PACKAGE_NAME", ACCESSTOKEN_PROCESS_NAME, "SCENE_ID", std::to_string(id).c_str(), "HAPPEN_TIME", time);
38     if (ret != 0) {
39         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to report performance, ret %{public}d.", ret);
40     }
41 }
42 
ReportSysEventServiceStart(int32_t pid,uint32_t hapSize,uint32_t nativeSize,uint32_t permDefSize)43 void ReportSysEventServiceStart(int32_t pid, uint32_t hapSize, uint32_t nativeSize, uint32_t permDefSize)
44 {
45     int32_t ret = HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::ACCESS_TOKEN, "ACCESSTOKEN_SERVICE_START",
46         HiviewDFX::HiSysEvent::EventType::STATISTIC,
47         "PID", pid, "HAP_SIZE", hapSize, "NATIVE_SIZE", nativeSize, "PERM_DEFINITION_SIZE", permDefSize);
48     if (ret != 0) {
49         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write hisysevent write, ret %{public}d.", ret);
50     }
51 }
52 
ReportSysEventServiceStartError(SceneCode scene,const std::string & errMsg,int32_t errCode)53 void ReportSysEventServiceStartError(SceneCode scene, const std::string& errMsg, int32_t errCode)
54 {
55     int32_t ret = HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::ACCESS_TOKEN, "ACCESSTOKEN_SERVICE_START_ERROR",
56         HiviewDFX::HiSysEvent::EventType::FAULT, "SCENE_CODE", scene, "ERROR_CODE", errCode, "ERROR_MSG", errMsg);
57     if (ret != 0) {
58         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write hisysevent write, ret %{public}d.", ret);
59     }
60 }
61 } // namespace AccessToken
62 } // namespace Security
63 } // namespace OHOS