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