Skip to content

Commit 64a7d67

Browse files
ktrzcinxlgirdwood
authored andcommitted
ipc: debug: Add memory usage probing possibility
This feature will be needed to monitor memory utilization and memory leaks. It may be usable also in realese builds, so removed conditional ipc_glb_test_message compilation. Signed-off-by: Karol Trzcinski <karolx.trzcinski@linux.intel.com>
1 parent 897152d commit 64a7d67

4 files changed

Lines changed: 118 additions & 1 deletion

File tree

src/include/ipc/debug.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* SPDX-License-Identifier: BSD-3-Clause
2+
*
3+
* Copyright(c) 2020 Intel Corporation. All rights reserved.
4+
*
5+
* Author: Author: Karol Trzcinski <karolx.trzcinski@linux.intel.com>
6+
*/
7+
8+
#ifndef __IPC_DEBUG_H__
9+
#define __IPC_DEBUG_H__
10+
11+
#include <ipc/header.h>
12+
#include <stdint.h>
13+
14+
/** ABI3.18 */
15+
enum sof_ipc_dbg_mem_zone {
16+
SOF_IPC_MEM_ZONE_SYS = 0, /**< System zone */
17+
SOF_IPC_MEM_ZONE_SYS_RUNTIME = 1, /**< System-runtime zone */
18+
SOF_IPC_MEM_ZONE_RUNTIME = 2, /**< Runtime zone */
19+
SOF_IPC_MEM_ZONE_BUFFER = 3, /**< Buffer zone */
20+
};
21+
22+
/** ABI3.18 */
23+
struct sof_ipc_dbg_mem_usage_elem {
24+
uint32_t zone; /**< see sof_ipc_dbg_mem_zone */
25+
uint32_t id; /**< heap index within zone */
26+
uint32_t used; /**< number of bytes used in zone */
27+
uint32_t free; /**< number of bytes free to use within zone */
28+
uint32_t reserved; /**< reserved for future use */
29+
} __attribute__((packed));
30+
31+
/** ABI3.18 */
32+
struct sof_ipc_dbg_mem_usage {
33+
struct sof_ipc_reply rhdr; /**< generic IPC reply header */
34+
uint32_t reserved[4]; /**< reserved for future use */
35+
uint32_t num_elems; /**< elems[] counter */
36+
struct sof_ipc_dbg_mem_usage_elem elems[]; /**< memory usage information */
37+
} __attribute__((packed));
38+
39+
#endif /* __IPC_DEBUG_H__ */

src/include/ipc/header.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@
186186
#define SOF_IPC_GLB_GDB_DEBUG SOF_GLB_TYPE(0xAU)
187187
#define SOF_IPC_GLB_TEST SOF_GLB_TYPE(0xBU)
188188
#define SOF_IPC_GLB_PROBE SOF_GLB_TYPE(0xCU)
189+
#define SOF_IPC_GLB_DEBUG SOF_GLB_TYPE(0xDU)
189190

190191
/** @} */
191192

@@ -285,6 +286,14 @@
285286

286287
/** @} */
287288

289+
/** \name DSP Command: Debug - additional services
290+
* @{
291+
*/
292+
293+
#define SOF_IPC_DEBUG_MEM_USAGE SOF_CMD_TYPE(0x001)
294+
295+
/** @} */
296+
288297
/** \name DSP Command: Test - Debug build only
289298
* @{
290299
*/

src/include/kernel/abi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
/** \brief SOF ABI version major, minor and patch numbers */
3131
#define SOF_ABI_MAJOR 3
32-
#define SOF_ABI_MINOR 17
32+
#define SOF_ABI_MINOR 18
3333
#define SOF_ABI_PATCH 0
3434

3535
/** \brief SOF ABI version number. Format within 32bit word is MMmmmppp */

src/ipc/handler.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <sof/lib/dma.h>
3131
#include <sof/lib/mailbox.h>
3232
#include <sof/lib/memory.h>
33+
#include <sof/lib/mm_heap.h>
3334
#include <sof/lib/pm_runtime.h>
3435
#include <sof/list.h>
3536
#include <sof/math/numbers.h>
@@ -42,6 +43,7 @@
4243
#include <sof/trace/trace.h>
4344
#include <ipc/control.h>
4445
#include <ipc/dai.h>
46+
#include <ipc/debug.h>
4547
#include <ipc/header.h>
4648
#include <ipc/pm.h>
4749
#include <ipc/stream.h>
@@ -1281,6 +1283,70 @@ static int ipc_glb_tplg_message(uint32_t header)
12811283
}
12821284
}
12831285

