TA使用内存分析

最近使用这个函数来排除TA中内存泄露问题。

函数:

#ifdef CFG_WITH_STATS
static
TEE_Result get_alloc_stat1() { struct malloc_stats stats; malloc_get_stats(&stats); strncpy(stats.desc, "TA Heap", sizeof(stats.desc)); DMSG("Pool: %*s\n", (int)strnlen(stats.desc, sizeof(stats.desc)), stats.desc); DMSG("Bytes allocated: %d\n", stats.allocated); DMSG("Max bytes allocated: %d\n", stats.max_allocated); DMSG("Size of pool: %d\n", stats.size); DMSG("Number of failed allocations: %d\n", stats.num_alloc_fail); DMSG("Size of larges allocation failure: %d\n", stats.biggest_alloc_fail); DMSG("Total bytes allocated at that failure: %d\n", stats.biggest_alloc_fail_used); return TEE_SUCCESS; }
#endif

注意:上述函数需要用宏CFG_WITH_STATS隔开,且TA中需要打开这个宏开关:

diff --git a/ta/Makefile b/ta/Makefile
index 1918266..0ee9ac7 100644
--- a/ta/Makefile
+++ b/ta/Makefile
@@ -1,5 +1,5 @@
CFG_TEE_TA_LOG_LEVEL ?= 3
-CFG_WITH_STATS ?= n
+CFG_WITH_STATS ?= y

同时optee os中也要相应地打开这个宏:

src/optee_os
diff --git a/core/arch/arm/plat-xxx/conf.mk b/core/arch/arm/plat-xxx/conf.mk
index 6250e81..dccdc6e 100644
--- a/core/arch/arm/plat-xxx/conf.mk
+++ b/core/arch/arm/plat-xxx/conf.mk
@@ -20,6 +20,7 @@ $(call force,CFG_PM_STUBS,y)
$(call force,CFG_SECURE_TIME_SOURCE_CNTPCT,y)
$(call force,CFG_WITH_ARM_TRUSTED_FW,y)        #todo
$(call force,CFG_ARM_GICV3,y)
+CFG_WITH_STATS ?= y
 # only build arm64
supported-ta-targets := ta_arm64

示例代码:

这段代码是直接在hello_world中添加的。为了测试该TA中内存情况:

get_alloc_stat1();
uint8_t *test = TEE_Malloc(10, TEE_MALLOC_FILL_ZERO);
DMSG("after malloc 10");
get_alloc_stat1();
DMSG("after free");
TEE_Free(test);
get_alloc_stat1();

test = TEE_Malloc(30, TEE_MALLOC_FILL_ZERO);
DMSG("after malloc 30");
get_alloc_stat1();
DMSG("after free");
TEE_Free(test);
get_alloc_stat1();

test = TEE_Malloc(60, TEE_MALLOC_FILL_ZERO);
DMSG("after malloc 60");
get_alloc_stat1();
DMSG("after free");
TEE_Free(test);
get_alloc_stat1();

test = TEE_Malloc(100, TEE_MALLOC_FILL_ZERO);
DMSG("after malloc 100");
get_alloc_stat1();
DMSG("after free");
TEE_Free(test);
get_alloc_stat1();

测试结果:

