You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2022/02/08 02:27:47 UTC

[incubator-nuttx] branch master updated: sched: Implement sysinfo function

This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new ab3b0d0  sched: Implement sysinfo function
ab3b0d0 is described below

commit ab3b0d0162c3282a2510e85c908c5421dc600f40
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Sat Feb 5 20:05:29 2022 +0800

    sched: Implement sysinfo function
    
    specify here:
    https://man7.org/linux/man-pages/man2/sysinfo.2.html
    
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
 include/sys/syscall_lookup.h |   2 +
 include/sys/sysinfo.h        |  78 +++++++++++++++++++++++++++++++++
 sched/sched/Make.defs        |   1 +
 sched/sched/sched_sysinfo.c  | 100 +++++++++++++++++++++++++++++++++++++++++++
 syscall/syscall.csv          |   1 +
 5 files changed, 182 insertions(+)

diff --git a/include/sys/syscall_lookup.h b/include/sys/syscall_lookup.h
index 8b677fa..9275261 100644
--- a/include/sys/syscall_lookup.h
+++ b/include/sys/syscall_lookup.h
@@ -54,6 +54,8 @@ SYSCALL_LOOKUP(nxsched_get_stackinfo,      2)
   SYSCALL_LOOKUP(sched_setaffinity,        3)
 #endif
 
+SYSCALL_LOOKUP(sysinfo,                    1)
+
 SYSCALL_LOOKUP(gethostname,                2)
 SYSCALL_LOOKUP(sethostname,                2)
 
diff --git a/include/sys/sysinfo.h b/include/sys/sysinfo.h
new file mode 100644
index 0000000..822bfce
--- /dev/null
+++ b/include/sys/sysinfo.h
@@ -0,0 +1,78 @@
+/****************************************************************************
+ * include/sys/sysinfo.h
+ *
+ * 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
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_SYS_SYSINFO_H
+#define __INCLUDE_SYS_SYSINFO_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define SI_LOAD_SHIFT 16
+
+/****************************************************************************
+ * Type Definitions
+ ****************************************************************************/
+
+struct sysinfo
+{
+  unsigned long uptime;
+  unsigned long loads[3];
+  unsigned long totalram;
+  unsigned long freeram;
+  unsigned long sharedram;
+  unsigned long bufferram;
+  unsigned long totalswap;
+  unsigned long freeswap;
+  unsigned short procs;
+  unsigned short pad;
+  unsigned long totalhigh;
+  unsigned long freehigh;
+  unsigned mem_unit;
+  char __reserved[256];
+};
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+#undef EXTERN
+#if defined(__cplusplus)
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+int sysinfo(FAR struct sysinfo *info);
+
+#undef EXTERN
+#if defined(__cplusplus)
+}
+#endif
+
+#endif /* __INCLUDE_SYS_SYSINFO_H */
diff --git a/sched/sched/Make.defs b/sched/sched/Make.defs
index 6477f63..5d2ecad 100644
--- a/sched/sched/Make.defs
+++ b/sched/sched/Make.defs
@@ -29,6 +29,7 @@ CSRCS += sched_setscheduler.c sched_getscheduler.c
 CSRCS += sched_yield.c sched_rrgetinterval.c sched_foreach.c
 CSRCS += sched_lock.c sched_unlock.c sched_lockcount.c
 CSRCS += sched_idletask.c sched_self.c sched_get_stackinfo.c
+CSRCS += sched_sysinfo.c
 
 ifeq ($(CONFIG_PRIORITY_INHERITANCE),y)
 CSRCS += sched_reprioritize.c
diff --git a/sched/sched/sched_sysinfo.c b/sched/sched/sched_sysinfo.c
new file mode 100644
index 0000000..5562ebd
--- /dev/null
+++ b/sched/sched/sched_sysinfo.c
@@ -0,0 +1,100 @@
+/****************************************************************************
+ * sched/sched/sched_sysinfo.c
+ *
+ * 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
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <sys/sysinfo.h>
+#include <sys/types.h>
+#include <errno.h>
+#include <string.h>
+
+#include <nuttx/clock.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/pgalloc.h>
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: sysinfo
+ *
+ * Description:
+ *   sysinfo() returns certain statistics on memory and swap usage,
+ *   as well as the load average.
+ *
+ ****************************************************************************/
+
+int sysinfo(FAR struct sysinfo *info)
+{
+#ifdef CONFIG_SCHED_CPULOAD
+  struct cpuload_s cpuload;
+#endif
+#ifdef CONFIG_MM_PGALLOC
+  struct pginfo_s pginfo;
+#endif
+  struct mallinfo minfo;
+
+  if (info == NULL)
+    {
+      set_errno(EINVAL);
+      return -1;
+    }
+
+  memset(info, 0, sizeof(*info));
+
+#ifdef CONFIG_SCHED_CPULOAD
+  clock_cpuload(0, &cpuload);
+
+  info->loads[0] = ((cpuload.total - cpuload.active) <<
+                     SI_LOAD_SHIFT) / cpuload.total;
+  info->loads[1] = info->loads[0];
+  info->loads[2] = info->loads[0];
+#endif
+
+#ifdef MM_KERNEL_USRHEAP_INIT
+  minfo = kumm_mallinfo();
+
+  info->totalram += minfo.arena;
+  info->freeram  += minfo.fordblks;
+#endif
+
+#ifdef CONFIG_MM_KERNEL_HEAP
+  minfo = kmm_mallinfo();
+
+  info->totalram += minfo.arena;
+  info->freeram  += minfo.fordblks;
+#endif
+
+#ifdef CONFIG_MM_PGALLOC
+  mm_pginfo(&pginfo);
+
+  info->totalram += pginfo.ntotal << MM_PGSHIFT;
+  info->freeram  += pginfo.nfree  << MM_PGSHIFT;
+#endif
+
+  info->uptime   = TICK2SEC(clock_systime_ticks());
+  info->procs    = CONFIG_SMP_NCPUS;
+  info->mem_unit = 1;
+
+  return 0;
+}
diff --git a/syscall/syscall.csv b/syscall/syscall.csv
index 2a6ae80..0962bd5 100644
--- a/syscall/syscall.csv
+++ b/syscall/syscall.csv
@@ -170,6 +170,7 @@
 "stat","sys/stat.h","","int","FAR const char *","FAR struct stat *"
 "statfs","sys/statfs.h","","int","FAR const char *","FAR struct statfs *"
 "symlink","unistd.h","defined(CONFIG_PSEUDOFS_SOFTLINKS)","int","FAR const char *","FAR const char *"
+"sysinfo","sys/sysinfo.h","","int","FAR struct sysinfo *"
 "task_create","sched.h","!defined(CONFIG_BUILD_KERNEL)", "int","FAR const char *","int","int","main_t","FAR char * const []|FAR char * const *"
 "task_delete","sched.h","","int","pid_t"
 "task_restart","sched.h","","int","pid_t"