2009-05-19 17:17:28 +02:00
|
|
|
/****************************************************************************
|
2014-09-22 18:53:50 +02:00
|
|
|
* mm/mm_heap/mm_mallinfo.c
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2024-09-12 16:44:55 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2021-02-08 13:50:10 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2021-02-08 13:50:10 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2021-02-08 13:50:10 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2009-05-19 17:17:28 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2009-05-19 17:17:28 +02:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Included Files
|
2009-05-19 17:17:28 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2013-03-08 21:36:18 +01:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <assert.h>
|
2013-03-08 21:36:18 +01:00
|
|
|
#include <debug.h>
|
2012-07-15 01:31:12 +02:00
|
|
|
|
2014-09-24 15:29:09 +02:00
|
|
|
#include <nuttx/mm/mm.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2021-03-02 09:03:00 +01:00
|
|
|
#include "mm_heap/mm.h"
|
|
|
|
|
2023-05-05 10:52:30 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct mm_mallinfo_handler_s
|
|
|
|
{
|
2023-05-28 13:23:56 +02:00
|
|
|
FAR const struct malltask *task;
|
2023-05-05 10:52:30 +02:00
|
|
|
FAR struct mallinfo_task *info;
|
|
|
|
};
|
|
|
|
|
2022-01-24 07:44:38 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void mallinfo_handler(FAR struct mm_allocnode_s *node, FAR void *arg)
|
|
|
|
{
|
|
|
|
FAR struct mallinfo *info = arg;
|
2023-09-06 05:48:30 +02:00
|
|
|
size_t nodesize = MM_SIZEOF_NODE(node);
|
2022-01-24 07:44:38 +01:00
|
|
|
|
2022-12-15 12:55:16 +01:00
|
|
|
minfo("node=%p size=%zu preceding=%u (%c)\n",
|
|
|
|
node, nodesize, (unsigned int)node->preceding,
|
2023-09-06 06:30:24 +02:00
|
|
|
MM_NODE_IS_ALLOC(node) ? 'A' : 'F');
|
2022-01-24 07:44:38 +01:00
|
|
|
|
|
|
|
/* Check if the node corresponds to an allocated memory chunk */
|
|
|
|
|
2023-09-06 06:30:24 +02:00
|
|
|
if (MM_NODE_IS_ALLOC(node))
|
2022-01-24 07:44:38 +01:00
|
|
|
{
|
2023-09-06 05:48:30 +02:00
|
|
|
DEBUGASSERT(nodesize >= MM_SIZEOF_ALLOCNODE);
|
2022-01-24 07:44:38 +01:00
|
|
|
info->aordblks++;
|
2022-12-15 12:55:16 +01:00
|
|
|
info->uordblks += nodesize;
|
2022-01-24 07:44:38 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FAR struct mm_freenode_s *fnode = (FAR void *)node;
|
2024-02-22 02:32:37 +01:00
|
|
|
|
2022-12-30 09:30:55 +01:00
|
|
|
DEBUGASSERT(nodesize >= MM_MIN_CHUNK);
|
2022-01-24 07:44:38 +01:00
|
|
|
DEBUGASSERT(fnode->blink->flink == fnode);
|
2023-09-06 05:48:30 +02:00
|
|
|
DEBUGASSERT(MM_SIZEOF_NODE(fnode->blink) <= nodesize);
|
2022-01-24 07:44:38 +01:00
|
|
|
DEBUGASSERT(fnode->flink == NULL ||
|
|
|
|
fnode->flink->blink == fnode);
|
|
|
|
DEBUGASSERT(fnode->flink == NULL ||
|
2023-09-06 05:48:30 +02:00
|
|
|
MM_SIZEOF_NODE(fnode->flink) == 0 ||
|
|
|
|
MM_SIZEOF_NODE(fnode->flink) >= nodesize);
|
2022-01-24 07:44:38 +01:00
|
|
|
|
|
|
|
info->ordblks++;
|
2022-12-15 12:55:16 +01:00
|
|
|
info->fordblks += nodesize;
|
2022-12-26 07:08:00 +01:00
|
|
|
if (node->size > (size_t)info->mxordblk)
|
2022-01-24 07:44:38 +01:00
|
|
|
{
|
2022-12-15 12:55:16 +01:00
|
|
|
info->mxordblk = nodesize;
|
2022-01-24 07:44:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-22 13:21:51 +01:00
|
|
|
static void mallinfo_task_handler(FAR struct mm_allocnode_s *node,
|
|
|
|
FAR void *arg)
|
|
|
|
{
|
2023-05-28 14:09:42 +02:00
|
|
|
FAR struct mm_mallinfo_handler_s *handler = arg;
|
|
|
|
FAR const struct malltask *task = handler->task;
|
|
|
|
FAR struct mallinfo_task *info = handler->info;
|
2023-09-06 05:48:30 +02:00
|
|
|
size_t nodesize = MM_SIZEOF_NODE(node);
|
2022-01-22 13:21:51 +01:00
|
|
|
|
|
|
|
/* Check if the node corresponds to an allocated memory chunk */
|
|
|
|
|
2023-09-06 06:30:24 +02:00
|
|
|
if (MM_NODE_IS_ALLOC(node))
|
2022-01-22 13:21:51 +01:00
|
|
|
{
|
2023-09-06 05:48:30 +02:00
|
|
|
DEBUGASSERT(nodesize >= MM_SIZEOF_ALLOCNODE);
|
2022-11-20 08:00:44 +01:00
|
|
|
#if CONFIG_MM_BACKTRACE < 0
|
2023-05-28 14:09:42 +02:00
|
|
|
if (task->pid == PID_MM_ALLOC)
|
2022-01-22 13:21:51 +01:00
|
|
|
{
|
2023-05-28 14:09:42 +02:00
|
|
|
info->aordblks++;
|
|
|
|
info->uordblks += nodesize;
|
2022-01-22 13:21:51 +01:00
|
|
|
}
|
2023-05-09 07:46:05 +02:00
|
|
|
#else
|
2023-09-11 11:52:17 +02:00
|
|
|
if ((MM_DUMP_ASSIGN(task->pid, node->pid) ||
|
|
|
|
MM_DUMP_ALLOC(task->pid, node->pid) ||
|
|
|
|
MM_DUMP_LEAK(task->pid, node->pid)) &&
|
2023-05-28 14:09:42 +02:00
|
|
|
node->seqno >= task->seqmin && node->seqno <= task->seqmax)
|
2023-05-09 07:46:05 +02:00
|
|
|
{
|
2023-05-28 14:09:42 +02:00
|
|
|
info->aordblks++;
|
|
|
|
info->uordblks += nodesize;
|
2023-05-09 07:46:05 +02:00
|
|
|
}
|
|
|
|
#endif
|
2022-01-22 13:21:51 +01:00
|
|
|
}
|
2023-05-28 14:09:42 +02:00
|
|
|
else if (task->pid == PID_MM_FREE)
|
2022-11-20 08:00:44 +01:00
|
|
|
{
|
2023-05-28 14:09:42 +02:00
|
|
|
info->aordblks++;
|
|
|
|
info->uordblks += nodesize;
|
2022-11-20 08:00:44 +01:00
|
|
|
}
|
2022-01-22 13:21:51 +01:00
|
|
|
}
|
|
|
|
|
2014-08-31 18:54:55 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2009-05-19 17:17:28 +02:00
|
|
|
/****************************************************************************
|
2013-03-08 21:36:18 +01:00
|
|
|
* Name: mm_mallinfo
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2013-03-08 19:29:56 +01:00
|
|
|
* mallinfo returns a copy of updated current heap information.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2009-05-19 17:17:28 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2023-06-02 10:12:27 +02:00
|
|
|
struct mallinfo mm_mallinfo(FAR struct mm_heap_s *heap)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2023-06-02 10:12:27 +02:00
|
|
|
struct mallinfo info;
|
2024-06-18 06:26:27 +02:00
|
|
|
#ifdef CONFIG_MM_HEAP_MEMPOOL
|
2023-06-02 10:12:27 +02:00
|
|
|
struct mallinfo poolinfo;
|
2007-03-04 16:23:22 +01:00
|
|
|
#endif
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2023-06-02 10:12:27 +02:00
|
|
|
memset(&info, 0, sizeof(info));
|
|
|
|
mm_foreach(heap, mallinfo_handler, &info);
|
|
|
|
info.arena = heap->mm_heapsize;
|
2023-05-28 18:03:12 +02:00
|
|
|
info.arena += sizeof(struct mm_heap_s);
|
|
|
|
info.uordblks += sizeof(struct mm_heap_s);
|
2023-10-31 09:37:14 +01:00
|
|
|
info.usmblks = heap->mm_maxused + sizeof(struct mm_heap_s);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2024-06-18 06:26:27 +02:00
|
|
|
#ifdef CONFIG_MM_HEAP_MEMPOOL
|
2023-06-02 10:12:27 +02:00
|
|
|
poolinfo = mempool_multiple_mallinfo(heap->mm_mpool);
|
2007-03-04 16:23:22 +01:00
|
|
|
|
2023-06-02 10:12:27 +02:00
|
|
|
info.uordblks -= poolinfo.fordblks;
|
|
|
|
info.fordblks += poolinfo.fordblks;
|
|
|
|
#endif
|
2022-12-22 09:02:42 +01:00
|
|
|
|
2023-05-28 18:03:12 +02:00
|
|
|
DEBUGASSERT(info.uordblks + info.fordblks == info.arena);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2023-06-02 10:12:27 +02:00
|
|
|
return info;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
2022-01-22 13:21:51 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mm_mallinfo_task
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* mallinfo returns a copy of updated current heap information for task
|
|
|
|
* with pid.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-05-05 10:52:30 +02:00
|
|
|
struct mallinfo_task mm_mallinfo_task(FAR struct mm_heap_s *heap,
|
2023-05-28 13:23:56 +02:00
|
|
|
FAR const struct malltask *task)
|
2022-01-22 13:21:51 +01:00
|
|
|
{
|
2023-05-05 10:52:30 +02:00
|
|
|
struct mm_mallinfo_handler_s handle;
|
|
|
|
struct mallinfo_task info =
|
|
|
|
{
|
|
|
|
0, 0
|
|
|
|
};
|
2022-11-06 16:27:44 +01:00
|
|
|
|
2024-06-18 06:26:27 +02:00
|
|
|
#ifdef CONFIG_MM_HEAP_MEMPOOL
|
2023-05-28 13:23:56 +02:00
|
|
|
info = mempool_multiple_info_task(heap->mm_mpool, task);
|
2022-11-06 16:27:44 +01:00
|
|
|
#endif
|
|
|
|
|
2023-05-28 13:23:56 +02:00
|
|
|
handle.task = task;
|
2023-05-05 10:52:30 +02:00
|
|
|
handle.info = &info;
|
|
|
|
mm_foreach(heap, mallinfo_task_handler, &handle);
|
|
|
|
|
|
|
|
return info;
|
2022-01-22 13:21:51 +01:00
|
|
|
}
|
2024-01-24 16:33:02 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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;
|
|
|
|
}
|
2024-06-30 10:10:22 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mm_heapfree_largest
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Return the largest chunk of contiguous memory in the heap
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
size_t mm_heapfree_largest(FAR struct mm_heap_s *heap)
|
|
|
|
{
|
|
|
|
FAR struct mm_freenode_s *node;
|
|
|
|
for (node = heap->mm_nodelist[MM_NNODES - 1].blink; node;
|
|
|
|
node = node->blink)
|
|
|
|
{
|
|
|
|
size_t nodesize = MM_SIZEOF_NODE(node);
|
|
|
|
if (nodesize != 0)
|
|
|
|
{
|
|
|
|
return nodesize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|