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