1 /*
2 * Copyright (C) 2023 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 <avcodec_dfx.h>
17 #include <unistd.h>
18 #include "securec.h"
19 #include "avcodec_log.h"
20 #include "avcodec_errors.h"
21 #include "hitrace_meter.h"
22 #include "dump_usage.h"
23
24 namespace {
25 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "AVCodecDFX"};
26 constexpr uint32_t MAX_STRING_SIZE = 256;
27 constexpr char HISYSEVENT_DOMAIN_AVCODEC[] = "AV_CODEC";
28 } // namespace
29
30 namespace OHOS {
31 namespace MediaAVCodec {
CreateMsg(const char * format,...)32 bool AVCodecEvent::CreateMsg(const char* format, ...)
33 {
34 va_list args;
35 va_start(args, format);
36 char msg[MAX_STRING_SIZE] = {0};
37 if (vsnprintf_s(msg, sizeof(msg), sizeof(msg) - 1, format, args) < 0) {
38 AVCODEC_LOGE("failed to call vsnprintf_s");
39 va_end(args);
40 return false;
41 }
42 va_end(args);
43 msg_ = msg;
44 return true;
45 }
46
FaultEventWrite(const std::string & eventName,OHOS::HiviewDFX::HiSysEvent::EventType type,FaultType faultType,const std::string & module)47 void AVCodecEvent::FaultEventWrite(const std::string& eventName,
48 OHOS::HiviewDFX::HiSysEvent::EventType type,
49 FaultType faultType,
50 const std::string& module)
51 {
52 std::string faultName;
53 switch (faultType) {
54 case FaultType::FAULT_TYPE_FREEZE:
55 faultName = "Freeze";
56 break;
57 case FaultType::FAULT_TYPE_CRASH:
58 faultName = "Crash";
59 break;
60 case FaultType::FAULT_TYPE_INNER_ERROR:
61 faultName = "Inner error";
62 break;
63 default:
64 AVCODEC_LOGE("Invalid fault type:%{public}d", faultType);
65 }
66 HiSysEventWrite(HISYSEVENT_DOMAIN_AVCODEC, eventName, type, "MODULE", module, "FAULTTYPE", faultName, "MSG", msg_);
67 }
68
FaultEventWrite(FaultType faultType,const std::string & msg,const std::string & module)69 void FaultEventWrite(FaultType faultType, const std::string& msg, const std::string& module)
70 {
71 AVCodecEvent event;
72 if (event.CreateMsg("%s", msg.c_str())) {
73 event.FaultEventWrite("FAULT", OHOS::HiviewDFX::HiSysEvent::EventType::FAULT, faultType, module);
74 } else {
75 AVCODEC_LOGW("Failed to call CreateMsg");
76 }
77 }
78
BehaviorEventWrite(uint32_t useTime,const std::string & module)79 void BehaviorEventWrite(uint32_t useTime, const std::string& module)
80 {
81 OHOS::HiviewDFX::DumpUsage dumpUse;
82 uint64_t useMemory = dumpUse.GetPss(getpid());
83 HiSysEventWrite(HISYSEVENT_DOMAIN_AVCODEC, "SERVICE_START_INFO",
84 OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR, "MODULE", module.c_str(), "TIME", useTime,
85 "MEMORY", useMemory);
86 }
87
AVCodecTrace(const std::string & funcName)88 AVCodecTrace::AVCodecTrace(const std::string& funcName)
89 {
90 StartTrace(HITRACE_TAG_ZMEDIA, funcName);
91 isSync_ = true;
92 }
93
TraceBegin(const std::string & funcName,int32_t taskId)94 void AVCodecTrace::TraceBegin(const std::string& funcName, int32_t taskId)
95 {
96 StartAsyncTrace(HITRACE_TAG_ZMEDIA, funcName, taskId);
97 }
98
TraceEnd(const std::string & funcName,int32_t taskId)99 void AVCodecTrace::TraceEnd(const std::string& funcName, int32_t taskId)
100 {
101 FinishAsyncTrace(HITRACE_TAG_ZMEDIA, funcName, taskId);
102 }
103
CounterTrace(const std::string & varName,int32_t val)104 void AVCodecTrace::CounterTrace(const std::string& varName, int32_t val)
105 {
106 CountTrace(HITRACE_TAG_ZMEDIA, varName, val);
107 }
108
~AVCodecTrace()109 AVCodecTrace::~AVCodecTrace()
110 {
111 if (isSync_) {
112 FinishTrace(HITRACE_TAG_ZMEDIA);
113 }
114 }
115 } // namespace MediaAVCodec
116 } // namespace OHOS
117