6229 59630.281 D/TA:  get_alloc_stat1:182 Pool:                TA Heap
6231 59630.292 D/TA:  get_alloc_stat1:184 Bytes allocated:                       128 //入口打印,还没查这个值是如何来的,待查
6233 59630.305 D/TA:  get_alloc_stat1:186 Max bytes allocated:                   256
6235 59630.317 D/TA:  get_alloc_stat1:188 Size of pool:                          32752
6237 59630.330 D/TA:  get_alloc_stat1:190 Number of failed allocations:          0
6239 59630.342 D/TA:  get_alloc_stat1:192 Size of larges allocation failure:     0
6241 59630.354 D/TA:  get_alloc_stat1:194 Total bytes allocated at that failure: 0
6243 59630.366 D/TA:  TA_InvokeCommandEntryPoint:225 after malloc 10 //预期分配10个字节
6245 59630.377 D/TA:  get_alloc_stat1:182 Pool:                TA Heap
6247 59630.388 D/TA:  get_alloc_stat1:184 Bytes allocated:                       160 //实际分配160-128=32个字节
6249 59630.401 D/TA:  get_alloc_stat1:186 Max bytes allocated:                   256
6251 59630.413 D/TA:  get_alloc_stat1:188 Size of pool:                          32752
6253 59630.426 D/TA:  get_alloc_stat1:190 Number of failed allocations:          0
6255 59630.438 D/TA:  get_alloc_stat1:192 Size of larges allocation failure:     0
6257 59630.450 D/TA:  get_alloc_stat1:194 Total bytes allocated at that failure: 0
6259 59630.462 D/TA:  TA_InvokeCommandEntryPoint:227 after free
6261 59630.473 D/TA:  get_alloc_stat1:182 Pool:                TA Heap
6263 59630.484 D/TA:  get_alloc_stat1:184 Bytes allocated:                       128 //释放之后,变回128
6265 59630.496 D/TA:  get_alloc_stat1:186 Max bytes allocated:                   256
6267 59630.508 D/TA:  get_alloc_stat1:188 Size of pool:                          32752
6269 59630.521 D/TA:  get_alloc_stat1:190 Number of failed allocations:          0
6271 59630.533 D/TA:  get_alloc_stat1:192 Size of larges allocation failure:     0
6273 59630.545 D/TA:  get_alloc_stat1:194 Total bytes allocated at that failure: 0
6275 59630.558 D/TA:  TA_InvokeCommandEntryPoint:232 after malloc 30 //预期分配30个字节
6277 59630.569 D/TA:  get_alloc_stat1:182 Pool:                TA Heap
6279 59630.580 D/TA:  get_alloc_stat1:184 Bytes allocated:                       176 //实际分配176-128=48个字节
6281 59630.592 D/TA:  get_alloc_stat1:186 Max bytes allocated:                   256
6283 59630.605 D/TA:  get_alloc_stat1:188 Size of pool:                          32752
6285 59630.617 D/TA:  get_alloc_stat1:190 Number of failed allocations:          0
6287 59630.629 D/TA:  get_alloc_stat1:192 Size of larges allocation failure:     0
6289 59630.642 D/TA:  get_alloc_stat1:194 Total bytes allocated at that failure: 0
6291 59630.654 D/TA:  TA_InvokeCommandEntryPoint:234 after free
6293 59630.664 D/TA:  get_alloc_stat1:182 Pool:                TA Heap
6295 59630.675 D/TA:  get_alloc_stat1:184 Bytes allocated:                       128 //释放后,变回128
6297 59630.688 D/TA:  get_alloc_stat1:186 Max bytes allocated:                   256
6299 59630.700 D/TA:  get_alloc_stat1:188 Size of pool:                          32752
6301 59630.713 D/TA:  get_alloc_stat1:190 Number of failed allocations:          0
6303 59630.725 D/TA:  get_alloc_stat1:192 Size of larges allocation failure:     0
6305 59630.737 D/TA:  get_alloc_stat1:194 Total bytes allocated at that failure: 0
6307 59630.749 D/TA:  TA_InvokeCommandEntryPoint:239 after malloc 60 //预期分配60个字节
6309 59630.760 D/TA:  get_alloc_stat1:182 Pool:                TA Heap
6311 59630.771 D/TA:  get_alloc_stat1:184 Bytes allocated:                       208 //实际分配208-128=80
6313 59630.784 D/TA:  get_alloc_stat1:186 Max bytes allocated:                   256
6315 59630.796 D/TA:  get_alloc_stat1:188 Size of pool:                          32752
6317 59630.809 D/TA:  get_alloc_stat1:190 Number of failed allocations:          0
6319 59630.821 D/TA:  get_alloc_stat1:192 Size of larges allocation failure:     0
6321 59630.833 D/TA:  get_alloc_stat1:194 Total bytes allocated at that failure: 0
6323 59630.845 D/TA:  TA_InvokeCommandEntryPoint:241 after free
6325 59630.856 D/TA:  get_alloc_stat1:182 Pool:                TA Heap
6327 59630.867 D/TA:  get_alloc_stat1:184 Bytes allocated:                       128
6329 59630.879 D/TA:  get_alloc_stat1:186 Max bytes allocated:                   256
6331 59630.892 D/TA:  get_alloc_stat1:188 Size of pool:                          32752
6333 59630.904 D/TA:  get_alloc_stat1:190 Number of failed allocations:          0
6335 59630.916 D/TA:  get_alloc_stat1:192 Size of larges allocation failure:     0
6337 59630.928 D/TA:  get_alloc_stat1:194 Total bytes allocated at that failure: 0
6339 59630.941 D/TA:  TA_InvokeCommandEntryPoint:246 after malloc 100 //预期100
6341 59630.952 D/TA:  get_alloc_stat1:182 Pool:                TA Heap
6343 59630.963 D/TA:  get_alloc_stat1:184 Bytes allocated:                       256 //实际256-128=128
6345 59630.975 D/TA:  get_alloc_stat1:186 Max bytes allocated:                   256
6347 59630.988 D/TA:  get_alloc_stat1:188 Size of pool:                          32752
6349 59631.000 D/TA:  get_alloc_stat1:190 Number of failed allocations:          0
6351 59631.012 D/TA:  get_alloc_stat1:192 Size of larges allocation failure:     0
6353 59631.025 D/TA:  get_alloc_stat1:194 Total bytes allocated at that failure: 0
6355 59631.037 D/TA:  TA_InvokeCommandEntryPoint:248 after free
6357 59631.047 D/TA:  get_alloc_stat1:182 Pool:                TA Heap
6359 59631.058 D/TA:  get_alloc_stat1:184 Bytes allocated:                       128
6361 59631.071 D/TA:  get_alloc_stat1:186 Max bytes allocated:                   256
6363 59631.083 D/TA:  get_alloc_stat1:188 Size of pool:                          32752
6365 59631.096 D/TA:  get_alloc_stat1:190 Number of failed allocations:          0
6367 59631.108 D/TA:  get_alloc_stat1:192 Size of larges allocation failure:     0
6369 59631.120 D/TA:  get_alloc_stat1:194 Total bytes allocated at that failure: 0

从结果来看,是存在对齐操作的。这个方法可以比较方便的用于跟踪打印TA侧是否存在内存泄露情况。

posted @ 2023-03-21 22:43  xiululu  阅读(49)  评论(0编辑  收藏  举报