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 #define MLOG_TAG "DfxTransaction"
17
18 #include "dfx_transaction.h"
19
20 #include "hisysevent.h"
21 #include "media_file_utils.h"
22 #include "media_log.h"
23
24 namespace OHOS {
25 namespace Media {
26 constexpr char MEDIA_LIBRARY[] = "MEDIALIBRARY";
27 constexpr int64_t TIME_COST_THRESHOLD = 2000; // 2s
DfxTransaction(std::string funcName)28 DfxTransaction::DfxTransaction(std::string funcName) : funcName_(funcName)
29 {
30 startTime_ = MediaFileUtils::UTCTimeMilliSeconds();
31 }
32
~DfxTransaction()33 DfxTransaction::~DfxTransaction() {}
34
Restart()35 void DfxTransaction::Restart()
36 {
37 timeCost_ = MediaFileUtils::UTCTimeMilliSeconds() - startTime_;
38 }
39
ReportIfTimeout()40 void DfxTransaction::ReportIfTimeout()
41 {
42 timeCost_ = MediaFileUtils::UTCTimeMilliSeconds() - startTime_;
43 if (timeCost_ < TIME_COST_THRESHOLD) {
44 return;
45 }
46 Report(AbnormalType::TIMEOUT_WARN);
47 }
48
ReportError(uint8_t abnormalType,int32_t errCode)49 void DfxTransaction::ReportError(uint8_t abnormalType, int32_t errCode)
50 {
51 timeCost_ = MediaFileUtils::UTCTimeMilliSeconds() - startTime_;
52 Report(abnormalType, errCode);
53 }
54
Report(uint8_t abnormalType,int32_t errCode)55 void DfxTransaction::Report(uint8_t abnormalType, int32_t errCode)
56 {
57 int ret = HiSysEventWrite(
58 MEDIA_LIBRARY,
59 "MEDIALIB_TRANSACTION_ERROR",
60 HiviewDFX::HiSysEvent::EventType::FAULT,
61 "FUNC_NAME", funcName_,
62 "START_TIME", startTime_,
63 "TIME_COST", timeCost_,
64 "ABNORMAL_TYPE", abnormalType,
65 "ERROR_CODE", errCode);
66 if (ret != 0) {
67 MEDIA_ERR_LOG("Dfx Transaction Report error: %{public}d", ret);
68 }
69 }
70 } // namespace Media
71 } // namespace OHOS