• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 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 #include "app_event_stat.h"
16 
17 #include <random>
18 
19 #include "hiappevent_base.h"
20 #include "hiappevent_write.h"
21 #include "time_util.h"
22 
23 namespace OHOS {
24 namespace HiviewDFX {
25 namespace AppEventStat {
26 namespace {
27 constexpr int BEHAVIOR = 4;
28 
RandomNum()29 uint64_t RandomNum()
30 {
31     std::random_device seed;
32     std::mt19937_64 gen(seed());
33     std::uniform_int_distribution<uint64_t> dis(0, std::numeric_limits<uint64_t>::max());
34     return dis(gen);
35 }
36 }
37 
WriteApiEndEventAsync(const std::string & apiName,uint64_t beginTime,int result,int errCode)38 void WriteApiEndEventAsync(const std::string& apiName, uint64_t beginTime, int result, int errCode)
39 {
40     auto appEventPack = std::make_shared<AppEventPack>("api_diagnostic", "api_exec_end", BEHAVIOR);
41     appEventPack->AddParam("trans_id", "transId_" + std::to_string(RandomNum()));
42     appEventPack->AddParam("api_name", apiName);
43     appEventPack->AddParam("sdk_name", "PerformanceAnalysisKit");
44     int64_t costTime = TimeUtil::GetElapsedMilliSecondsSinceBoot() - static_cast<int64_t>(beginTime);
45     int64_t realEndTime = TimeUtil::GetMilliSecondsTimestamp(CLOCK_REALTIME);
46     int64_t realBeginTime = realEndTime - costTime;
47     if (realBeginTime < 0 || realEndTime < 0) {
48         return;
49     }
50     appEventPack->AddParam("begin_time", realBeginTime);
51     appEventPack->AddParam("end_time", realEndTime);
52     appEventPack->AddParam("result", result);
53     appEventPack->AddParam("error_code", errCode);
54     SubmitWritingTask(appEventPack, "appevent_api_end");
55 }
56 } // namespace AppEventStat
57 } // namespace HiviewDFX
58 } // namespace OHOS
59