diff --git a/kernel/Makefile b/kernel/Makefile new file mode 100644 index 00000000..d24e3ba8 --- /dev/null +++ b/kernel/Makefile @@ -0,0 +1,13 @@ +# +# Makefile for the linux system information tables. +# +# Note! Dependencies are done automagically by 'make dep', which also +# removes any old dependencies. DON'T put your own dependencies here +# unless it's something special (ie not a .c file). +# +# Note 2! The CFLAGS definition is now in the main makefile... + +O_TARGET := table.o +O_OBJS := main.o + +include $(TOPDIR)/Rules.make diff --git a/kernel/main.c b/kernel/main.c new file mode 100644 index 00000000..7f6381bb --- /dev/null +++ b/kernel/main.c @@ -0,0 +1,90 @@ +/* + * linux/table/table_impl.c + * Copyright (C) 1998 Martin Baulig + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include + +#include "version.h" + +asmlinkage int +sys_table (int type, union table *buf) +{ + union table tbl; + struct sysinfo i; + int err; + + if (type == TABLE_VERSION) + return _TABLE_VERSION; + + if (!buf) + return -EFAULT; + + switch (type) { + case TABLE_CPU: + tbl.cpu.total = jiffies; + tbl.cpu.user = kstat.cpu_user; + tbl.cpu.nice = kstat.cpu_nice; + tbl.cpu.sys = kstat.cpu_system; + tbl.cpu.idle = tbl.cpu.total - (tbl.cpu.user + tbl.cpu.nice + tbl.cpu.sys); + tbl.cpu.frequency = HZ; + break; + case TABLE_MEM: + si_meminfo (&i); + tbl.mem.total = i.totalram; + tbl.mem.used = i.totalram - i.freeram; + tbl.mem.free = i.freeram; + tbl.mem.shared = i.sharedram; + tbl.mem.buffer = i.bufferram; + tbl.mem.cached = page_cache_size << PAGE_SHIFT; + break; + case TABLE_SWAP: + si_swapinfo (&i); + tbl.swap.total = i.totalswap; + tbl.swap.used = i.totalswap - i.freeswap; + tbl.swap.free = i.freeswap; + break; + case TABLE_LOADAVG: + tbl.loadavg.loadavg [0] = avenrun [0]; + tbl.loadavg.loadavg [1] = avenrun [1]; + tbl.loadavg.loadavg [2] = avenrun [2]; + tbl.loadavg.nr_running = nr_running; + tbl.loadavg.nr_tasks = nr_tasks; + tbl.loadavg.last_pid = last_pid; + break; + case TABLE_UPTIME: + tbl.uptime.uptime = jiffies; + tbl.uptime.idle = task[0]->utime + task[0]->stime; + break; + default: + return -EINVAL; + } + + err = verify_area (VERIFY_WRITE, buf, sizeof (struct table)); + if (err) + return err; + + memcpy_tofs (buf, &tbl, sizeof (union table)); + return 0; +} diff --git a/kernel/table.h b/kernel/table.h new file mode 100644 index 00000000..9515fdc9 --- /dev/null +++ b/kernel/table.h @@ -0,0 +1,80 @@ +#ifndef _LINUX_TABLE_H +#define _LINUX_TABLE_H +#include + +#define TABLE_VERSION 0 +#define TABLE_CPU 1 +#define TABLE_MEM 2 +#define TABLE_SWAP 3 +#define TABLE_LOADAVG 4 +#define TABLE_UPTIME 5 + +/* CPU Usage (in jiffies = 1/100th seconds) */ + +struct table_cpu +{ + unsigned long total; /* Total CPU Time */ + unsigned long user; /* CPU Time in User Mode */ + unsigned long nice; /* CPU Time in User Mode (nice) */ + unsigned long sys; /* CPU Time in System Mode */ + unsigned long idle; /* CPU Time in the Idle Task */ + unsigned long frequency; /* Tick frequency */ +}; + +/* Memory Usage (in bytes) */ + +struct table_mem +{ + unsigned long total; /* Total physical memory */ + unsigned long used; /* Used memory size */ + unsigned long free; /* Free memory size */ + unsigned long shared; /* Shared memory size */ + unsigned long buffer; /* Size of buffers */ + unsigned long cached; /* Size of cached memory */ +}; + +/* Swap Space (in bytes) */ + +struct table_swap +{ + unsigned long total; /* Total swap space */ + unsigned long used; /* Used swap space */ + unsigned long free; /* Free swap space */ +}; + +/* Load average */ + +struct table_loadavg +{ + unsigned long loadavg [3]; + unsigned nr_running; + unsigned nr_tasks; + unsigned last_pid; +}; + +/* Uptime */ + +struct table_uptime +{ + unsigned long uptime; + unsigned long idle; +}; + +/* Union */ + +union table +{ + struct table_cpu cpu; + struct table_mem mem; + struct table_swap swap; + struct table_loadavg loadavg; + struct table_uptime uptime; +}; + +#ifdef __KERNEL__ + +#endif /* __KERNEL__ */ + +#endif /* _LINUX_IPC_H */ + + diff --git a/kernel/test.c b/kernel/test.c new file mode 100644 index 00000000..a6ba8dc7 --- /dev/null +++ b/kernel/test.c @@ -0,0 +1,82 @@ +#include +#include + +#include +#include +#include + +#include + +static inline _syscall2 (int, table, int, type, union table *, tbl); + +int +main (void) +{ + union table tbl; + int ret; + + ret = table (TABLE_VERSION, NULL); + + if (ret == -1) { + fprintf (stderr, "table(%u): %s\n", TABLE_VERSION, sys_errlist [errno]); + exit (-errno); + } + + fprintf (stderr, "Table (%u) = %u\n", TABLE_VERSION, ret); + + ret = table (TABLE_CPU, &tbl); + + if (ret == -1) { + fprintf (stderr, "table(%u): %s\n", TABLE_CPU, sys_errlist [errno]); + exit (-errno); + } + + fprintf (stderr, "Table (%u) = %lu, %lu, %lu, %lu, %lu, %lu\n", + TABLE_CPU, tbl.cpu.total, tbl.cpu.user, tbl.cpu.nice, + tbl.cpu.sys, tbl.cpu.idle, tbl.cpu.frequency); + + ret = table (TABLE_MEM, &tbl); + + if (ret == -1) { + fprintf (stderr, "table(%u): %s\n", TABLE_MEM, sys_errlist [errno]); + exit (-errno); + } + + fprintf (stderr, "Table (%u) = %lu, %lu, %lu, %lu, %lu, %lu\n", + TABLE_MEM, tbl.mem.total, tbl.mem.used, tbl.mem.free, + tbl.mem.shared, tbl.mem.buffer, tbl.mem.cached); + + ret = table (TABLE_SWAP, &tbl); + + if (ret == -1) { + fprintf (stderr, "table(%u): %s\n", TABLE_SWAP, sys_errlist [errno]); + exit (-errno); + } + + fprintf (stderr, "Table (%u) = %lu, %lu, %lu\n", + TABLE_SWAP, tbl.swap.total, tbl.swap.used, tbl.swap.free); + + ret = table (TABLE_LOADAVG, &tbl); + + if (ret == -1) { + fprintf (stderr, "table(%u): %s\n", TABLE_LOADAVG, sys_errlist [errno]); + exit (-errno); + } + + fprintf (stderr, "Table (%u) = (%lu, %lu, %lu) - %u, %u, %u\n", + TABLE_LOADAVG, tbl.loadavg.loadavg [0], tbl.loadavg.loadavg [1], + tbl.loadavg.loadavg [2], tbl.loadavg.nr_running, + tbl.loadavg.nr_tasks, tbl.loadavg.last_pid); + + ret = table (TABLE_UPTIME, &tbl); + + if (ret == -1) { + fprintf (stderr, "table(%u): %s\n", TABLE_UPTIME, sys_errlist [errno]); + exit (-errno); + } + + fprintf (stderr, "Table (%u) = %lu, %lu\n", + TABLE_UPTIME, tbl.uptime.uptime, tbl.uptime.idle); + + exit (0); +} diff --git a/kernel/version.h b/kernel/version.h new file mode 100644 index 00000000..d47411ee --- /dev/null +++ b/kernel/version.h @@ -0,0 +1 @@ +#define _TABLE_VERSION 1