2017-11-13 16:08:39 +01:00
|
|
|
/****************************************************************************
|
2017-11-13 20:33:12 +01:00
|
|
|
* fs/procfs/fs_procfsmeminfo.c
|
2017-11-13 16:08:39 +01:00
|
|
|
*
|
2020-04-11 19:17:55 +02: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
|
2017-11-13 16:08:39 +01:00
|
|
|
*
|
2020-04-11 19:17:55 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-11-13 16:08:39 +01:00
|
|
|
*
|
2020-04-11 19:17:55 +02: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.
|
2017-11-13 16:08:39 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
2017-11-14 21:59:51 +01:00
|
|
|
#include <nuttx/pgalloc.h>
|
2018-04-20 23:55:16 +02:00
|
|
|
#include <nuttx/progmem.h>
|
2017-11-13 20:33:12 +01:00
|
|
|
#include <nuttx/mm/mm.h>
|
2017-11-13 16:08:39 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
#include <nuttx/fs/procfs.h>
|
|
|
|
|
2017-11-13 20:33:12 +01:00
|
|
|
#ifndef CONFIG_FS_PROCFS_EXCLUDE_MEMINFO
|
2017-11-13 16:08:39 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
2019-12-01 20:01:16 +01:00
|
|
|
|
2017-11-13 16:08:39 +01:00
|
|
|
/* Determines the size of an intermediate buffer that must be large enough
|
|
|
|
* to handle the longest line generated by this logic.
|
|
|
|
*/
|
|
|
|
|
2021-04-28 05:05:20 +02:00
|
|
|
#define MEMINFO_LINELEN 80
|
2017-11-13 16:08:39 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This structure describes one open "file" */
|
|
|
|
|
2017-11-13 20:33:12 +01:00
|
|
|
struct meminfo_file_s
|
2017-11-13 16:08:39 +01:00
|
|
|
{
|
|
|
|
struct procfs_file_s base; /* Base open file structure */
|
|
|
|
unsigned int linesize; /* Number of valid characters in line[] */
|
2017-11-13 20:33:12 +01:00
|
|
|
char line[MEMINFO_LINELEN]; /* Pre-allocated buffer for formatted lines */
|
2017-11-13 16:08:39 +01:00
|
|
|
};
|
|
|
|
|
2017-11-13 20:33:12 +01:00
|
|
|
#if defined(CONFIG_ARCH_HAVE_PROGMEM) && defined(CONFIG_FS_PROCFS_INCLUDE_PROGMEM)
|
2017-11-13 16:08:39 +01:00
|
|
|
struct progmem_info_s
|
|
|
|
{
|
|
|
|
int arena; /* Total size of available progmem. */
|
|
|
|
int ordblks; /* This is the number of free chunks */
|
2021-04-28 05:05:20 +02:00
|
|
|
int aordblks; /* This is the number of allocated chunks */
|
2017-11-13 16:08:39 +01:00
|
|
|
int mxordblk; /* Size of the largest free chunk */
|
|
|
|
int uordblks; /* Total size of memory for allocated chunks */
|
2019-03-01 22:00:00 +01:00
|
|
|
int fordblks; /* Total size of memory for free chunks. */
|
2017-11-13 16:08:39 +01:00
|
|
|
};
|
2017-11-13 20:33:12 +01:00
|
|
|
#endif
|
2017-11-13 16:08:39 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-11-13 20:33:12 +01:00
|
|
|
#if defined(CONFIG_ARCH_HAVE_PROGMEM) && defined(CONFIG_FS_PROCFS_INCLUDE_PROGMEM)
|
|
|
|
static void meminfo_progmem(FAR struct progmem_info_s *progmem);
|
|
|
|
#endif
|
2017-11-13 16:08:39 +01:00
|
|
|
|
|
|
|
/* File system methods */
|
|
|
|
|
2017-11-13 20:33:12 +01:00
|
|
|
static int meminfo_open(FAR struct file *filep, FAR const char *relpath,
|
2017-11-13 16:08:39 +01:00
|
|
|
int oflags, mode_t mode);
|
2017-11-13 20:33:12 +01:00
|
|
|
static int meminfo_close(FAR struct file *filep);
|
2022-01-06 13:44:17 +01:00
|
|
|
#ifndef CONFIG_FS_PROCFS_EXCLUDE_MEMDUMP
|
|
|
|
static ssize_t memdump_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
static ssize_t memdump_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
#endif
|
2017-11-13 20:33:12 +01:00
|
|
|
static ssize_t meminfo_read(FAR struct file *filep, FAR char *buffer,
|
2017-11-13 16:08:39 +01:00
|
|
|
size_t buflen);
|
2017-11-13 20:33:12 +01:00
|
|
|
static int meminfo_dup(FAR const struct file *oldp,
|
2017-11-13 16:08:39 +01:00
|
|
|
FAR struct file *newp);
|
2017-11-13 20:33:12 +01:00
|
|
|
static int meminfo_stat(FAR const char *relpath, FAR struct stat *buf);
|
2017-11-13 16:08:39 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* See fs_mount.c -- this structure is explicitly externed there.
|
|
|
|
* We use the old-fashioned kind of initializers so that this will compile
|
|
|
|
* with any compiler.
|
|
|
|
*/
|
|
|
|
|
2017-11-13 20:33:12 +01:00
|
|
|
const struct procfs_operations meminfo_operations =
|
2017-11-13 16:08:39 +01:00
|
|
|
{
|
2017-11-13 20:33:12 +01:00
|
|
|
meminfo_open, /* open */
|
|
|
|
meminfo_close, /* close */
|
|
|
|
meminfo_read, /* read */
|
2017-11-13 16:08:39 +01:00
|
|
|
NULL, /* write */
|
2017-11-13 20:33:12 +01:00
|
|
|
meminfo_dup, /* dup */
|
2017-11-13 16:08:39 +01:00
|
|
|
NULL, /* opendir */
|
|
|
|
NULL, /* closedir */
|
|
|
|
NULL, /* readdir */
|
|
|
|
NULL, /* rewinddir */
|
2017-11-13 20:33:12 +01:00
|
|
|
meminfo_stat /* stat */
|
2017-11-13 16:08:39 +01:00
|
|
|
};
|
|
|
|
|
2022-01-06 13:44:17 +01:00
|
|
|
#ifndef CONFIG_FS_PROCFS_EXCLUDE_MEMDUMP
|
|
|
|
const struct procfs_operations memdump_operations =
|
|
|
|
{
|
|
|
|
meminfo_open, /* open */
|
|
|
|
meminfo_close, /* close */
|
|
|
|
memdump_read, /* read */
|
|
|
|
memdump_write, /* write */
|
|
|
|
meminfo_dup, /* dup */
|
|
|
|
NULL, /* opendir */
|
|
|
|
NULL, /* closedir */
|
|
|
|
NULL, /* readdir */
|
|
|
|
NULL, /* rewinddir */
|
|
|
|
meminfo_stat /* stat */
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2021-02-12 05:50:12 +01:00
|
|
|
FAR struct procfs_meminfo_entry_s *g_procfs_meminfo = NULL;
|
|
|
|
|
2017-11-13 16:08:39 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-11-13 20:33:12 +01:00
|
|
|
* Name: meminfo_progmem
|
2017-11-13 16:08:39 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The moral equivalent of mallinfo() for prog mem
|
|
|
|
*
|
|
|
|
* TODO Max block size only works on uniform prog mem
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-11-13 20:33:12 +01:00
|
|
|
#if defined(CONFIG_ARCH_HAVE_PROGMEM) && defined(CONFIG_FS_PROCFS_INCLUDE_PROGMEM)
|
|
|
|
static void meminfo_progmem(FAR struct progmem_info_s *progmem)
|
2017-11-13 16:08:39 +01:00
|
|
|
{
|
|
|
|
size_t page = 0;
|
|
|
|
size_t stpage = 0xffff;
|
|
|
|
size_t pagesize = 0;
|
|
|
|
ssize_t status;
|
|
|
|
|
|
|
|
progmem->arena = 0;
|
2021-04-28 05:05:20 +02:00
|
|
|
progmem->ordblks = 0;
|
|
|
|
progmem->aordblks = 0;
|
2017-11-13 16:08:39 +01:00
|
|
|
progmem->fordblks = 0;
|
|
|
|
progmem->uordblks = 0;
|
|
|
|
progmem->mxordblk = 0;
|
|
|
|
|
|
|
|
for (status = 0, page = 0; status >= 0; page++)
|
|
|
|
{
|
|
|
|
status = up_progmem_ispageerased(page);
|
|
|
|
pagesize = up_progmem_pagesize(page);
|
|
|
|
|
|
|
|
progmem->arena += pagesize;
|
|
|
|
|
|
|
|
/* Is this beginning of new free space section */
|
|
|
|
|
|
|
|
if (status == 0)
|
|
|
|
{
|
|
|
|
if (stpage == 0xffff)
|
|
|
|
{
|
|
|
|
stpage = page;
|
|
|
|
}
|
|
|
|
|
|
|
|
progmem->fordblks += pagesize;
|
2021-07-12 16:59:46 +02:00
|
|
|
progmem->ordblks++;
|
2017-11-13 16:08:39 +01:00
|
|
|
}
|
|
|
|
else if (status != 0)
|
|
|
|
{
|
2021-04-28 05:05:20 +02:00
|
|
|
progmem->aordblks++;
|
2017-11-13 16:08:39 +01:00
|
|
|
progmem->uordblks += pagesize;
|
|
|
|
|
|
|
|
if (stpage != 0xffff && up_progmem_isuniform())
|
|
|
|
{
|
|
|
|
stpage = page - stpage;
|
|
|
|
if (stpage > progmem->mxordblk)
|
|
|
|
{
|
|
|
|
progmem->mxordblk = stpage;
|
|
|
|
}
|
|
|
|
|
|
|
|
stpage = 0xffff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
progmem->mxordblk *= pagesize;
|
|
|
|
}
|
2017-11-13 20:33:12 +01:00
|
|
|
#endif
|
2017-11-13 16:08:39 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
2017-11-13 20:33:12 +01:00
|
|
|
* Name: meminfo_open
|
2017-11-13 16:08:39 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2017-11-13 20:33:12 +01:00
|
|
|
static int meminfo_open(FAR struct file *filep, FAR const char *relpath,
|
|
|
|
int oflags, mode_t mode)
|
2017-11-13 16:08:39 +01:00
|
|
|
{
|
2017-11-13 20:33:12 +01:00
|
|
|
FAR struct meminfo_file_s *procfile;
|
2017-11-13 16:08:39 +01:00
|
|
|
|
|
|
|
finfo("Open '%s'\n", relpath);
|
|
|
|
|
|
|
|
/* Allocate a container to hold the file attributes */
|
|
|
|
|
2022-01-06 13:44:17 +01:00
|
|
|
procfile = kmm_zalloc(sizeof(struct meminfo_file_s));
|
2017-11-13 20:33:12 +01:00
|
|
|
if (!procfile)
|
2017-11-13 16:08:39 +01:00
|
|
|
{
|
|
|
|
ferr("ERROR: Failed to allocate file attributes\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the attributes as the open-specific state in filep->f_priv */
|
|
|
|
|
2022-01-06 13:44:17 +01:00
|
|
|
filep->f_priv = procfile;
|
2017-11-13 16:08:39 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-11-13 20:33:12 +01:00
|
|
|
* Name: meminfo_close
|
2017-11-13 16:08:39 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2017-11-13 20:33:12 +01:00
|
|
|
static int meminfo_close(FAR struct file *filep)
|
2017-11-13 16:08:39 +01:00
|
|
|
{
|
2017-11-13 20:33:12 +01:00
|
|
|
FAR struct meminfo_file_s *procfile;
|
2017-11-13 16:08:39 +01:00
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
2017-11-13 20:33:12 +01:00
|
|
|
procfile = (FAR struct meminfo_file_s *)filep->f_priv;
|
2017-11-13 16:08:39 +01:00
|
|
|
DEBUGASSERT(procfile);
|
|
|
|
|
|
|
|
/* Release the file attributes structure */
|
|
|
|
|
|
|
|
kmm_free(procfile);
|
|
|
|
filep->f_priv = NULL;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-11-13 20:33:12 +01:00
|
|
|
* Name: meminfo_read
|
2017-11-13 16:08:39 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2017-11-13 20:33:12 +01:00
|
|
|
static ssize_t meminfo_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen)
|
2017-11-13 16:08:39 +01:00
|
|
|
{
|
2017-11-13 20:33:12 +01:00
|
|
|
FAR struct meminfo_file_s *procfile;
|
2017-11-13 16:08:39 +01:00
|
|
|
size_t linesize;
|
|
|
|
size_t copysize;
|
|
|
|
size_t totalsize;
|
|
|
|
off_t offset;
|
|
|
|
|
|
|
|
finfo("buffer=%p buflen=%d\n", buffer, (int)buflen);
|
|
|
|
|
|
|
|
DEBUGASSERT(filep != NULL && buffer != NULL && buflen > 0);
|
|
|
|
offset = filep->f_pos;
|
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
2017-11-13 20:33:12 +01:00
|
|
|
procfile = (FAR struct meminfo_file_s *)filep->f_priv;
|
2017-11-13 16:08:39 +01:00
|
|
|
DEBUGASSERT(procfile);
|
|
|
|
|
|
|
|
/* The first line is the headers */
|
|
|
|
|
2021-07-06 10:35:55 +02:00
|
|
|
linesize = procfs_snprintf(procfile->line, MEMINFO_LINELEN,
|
|
|
|
"%13s%11s%11s%11s%11s%7s%7s\n", "", "total",
|
|
|
|
"used", "free", "largest", "nused", "nfree");
|
2020-04-11 19:43:37 +02:00
|
|
|
|
2017-11-13 16:08:39 +01:00
|
|
|
copysize = procfs_memcpy(procfile->line, linesize, buffer, buflen,
|
|
|
|
&offset);
|
|
|
|
totalsize = copysize;
|
|
|
|
|
2017-11-13 20:33:12 +01:00
|
|
|
/* Followed by information about the memory resources */
|
|
|
|
|
2021-02-12 05:50:12 +01:00
|
|
|
FAR const struct procfs_meminfo_entry_s *entry;
|
|
|
|
|
|
|
|
for (entry = g_procfs_meminfo; entry != NULL; entry = entry->next)
|
|
|
|
{
|
|
|
|
if (totalsize < buflen)
|
|
|
|
{
|
|
|
|
struct mallinfo minfo;
|
|
|
|
|
|
|
|
buffer += copysize;
|
|
|
|
buflen -= copysize;
|
|
|
|
|
|
|
|
/* Show heap information */
|
|
|
|
|
2022-03-17 04:20:20 +01:00
|
|
|
mm_mallinfo(entry->heap, &minfo);
|
2021-04-14 16:37:52 +02:00
|
|
|
linesize = procfs_snprintf(procfile->line, MEMINFO_LINELEN,
|
2021-07-06 10:35:55 +02:00
|
|
|
"%12s:%11lu%11lu%11lu%11lu%7lu%7lu\n",
|
2021-04-14 16:37:52 +02:00
|
|
|
entry->name,
|
|
|
|
(unsigned long)minfo.arena,
|
|
|
|
(unsigned long)minfo.uordblks,
|
|
|
|
(unsigned long)minfo.fordblks,
|
|
|
|
(unsigned long)minfo.mxordblk,
|
|
|
|
(unsigned long)minfo.aordblks,
|
|
|
|
(unsigned long)minfo.ordblks);
|
2021-02-12 05:50:12 +01:00
|
|
|
copysize = procfs_memcpy(procfile->line, linesize, buffer,
|
|
|
|
buflen, &offset);
|
|
|
|
totalsize += copysize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-14 21:59:51 +01:00
|
|
|
#ifdef CONFIG_MM_PGALLOC
|
|
|
|
if (totalsize < buflen)
|
|
|
|
{
|
|
|
|
struct pginfo_s pginfo;
|
|
|
|
unsigned long total;
|
|
|
|
unsigned long available;
|
|
|
|
unsigned long allocated;
|
|
|
|
unsigned long max;
|
|
|
|
|
|
|
|
buffer += copysize;
|
|
|
|
buflen -= copysize;
|
|
|
|
|
|
|
|
/* Show page allocator information */
|
|
|
|
|
|
|
|
mm_pginfo(&pginfo);
|
|
|
|
|
|
|
|
total = (unsigned long)pginfo.ntotal << MM_PGSHIFT;
|
|
|
|
available = (unsigned long)pginfo.nfree << MM_PGSHIFT;
|
|
|
|
allocated = total - available;
|
|
|
|
max = (unsigned long)pginfo.mxfree << MM_PGSHIFT;
|
|
|
|
|
2021-04-14 16:37:52 +02:00
|
|
|
linesize = procfs_snprintf(procfile->line, MEMINFO_LINELEN,
|
2021-07-06 10:35:55 +02:00
|
|
|
"%12s:%11lu%11lu%11lu%11lu\n",
|
|
|
|
"Page", total, allocated, available, max);
|
2017-11-14 21:59:51 +01:00
|
|
|
|
2017-11-13 20:33:12 +01:00
|
|
|
copysize = procfs_memcpy(procfile->line, linesize, buffer, buflen,
|
|
|
|
&offset);
|
|
|
|
totalsize += copysize;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CONFIG_ARCH_HAVE_PROGMEM) && defined(CONFIG_FS_PROCFS_INCLUDE_PROGMEM)
|
2017-11-13 16:08:39 +01:00
|
|
|
if (totalsize < buflen)
|
|
|
|
{
|
2017-11-14 21:59:51 +01:00
|
|
|
struct progmem_info_s progmem;
|
|
|
|
|
|
|
|
buffer += copysize;
|
|
|
|
buflen -= copysize;
|
2017-11-13 16:08:39 +01:00
|
|
|
|
|
|
|
/* The second line is the memory data */
|
|
|
|
|
2017-11-13 20:33:12 +01:00
|
|
|
meminfo_progmem(&progmem);
|
2017-11-13 16:08:39 +01:00
|
|
|
|
2021-04-14 16:37:52 +02:00
|
|
|
linesize = procfs_snprintf(procfile->line, MEMINFO_LINELEN,
|
2021-07-06 10:35:55 +02:00
|
|
|
"%12s:%11lu%11lu%11lu%11lu%7lu%7lu\n",
|
|
|
|
"Prog",
|
2021-04-14 16:37:52 +02:00
|
|
|
(unsigned long)progmem.arena,
|
|
|
|
(unsigned long)progmem.uordblks,
|
|
|
|
(unsigned long)progmem.fordblks,
|
|
|
|
(unsigned long)progmem.mxordblk,
|
|
|
|
(unsigned long)progmem.aordblks,
|
|
|
|
(unsigned long)progmem.ordblks);
|
2017-11-13 16:08:39 +01:00
|
|
|
copysize = procfs_memcpy(procfile->line, linesize, buffer, buflen,
|
|
|
|
&offset);
|
|
|
|
totalsize += copysize;
|
|
|
|
}
|
2017-11-13 20:33:12 +01:00
|
|
|
#endif
|
2017-11-13 16:08:39 +01:00
|
|
|
|
|
|
|
/* Update the file offset */
|
|
|
|
|
|
|
|
filep->f_pos += totalsize;
|
|
|
|
return totalsize;
|
|
|
|
}
|
|
|
|
|
2022-01-06 13:44:17 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: memdump_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_FS_PROCFS_EXCLUDE_MEMDUMP
|
|
|
|
static ssize_t memdump_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
|
|
|
FAR struct meminfo_file_s *procfile;
|
|
|
|
size_t linesize;
|
|
|
|
size_t copysize;
|
|
|
|
size_t totalsize;
|
|
|
|
off_t offset;
|
|
|
|
|
|
|
|
finfo("buffer=%p buflen=%d\n", buffer, (int)buflen);
|
|
|
|
|
|
|
|
DEBUGASSERT(filep != NULL && buffer != NULL && buflen > 0);
|
|
|
|
offset = filep->f_pos;
|
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
|
|
|
procfile = (FAR struct meminfo_file_s *)filep->f_priv;
|
|
|
|
DEBUGASSERT(procfile);
|
|
|
|
|
2022-07-04 09:41:06 +02:00
|
|
|
#if CONFIG_MM_BACKTRACE >= 0
|
2022-01-20 10:17:29 +01:00
|
|
|
linesize = procfs_snprintf(procfile->line, MEMINFO_LINELEN,
|
2022-03-17 07:02:37 +01:00
|
|
|
"usage: <pid/used/free/on/off>\n"
|
|
|
|
"on/off backtrace\n"
|
2022-01-20 10:17:29 +01:00
|
|
|
"pid: dump pid allocated node\n");
|
|
|
|
#else
|
2022-01-06 13:44:17 +01:00
|
|
|
linesize = procfs_snprintf(procfile->line, MEMINFO_LINELEN,
|
|
|
|
"usage: <used/free>\n");
|
2022-01-20 10:17:29 +01:00
|
|
|
#endif
|
2022-01-06 13:44:17 +01:00
|
|
|
|
|
|
|
copysize = procfs_memcpy(procfile->line, linesize, buffer, buflen,
|
|
|
|
&offset);
|
|
|
|
totalsize = copysize;
|
|
|
|
buffer += copysize;
|
|
|
|
buflen -= copysize;
|
2022-01-20 10:17:29 +01:00
|
|
|
linesize = procfs_snprintf(procfile->line, MEMINFO_LINELEN,
|
2022-01-06 13:44:17 +01:00
|
|
|
"used: dump all allocated node\n"
|
|
|
|
"free: dump all free node\n");
|
|
|
|
|
|
|
|
totalsize += procfs_memcpy(procfile->line, linesize, buffer, buflen,
|
|
|
|
&offset);
|
|
|
|
filep->f_pos += totalsize;
|
|
|
|
return totalsize;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: memdump_write
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_FS_PROCFS_EXCLUDE_MEMDUMP
|
|
|
|
static ssize_t memdump_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
2022-03-17 07:02:37 +01:00
|
|
|
FAR struct procfs_meminfo_entry_s *entry;
|
2022-01-06 13:44:17 +01:00
|
|
|
FAR struct meminfo_file_s *procfile;
|
2022-03-21 23:47:09 +01:00
|
|
|
pid_t pid = INVALID_PROCESS_ID;
|
2022-01-06 13:44:17 +01:00
|
|
|
|
|
|
|
DEBUGASSERT(filep != NULL && buffer != NULL && buflen > 0);
|
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
|
|
|
procfile = filep->f_priv;
|
|
|
|
DEBUGASSERT(procfile);
|
|
|
|
|
2022-07-04 09:41:06 +02:00
|
|
|
#if CONFIG_MM_BACKTRACE > 0
|
2022-03-17 07:02:37 +01:00
|
|
|
if (strcmp(buffer, "on") == 0)
|
|
|
|
{
|
|
|
|
for (entry = g_procfs_meminfo; entry != NULL; entry = entry->next)
|
|
|
|
{
|
|
|
|
entry->backtrace = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return buflen;
|
|
|
|
}
|
|
|
|
else if (strcmp(buffer, "off") == 0)
|
|
|
|
{
|
|
|
|
for (entry = g_procfs_meminfo; entry != NULL; entry = entry->next)
|
|
|
|
{
|
|
|
|
entry->backtrace = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return buflen;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-01-06 13:44:17 +01:00
|
|
|
switch (buffer[0])
|
|
|
|
{
|
|
|
|
case 'u':
|
2022-03-21 23:47:09 +01:00
|
|
|
pid = (pid_t)-1;
|
2022-01-06 13:44:17 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
2022-03-21 23:47:09 +01:00
|
|
|
pid = (pid_t)-2;
|
2022-01-06 13:44:17 +01:00
|
|
|
break;
|
2022-07-04 09:41:06 +02:00
|
|
|
#if CONFIG_MM_BACKTRACE >= 0
|
2022-01-20 10:17:29 +01:00
|
|
|
default:
|
|
|
|
pid = atoi(buffer);
|
|
|
|
#endif
|
2022-01-06 13:44:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (entry = g_procfs_meminfo; entry != NULL; entry = entry->next)
|
|
|
|
{
|
2022-03-17 04:20:20 +01:00
|
|
|
mm_memdump(entry->heap, pid);
|
2022-01-06 13:44:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return buflen;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-11-13 16:08:39 +01:00
|
|
|
/****************************************************************************
|
2017-11-13 20:33:12 +01:00
|
|
|
* Name: meminfo_dup
|
2017-11-13 16:08:39 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Duplicate open file data in the new file structure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-11-13 20:33:12 +01:00
|
|
|
static int meminfo_dup(FAR const struct file *oldp, FAR struct file *newp)
|
2017-11-13 16:08:39 +01:00
|
|
|
{
|
2017-11-13 20:33:12 +01:00
|
|
|
FAR struct meminfo_file_s *oldattr;
|
|
|
|
FAR struct meminfo_file_s *newattr;
|
2017-11-13 16:08:39 +01:00
|
|
|
|
|
|
|
finfo("Dup %p->%p\n", oldp, newp);
|
|
|
|
|
|
|
|
/* Recover our private data from the old struct file instance */
|
|
|
|
|
2017-11-13 20:33:12 +01:00
|
|
|
oldattr = (FAR struct meminfo_file_s *)oldp->f_priv;
|
2017-11-13 16:08:39 +01:00
|
|
|
DEBUGASSERT(oldattr);
|
|
|
|
|
|
|
|
/* Allocate a new container to hold the task and attribute selection */
|
|
|
|
|
2017-11-13 20:33:12 +01:00
|
|
|
newattr = (FAR struct meminfo_file_s *)
|
|
|
|
kmm_malloc(sizeof(struct meminfo_file_s));
|
2017-11-13 16:08:39 +01:00
|
|
|
if (!newattr)
|
|
|
|
{
|
|
|
|
ferr("ERROR: Failed to allocate file attributes\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The copy the file attributes from the old attributes to the new */
|
|
|
|
|
2017-11-13 20:33:12 +01:00
|
|
|
memcpy(newattr, oldattr, sizeof(struct meminfo_file_s));
|
2017-11-13 16:08:39 +01:00
|
|
|
|
|
|
|
/* Save the new attributes in the new file structure */
|
|
|
|
|
|
|
|
newp->f_priv = (FAR void *)newattr;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-11-13 20:33:12 +01:00
|
|
|
* Name: meminfo_stat
|
2017-11-13 16:08:39 +01:00
|
|
|
*
|
|
|
|
* Description: Return information about a file or directory
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-11-13 20:33:12 +01:00
|
|
|
static int meminfo_stat(FAR const char *relpath, FAR struct stat *buf)
|
2017-11-13 16:08:39 +01:00
|
|
|
{
|
2017-11-13 20:33:12 +01:00
|
|
|
/* "meminfo" is the name for a read-only file */
|
2017-11-13 16:08:39 +01:00
|
|
|
|
|
|
|
memset(buf, 0, sizeof(struct stat));
|
|
|
|
buf->st_mode = S_IFREG | S_IROTH | S_IRGRP | S_IRUSR;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-02-12 05:50:12 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: procfs_register_meminfo
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Add a new meminfo entry to the procfs file system.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* entry - Describes the entry to be registered.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void procfs_register_meminfo(FAR struct procfs_meminfo_entry_s *entry)
|
|
|
|
{
|
|
|
|
entry->next = g_procfs_meminfo;
|
|
|
|
g_procfs_meminfo = entry;
|
|
|
|
}
|
|
|
|
|
2022-06-10 03:47:41 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: procfs_unregister_meminfo
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Remove a meminfo entry from the procfs file system.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* entry - Describes the entry to be unregistered.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void procfs_unregister_meminfo(FAR struct procfs_meminfo_entry_s *entry)
|
|
|
|
{
|
|
|
|
FAR struct procfs_meminfo_entry_s **cur;
|
|
|
|
|
|
|
|
for (cur = &g_procfs_meminfo; *cur != NULL; cur = &(*cur)->next)
|
|
|
|
{
|
|
|
|
if (*cur == entry)
|
|
|
|
{
|
|
|
|
*cur = entry->next;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-13 20:33:12 +01:00
|
|
|
#endif /* !CONFIG_FS_PROCFS_EXCLUDE_MEMINFO */
|