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
ServiceStartEventWrite(uint32_t useTime,const std::string & module)79 void ServiceStartEventWrite(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
CodecStartEventWrite(CodecDfxInfo & codecDfxInfo)88 void CodecStartEventWrite(CodecDfxInfo& codecDfxInfo)
89 {
90 HiSysEventWrite(HISYSEVENT_DOMAIN_AVCODEC, "CODEC_START_INFO",
91 OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
92 "CLIENT_PID", codecDfxInfo.clientPid,
93 "CLIENT_UID", codecDfxInfo.clientUid,
94 "CODEC_INSTANCE_ID", codecDfxInfo.codecInstanceId,
95 "CODEC_NAME", codecDfxInfo.codecName,
96 "CODEC_IS_VENDOR", codecDfxInfo.codecIsVendor,
97 "CODEC_MODE", codecDfxInfo.codecMode,
98 "ENCODER_BITRATE", codecDfxInfo.encoderBitRate,
99 "VIDEO_WIDTH", codecDfxInfo.videoWidth,
100 "VIDEO_HEIGHT", codecDfxInfo.videoHeight,
101 "VIDEO_FRAMERATE", codecDfxInfo.videoFrameRate,
102 "VIDEO_PIXEL_FORMAT", codecDfxInfo.videoPixelFormat,
103 "AUDIO_CHANNEL_COUNT", codecDfxInfo.audioChannelCount,
104 "AUDIO_SAMPLE_RATE", codecDfxInfo.audioSampleRate);
105 }
106
CodecStopEventWrite(uint32_t clientPid,uint32_t clientUid,uint32_t codecInstanceId)107 void CodecStopEventWrite(uint32_t clientPid, uint32_t clientUid, uint32_t codecInstanceId)
108 {
109 HiSysEventWrite(HISYSEVENT_DOMAIN_AVCODEC, "CODEC_STOP_INFO",
110 OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
111 "CLIENT_PID", clientPid, "CLIENT_UID", clientUid, "CODEC_INSTANCE_ID", codecInstanceId);
112 }
113
AVCodecTrace(const std::string & funcName)114 AVCodecTrace::AVCodecTrace(const std::string& funcName)
115 {
116 StartTrace(HITRACE_TAG_ZMEDIA, funcName);
117 isSync_ = true;
118 }
119
TraceBegin(const std::string & funcName,int32_t taskId)120 void AVCodecTrace::TraceBegin(const std::string& funcName, int32_t taskId)
121 {
122 StartAsyncTrace(HITRACE_TAG_ZMEDIA, funcName, taskId);
123 }
124
TraceEnd(const std::string & funcName,int32_t taskId)125 void AVCodecTrace::TraceEnd(const std::string& funcName, int32_t taskId)
126 {
127 FinishAsyncTrace(HITRACE_TAG_ZMEDIA, funcName, taskId);
128 }
129
CounterTrace(const std::string & varName,int32_t val)130 void AVCodecTrace::CounterTrace(const std::string& varName, int32_t val)
131 {
132 CountTrace(HITRACE_TAG_ZMEDIA, varName, val);
133 }
134
~AVCodecTrace()135 AVCodecTrace::~AVCodecTrace()
136 {
137 if (isSync_) {
138 FinishTrace(HITRACE_TAG_ZMEDIA);
139 }
140 }
141 } // namespace MediaAVCodec
142 } // namespace OHOS
143