From b227f3cad94e848a6fc38650123c6ad50e589cb2 Mon Sep 17 00:00:00 2001 From: cjdebenh Date: Wed, 20 Dec 2000 00:39:16 +0000 Subject: [PATCH] Finished port of lcdproc to Solaris. --- clients/lcdproc/chrono.c | 18 +++++++- clients/lcdproc/cpu.c | 50 +++++++++++++++++++++++ clients/lcdproc/cpu_smp.c | 62 ++++++++++++++++++++++++++++ clients/lcdproc/disk.c | 4 ++ clients/lcdproc/load.c | 11 ++++- clients/lcdproc/mem.c | 86 ++++++++++++++++++++++++++++++++++++--- 6 files changed, 222 insertions(+), 9 deletions(-) diff --git a/clients/lcdproc/chrono.c b/clients/lcdproc/chrono.c index 0bb8378..60538ee 100644 --- a/clients/lcdproc/chrono.c +++ b/clients/lcdproc/chrono.c @@ -12,6 +12,10 @@ #include "mode.h" #include "chrono.h" +#ifdef SOLARIS +#include +#endif + int TwentyFourHour = 1; int uptime_fd = 0; @@ -25,8 +29,18 @@ get_uptime (double *up, double *idle) { double uptime; +#ifndef SOLARIS reread (uptime_fd, "get_uptime:"); sscanf (buffer, "%lf %lf", &uptime, idle); +#else + struct utmpx *u, id; + + id.ut_type = BOOT_TIME; + u = getutxid(&id); + + + uptime=time(0) - u->ut_xtime; +#endif *up = uptime; @@ -91,14 +105,14 @@ chrono_init () unamebuf = (struct utsname *) malloc (sizeof (struct utsname)); uptime_fd = open ("/proc/uptime", O_RDONLY); -#if 0 +#ifdef 0 kversion_fd = open ("/proc/sys/kernel/osrelease", O_RDONLY); reread (kversion_fd, "main:"); sscanf (buffer, "%s", kver); close (kversion_fd); -# endif +#endif /* Get OS name and version from uname() */ /* Changed to check if eq -1 instead of non-zero */ diff --git a/clients/lcdproc/cpu.c b/clients/lcdproc/cpu.c index 75cce48..d9a867a 100644 --- a/clients/lcdproc/cpu.c +++ b/clients/lcdproc/cpu.c @@ -4,6 +4,7 @@ #include #include + #include "../../shared/sockets.h" #include "../../shared/debug.h" @@ -11,10 +12,24 @@ #include "mode.h" #include "cpu.h" +#ifdef SOLARIS +#include +#include +#include +#endif + +#ifdef HAVE_LIBKSTAT +#include +#endif + struct load { unsigned long total, user, system, nice, idle; }; +#ifdef HAVE_LIBKSTAT +kstat_ctl_t *kc; +#endif + int load_fd = 0; static void get_load (struct load *result); @@ -24,8 +39,31 @@ get_load (struct load *result) static struct load last_load = { 0, 0, 0, 0, 0 }; struct load curr_load; +#ifndef HAVE_LIBKSTAT reread (load_fd, "get_load:"); sscanf (buffer, "%*s %lu %lu %lu %lu\n", &curr_load.user, &curr_load.nice, &curr_load.system, &curr_load.idle); +#else + kstat_t *k_space; + void *val_ptr; + k_space = kstat_lookup(kc, "cpu_stat", 0, "cpu_stat0"); + if (k_space == NULL) { + printf("\nkstat lookup error"); + exit(1); + } else if (kstat_read(kc, k_space, NULL) == -1) { + printf("\nkstat read error"); + exit(1); + } + k_space=kstat_lookup(kc, "cpu_stat", -1, "cpu_stat0"); + { + struct cpu_stat cinfo; + kstat_read(kc,k_space,&cinfo); + curr_load.idle=cinfo.cpu_sysinfo.cpu[CPU_IDLE]; + curr_load.user=cinfo.cpu_sysinfo.cpu[CPU_USER]; + curr_load.system=cinfo.cpu_sysinfo.cpu[CPU_KERNEL]; + curr_load.nice=cinfo.cpu_sysinfo.cpu[CPU_WAIT]; + } + curr_load.nice=0; +#endif curr_load.total = curr_load.user + curr_load.nice + curr_load.system + curr_load.idle; result->total = curr_load.total - last_load.total; result->user = curr_load.user - last_load.user; @@ -42,9 +80,17 @@ get_load (struct load *result) int cpu_init () { +#ifndef HAVE_LIBKSTAT if (!load_fd) { load_fd = open ("/proc/stat", O_RDONLY); } +#else + kc = kstat_open(); + if (kc == NULL) { + exit(1); + } +#endif + return 0; } @@ -52,8 +98,12 @@ cpu_init () int cpu_close () { +#ifndef HAVE_LIBKSTAT if (load_fd) close (load_fd); +#else + kstat_close(kc); +#endif load_fd = 0; diff --git a/clients/lcdproc/cpu_smp.c b/clients/lcdproc/cpu_smp.c index 75ba64d..62df1e9 100644 --- a/clients/lcdproc/cpu_smp.c +++ b/clients/lcdproc/cpu_smp.c @@ -56,12 +56,25 @@ #include "mode.h" #include "cpu_smp.h" +#ifdef SOLARIS +#include +#include +#include +#endif +#ifdef HAVE_LIBKSTAT +#include +#endif + #define MAX_CPUS 8 struct load { unsigned long total, user, system, nice, idle; }; +#ifdef HAVE_LIBKSTAT +kstat_ctl_t *kc; +#endif + static int load_fd = 0; static char *numnames[MAX_CPUS] = { "one", "two", "three", "four", "five", "six", "seven", "eight" }; @@ -73,6 +86,8 @@ get_load (struct load *result, int *numcpus) static struct load last_load[MAX_CPUS]; struct load curr_load[MAX_CPUS]; + +#ifndef HAVE_LIBKSTAT *numcpus = 0; reread (load_fd, "get_load"); @@ -104,14 +119,57 @@ get_load (struct load *result, int *numcpus) } token = strtok (NULL, "\n"); } +#else + kstat_t *k_space; + void *val_ptr; + int ncpu=0,count; + char buffer[10]; + + *numcpus=sysconf(_SC_NPROCESSORS_CONF); + for (count = 0; count < MAX_CPUS;count++) { + sprintf (buffer, "cpu_stat%d",count); + k_space = kstat_lookup(kc, "cpu_stat", count, buffer); + if (k_space == NULL) { + } else if (kstat_read(kc, k_space, NULL) == -1) { + } else { + struct cpu_stat cinfo; + k_space=kstat_lookup(kc, "cpu_stat", -1, buffer); + kstat_read(kc,k_space,&cinfo); + curr_load[ncpu].idle=cinfo.cpu_sysinfo.cpu[CPU_IDLE]; + curr_load[ncpu].user=cinfo.cpu_sysinfo.cpu[CPU_USER]; + curr_load[ncpu].system=cinfo.cpu_sysinfo.cpu[CPU_KERNEL]; + curr_load[ncpu].nice=cinfo.cpu_sysinfo.cpu[CPU_WAIT]; + curr_load[ncpu].nice=0; + curr_load[ncpu].total = curr_load[ncpu].user + curr_load[ncpu].nice + curr_load[ncpu].system + curr_load[ncpu].idle; + result[ncpu].total = curr_load[ncpu].total - last_load[ncpu].total; + result[ncpu].user = curr_load[ncpu].user - last_load[ncpu].user; + result[ncpu].nice = curr_load[ncpu].nice - last_load[ncpu].nice; + result[ncpu].system = curr_load[ncpu].system - last_load[ncpu].system; + result[ncpu].idle = curr_load[ncpu].idle - last_load[ncpu].idle; + last_load[ncpu].total = curr_load[ncpu].total; + last_load[ncpu].user = curr_load[ncpu].user; + last_load[ncpu].nice = curr_load[ncpu].nice; + last_load[ncpu].system = curr_load[ncpu].system; + last_load[ncpu].idle = curr_load[ncpu].idle; + ncpu=ncpu++; + } + } +#endif } int cpu_smp_init () { +#ifndef HAVE_LIBKSTAT if (!load_fd) { load_fd = open ("/proc/stat", O_RDONLY); } +#else + kc = kstat_open(); + if (kc == NULL) { + exit(1); + } +#endif return 0; } @@ -119,8 +177,12 @@ cpu_smp_init () int cpu_smp_close () { +#ifndef HAVE_LIBKSTAT if (load_fd) close (load_fd); +#else + kstat_close(kc); +#endif load_fd = 0; diff --git a/clients/lcdproc/disk.c b/clients/lcdproc/disk.c index b872a0b..e27b7e8 100644 --- a/clients/lcdproc/disk.c +++ b/clients/lcdproc/disk.c @@ -51,7 +51,11 @@ get_fs (mounts fs[]) char line[256]; int x = 0, y; +#ifdef SOLARIS + mtab_fd = fopen ("/etc/mnttab", "r"); +#else mtab_fd = fopen ("/etc/mtab", "r"); +#endif // Get rid of old, unmounted filesystems... memset (fs, 0, sizeof (mounts) * 256); diff --git a/clients/lcdproc/load.c b/clients/lcdproc/load.c index e81da7e..40de066 100644 --- a/clients/lcdproc/load.c +++ b/clients/lcdproc/load.c @@ -10,6 +10,10 @@ #include "mode.h" #include "load.h" +#ifdef HAVE_GETLOADAVG +#include +#endif + int loadavg_fd = 0; static double get_loadavg (void); @@ -18,9 +22,15 @@ static double get_loadavg (void) { double load; + double loadavg[LOADAVG_NSTATS]; +#ifndef HAVE_GETLOADAVG reread (loadavg_fd, "get_load:"); sscanf (buffer, "%lf", &load); +#else + getloadavg(loadavg,LOADAVG_NSTATS); + load = loadavg[LOADAVG_1MIN]; +#endif return load; } @@ -30,7 +40,6 @@ load_init () #ifndef SOLARIS if (!loadavg_fd) loadavg_fd = open ("/proc/loadavg", O_RDONLY); - #endif return 0; } diff --git a/clients/lcdproc/mem.c b/clients/lcdproc/mem.c index 35bb24b..4f3156a 100644 --- a/clients/lcdproc/mem.c +++ b/clients/lcdproc/mem.c @@ -17,6 +17,9 @@ #ifdef SOLARIS #include +#include +#include +#include #endif @@ -32,7 +35,7 @@ static void get_mem_info (struct meminfo *result) { // int i, res; char *bufptr; - int pagesize,page,i; + int pagesize,page; #ifndef SOLARIS reread (meminfo_fd, "get_meminfo:"); @@ -44,13 +47,61 @@ get_mem_info (struct meminfo *result) result[1].total = getentry ("SwapTotal:", buffer); result[1].free = getentry ("SwapFree:", buffer); #else - result[0].total = 0; - result[0].free = 0; + #define MAXSTRSIZE 80 + swaptbl_t *s=NULL; + int i, n, num; + char *strtab; /* string table for path names */ + + result[0].total = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE) / 1024; + result[0].free = sysconf(_SC_AVPHYS_PAGES) * sysconf(_SC_PAGESIZE) / 1024; result[0].shared = 0; result[0].buffers = 0; result[0].cache = 0; +again: + if ((num = swapctl(SC_GETNSWP, 0)) == -1) { + perror("swapctl: GETNSWP"); + exit(1); + } + if (num == 0) { + fprintf(stderr, "No Swap Devices Configured\n"); + exit(2); + } + /* allocate swaptable for num+1 entries */ + if ((s = (swaptbl_t *) + malloc(num * sizeof(swapent_t) + + sizeof(struct swaptable))) == + (void *) 0) { + fprintf(stderr, "Malloc Failed\n"); + exit(3); + } + /* allocate num+1 string holders */ + if ((strtab = (char *) + malloc((num + 1) * MAXSTRSIZE)) == (void *) 0) { + fprintf(stderr, "Malloc Failed\n"); + exit(3); + } + /* initialize string pointers */ + for (i = 0; i < (num + 1); i++) { + s->swt_ent[i].ste_path = strtab + (i * MAXSTRSIZE); + } + + + s->swt_n = num + 1; + if ((n = swapctl(SC_LIST, s)) < 0) { + perror("swapctl"); + exit(1); + } + if (n > num) { /* more were added */ + free(s); + free(strtab); + goto again; + } result[1].total = 0; result[1].free = 0; + for (i = 0; i < n; i++) { + result[1].total = result[1].total + s->swt_ent[i].ste_pages * sysconf(_SC_PAGESIZE) / 1024; + result[1].free = result[1].free + s->swt_ent[i].ste_free * sysconf(_SC_PAGESIZE) / 1024; + } #endif } @@ -285,7 +336,6 @@ mem_top_screen (int rep, int display) if (first) { first = 0; - sock_send_string (sock, "screen_add S\n"); sprintf (buffer, "screen_set S -name {Top Memory Use: %s}\n", host); sock_send_string (sock, buffer); @@ -320,6 +370,7 @@ mem_top_screen (int rep, int display) if (!index ("1234567890", procdir->d_name[0])) { continue; } + #ifndef SOLARIS sprintf (buf, "/proc/%s/status", procdir->d_name); if ((StatusFile = fopen (buf, "r")) == NULL) { // Not a serious error; process has finished before we could @@ -351,6 +402,29 @@ mem_top_screen (int rep, int display) sscanf (buf, "%*s %d", &procExe); } } + #else + sprintf (buf, "/proc/%s/psinfo", procdir->d_name); + if ((StatusFile = fopen (buf, "r")) == NULL) { + // Not a serious error; process has finished before we could + // examine it: + //fprintf ( stderr , "mem_top_screen: cannot open %s for reading" , + // buf ) ; + //perror ( "" ) ; + continue; + } + { + psinfo_t psinfo; + fread(&psinfo,sizeof(psinfo),1,StatusFile); + procRSS = procSize = procData = procStk = procExe = 0; + strcpy(procName,psinfo.pr_fname); + procSize=psinfo.pr_size; + procRSS=psinfo.pr_rssize; + // Following values not accurate, not sure what needs to be set to + procData=psinfo.pr_size; + procStk=0; + procExe=0; + } + #endif fclose (StatusFile); if (procSize > threshold) { // Figure out if it's sharing any memory... @@ -385,7 +459,6 @@ mem_top_screen (int rep, int display) } closedir (proc); - // Now, print some info... LL_Rewind (procs); LL_Sort (procs, sort_procs); @@ -402,7 +475,8 @@ mem_top_screen (int rep, int display) sock_send_string (sock, buffer); } else { //printf("Mem hog: none?\n"); - sprintf (buffer, "widget_set S %i 1 %i {}\n", i, i); + //sprintf (buffer, "widget_set S %i 1 %i {}\n", i, i); + sprintf (buffer, "widget_set S %i 1 %i \" \"\n", i, i); if (display) sock_send_string (sock, buffer); }