generic: indent and format

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2021-02-04 01:01:16 +02:00
parent 052e4456b1
commit a433444a79
4 changed files with 462 additions and 481 deletions

View file

@ -47,91 +47,91 @@ CONTAINER ProcessList = 0;
typedef struct typedef struct
{ {
/* This flag is set for all found processes at the beginning of the /* This flag is set for all found processes at the beginning of the
* process list update. Processes that do not have this flag set will * process list update. Processes that do not have this flag set will
* be assumed dead and removed from the list. The flag is cleared after * be assumed dead and removed from the list. The flag is cleared after
* each list update. */ * each list update. */
int alive; int alive;
/* the process ID */ /* the process ID */
pid_t pid; pid_t pid;
/* the parent process ID */ /* the parent process ID */
pid_t ppid; pid_t ppid;
/* the real user ID */ /* the real user ID */
uid_t uid; uid_t uid;
/* the real group ID */ /* the real group ID */
gid_t gid; gid_t gid;
/* a character description of the process status */ /* a character description of the process status */
char status[16]; char status[16];
/* the number of the tty the process owns */ /* the number of the tty the process owns */
int ttyNo; int ttyNo;
/* /*
* The nice level. The range should be -20 to 20. I'm not sure * The nice level. The range should be -20 to 20. I'm not sure
* whether this is true for all platforms. * whether this is true for all platforms.
*/ */
int niceLevel; int niceLevel;
/* /*
* The scheduling priority. * The scheduling priority.
*/ */
int priority; int priority;
/* /*
* The total amount of memory the process uses. This includes shared and * The total amount of memory the process uses. This includes shared and
* swapped memory. * swapped memory.
*/ */
unsigned int vmSize; unsigned int vmSize;
/* /*
* The amount of physical memory the process currently uses. * The amount of physical memory the process currently uses.
*/ */
unsigned int vmRss; unsigned int vmRss;
/* /*
* The amount of memory (shared/swapped/etc) the process shares with * The amount of memory (shared/swapped/etc) the process shares with
* other processes. * other processes.
*/ */
unsigned int vmLib; unsigned int vmLib;
/* /*
* The number of 1/100 of a second the process has spend in user space. * The number of 1/100 of a second the process has spend in user space.
* If a machine has an uptime of 1 1/2 years or longer this is not a * If a machine has an uptime of 1 1/2 years or longer this is not a
* good idea. I never thought that the stability of UNIX could get me * good idea. I never thought that the stability of UNIX could get me
* into trouble! ;) * into trouble! ;)
*/ */
unsigned int userTime; unsigned int userTime;
/* /*
* The number of 1/100 of a second the process has spend in system space. * The number of 1/100 of a second the process has spend in system space.
* If a machine has an uptime of 1 1/2 years or longer this is not a * If a machine has an uptime of 1 1/2 years or longer this is not a
* good idea. I never thought that the stability of UNIX could get me * good idea. I never thought that the stability of UNIX could get me
* into trouble! ;) * into trouble! ;)
*/ */
unsigned int sysTime; unsigned int sysTime;
/* system time as multime of 100ms */ /* system time as multime of 100ms */
int centStamp; int centStamp;
/* the current CPU load (in %) from user space */ /* the current CPU load (in %) from user space */
double userLoad; double userLoad;
/* the current CPU load (in %) from system space */ /* the current CPU load (in %) from system space */
double sysLoad; double sysLoad;
/* the name of the process */ /* the name of the process */
char name[64]; char name[64];
/* the command used to start the process */ /* the command used to start the process */
char cmdline[256]; char cmdline[256];
/* the login name of the user that owns this process */ /* the login name of the user that owns this process */
char userName[32]; char userName[32];
} ProcessInfo; } ProcessInfo;
static unsigned ProcessCount; static unsigned ProcessCount;
@ -139,146 +139,141 @@ static unsigned ProcessCount;
static int static int
processCmp(void* p1, void* p2) processCmp(void* p1, void* p2)
{ {
return (((ProcessInfo*) p1)->pid - ((ProcessInfo*) p2)->pid); return (((ProcessInfo*) p1)->pid - ((ProcessInfo*) p2)->pid);
} }
static ProcessInfo* static ProcessInfo*
findProcessInList(int pid) findProcessInList(int pid)
{ {
ProcessInfo key; ProcessInfo key;
long index; long index;
key.pid = pid; key.pid = pid;
if ((index = search_ctnr(ProcessList, processCmp, &key)) < 0) if ((index = search_ctnr(ProcessList, processCmp, &key)) < 0)
return (0); return (0);
return (get_ctnr(ProcessList, index)); return (get_ctnr(ProcessList, index));
} }
static void static void
fillProcessCmdline(char *cmdline, struct kinfo_proc *p, size_t maxlen) fillProcessCmdline(char *cmdline, struct kinfo_proc *p, size_t maxlen)
{ {
int mib[4]; int mib[4];
int ret = -1; int ret = -1;
static char *argbuf = NULL; static char *argbuf = NULL;
static size_t arglen = 0; static size_t arglen = 0;
strlcpy(cmdline, p->p_comm, maxlen); strlcpy(cmdline, p->p_comm, maxlen);
if (!argbuf) { if (!argbuf) {
arglen = 1024; arglen = 1024;
argbuf = malloc(arglen); argbuf = malloc(arglen);
} }
mib[0] = CTL_KERN; mib[0] = CTL_KERN;
mib[1] = KERN_PROC_ARGS; mib[1] = KERN_PROC_ARGS;
mib[2] = p->p_pid; mib[2] = p->p_pid;
mib[3] = KERN_PROC_ARGV; mib[3] = KERN_PROC_ARGV;
while (argbuf) { while (argbuf) {
ret = sysctl(mib, 4, argbuf, &arglen, NULL, 0); ret = sysctl(mib, 4, argbuf, &arglen, NULL, 0);
if (ret == -1 && errno == ENOMEM) { if (ret == -1 && errno == ENOMEM) {
char *n; char *n;
n = realloc(argbuf, arglen * 2); n = realloc(argbuf, arglen * 2);
if (n != 0) { if (n != 0) {
argbuf = n; argbuf = n;
arglen *= 2; arglen *= 2;
continue; continue;
} }
} }
break; break;
} }
if (ret != 1) { if (ret != 1) {
char **argv; char **argv;
int argc; int argc;
argv = (char **)argbuf; argv = (char **)argbuf;
if (argv[0] != NULL) if (argv[0] != NULL)
strlcpy(cmdline, argv[0], maxlen); strlcpy(cmdline, argv[0], maxlen);
for (argc = 1; argv[argc] != NULL; argc++) { for (argc = 1; argv[argc] != NULL; argc++) {
strlcat(cmdline, " ", maxlen); strlcat(cmdline, " ", maxlen);
strlcat(cmdline, argv[argc], maxlen); strlcat(cmdline, argv[argc], maxlen);
} }
} else { } else {
strlcpy(cmdline, p->p_comm, maxlen); strlcpy(cmdline, p->p_comm, maxlen);
} }
} }
static int static int
updateProcess(struct kinfo_proc *p) updateProcess(struct kinfo_proc *p)
{ {
static const char * const statuses[] = { "idle","run","sleep","stop","zombie" }; static const char * const statuses[] = { "idle","run","sleep","stop","zombie" };
ProcessInfo* ps; ProcessInfo* ps;
struct passwd* pwent; struct passwd* pwent;
pid_t pid = p->p_pid; pid_t pid = p->p_pid;
if ((ps = findProcessInList(pid)) == 0) if ((ps = findProcessInList(pid)) == 0) {
{ ps = (ProcessInfo*) malloc(sizeof(ProcessInfo));
ps = (ProcessInfo*) malloc(sizeof(ProcessInfo)); ps->pid = pid;
ps->pid = pid; ps->centStamp = 0;
ps->centStamp = 0; push_ctnr(ProcessList, ps);
push_ctnr(ProcessList, ps); bsort_ctnr(ProcessList, processCmp);
bsort_ctnr(ProcessList, processCmp); }
}
ps->alive = 1; ps->alive = 1;
ps->pid = p->p_pid; ps->pid = p->p_pid;
ps->ppid = p->p_ppid; ps->ppid = p->p_ppid;
ps->uid = p->p_uid; ps->uid = p->p_uid;
ps->gid = p->p_gid; ps->gid = p->p_gid;
ps->priority = p->p_priority; ps->priority = p->p_priority;
ps->niceLevel = p->p_nice; ps->niceLevel = p->p_nice;
/* this isn't usertime -- it's total time (??) */ /* this isn't usertime -- it's total time (??) */
ps->userTime = p->p_uutime_sec*100+p->p_uutime_usec/100; ps->userTime = p->p_uutime_sec*100+p->p_uutime_usec/100;
ps->sysTime = 0; ps->sysTime = 0;
ps->sysLoad = 0; ps->sysLoad = 0;
/* memory, process name, process uid */ /* memory, process name, process uid */
/* find out user name with process uid */ /* find out user name with process uid */
pwent = getpwuid(ps->uid); pwent = getpwuid(ps->uid);
strlcpy(ps->userName,pwent&&pwent->pw_name? pwent->pw_name:"????",sizeof(ps->userName)); strlcpy(ps->userName,pwent&&pwent->pw_name? pwent->pw_name:"????",sizeof(ps->userName));
ps->userName[sizeof(ps->userName)-1]='\0'; ps->userName[sizeof(ps->userName)-1]='\0';
ps->userLoad = p->p_pctcpu / 100; ps->userLoad = p->p_pctcpu / 100;
ps->vmSize = (p->p_vm_tsize + ps->vmSize = (p->p_vm_tsize +
p->p_vm_dsize + p->p_vm_dsize +
p->p_vm_ssize) * getpagesize(); p->p_vm_ssize) * getpagesize();
ps->vmRss = p->p_vm_rssize * getpagesize(); ps->vmRss = p->p_vm_rssize * getpagesize();
strlcpy(ps->name,p->p_comm ? p->p_comm : "????", sizeof(ps->name)); strlcpy(ps->name,p->p_comm ? p->p_comm : "????", sizeof(ps->name));
strlcpy(ps->status,(p->p_stat>=1)&&(p->p_stat<=5)? statuses[p->p_stat-1]:"????", sizeof(ps->status)); strlcpy(ps->status,(p->p_stat>=1)&&(p->p_stat<=5)? statuses[p->p_stat-1]:"????", sizeof(ps->status));
fillProcessCmdline(ps->cmdline, p, sizeof(ps->cmdline)); fillProcessCmdline(ps->cmdline, p, sizeof(ps->cmdline));
/* process command line */ /* process command line */
return (0); return (0);
} }
static void static void
cleanupProcessList(void) cleanupProcessList(void)
{ {
ProcessInfo* ps; ProcessInfo* ps;
ProcessCount = 0; ProcessCount = 0;
/* All processes that do not have the active flag set are assumed dead /* All processes that do not have the active flag set are assumed dead
* and will be removed from the list. The alive flag is cleared. */ * and will be removed from the list. The alive flag is cleared. */
for (ps = first_ctnr(ProcessList); ps; ps = next_ctnr(ProcessList)) for (ps = first_ctnr(ProcessList); ps; ps = next_ctnr(ProcessList)) {
{ if (ps->alive) {
if (ps->alive) /* Process is still alive. Just clear flag. */
{ ps->alive = 0;
/* Process is still alive. Just clear flag. */ ProcessCount++;
ps->alive = 0; } else {
ProcessCount++; /* Process has probably died. We remove it from the list and
} destruct the data structure. */
else free(remove_ctnr(ProcessList));
{ }
/* Process has probably died. We remove it from the list and }
* destruct the data structure. */
free(remove_ctnr(ProcessList));
}
}
} }
/* /*
@ -288,211 +283,203 @@ cleanupProcessList(void)
void void
initProcessList(struct SensorModul* sm) initProcessList(struct SensorModul* sm)
{ {
ProcessList = new_ctnr(); ProcessList = new_ctnr();
registerMonitor("ps", "table", printProcessList, printProcessListInfo, sm); registerMonitor("ps", "table", printProcessList, printProcessListInfo, sm);
registerMonitor("pscount", "integer", printProcessCount, printProcessCountInfo, sm); registerMonitor("pscount", "integer", printProcessCount, printProcessCountInfo, sm);
if (!RunAsDaemon) if (!RunAsDaemon) {
{ registerCommand("kill", killProcess);
registerCommand("kill", killProcess); registerCommand("setpriority", setPriority);
registerCommand("setpriority", setPriority); }
}
updateProcessList(); updateProcessList();
} }
void void
exitProcessList(void) exitProcessList(void)
{ {
removeMonitor("ps"); removeMonitor("ps");
removeMonitor("pscount"); removeMonitor("pscount");
if (ProcessList) if (ProcessList)
free (ProcessList); free (ProcessList);
} }
int int
updateProcessList(void) updateProcessList(void)
{ {
int mib[6]; int mib[6];
size_t len; size_t len;
size_t num; size_t num;
struct kinfo_proc *p; struct kinfo_proc *p;
mib[0] = CTL_KERN; mib[0] = CTL_KERN;
mib[1] = KERN_PROC; mib[1] = KERN_PROC;
mib[2] = KERN_PROC_ALL; mib[2] = KERN_PROC_ALL;
mib[3] = 0; mib[3] = 0;
mib[4] = sizeof(struct kinfo_proc); mib[4] = sizeof(struct kinfo_proc);
mib[5] = 0; mib[5] = 0;
if (sysctl(mib, 6, NULL, &len, NULL, 0) == -1) if (sysctl(mib, 6, NULL, &len, NULL, 0) == -1)
return 0; return 0;
len = 5 * len / 4; len = 5 * len / 4;
p = malloc(len); p = malloc(len);
if (!p) if (!p)
return 0; return 0;
mib[5] = len/ sizeof(struct kinfo_proc); mib[5] = len/ sizeof(struct kinfo_proc);
if (sysctl(mib, 6, p, &len, NULL, 0) == -1) if (sysctl(mib, 6, p, &len, NULL, 0) == -1)
return 0; return 0;
for (num = 0; num < len / sizeof(struct kinfo_proc); num++) for (num = 0; num < len / sizeof(struct kinfo_proc); num++)
updateProcess(&p[num]); updateProcess(&p[num]);
free(p); free(p);
cleanupProcessList(); cleanupProcessList();
return (0); return (0);
} }
void void
printProcessListInfo(const char* cmd) printProcessListInfo(const char* cmd)
{ {
fprintf(CurrentClient, "Name\tPID\tPPID\tUID\tGID\tStatus\tUser%%\tSystem%%\tNice\tVmSize\tVmRss\tLogin\tCommand\n"); fprintf(CurrentClient, "Name\tPID\tPPID\tUID\tGID\tStatus\tUser%%\tSystem%%\tNice\tVmSize\tVmRss\tLogin\tCommand\n");
fprintf(CurrentClient, "s\td\td\td\td\tS\tf\tf\td\tD\tD\ts\ts\n"); fprintf(CurrentClient, "s\td\td\td\td\tS\tf\tf\td\tD\tD\ts\ts\n");
} }
void void
printProcessList(const char* cmd) printProcessList(const char* cmd)
{ {
ProcessInfo* ps; ProcessInfo* ps;
for (ps = first_ctnr(ProcessList); ps; ps = next_ctnr(ProcessList)) for (ps = first_ctnr(ProcessList); ps; ps = next_ctnr(ProcessList)) {
{ fprintf(CurrentClient, "%s\t%ld\t%ld\t%ld\t%ld\t%s\t%.2f\t%.2f\t%d\t%d\t%d\t%s\t%s\n",
fprintf(CurrentClient, "%s\t%ld\t%ld\t%ld\t%ld\t%s\t%.2f\t%.2f\t%d\t%d\t%d\t%s\t%s\n", ps->name, (long)ps->pid, (long)ps->ppid,
ps->name, (long)ps->pid, (long)ps->ppid, (long)ps->uid, (long)ps->gid, ps->status,
(long)ps->uid, (long)ps->gid, ps->status, ps->userLoad, ps->sysLoad, ps->niceLevel,
ps->userLoad, ps->sysLoad, ps->niceLevel, ps->vmSize / 1024, ps->vmRss / 1024, ps->userName, ps->cmdline);
ps->vmSize / 1024, ps->vmRss / 1024, ps->userName, ps->cmdline); }
}
} }
void void
printProcessCount(const char* cmd) printProcessCount(const char* cmd)
{ {
fprintf(CurrentClient, "%d\n", ProcessCount); fprintf(CurrentClient, "%d\n", ProcessCount);
} }
void void
printProcessCountInfo(const char* cmd) printProcessCountInfo(const char* cmd)
{ {
fprintf(CurrentClient, "Number of Processes\t1\t65535\t\n"); fprintf(CurrentClient, "Number of Processes\t1\t65535\t\n");
} }
void void
killProcess(const char* cmd) killProcess(const char* cmd)
{ {
int sig, pid; int sig, pid;
sscanf(cmd, "%*s %d %d", &pid, &sig); sscanf(cmd, "%*s %d %d", &pid, &sig);
switch(sig) switch(sig) {
{ case MENU_ID_SIGABRT:
case MENU_ID_SIGABRT: sig = SIGABRT;
sig = SIGABRT; break;
break; case MENU_ID_SIGALRM:
case MENU_ID_SIGALRM: sig = SIGALRM;
sig = SIGALRM; break;
break; case MENU_ID_SIGCHLD:
case MENU_ID_SIGCHLD: sig = SIGCHLD;
sig = SIGCHLD; break;
break; case MENU_ID_SIGCONT:
case MENU_ID_SIGCONT: sig = SIGCONT;
sig = SIGCONT; break;
break; case MENU_ID_SIGFPE:
case MENU_ID_SIGFPE: sig = SIGFPE;
sig = SIGFPE; break;
break; case MENU_ID_SIGHUP:
case MENU_ID_SIGHUP: sig = SIGHUP;
sig = SIGHUP; break;
break; case MENU_ID_SIGILL:
case MENU_ID_SIGILL: sig = SIGILL;
sig = SIGILL; break;
break; case MENU_ID_SIGINT:
case MENU_ID_SIGINT: sig = SIGINT;
sig = SIGINT; break;
break; case MENU_ID_SIGKILL:
case MENU_ID_SIGKILL: sig = SIGKILL;
sig = SIGKILL; break;
break; case MENU_ID_SIGPIPE:
case MENU_ID_SIGPIPE: sig = SIGPIPE;
sig = SIGPIPE; break;
break; case MENU_ID_SIGQUIT:
case MENU_ID_SIGQUIT: sig = SIGQUIT;
sig = SIGQUIT; break;
break; case MENU_ID_SIGSEGV:
case MENU_ID_SIGSEGV: sig = SIGSEGV;
sig = SIGSEGV; break;
break; case MENU_ID_SIGSTOP:
case MENU_ID_SIGSTOP: sig = SIGSTOP;
sig = SIGSTOP; break;
break; case MENU_ID_SIGTERM:
case MENU_ID_SIGTERM: sig = SIGTERM;
sig = SIGTERM; break;
break; case MENU_ID_SIGTSTP:
case MENU_ID_SIGTSTP: sig = SIGTSTP;
sig = SIGTSTP; break;
break; case MENU_ID_SIGTTIN:
case MENU_ID_SIGTTIN: sig = SIGTTIN;
sig = SIGTTIN; break;
break; case MENU_ID_SIGTTOU:
case MENU_ID_SIGTTOU: sig = SIGTTOU;
sig = SIGTTOU; break;
break; case MENU_ID_SIGUSR1:
case MENU_ID_SIGUSR1: sig = SIGUSR1;
sig = SIGUSR1; break;
break; case MENU_ID_SIGUSR2:
case MENU_ID_SIGUSR2: sig = SIGUSR2;
sig = SIGUSR2; break;
break; }
} if (kill((pid_t) pid, sig)) {
if (kill((pid_t) pid, sig)) switch(errno) {
{ case EINVAL:
switch(errno) fprintf(CurrentClient, "4\t%d\n", pid);
{ break;
case EINVAL: case ESRCH:
fprintf(CurrentClient, "4\t%d\n", pid); fprintf(CurrentClient, "3\t%d\n", pid);
break; break;
case ESRCH: case EPERM:
fprintf(CurrentClient, "3\t%d\n", pid); fprintf(CurrentClient, "2\t%d\n", pid);
break; break;
case EPERM: default:
fprintf(CurrentClient, "2\t%d\n", pid); fprintf(CurrentClient, "1\t%d\n", pid); /* unknown error */
break; break;
default: }
fprintf(CurrentClient, "1\t%d\n", pid); /* unknown error */ } else {
break; fprintf(CurrentClient, "0\t%d\n", pid);
} }
}
else
fprintf(CurrentClient, "0\t%d\n", pid);
} }
void void
setPriority(const char* cmd) setPriority(const char* cmd)
{ {
int pid, prio; int pid, prio;
sscanf(cmd, "%*s %d %d", &pid, &prio); sscanf(cmd, "%*s %d %d", &pid, &prio);
if (setpriority(PRIO_PROCESS, pid, prio)) if (setpriority(PRIO_PROCESS, pid, prio)) {
{ switch(errno) {
switch(errno) case EINVAL:
{ fprintf(CurrentClient, "4\n");
case EINVAL: break;
fprintf(CurrentClient, "4\n"); case ESRCH:
break; fprintf(CurrentClient, "3\n");
case ESRCH: break;
fprintf(CurrentClient, "3\n"); case EPERM:
break; case EACCES:
case EPERM: fprintf(CurrentClient, "2\n");
case EACCES: break;
fprintf(CurrentClient, "2\n"); default:
break; fprintf(CurrentClient, "1\n"); /* unknown error */
default: break;
fprintf(CurrentClient, "1\n"); /* unknown error */ }
break; } else {
} fprintf(CurrentClient, "0\n");
} }
else
fprintf(CurrentClient, "0\n");
} }

