mm: add memory pressure notification support

Add mm_heap_free interface to pass remaining memory to memory pressure

Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
This commit is contained in:
yinshengkai 2024-01-24 23:33:02 +08:00 committed by Xiang Xiao
parent f44a31c337
commit 49d1b4198f
10 changed files with 82 additions and 9 deletions

View file

@ -611,6 +611,18 @@ void up_allocate_heap(void **heap_start, size_t *heap_size)
*heap_size = 0;
}
/****************************************************************************
* Name: mm_heapfree
*
* Description:
* Return the total free size (in bytes) in the heap
*
****************************************************************************/
size_t mm_heapfree(struct mm_heap_s *heap)
{
return SIZE_MAX;
}
#else /* CONFIG_MM_CUSTOMIZE_MANAGER */
void up_allocate_heap(void **heap_start, size_t *heap_size)

View file

@ -159,19 +159,25 @@ static ssize_t pressure_read(FAR struct file *filep, FAR char *buffer,
{
char buf[128];
uint32_t flags;
size_t remain;
off_t offset;
ssize_t ret;
flags = spin_lock_irqsave(&g_pressure_lock);
ret = procfs_snprintf(buf, sizeof(buf), "remaining %zu\n",
g_remaining);
flags = spin_lock_irqsave(&g_pressure_lock);
remain = g_remaining;
spin_unlock_irqrestore(&g_pressure_lock, flags);
ret = procfs_snprintf(buf, sizeof(buf), "remaining %zu\n", remain);
if (ret > buflen)
{
return -ENOMEM;
}
memcpy(buffer, buf, ret);
offset = filep->f_pos;
ret = procfs_memcpy(buf, ret, buffer, buflen, &offset);
filep->f_pos += ret;
return ret;
}
@ -216,9 +222,9 @@ static ssize_t pressure_write(FAR struct file *filep, FAR const char *buffer,
/* We should trigger the first event immediately */
priv->lasttick = CLOCK_MAX;
priv->lasttick = CLOCK_MAX;
priv->threshold = threshold;
priv->interval = interval;
priv->interval = interval;
spin_unlock_irqrestore(&g_pressure_lock, flags);
return buflen;
}
@ -365,7 +371,7 @@ static int pressure_rewinddir(FAR struct fs_dirent_s *dir)
FAR struct procfs_dir_priv_s *level;
DEBUGASSERT(dir);
level = (FAR struct procfs_dir_priv_s *)dir;
level = (FAR struct procfs_dir_priv_s *)dir;
level->index = 0;
return OK;
}
@ -411,7 +417,7 @@ void mm_notify_pressure(size_t remaining)
FAR dq_entry_t *tmp;
uint32_t flags;
flags = spin_lock_irqsave(&g_pressure_lock);
flags = spin_lock_irqsave(&g_pressure_lock);
g_remaining = remaining;
dq_for_every_safe(&g_pressure_memory_queue, entry, tmp)
{

View file

@ -389,6 +389,8 @@ struct mallinfo mm_mallinfo(FAR struct mm_heap_s *heap);
struct mallinfo_task mm_mallinfo_task(FAR struct mm_heap_s *heap,
FAR const struct malltask *task);
size_t mm_heapfree(FAR struct mm_heap_s *heap);
/* Functions contained in kmm_mallinfo.c ************************************/
#ifdef CONFIG_MM_KERNEL_HEAP

View file

@ -187,3 +187,16 @@ struct mallinfo_task mm_mallinfo_task(FAR struct mm_heap_s *heap,
return info;
}
/****************************************************************************
* Name: mm_heapfree
*
* Description:
* Return the total free size (in bytes) in the heap
*
****************************************************************************/
size_t mm_heapfree(FAR struct mm_heap_s *heap)
{
return heap->mm_heapsize - heap->mm_curused;
}

View file

@ -1463,3 +1463,16 @@ void mm_free_delaylist(FAR struct mm_heap_s *heap)
free_delaylist(heap, true);
}
}
/****************************************************************************
* Name: mm_heapfree
*
* Description:
* Return the total free size (in bytes) in the heap
*
****************************************************************************/
size_t mm_heapfree(FAR struct mm_heap_s *heap)
{
return heap->mm_heapsize - heap->mm_curused;
}

View file

@ -70,6 +70,17 @@ FAR void *calloc(size_t n, size_t elem_size)
#else
/* Use mm_calloc() because it implements the clear */
return mm_calloc(USR_HEAP, n, elem_size);
FAR void *mem = mm_calloc(USR_HEAP, n, elem_size);
if (mem == NULL)
{
set_errno(ENOMEM);
}
else
{
mm_notify_pressure(mm_heapfree(USR_HEAP));
}
return mem;
#endif
}

View file

@ -66,6 +66,10 @@ FAR void *malloc(size_t size)
{
set_errno(ENOMEM);
}
else
{
mm_notify_pressure(mm_heapfree(USR_HEAP));
}
return ret;
#endif

View file

@ -93,6 +93,10 @@ FAR void *memalign(size_t alignment, size_t size)
{
set_errno(ENOMEM);
}
else
{
mm_notify_pressure(mm_heapfree(USR_HEAP));
}
return ret;
#endif

View file

@ -95,6 +95,10 @@ FAR void *realloc(FAR void *oldmem, size_t size)
{
set_errno(ENOMEM);
}
else
{
mm_notify_pressure(mm_heapfree(USR_HEAP));
}
return ret;
#endif

View file

@ -72,6 +72,10 @@ FAR void *zalloc(size_t size)
{
set_errno(ENOMEM);
}
else
{
mm_notify_pressure(mm_heapfree(USR_HEAP));
}
return ret;
#endif