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 "codec_log_wrapper.h"
17 #include "codec_dfx_service.h"
18 #include "malloc.h"
19 #include <mutex>
20 namespace OHOS {
21 namespace HDI {
22 namespace Codec {
23 namespace V3_0 {
24 #define ARGV_FLAG 1
25 #define INPUT_PORT_INDEX 0
26 #define OUTPUT_PORT_INDEX 1
27 CodecDfxService CodecDfxService::dfxInstance_;
28 HdfSBuf *CodecDfxService::reply_;
29 std::mutex g_mtx;
GetCodecComponentListInfo(struct HdfSBuf * reply)30 int32_t CodecDfxService::GetCodecComponentListInfo(struct HdfSBuf *reply)
31 {
32 CodecStateType state;
33 uint32_t inputBuffCount = 0;
34 uint32_t outputBuffCount = 0;
35 std::shared_ptr<OHOS::Codec::Omx::ComponentNode> dumpNode = nullptr;
36 std::map<uint32_t, sptr<ICodecComponent>> dumpMap = {};
37
38 GetInstance().managerService_->GetManagerMap(dumpMap);
39 if (dumpMap.empty()) {
40 CODEC_LOGE("get manager map failed!");
41 return HDF_ERR_INVALID_PARAM;
42 }
43 for (auto it : dumpMap) {
44 std::string dump = "compName = ";
45 CodecComponentService *componentService = reinterpret_cast<CodecComponentService *>(it.second.GetRefPtr());
46 dump.append(componentService->GetComponentCompName())
47 .append(", compId = ")
48 .append(std::to_string(it.first))
49 .append(", state = ");
50 componentService->GetComponentNode(dumpNode);
51 if (dumpNode == nullptr) {
52 CODEC_LOGE("get dumpNode failed!");
53 return HDF_ERR_INVALID_PARAM;
54 }
55 dumpNode->GetState(state);
56 dump.append(std::to_string(state));
57 dumpNode->GetBuffCount(inputBuffCount, outputBuffCount);
58 dump.append(", inputPortIndex = ")
59 .append(std::to_string(INPUT_PORT_INDEX))
60 .append(", inputBuffCount = ")
61 .append(std::to_string(inputBuffCount))
62 .append(", outputPortIndex = ")
63 .append(std::to_string(OUTPUT_PORT_INDEX))
64 .append(", outputBuffCount = ")
65 .append(std::to_string(outputBuffCount))
66 .append("\n");
67 if (!HdfSbufWriteString(reply, dump.c_str())) {
68 CODEC_LOGE("dump write Fail!");
69 return HDF_ERR_INVALID_PARAM;
70 }
71 if (!HdfSbufWriteString(reply, "------------------------------------------------------------------------ \n")) {
72 CODEC_LOGE("Split symbol write Fail!");
73 return HDF_ERR_INVALID_PARAM;
74 }
75 inputBuffCount = 0;
76 outputBuffCount = 0;
77 componentService = nullptr;
78 }
79 return HDF_SUCCESS;
80 }
81
SetComponentManager(sptr<CodecComponentManagerService> manager)82 void CodecDfxService::SetComponentManager(sptr<CodecComponentManagerService> manager)
83 {
84 managerService_ = manager;
85 }
86
GetInstance()87 CodecDfxService& CodecDfxService::GetInstance()
88 {
89 return dfxInstance_;
90 }
91
GetReply()92 HdfSBuf* CodecDfxService::GetReply()
93 {
94 return reply_;
95 }
96
WriteMemoryInfo(void * fp,const char * memInfo)97 static void WriteMemoryInfo(void *fp, const char *memInfo)
98 {
99 if (memInfo == nullptr) {
100 return;
101 }
102 if (!HdfSbufWriteString(CodecDfxService::GetReply(), memInfo)) {
103 CODEC_LOGE("write memory info error!");
104 }
105 }
106
GetCodecMemoryInfo()107 void CodecDfxService::GetCodecMemoryInfo()
108 {
109 malloc_stats_print(WriteMemoryInfo, nullptr, nullptr);
110 }
111
DevCodecHostDump(struct HdfSBuf * data,struct HdfSBuf * reply)112 int32_t CodecDfxService::DevCodecHostDump(struct HdfSBuf *data, struct HdfSBuf *reply)
113 {
114 uint32_t argv = 0;
115 std::lock_guard<std::mutex> lk{g_mtx};
116 reply_ = reply;
117 if (!HdfSbufReadUint32(data, &argv)) {
118 CODEC_LOGE("idl_service read argv failed!");
119 return HDF_FAILURE;
120 }
121 if (argv != ARGV_FLAG) {
122 if (!HdfSbufWriteString(reply, "please enter -h for help! \n")) {
123 CODEC_LOGE("help write Fail!");
124 return HDF_ERR_INVALID_PARAM;
125 }
126 return HDF_SUCCESS;
127 }
128
129 const char *para = HdfSbufReadString(data);
130 if (para == nullptr) {
131 CODEC_LOGE("read string data failed");
132 return HDF_ERR_INVALID_PARAM;
133 }
134 if (strcmp(para, "-h") == EOK) {
135 if (!HdfSbufWriteString(reply, "-h: codec dump help! \n")) {
136 CODEC_LOGE("-h write Fail!");
137 return HDF_ERR_INVALID_PARAM;
138 }
139 if (!HdfSbufWriteString(reply, "-l: dump codec components info list! \n")) {
140 CODEC_LOGE("-l write Fail!");
141 return HDF_ERR_INVALID_PARAM;
142 }
143 return HDF_SUCCESS;
144 } else if (strcmp(para, "-l") == EOK) {
145 GetInstance().GetCodecComponentListInfo(reply);
146 } else if (strcmp(para, "-m") == EOK) {
147 GetInstance().GetCodecMemoryInfo();
148 } else {
149 HdfSbufWriteString(reply, "unknow param, please enter -h for help! \n");
150 }
151 return HDF_SUCCESS;
152 }
153 } // namespace V3_0
154 } // namespace Codec
155 } // namespace HDI
156 } // namespace OHOS
157