View file

@ -43,107 +43,107 @@ int cpu_states[CPUSTATES];
void void
initCpuInfo(struct SensorModul* sm) initCpuInfo(struct SensorModul* sm)
{ {
/* Total CPU load */ /* Total CPU load */
registerMonitor("cpu/system/user", "integer", printCPUUser, printCPUUserInfo, sm); registerMonitor("cpu/system/user", "integer", printCPUUser, printCPUUserInfo, sm);
registerMonitor("cpu/system/nice", "integer", printCPUNice, printCPUNiceInfo, sm); registerMonitor("cpu/system/nice", "integer", printCPUNice, printCPUNiceInfo, sm);
registerMonitor("cpu/system/sys", "integer", printCPUSys, printCPUSysInfo, sm); registerMonitor("cpu/system/sys", "integer", printCPUSys, printCPUSysInfo, sm);
registerMonitor("cpu/system/idle", "integer", printCPUIdle, printCPUIdleInfo, sm); registerMonitor("cpu/system/idle", "integer", printCPUIdle, printCPUIdleInfo, sm);
registerMonitor("cpu/interrupt", "integer", printCPUInterrupt, printCPUInterruptInfo, sm); registerMonitor("cpu/interrupt", "integer", printCPUInterrupt, printCPUInterruptInfo, sm);
/* Monitor names changed from kde3 => kde4. Remain compatible with legacy requests when possible. */ /* Monitor names changed from kde3 => kde4. Remain compatible with legacy requests when possible. */
registerLegacyMonitor("cpu/user", "integer", printCPUUser, printCPUUserInfo, sm); registerLegacyMonitor("cpu/user", "integer", printCPUUser, printCPUUserInfo, sm);
registerLegacyMonitor("cpu/nice", "integer", printCPUNice, printCPUNiceInfo, sm); registerLegacyMonitor("cpu/nice", "integer", printCPUNice, printCPUNiceInfo, sm);
registerLegacyMonitor("cpu/sys", "integer", printCPUSys, printCPUSysInfo, sm); registerLegacyMonitor("cpu/sys", "integer", printCPUSys, printCPUSysInfo, sm);
registerLegacyMonitor("cpu/idle", "integer", printCPUIdle, printCPUIdleInfo, sm); registerLegacyMonitor("cpu/idle", "integer", printCPUIdle, printCPUIdleInfo, sm);
updateCpuInfo(); updateCpuInfo();
} }
void void
exitCpuInfo(void) exitCpuInfo(void)
{ {
removeMonitor("cpu/system/user"); removeMonitor("cpu/system/user");
removeMonitor("cpu/system/nice"); removeMonitor("cpu/system/nice");
removeMonitor("cpu/system/sys"); removeMonitor("cpu/system/sys");
removeMonitor("cpu/system/idle"); removeMonitor("cpu/system/idle");
removeMonitor("cpu/interrupt"); removeMonitor("cpu/interrupt");
/* These were registered as legacy monitors */ /* These were registered as legacy monitors */
removeMonitor("cpu/user"); removeMonitor("cpu/user");
removeMonitor("cpu/nice"); removeMonitor("cpu/nice");
removeMonitor("cpu/sys"); removeMonitor("cpu/sys");
removeMonitor("cpu/idle"); removeMonitor("cpu/idle");
} }
int int
updateCpuInfo(void) updateCpuInfo(void)
{ {
static int cp_time_mib[] = {CTL_KERN, KERN_CPTIME}; static int cp_time_mib[] = {CTL_KERN, KERN_CPTIME};
size_t size; size_t size;
size=sizeof(cp_time); size=sizeof(cp_time);
sysctl(cp_time_mib, 2, &cp_time, &size, NULL, 0); sysctl(cp_time_mib, 2, &cp_time, &size, NULL, 0);
percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff); percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff);
return (0); return (0);
} }
void void
printCPUUser(const char* cmd) printCPUUser(const char* cmd)
{ {
fprintf(CurrentClient, "%d\n", cpu_states[CP_USER]/10); fprintf(CurrentClient, "%d\n", cpu_states[CP_USER]/10);
} }
void void
printCPUUserInfo(const char* cmd) printCPUUserInfo(const char* cmd)
{ {
fprintf(CurrentClient, "CPU User Load\t0\t100\t%%\n"); fprintf(CurrentClient, "CPU User Load\t0\t100\t%%\n");
} }
void void
printCPUNice(const char* cmd) printCPUNice(const char* cmd)
{ {
fprintf(CurrentClient, "%d\n", cpu_states[CP_NICE]/10); fprintf(CurrentClient, "%d\n", cpu_states[CP_NICE]/10);
} }
void void
printCPUNiceInfo(const char* cmd) printCPUNiceInfo(const char* cmd)
{ {
fprintf(CurrentClient, "CPU Nice Load\t0\t100\t%%\n"); fprintf(CurrentClient, "CPU Nice Load\t0\t100\t%%\n");
} }
void void
printCPUSys(const char* cmd) printCPUSys(const char* cmd)
{ {
fprintf(CurrentClient, "%d\n", cpu_states[CP_SYS]/10); fprintf(CurrentClient, "%d\n", cpu_states[CP_SYS]/10);
} }
void void
printCPUSysInfo(const char* cmd) printCPUSysInfo(const char* cmd)
{ {
fprintf(CurrentClient, "CPU System Load\t0\t100\t%%\n"); fprintf(CurrentClient, "CPU System Load\t0\t100\t%%\n");
} }
void void
printCPUIdle(const char* cmd) printCPUIdle(const char* cmd)
{ {
fprintf(CurrentClient, "%d\n", cpu_states[CP_IDLE]/10); fprintf(CurrentClient, "%d\n", cpu_states[CP_IDLE]/10);
} }
void void
printCPUIdleInfo(const char* cmd) printCPUIdleInfo(const char* cmd)
{ {
fprintf(CurrentClient, "CPU Idle Load\t0\t100\t%%\n"); fprintf(CurrentClient, "CPU Idle Load\t0\t100\t%%\n");
} }
void void
printCPUInterrupt(const char* cmd) printCPUInterrupt(const char* cmd)
{ {
fprintf(CurrentClient, "%d\n", cpu_states[CP_INTR]/10); fprintf(CurrentClient, "%d\n", cpu_states[CP_INTR]/10);
} }
void void
printCPUInterruptInfo(const char* cmd) printCPUInterruptInfo(const char* cmd)
{ {
fprintf(CurrentClient, "CPU Interrupt Load\t0\t100\t%%\n"); fprintf(CurrentClient, "CPU Interrupt Load\t0\t100\t%%\n");
} }
/* The part ripped from top... */ /* The part ripped from top... */
@ -187,22 +187,18 @@ long *diffs;
dp = diffs; dp = diffs;
/* calculate changes for each state and the overall change */ /* calculate changes for each state and the overall change */
for (i = 0; i < cnt; i++) for (i = 0; i < cnt; i++) {
{ if ((change = *new - *old) < 0) {
if ((change = *new - *old) < 0) /* this only happens when the counter wraps */
{ change = (int)((unsigned long)*new-(unsigned long)*old);
/* this only happens when the counter wraps */ }
change = (int) total_change += (*dp++ = change);
((unsigned long)*new-(unsigned long)*old); *old++ = *new++;
}
total_change += (*dp++ = change);
*old++ = *new++;
} }
/* avoid divide by zero potential */ /* avoid divide by zero potential */
if (total_change == 0) if (total_change == 0) {
{ total_change = 1;
total_change = 1;
} }
/* calculate percentages based on overall change, rounding up */ /* calculate percentages based on overall change, rounding up */
@ -210,9 +206,8 @@ long *diffs;
/* Do not divide by 0. Causes Floating point exception */ /* Do not divide by 0. Causes Floating point exception */
if(total_change) { if(total_change) {
for (i = 0; i < cnt; i++) for (i = 0; i < cnt; i++) {
{ *out++ = (int)((*diffs++ * 1000 + half_total) / total_change);
*out++ = (int)((*diffs++ * 1000 + half_total) / total_change);
} }
} }