1286+
static int fill_mem_usage_elems(int zone, int elem_number,
1287+
struct sof_ipc_dbg_mem_usage_elem *elems)
1288+
{
1289+
struct mm_info info;
1290+
int ret;
1291+
int i;
1292+
1293+
for (i = 0; i < elem_number; ++i) {
1294+
ret = heap_info(zone, i, &info);
1295+
elems[i].zone = zone;
1296+
elems[i].id = i;
1297+
elems[i].used = ret < 0 ? UINT32_MAX : info.used;
1298+
elems[i].free = ret < 0 ? 0 : info.free;
1299+
}
1300+
1301+
return elem_number;
1302+
}
1303+
1304+
static int ipc_glb_test_mem_usage(uint32_t header)
1305+
{
1306+
/* count number heaps */
1307+
int elem_cnt = PLATFORM_HEAP_SYSTEM + PLATFORM_HEAP_SYSTEM_RUNTIME +
1308+
PLATFORM_HEAP_RUNTIME + PLATFORM_HEAP_BUFFER;
1309+
size_t size = sizeof(struct sof_ipc_dbg_mem_usage) +
1310+
elem_cnt * sizeof(struct sof_ipc_dbg_mem_usage_elem);
1311+
struct sof_ipc_dbg_mem_usage_elem *elems;
1312+
struct sof_ipc_dbg_mem_usage *mem_usage;
1313+
1314+
mem_usage = rzalloc(SOF_MEM_ZONE_RUNTIME, 0, 0, size);
1315+
if (!mem_usage)
1316+
return -ENOMEM;
1317+
1318+
mem_usage->rhdr.hdr.cmd = header;
1319+
mem_usage->rhdr.hdr.size = size;
1320+
mem_usage->num_elems = elem_cnt;
1321+
1322+
/* fill list of elems */
1323+
elems = mem_usage->elems;
1324+
elems += fill_mem_usage_elems(SOF_IPC_MEM_ZONE_SYS, PLATFORM_HEAP_SYSTEM, elems);
1325+
elems += fill_mem_usage_elems(SOF_IPC_MEM_ZONE_SYS_RUNTIME, PLATFORM_HEAP_SYSTEM_RUNTIME,
1326+
elems);
1327+
elems += fill_mem_usage_elems(SOF_IPC_MEM_ZONE_RUNTIME, PLATFORM_HEAP_RUNTIME, elems);
1328+
elems += fill_mem_usage_elems(SOF_IPC_MEM_ZONE_BUFFER, PLATFORM_HEAP_BUFFER, elems);
1329+
1330+
/* write component values to the outbox */
1331+
mailbox_hostbox_write(0, mem_usage, mem_usage->rhdr.hdr.size);
1332+
1333+
rfree(mem_usage);
1334+
return 1;
1335+
}
1336+
1337+
static int ipc_glb_debug_message(uint32_t header)
1338+
{
1339+
uint32_t cmd = iCS(header);
1340+
1341+
switch (cmd) {
1342+
case SOF_IPC_DEBUG_MEM_USAGE:
1343+
return ipc_glb_test_mem_usage(header);
1344+
default:
1345+
tr_err(&ipc_tr, "ipc: unknown debug header 0x%x", header);
1346+
return -EINVAL;
1347+
}
1348+
}
1349+
12841350
#if CONFIG_DEBUG
12851351
static int ipc_glb_test_message(uint32_t header)
12861352
{
@@ -1345,6 +1411,9 @@ void ipc_cmd(struct sof_ipc_cmd_hdr *hdr)
13451411
case SOF_IPC_GLB_PROBE:
13461412
ret = ipc_glb_probe(hdr->cmd);
13471413
break;
1414+
case SOF_IPC_GLB_DEBUG:
1415+
ret = ipc_glb_debug_message(hdr->cmd);
1416+
break;
13481417
#if CONFIG_DEBUG
13491418
case SOF_IPC_GLB_TEST:
13501419
ret = ipc_glb_test_message(hdr->cmd);

0 commit comments

Comments
 (0)