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