View file

@ -54,29 +54,29 @@ void swapmode(int *used, int *total);
void void
initMemory(struct SensorModul* sm) initMemory(struct SensorModul* sm)
{ {
int pagesize; int pagesize;
static int physmem_mib[] = { CTL_HW, HW_PHYSMEM }; static int physmem_mib[] = { CTL_HW, HW_PHYSMEM };
size_t size; size_t size;
/* get the page size with "getpagesize" and calculate pageshift from /* get the page size with "getpagesize" and calculate pageshift from
* it */ * it */
pagesize = getpagesize(); pagesize = getpagesize();
pageshift = 0; pageshift = 0;
while (pagesize > 1) { while (pagesize > 1) {
pageshift++; pageshift++;
pagesize >>= 1; pagesize >>= 1;
} }
size = sizeof(Total); size = sizeof(Total);
sysctl(physmem_mib, 2, &Total, &size, NULL, 0); sysctl(physmem_mib, 2, &Total, &size, NULL, 0);
Total /= 1024; Total /= 1024;
swapmode(&SUsed, &STotal); swapmode(&SUsed, &STotal);
registerMonitor("mem/physical/free", "integer", printMFree, printMFreeInfo, sm); registerMonitor("mem/physical/free", "integer", printMFree, printMFreeInfo, sm);
registerMonitor("mem/physical/active", "integer", printActive, printActiveInfo, sm); registerMonitor("mem/physical/active", "integer", printActive, printActiveInfo, sm);
registerMonitor("mem/physical/inactive", "integer", printInActive, printInActiveInfo, sm); registerMonitor("mem/physical/inactive", "integer", printInActive, printInActiveInfo, sm);
registerMonitor("mem/physical/used", "integer", printUsed, printUsedInfo, sm); registerMonitor("mem/physical/used", "integer", printUsed, printUsedInfo, sm);
registerMonitor("mem/physical/application", "integer", printApplication, printApplicationInfo, sm); registerMonitor("mem/physical/application", "integer", printApplication, printApplicationInfo, sm);
registerMonitor("mem/swap/free", "integer", printSwapFree, printSwapFreeInfo, sm); registerMonitor("mem/swap/free", "integer", printSwapFree, printSwapFreeInfo, sm);
registerMonitor("mem/swap/used", "integer", printSwapUsed, printSwapUsedInfo, sm); registerMonitor("mem/swap/used", "integer", printSwapUsed, printSwapUsedInfo, sm);
} }
void void
@ -87,112 +87,112 @@ exitMemory(void)
int int
updateMemory(void) updateMemory(void)
{ {
static int vmtotal_mib[] = {CTL_VM, VM_METER}; static int vmtotal_mib[] = {CTL_VM, VM_METER};
size_t size; size_t size;
struct vmtotal vmtotal; struct vmtotal vmtotal;
size = sizeof(vmtotal); size = sizeof(vmtotal);
if (sysctl(vmtotal_mib, 2, &vmtotal, &size, NULL, 0) < 0) if (sysctl(vmtotal_mib, 2, &vmtotal, &size, NULL, 0) < 0)
return -1; return -1;
MFree = pagetok(vmtotal.t_free); MFree = pagetok(vmtotal.t_free);
MFree /= 1024; MFree /= 1024;
Active = pagetok(vmtotal.t_arm); Active = pagetok(vmtotal.t_arm);
Active /= 1024; Active /= 1024;
InActive = pagetok(vmtotal.t_rm); InActive = pagetok(vmtotal.t_rm);
InActive /= 1024; InActive /= 1024;
InActive -= Active; InActive -= Active;
Used = Total - MFree; Used = Total - MFree;
Application = Used; Application = Used;
swapmode(&SUsed, &STotal); swapmode(&SUsed, &STotal);
SFree = STotal - SUsed; SFree = STotal - SUsed;
return 0; return 0;
} }
void void
printMFree(const char* cmd) printMFree(const char* cmd)
{ {
fprintf(CurrentClient, "%d\n", MFree); fprintf(CurrentClient, "%d\n", MFree);
} }
void void
printMFreeInfo(const char* cmd) printMFreeInfo(const char* cmd)
{ {
fprintf(CurrentClient, "Free Memory\t0\t%d\tKB\n", Total); fprintf(CurrentClient, "Free Memory\t0\t%d\tKB\n", Total);
} }
void void
printUsed(const char* cmd) printUsed(const char* cmd)
{ {
fprintf(CurrentClient, "%d\n", Used); fprintf(CurrentClient, "%d\n", Used);
} }
void void
printUsedInfo(const char* cmd) printUsedInfo(const char* cmd)
{ {
fprintf(CurrentClient, "Used Memory\t0\t%d\tKB\n", Total); fprintf(CurrentClient, "Used Memory\t0\t%d\tKB\n", Total);
} }
void void
printApplication(const char* cmd) printApplication(const char* cmd)
{ {
fprintf(CurrentClient, "%d\n", Application); fprintf(CurrentClient, "%d\n", Application);
} }
void void
printApplicationInfo(const char* cmd) printApplicationInfo(const char* cmd)
{ {
fprintf(CurrentClient, "Application Memory\t0\t%ld\tKB\n", Total); fprintf(CurrentClient, "Application Memory\t0\t%ld\tKB\n", Total);
} }
void void
printActive(const char* cmd) printActive(const char* cmd)
{ {
fprintf(CurrentClient, "%d\n", Active); fprintf(CurrentClient, "%d\n", Active);
} }
void void
printActiveInfo(const char* cmd) printActiveInfo(const char* cmd)
{ {
fprintf(CurrentClient, "Active Memory\t0\t%d\tKB\n", Total); fprintf(CurrentClient, "Active Memory\t0\t%d\tKB\n", Total);
} }
void void
printInActive(const char* cmd) printInActive(const char* cmd)
{ {
fprintf(CurrentClient, "%d\n", InActive); fprintf(CurrentClient, "%d\n", InActive);
} }
void void
printInActiveInfo(const char* cmd) printInActiveInfo(const char* cmd)
{ {
fprintf(CurrentClient, "InActive Memory\t0\t%d\tKB\n", Total); fprintf(CurrentClient, "InActive Memory\t0\t%d\tKB\n", Total);
} }
void void
printSwapUsed(const char* cmd) printSwapUsed(const char* cmd)
{ {
fprintf(CurrentClient, "%d\n", SUsed); fprintf(CurrentClient, "%d\n", SUsed);
} }
void void
printSwapUsedInfo(const char* cmd) printSwapUsedInfo(const char* cmd)
{ {
fprintf(CurrentClient, "Used Swap Memory\t0\t%d\tKB\n", STotal); fprintf(CurrentClient, "Used Swap Memory\t0\t%d\tKB\n", STotal);
} }
void void
printSwapFree(const char* cmd) printSwapFree(const char* cmd)
{ {
fprintf(CurrentClient, "%d\n", SFree); fprintf(CurrentClient, "%d\n", SFree);
} }
void void
printSwapFreeInfo(const char* cmd) printSwapFreeInfo(const char* cmd)
{ {
fprintf(CurrentClient, "Free Swap Memory\t0\t%d\tKB\n", STotal); fprintf(CurrentClient, "Free Swap Memory\t0\t%d\tKB\n", STotal);
} }
/* /*
@ -204,35 +204,35 @@ Taken from OpenBSD top command
void void
swapmode (int *used, int *total) swapmode (int *used, int *total)
{ {
int nswap, rnswap, i; int nswap, rnswap, i;
struct swapent *swdev; struct swapent *swdev;
*total = *used = 0; *total = *used = 0;
/* Number of swap devices */ /* Number of swap devices */
nswap = swapctl(SWAP_NSWAP, 0, 0); nswap = swapctl(SWAP_NSWAP, 0, 0);
if (nswap == 0) if (nswap == 0)
return; return;
swdev = (struct swapent *) malloc(nswap * sizeof(*swdev)); swdev = (struct swapent *) malloc(nswap * sizeof(*swdev));
if (swdev == NULL) if (swdev == NULL)
return; return;
rnswap = swapctl(SWAP_STATS, swdev, nswap);
if (rnswap == -1) {
free(swdev);
return;
}
/* if rnswap != nswap, then what? */
/* Total things up */
for (i = 0; i < nswap; i++) {
if (swdev[i].se_flags & SWF_ENABLE) {
*used += (swdev[i].se_inuse / (1024 / DEV_BSIZE));
*total += (swdev[i].se_nblks / (1024 / DEV_BSIZE));
}
}
rnswap = swapctl(SWAP_STATS, swdev, nswap);
if (rnswap == -1) {
free(swdev); free(swdev);
return;
}
/* if rnswap != nswap, then what? */
/* Total things up */
for (i = 0; i < nswap; i++) {
if (swdev[i].se_flags & SWF_ENABLE) {
*used += (swdev[i].se_inuse / (1024 / DEV_BSIZE));
*total += (swdev[i].se_nblks / (1024 / DEV_BSIZE));
}
}
free(swdev);
} }

