Skip to content

Commit 49b49b3

Browse files
nordic-krchnordicjm
authored andcommitted
[nrf fromtree] soc: nordic: common: dmm: Add optional usage stats
Add support for getting usage statistics for DMM. Signed-off-by: Krzysztof Chruściński <[email protected]> (cherry picked from commit d10ee98)
1 parent 5fe850a commit 49b49b3

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

soc/nordic/common/Kconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ config DMM_HEAP_CHUNKS
6060
Number of chunks is a trade-off between performance and granularity.
6161
Must be multiply of 32.
6262

63+
config DMM_STATS
64+
bool "Usage statistics"
65+
6366
endif # HAS_NORDIC_DMM
6467

6568
rsource "vpr/Kconfig"

soc/nordic/common/dmm.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ struct dmm_heap {
5252
size_t blk_size;
5353
const struct dmm_region *region;
5454
sys_bitarray_t bitarray;
55+
#ifdef CONFIG_DMM_STATS
56+
atomic_t curr_use;
57+
uint32_t max_use;
58+
struct k_spinlock lock;
59+
#endif
5560
};
5661

5762
static const struct dmm_region dmm_regions[] = {
@@ -235,6 +240,15 @@ static void *dmm_buffer_alloc(struct dmm_heap *dh, size_t length)
235240

236241
tail_mask_set(dh->tail_mask, num_bits, off);
237242

243+
#ifdef CONFIG_DMM_STATS
244+
k_spinlock_key_t key;
245+
246+
key = k_spin_lock(&dh->lock);
247+
dh->curr_use += num_bits;
248+
dh->max_use = MAX(dh->max_use, dh->curr_use);
249+
k_spin_unlock(&dh->lock, key);
250+
#endif
251+
238252
return (void *)(dh->ptr + dh->blk_size * off);
239253
}
240254

@@ -244,6 +258,9 @@ static void dmm_buffer_free(struct dmm_heap *dh, void *buffer)
244258
size_t num_bits = num_bits_get(dh->tail_mask, offset);
245259
int rv;
246260

261+
#ifdef CONFIG_DMM_STATS
262+
atomic_sub(&dh->curr_use, num_bits);
263+
#endif
247264
rv = sys_bitarray_free(&dh->bitarray, num_bits, offset);
248265
(void)rv;
249266
__ASSERT_NO_MSG(rv == 0);
@@ -431,6 +448,34 @@ int dmm_buffer_in_release(void *region, void *user_buffer, size_t user_length, v
431448
return 0;
432449
}
433450

451+
int dmm_stats_get(void *region, uintptr_t *start_addr, uint32_t *curr_use, uint32_t *max_use)
452+
{
453+
#ifdef CONFIG_DMM_STATS
454+
struct dmm_heap *dh;
455+
456+
dh = dmm_heap_find(region);
457+
if (dh == NULL) {
458+
return -EINVAL;
459+
}
460+
461+
if (start_addr) {
462+
*start_addr = dh->ptr;
463+
}
464+
465+
if (curr_use) {
466+
*curr_use = (100 * dh->curr_use) / dh->bitarray.num_bits;
467+
}
468+
469+
if (max_use) {
470+
*max_use = (100 * dh->max_use) / dh->bitarray.num_bits;
471+
}
472+
473+
return 0;
474+
#else
475+
return -ENOTSUP;
476+
#endif
477+
}
478+
434479
int dmm_init(void)
435480
{
436481
struct dmm_heap *dh;

soc/nordic/common/dmm.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,22 @@ int dmm_buffer_in_prepare(void *region, void *user_buffer, size_t user_length, v
163163
*/
164164
int dmm_buffer_in_release(void *region, void *user_buffer, size_t user_length, void *buffer_in);
165165

166+
/**
167+
* @brief Get statistics.
168+
*
169+
* Must be enabled with CONFIG_DMM_STATS.
170+
*
171+
* @param[in] region DMM memory region.
172+
* @param[out] start_addr Location where starting address of the memory region is set. Can be null.
173+
* @param[out] curr_use Location where current use in percent is written. Can be null.
174+
* @param[out] max_use Location where maximum use in percent is written. Can be null.
175+
*
176+
* @retval 0 on success.
177+
* @retval -EINVAL Invalid region.
178+
* @retval -ENOTSUP Feature is disabled.
179+
*/
180+
int dmm_stats_get(void *region, uintptr_t *start_addr, uint32_t *curr_use, uint32_t *max_use);
181+
166182
/**
167183
* @brief Initialize DMM.
168184
*
@@ -210,6 +226,17 @@ static ALWAYS_INLINE int dmm_buffer_in_release(void *region, void *user_buffer,
210226
return 0;
211227
}
212228

229+
static ALWAYS_INLINE int dmm_stats_get(void *region, uintptr_t *start_addr,
230+
uint32_t *curr_use, uint32_t *max_use)
231+
{
232+
ARG_UNUSED(region);
233+
ARG_UNUSED(start_addr);
234+
ARG_UNUSED(curr_use);
235+
ARG_UNUSED(max_use);
236+
237+
return 0;
238+
}
239+
213240
static ALWAYS_INLINE int dmm_init(void)
214241
{
215242
return 0;

0 commit comments

Comments
 (0)