sched: Implement sysinfo function

specify here:
https://man7.org/linux/man-pages/man2/sysinfo.2.html

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
Xiang Xiao 2022-02-05 20:05:29 +08:00 committed by Xiang Xiao
parent b16e022ead
commit ab3b0d0162
5 changed files with 182 additions and 0 deletions

View File

@ -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)

78
include/sys/sysinfo.h Normal file
View File

@ -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 */

View File

@ -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

100
sched/sched/sched_sysinfo.c Normal file
View File

@ -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;
}

View File

@ -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"

Can't render this file because it has a wrong number of fields in line 2.