View file

@ -91,25 +91,25 @@ void ProcessesLocal::Private::readProcStat(struct kinfo_proc *p, Process *ps)
ps->setVmRSS(p->ki_rssize * getpagesize() / 1024); ps->setVmRSS(p->ki_rssize * getpagesize() / 1024);
status = p->ki_stat; status = p->ki_stat;
// "idle","run","sleep","stop","zombie" // "idle","run","sleep","stop","zombie"
switch( status ) { switch( status ) {
case SRUN: case SRUN:
ps->setStatus(Process::Running); ps->setStatus(Process::Running);
break; break;
case SSLEEP: case SSLEEP:
case SWAIT: case SWAIT:
case SLOCK: case SLOCK:
ps->setStatus(Process::Sleeping); ps->setStatus(Process::Sleeping);
break; break;
case SSTOP: case SSTOP:
ps->setStatus(Process::Stopped); ps->setStatus(Process::Stopped);
break; break;
case SZOMB: case SZOMB:
ps->setStatus(Process::Zombie); ps->setStatus(Process::Zombie);
break; break;
default: default:
ps->setStatus(Process::OtherStatus); ps->setStatus(Process::OtherStatus);
break; break;
} }
} }
@ -149,8 +149,7 @@ ProcessesLocal::ProcessesLocal() : d(new Private())
long ProcessesLocal::getParentPid(long pid) { long ProcessesLocal::getParentPid(long pid) {
long long ppid = 0; long long ppid = 0;
struct kinfo_proc p; struct kinfo_proc p;
if(d->readProc(pid, &p)) if(d->readProc(pid, &p)) {
{
ppid = p.ki_ppid; ppid = p.ki_ppid;
} }
return ppid; return ppid;