Skip to content

Commit 9d23b7c

Browse files
author
Arto Kinnunen
authored
Merge pull request #13909 from mikaleppanen/feature-wisun-dyn-mem-track
[feature-wisun] Add nanostack dynamic memory tracker and hooks to dynmem library
2 parents 649ee6c + a9746a5 commit 9d23b7c

File tree

7 files changed

+649
-1
lines changed

7 files changed

+649
-1
lines changed

features/frameworks/nanostack-libservice/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ source/libip6string/ip6tos.c \
55
source/libip6string/stoip6.c \
66
source/libList/ns_list.c \
77
source/nsdynmemLIB/nsdynmemLIB.c \
8+
source/nsdynmemtracker/nsdynmem_tracker_lib.c \
89
source/nvmHelper/ns_nvm_helper.c \
910

1011
LIB := libservice.a

features/frameworks/nanostack-libservice/mbed-client-libservice/nsdynmemLIB.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ extern "C" {
3737
typedef size_t ns_mem_block_size_t; //external interface unsigned heap block size type
3838
typedef size_t ns_mem_heap_size_t; //total heap size type.
3939

40+
// Can be used to enable tracking of dynamic memory allocations
41+
#include "nsdynmem_tracker.h"
42+
4043
/*!
4144
* \enum heap_fail_t
4245
* \brief Dynamically heap system failure call back event types.
@@ -99,7 +102,9 @@ extern int ns_dyn_mem_region_add(void *region_ptr, ns_mem_heap_size_t region_siz
99102
* \return 0, Free OK
100103
* \return <0, Free Fail
101104
*/
105+
#if NSDYNMEM_TRACKER_ENABLED!=1
102106
extern void ns_dyn_mem_free(void *heap_ptr);
107+
#endif
103108

104109
/**
105110
* \brief Allocate temporary data.
@@ -111,7 +116,9 @@ extern void ns_dyn_mem_free(void *heap_ptr);
111116
* \return 0, Allocate Fail
112117
* \return >0, Pointer to allocated data sector.
113118
*/
119+
#if NSDYNMEM_TRACKER_ENABLED!=1
114120
extern void *ns_dyn_mem_temporary_alloc(ns_mem_block_size_t alloc_size);
121+
#endif
115122

116123
/**
117124
* \brief Allocate long period data.
@@ -123,7 +130,9 @@ extern void *ns_dyn_mem_temporary_alloc(ns_mem_block_size_t alloc_size);
123130
* \return 0, Allocate Fail
124131
* \return >0, Pointer to allocated data sector.
125132
*/
133+
#if NSDYNMEM_TRACKER_ENABLED!=1
126134
extern void *ns_dyn_mem_alloc(ns_mem_block_size_t alloc_size);
135+
#endif
127136

128137
/**
129138
* \brief Get pointer to the current mem_stat_t set via ns_dyn_mem_init.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2020 ARM Limited. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Licensed under the Apache License, Version 2.0 (the License); you may
5+
* not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* \file nsdynmem_tracker.h
19+
* \brief Dynamical Memory Tracker definitions to override the default NS dynamic memory functionality
20+
* Provides tracking and tracing of dynamic memory blocks
21+
*/
22+
23+
#ifndef NSDYNMEM_TRACKER_H_
24+
#define NSDYNMEM_TRACKER_H_
25+
#ifdef __cplusplus
26+
extern "C" {
27+
#endif
28+
29+
#if NSDYNMEM_TRACKER_ENABLED==1
30+
31+
#define ns_dyn_mem_free(block) ns_dyn_mem_tracker_dyn_mem_free(block, __func__, __LINE__)
32+
#define ns_dyn_mem_temporary_alloc(alloc_size) ns_dyn_mem_tracker_dyn_mem_temporary_alloc(alloc_size, __func__, __LINE__)
33+
#define ns_dyn_mem_alloc(alloc_size) ns_dyn_mem_tracker_dyn_mem_alloc(alloc_size, __func__, __LINE__)
34+
35+
void *ns_dyn_mem_tracker_dyn_mem_alloc(ns_mem_heap_size_t alloc_size, const char *function, uint32_t line);
36+
void *ns_dyn_mem_tracker_dyn_mem_temporary_alloc(ns_mem_heap_size_t alloc_size, const char *function, uint32_t line);
37+
void ns_dyn_mem_tracker_dyn_mem_free(void *block, const char *function, uint32_t line);
38+
39+
#endif
40+
41+
#ifdef __cplusplus
42+
}
43+
#endif
44+
#endif /* NSDYNMEM_TRACKER_H_ */
45+
46+
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright (c) 2020 ARM Limited. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Licensed under the Apache License, Version 2.0 (the License); you may
5+
* not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
18+
/**
19+
* \file nsdynmem_tracker_lib.h
20+
* \brief Dynamical Memory Tracker library API
21+
* Provides tracking and tracing of dynamic memory blocks
22+
*/
23+
24+
#ifndef NSDYNMEM_TRACKER_LIB_H_
25+
#define NSDYNMEM_TRACKER_LIB_H_
26+
#ifdef __cplusplus
27+
extern "C" {
28+
#endif
29+
30+
#if NSDYNMEM_TRACKER_ENABLED==1
31+
32+
// Memory block structure with caller information
33+
typedef struct ns_dyn_mem_tracker_lib_mem_blocks_s {
34+
void *block; /**< Allocated memory block */
35+
void *caller_addr; /**< Caller address */
36+
uint32_t size; /**< Allocation size */
37+
uint32_t total_size; /**< Total allocation size for all allocations */
38+
uint32_t lifetime; /**< Memory block lifetime in steps (e.g. seconds) */
39+
uint32_t ref_count; /**< Reference count */
40+
const char *function; /**< Caller function */
41+
uint16_t line; /**< Caller line in module */
42+
bool permanent : 1; /**< Permanent memory block */
43+
bool permanent_printed : 1; /**< Permanent memory block printed */
44+
} ns_dyn_mem_tracker_lib_mem_blocks_t;
45+
46+
// Extended memory block structure that is used if same caller allocates multiple memory blocks
47+
typedef struct ns_dyn_mem_tracker_lib_mem_blocks_ext_s {
48+
void *block; /**< Allocated memory block */
49+
void *caller_addr; /**< Caller address */
50+
uint32_t size; /**< Allocation size */
51+
} ns_dyn_mem_tracker_lib_mem_blocks_ext_t;
52+
53+
// Allocator information structure
54+
typedef struct ns_dyn_mem_tracker_lib_allocators_s {
55+
void *caller_addr; /**< Caller address */
56+
uint32_t alloc_count; /**< Number of allocations */
57+
uint32_t total_memory; /**< Total memory used by allocations */
58+
uint32_t min_lifetime; /**< Shortest lifetime among the allocations */
59+
const char *function; /**< Function name string */
60+
uint16_t line; /**< Module line */
61+
} ns_dyn_mem_tracker_lib_allocators_t;
62+
63+
// Memory block array allocator / array size increase allocator
64+
typedef ns_dyn_mem_tracker_lib_mem_blocks_t *ns_dyn_mem_tracker_lib_alloc_mem_blocks(ns_dyn_mem_tracker_lib_mem_blocks_t *blocks, uint16_t *mem_blocks_count);
65+
// Extended memory block array allocator / array size increase allocator
66+
typedef ns_dyn_mem_tracker_lib_mem_blocks_ext_t *ns_dyn_mem_tracker_lib_alloc_mem_blocks_ext(ns_dyn_mem_tracker_lib_mem_blocks_ext_t *blocks, uint32_t *mem_blocks_count);
67+
// Extended memory block array index hash function to get memory block (allocation/search start) index from block address
68+
typedef uint32_t ns_dyn_mem_tracker_lib_mem_block_index_hash(void *block, uint32_t ext_mem_blocks_count);
69+
70+
typedef struct ns_dyn_mem_tracker_lib_conf_s {
71+
ns_dyn_mem_tracker_lib_mem_blocks_t *mem_blocks; /**< Memory blocks array, if NULL calls allocator on init */
72+
ns_dyn_mem_tracker_lib_mem_blocks_ext_t *ext_mem_blocks; /**< Extended memory blocks array, if NULL calls allocator on init */
73+
ns_dyn_mem_tracker_lib_allocators_t *top_allocators; /**< Top allocators array */
74+
ns_dyn_mem_tracker_lib_allocators_t *permanent_allocators; /**< Permanent allocators */
75+
ns_dyn_mem_tracker_lib_allocators_t *to_permanent_allocators; /**< To permanent allocators */
76+
ns_dyn_mem_tracker_lib_allocators_t *max_snap_shot_allocators; /**< Snap shot of maximum memory used by allocators */
77+
ns_dyn_mem_tracker_lib_alloc_mem_blocks *alloc_mem_blocks; /**< Memory block array allocator / array size increase allocator */
78+
ns_dyn_mem_tracker_lib_alloc_mem_blocks_ext *ext_alloc_mem_blocks; /**< Extended memory block array allocator / array size increase allocator */
79+
ns_dyn_mem_tracker_lib_mem_block_index_hash *block_index_hash; /**< Hash function to get memory block index from block address */
80+
uint32_t allocated_memory; /**< Currently allocated memory */
81+
uint16_t mem_blocks_count; /**< Number of entries in memory blocks array */
82+
uint32_t ext_mem_blocks_count; /**< Number of entries in extended memory blocks array */
83+
uint16_t last_mem_block_index; /**< Last memory block in memory blocks array */
84+
uint16_t top_allocators_count; /**< Top allocators array count */
85+
uint16_t permanent_allocators_count; /**< Permanent allocators array count */
86+
uint16_t to_permanent_allocators_count; /**< To permanent allocators array count */
87+
uint16_t max_snap_shot_allocators_count; /**< Snap shot of maximum memory used by allocators array count */
88+
uint16_t to_permanent_steps_count; /**< How many steps before moving block to permanent allocators list */
89+
} ns_dyn_mem_tracker_lib_conf_t;
90+
91+
int8_t ns_dyn_mem_tracker_lib_alloc(ns_dyn_mem_tracker_lib_conf_t *conf, void *caller_addr, const char *function, uint32_t line, void *block, uint32_t alloc_size);
92+
int8_t ns_dyn_mem_tracker_lib_free(ns_dyn_mem_tracker_lib_conf_t *conf, void *caller_addr, const char *function, uint32_t line, void *block);
93+
void ns_dyn_mem_tracker_lib_step(ns_dyn_mem_tracker_lib_conf_t *conf);
94+
int8_t ns_dyn_mem_tracker_lib_allocator_lists_update(ns_dyn_mem_tracker_lib_conf_t *conf);
95+
void ns_dyn_mem_tracker_lib_max_snap_shot_update(ns_dyn_mem_tracker_lib_conf_t *conf);
96+
97+
#endif
98+
99+
#ifdef __cplusplus
100+
}
101+
#endif
102+
#endif /* NSDYNMEM_TRACKER_LIB_H_ */
103+
104+
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
{
2-
"name": "nanostack-libservice"
2+
"name": "nanostack-libservice",
3+
"macros": ["NSDYNMEM_TRACKER_ENABLED=MBED_CONF_NANOSTACK_LIBSERVICE_NSDYNMEM_TRACKER_ENABLED"],
4+
"config": {
5+
"nsdynmem-tracker-enabled": {
6+
"help": "Use to enable dynamic memory tracker",
7+
"value": 0
8+
}
9+
}
310
}

features/frameworks/nanostack-libservice/source/nsdynmemLIB/nsdynmemLIB.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
#include <stdint.h>
1717
#include <string.h>
18+
#undef NSDYNMEM_TRACKER_ENABLED
1819
#include "nsdynmemLIB.h"
1920
#include "platform/arm_hal_interrupt.h"
2021
#include <stdlib.h>

0 commit comments

Comments
 (0)