Files
lcdproc/clients/lcdproc/mem.c
T

337 lines
9.2 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef IRIX
#include <strings.h>
#endif
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include "shared/sockets.h"
#include "shared/LL.h"
#include "main.h"
#include "mode.h"
#include "machine.h"
#include "mem.h"
static char *sprintmem(char *dest, double value, double roundlimit)
{
if (dest != NULL) {
static char *units[] = { "", "k", "M", "G", "T", "P", "E", "Z", "Y", NULL };
int offs = 0;
char *format = "%3.1f%s";
if ((roundlimit <= 0.0) || (roundlimit > 1.0))
roundlimit = 0.5;
while (units[offs] != NULL > 0) {
if (value <= 1024 * roundlimit)
break;
offs++;
value /= 1024;
}
if (value < 100)
format = "%2.2f%s";
if (value < 10)
format = "%1.3f%s";
sprintf(dest, format, value, units[offs]);
}
return dest;
}
/////////////////////////////////////////////////////////////////////////
// Mem Screen displays info about memory and swap usage...
//
int
mem_screen (int rep, int display)
{
meminfo_type mem[2];
static int first = 1;
static int which_title = 0;
static int gauge_wid = 0;
static int gauge_offs = 0;
double value;
if (first) {
first = 0;
sock_send_string (sock, "screen_add M\n");
sprintf (buffer, "screen_set M -name {Memory & Swap: %s}\n", get_hostname());
sock_send_string (sock, buffer);
if (lcd_hgt >= 4) {
gauge_wid = (lcd_wid >= 18)
? (lcd_wid - 6) / 2 // room for E..F pairs and 2 spaces in between
: (lcd_wid - 4) / 2; // leave room for the E...F pairs
sock_send_string(sock, "widget_add M title title\n");
sock_send_string(sock, "widget_set M title { MEM -==- SWAP}\n");
sock_send_string(sock, "widget_add M totl string\n");
sock_send_string(sock, "widget_add M used string\n");
sprintf(buffer, "widget_set M totl %i 2 Totl\n", lcd_wid/2 - 1);
sock_send_string (sock, buffer);
sprintf(buffer, "widget_set M used %i 3 Free\n", lcd_wid/2 - 1);
sock_send_string(sock, buffer);
sock_send_string(sock, "widget_add M EFmem string\n");
sprintf(buffer, "widget_set M EFmem 1 4 {E%*sF}\n", gauge_wid, "");
sock_send_string(sock, buffer);
sock_send_string(sock, "widget_add M EFswap string\n");
sprintf(buffer, "widget_set M EFswap %i 4 {E%*sF}\n",
lcd_wid - gauge_wid - 1, gauge_wid, "");
sock_send_string(sock, buffer);
sock_send_string(sock, "widget_add M memused string\n");
sock_send_string(sock, "widget_add M swapused string\n");
} else {
gauge_wid = (lcd_wid >= 18) ? max(2, lcd_wid - 18) : 0;
gauge_offs = (lcd_wid - gauge_wid) / 2 + 2;
sock_send_string(sock, "widget_add M m string\n");
sock_send_string(sock, "widget_add M s string\n");
if (gauge_wid > 0) {
sprintf(buffer, "widget_set M m 1 1 {M%*s[%*s]}\n",
gauge_offs - 3, "", gauge_wid, "");
sock_send_string (sock, buffer);
sprintf(buffer, "widget_set M s 1 2 {S%*s[%*s]}\n",
gauge_offs - 3, "", gauge_wid, "");
sock_send_string (sock, buffer);
}
else {
sock_send_string(sock, "widget_set M m 1 1 {M }\n");
sock_send_string(sock, "widget_set M s 1 2 {S }\n");
}
sock_send_string (sock, "widget_add M mem% string\n");
sock_send_string (sock, "widget_add M swap% string\n");
//sock_send_string (sock, "widget_set M mem% 16 1 { 0.0%}\n");
//sock_send_string (sock, "widget_set M swap% 16 2 { 0.0%}\n");
}
sock_send_string (sock, "widget_add M memtotl string\n");
sock_send_string (sock, "widget_add M swaptotl string\n");
sock_send_string (sock, "widget_add M memgauge hbar\n");
sock_send_string (sock, "widget_add M swapgauge hbar\n");
}
machine_get_meminfo(mem);
// flip the title back and forth...
if (lcd_hgt >= 4) {
if (which_title & 4) {
sprintf (buffer, "widget_set M title {%s}\n", get_hostname());
sock_send_string (sock, buffer);
} else
sock_send_string (sock, "widget_set M title { MEM -==- SWAP}\n");
which_title = (which_title + 1) & 7;
// Total memory
sprintmem(tmp, mem[0].total * 1024, 1);
sprintf (buffer, "widget_set M memtotl 1 2 {%7s}\n", tmp);
if (display)
sock_send_string (sock, buffer);
// Free memory (plus buffers and cache)
sprintmem(tmp, (mem[0].free + mem[0].buffers + mem[0].cache) * 1024, 1);
sprintf (buffer, "widget_set M memused 1 3 {%7s}\n", tmp);
if (display)
sock_send_string (sock, buffer);
// Total swap
sprintmem(tmp, mem[1].total * 1024, 1);
sprintf (buffer, "widget_set M swaptotl %i 2 {%7s}\n", lcd_wid - 7, tmp);
if (display)
sock_send_string (sock, buffer);
// Free swap
sprintmem(tmp, mem[1].free * 1024, 1);
sprintf (buffer, "widget_set M swapused %i 3 {%7s}\n", lcd_wid - 7, tmp);
if (display)
sock_send_string (sock, buffer);
if (gauge_wid > 0) {
// Free memory graph
if (mem[0].total > 0) {
value = 1.0 - (double) (mem[0].free + mem[0].buffers + mem[0].cache)
/ (double) mem[0].total;
// printf(".0f", val) only prints the integer part
sprintf(buffer, "widget_set M memgauge 2 4 %.0f\n",
lcd_cellwid * gauge_wid * value);
if (display)
sock_send_string (sock, buffer);
}
// Free swap graph
if (mem[1].total > 0) {
value = 1.0 - ((double) mem[1].free / (double) mem[1].total);
// printf(".0f", val) only prints the integer part
sprintf(buffer, "widget_set M swapgauge %i 4 %.0f\n",
lcd_wid - gauge_wid, lcd_cellwid * gauge_wid * value);
if (display)
sock_send_string (sock, buffer);
}
}
}
else {
// Total memory
sprintmem(tmp, mem[0].total * 1024, 1);
sprintf(buffer, "widget_set M memtotl 3 1 {%6s}\n", tmp);
if (display)
sock_send_string (sock, buffer);
// Total swap
sprintmem(tmp, mem[1].total * 1024, 1);
sprintf(buffer, "widget_set M swaptotl 3 2 {%6s}\n", tmp);
if (display)
sock_send_string (sock, buffer);
// Free memory graph
sprintf(tmp, "N/A");
if (mem[0].total > 0) {
value = 1.0 - (double) (mem[0].free + mem[0].buffers + mem[0].cache)
/ (double) mem[0].total;
if (gauge_wid > 0) {
// printf(".0f", val) only prints the integer part
sprintf(buffer, "widget_set M memgauge %i 1 %.0f\n",
gauge_offs, lcd_cellwid * gauge_wid * value);
if (display)
sock_send_string(sock, buffer);
}
value = min(value * 100.0, 100.0);
sprintf(tmp, (value > 99.9) ? "%3.0f%%" : "%4.1f%%", value);
}
sprintf(buffer, "widget_set M mem%% %i 1 {%s}\n", lcd_wid - strlen(tmp), tmp);
if (display)
sock_send_string (sock, buffer);
// Free swap graph
sprintf(tmp, "N/A");
if (mem[1].total > 0) {
value = 1.0 - ((double) mem[1].free / (double) mem[1].total);
if (gauge_wid > 0) {
// printf(".0f", val) only prints the integer part
sprintf(buffer, "widget_set M swapgauge %i 2 %.0f\n",
gauge_offs, lcd_cellwid * gauge_wid * value);
if (display)
sock_send_string (sock, buffer);
}
value = min(value * 100.0, 100.0);
sprintf(tmp, (value > 99.9) ? "%3.0f%%" : "%4.1f%%", value);
}
sprintf(buffer, "widget_set M swap%% %i 2 {%s}\n", lcd_wid - strlen(tmp), tmp);
if (display)
sock_send_string (sock, buffer);
}
return 0;
} // End mem_screen()
static int
sort_procs (void *a, void *b)
{
procinfo_type *one, *two;
if (!a)
return 0;
if (!b)
return 0;
one = (procinfo_type *) a;
two = (procinfo_type *) b;
return (two->totl > one->totl);
}
int
mem_top_screen (int rep, int display)
{
int i;
procinfo_type *p;
LinkedList *procs;
static int first = 1;
if (first) {
first = 0;
sock_send_string (sock, "screen_add S\n");
sprintf (buffer, "screen_set S -name {Top Memory Use: %s}\n", get_hostname());
sock_send_string (sock, buffer);
sock_send_string (sock, "widget_add S title title\n");
sprintf (buffer, "widget_set S title {TOP MEM: %s}\n", get_hostname());
sock_send_string (sock, buffer);
// frame from (2nd line, left) to (last line, right)
sock_send_string (sock, "widget_add S f frame\n");
sprintf(buffer, "widget_set S f 1 2 %i %i %i 5 v %i\n",
lcd_wid, lcd_hgt, lcd_wid, ((lcd_hgt >= 4) ? 8 : 12));
sock_send_string (sock, buffer);
// frame contents
for (i = 1; i <= 5; i++) {
sprintf (buffer, "widget_add S %i string -in f\n", i);
sock_send_string (sock, buffer);
}
sock_send_string (sock, "widget_set S 1 1 1 Checking...\n");
}
procs = LL_new ();
if (!procs) {
fprintf (stderr, "mem_top_screen: Error allocating list\n");
return -1;
}
machine_get_procs(procs);
// Now, print some info...
LL_Rewind (procs);
LL_Sort (procs, sort_procs);
LL_Rewind (procs);
for (i = 1; i <= 5; i++) {
p = LL_Get (procs);
if (p) {
//printf("Mem hog: %s: %ik\n", p->name, p->size);
if (p->number > 1)
sprintf(tmp, "%i%6ik %s(%i)", i, p->totl, p->name, p->number);
else
sprintf(tmp, "%i%6ik %s", i, p->totl, p->name);
sprintf(buffer, "widget_set S %i 1 %i {%s}\n", i, i, tmp);
if (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);
if (display)
sock_send_string (sock, buffer);
}
LL_Next (procs);
}
// Now clean it all up...
LL_Rewind (procs);
do {
p = (procinfo_type *) LL_Get (procs);
if (p) {
//printf("Proc: %6ik %s\n", p->size, p->name);
free (p);
}
} while (LL_Next (procs) == 0);
LL_Destroy (procs);
return 0;
}