diff --git a/include/sys/syscall_lookup.h b/include/sys/syscall_lookup.h index 8b677fa22b..92752613c0 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 0000000000..822bfce4cb --- /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 + +/**************************************************************************** + * 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 6477f63963..5d2ecad423 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 0000000000..5562ebd0d3 --- /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 +#include +#include +#include + +#include +#include +#include + +/**************************************************************************** + * 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 2a6ae80e9b..0962bd5d91 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"