$NetBSD: patch-aa,v 1.2 2007/02/15 21:46:54 joerg Exp $ --- bbsload.cc.orig 2000-05-22 18:53:37.000000000 +0000 +++ bbsload.cc @@ -21,6 +21,20 @@ #include "bbsload.hh" +#ifdef __NetBSD__ +#include +#include +#include +#elif defined(__DragonFly__) +#include +#include +#include +#define template +#include +#undef template +#include +#endif + CheckLoad::CheckLoad(ToolWindow *toolwindow) { bbtool=toolwindow; timer=new BTimer(bbtool->getCurrentScreenInfo()->getBaseDisplay(),this) ; @@ -40,97 +54,106 @@ void CheckLoad::Reconfigure() { void CheckLoad::timeout() { - FILE *fp; - - char loadstr[28]; - char buffer[128]; - bool redraw=False; - struct stat file_status; - static time_t last_mtime; - static unsigned long old_user=0; - static unsigned long old_nice=0; - static unsigned long old_system=0; - static unsigned long old_idle=0; - broken=False; + static u_int64_t old_user=0, old_nice=0, old_system=0, + old_idle=0; + u_int64_t user,nice,system,idle,total; +#if defined(__DragonFly__) + struct kinfo_cputime cpu_time; +#else + u_int64_t cputimes[5]; +#endif + double lavg[3]; +#if defined(__DragonFly__) + size_t vm_stat_size = sizeof(struct vmstats); + struct vmstats vm_stat; +#else + struct uvmexp uvm_stat; +#endif + int ctl_name[3]; + size_t ctllen; - if (!(fp = fopen("/proc/loadavg", "r"))) - broken=True; - else - { - fstat(fileno(fp),&file_status); - if (file_status.st_mtime != last_mtime) - { - fgets(loadstr, 28, fp); - sscanf(loadstr,"%e %e %e %d/%d",&load.average1m, - &load.average5m,&load.average15m,&task.running, - &task.total); - redraw=true; - } - } - fclose(fp); - if (!(fp = fopen("/proc/meminfo", "r"))) - broken=True; - else - { - fstat(fileno(fp),&file_status); - if (file_status.st_mtime != last_mtime) - { - /* should work for now */ - fgets(buffer,128,fp); - fgets(buffer,128,fp); - sscanf(buffer,"Mem: %lu %lu %lu %lu %lu %lu",&mem.total, - &mem.used,&mem.free,&mem.shared,&mem.buffers, - &mem.cached); - fgets(buffer,128,fp); - sscanf(buffer,"Swap: %lu %lu %lu", - &swap.total,&swap.used,&swap.free); - - norm.total_used=(double)(mem.used+swap.used)/ - (double)(mem.total+swap.total); - norm.mem_used=(double)mem.used/(double)mem.total; - if (swap.total!=0) - norm.swap_used=(double)swap.used/(double)swap.total; - - redraw=true; - } - } - fclose(fp); + int i; - if (!(fp = fopen("/proc/stat", "r"))) - broken=True; - else - { - fstat(fileno(fp),&file_status); - if (file_status.st_mtime != last_mtime) - { - unsigned long user,nice,system,idle,total; - /* should work for now */ - fgets(buffer,128,fp); - sscanf(buffer,"cpu %lu %lu %lu %lu",&user, - &nice,&system,&idle); - total=(user-old_user)+(nice-old_nice)+ - (system-old_system)+(idle-old_idle); + broken=False; - if ((total!=0)&(old_user!=0)) - { - cpu.user=(user-old_user)/(double)total; - cpu.nice=(nice-old_nice)/(double)user; - cpu.system=(system-old_system)/(double)total; - cpu.idle=(idle-old_idle)/(double)total; - } - else - cpu.user=cpu.nice=cpu.system=cpu.idle=0; + /* Get system load */ - old_user=user; - old_nice=nice; - old_system=system; - old_idle=idle; - redraw=true; - } - } - fclose(fp); - if (redraw) - bbtool->Redraw(); + getloadavg(lavg,3); + load.average1m=lavg[0]; + load.average5m=lavg[1]; + load.average15m=lavg[2]; + + /* Get total amount of processes */ + + ctl_name[0]=CTL_KERN; + ctl_name[1]=KERN_PROC; + ctl_name[2]=KERN_PROC_ALL; + sysctl(ctl_name, 3, NULL, &ctllen,NULL,0); /* get size of proc table */ + task.total=ctllen/sizeof(struct kinfo_proc); /* Anybody have an + alternative ? */ + +#if defined(__DragonFly__) + sysctlbyname("vm.vmstats", &vm_stat, &vm_stat_size, NULL, 0); + mem.total = vm_stat.v_page_count; + mem.used = mem.total - vm_stat.v_free_count; + swap.total = 0; /* XXX fill me in, expensive! */ + swap.used = 0; +#else + /* task.running is missing */ + /* get memory statistics */ + ctllen=sizeof(uvm_stat); + ctl_name[0]=CTL_VM; + ctl_name[1]=VM_UVMEXP; + sysctl(ctl_name,2,&uvm_stat,&ctllen,NULL,0); + + mem.total=uvm_stat.npages; + mem.used=uvm_stat.npages-uvm_stat.free; + + swap.total=uvm_stat.swpages; + swap.used=uvm_stat.swpgonly; /* Only count pages in swap which are + not also present in memory */ +#endif + + norm.total_used=(double)(mem.used+swap.used)/ + (double)(mem.total+swap.total); + norm.mem_used=(double)mem.used/(double)mem.total; + if (swap.total!=0) + norm.swap_used=(double)swap.used/(double)swap.total; + +#if defined(__DragonFly__) + kinfo_get_sched_cputime(&cpu_time); + user = cpu_time.cp_user; + nice = cpu_time.cp_nice; + system = cpu_time.cp_sys + cpu_time.cp_intr; + idle = cpu_time.cp_idle; +#else + ctl_name[0]=CTL_KERN; + ctl_name[1]=KERN_CP_TIME; + ctllen=sizeof(cputimes); + if(sysctl(ctl_name,2,cputimes,&ctllen,NULL,0)) + perror("sysctl"); + user=cputimes[0]; + nice=cputimes[1]; + system=cputimes[2] /*sys*/ + +cputimes[3]/*intr*/; + idle=cputimes[4]; +#endif + total=(user-old_user)+(nice-old_nice)+ + (system-old_system)+(idle-old_idle); + if ((total!=0)&(old_user!=0)) + { + cpu.user=(double)(user-old_user)/(double)total; + cpu.nice=(double)(nice-old_nice)/(double)total; + cpu.system=(double)(system-old_system)/(double)total; + cpu.idle=(double)(idle-old_idle)/(double)total; + } + else + cpu.user=cpu.nice=cpu.system=cpu.idle=0; + old_user=user; + old_nice=nice; + old_system=system; + old_idle=idle; + bbtool->Redraw(); } @@ -233,15 +256,15 @@ void ToolWindow::MakeWindow(bool reconfi label_counter++; } if (resource->show.label_totalused) { - label.width[6] = XTextWidth(resource->label.font,"0.00",strlen("0.00")); + label.width[8] = XTextWidth(resource->label.font,"0.00",strlen("0.00")); label_counter++; } if (resource->show.label_memused) { - label.width[7] = XTextWidth(resource->label.font,"0.00",strlen("0.00")); + label.width[6] = XTextWidth(resource->label.font,"0.00",strlen("0.00")); label_counter++; } if (resource->show.label_swapused) { - label.width[8] = XTextWidth(resource->label.font,"0.00",strlen("0.00")); + label.width[7] = XTextWidth(resource->label.font,"0.00",strlen("0.00")); label_counter++; } if (resource->show.label_usercpu) { @@ -545,7 +568,7 @@ void ToolWindow::RedrawGauge(double leve } } -void ToolWindow::RedrawLabel(double level,int labelnr) +void ToolWindow::RedrawLabel(double level,int labelnr,int idx, BColor colour) { char t[6]; @@ -561,15 +584,14 @@ void ToolWindow::RedrawLabel(double leve xposition+=label.seperator_width; } - XSetForeground(getXDisplay(),frameGC, - resource->label.load1m_textColor.getPixel()); + XSetForeground(getXDisplay(),frameGC, colour.getPixel()); labelnr++; sprintf(t,"%1.2f",level); XDrawString(getXDisplay(), labelwin, frameGC, xposition, (label.height+resource->label.font->ascent- resource->label.font->descent) / 2, t, strlen(t)); - xposition+=label.width[2]; + xposition+=label.width[idx]; } void ToolWindow::Redraw() @@ -577,12 +599,12 @@ void ToolWindow::Redraw() int offset=0; if (resource->show.load1m) { - RedrawGauge(check_load->getLoad().average1m,offset); + RedrawGauge(check_load->getLoad().average1m,offset); offset++; } if (resource->show.load5m) { - RedrawGauge(check_load->getLoad().average5m,offset); + RedrawGauge(check_load->getLoad().average5m,offset); offset++; } if (resource->show.load15m) @@ -678,53 +700,63 @@ void ToolWindow::Redraw() if (resource->show.label_load1m) { - RedrawLabel(check_load->getLoad().average1m,labelnr); - labelnr++; + RedrawLabel(check_load->getLoad().average1m,offset,2, + resource->label.load1m_textColor); + offset++; } if (resource->show.label_load5m) { - RedrawLabel(check_load->getLoad().average5m,labelnr); - labelnr++; + RedrawLabel(check_load->getLoad().average5m,offset,3, + resource->label.load5m_textColor); + offset++; } if (resource->show.label_load15m) { - RedrawLabel(check_load->getLoad().average15m,labelnr); - labelnr++; + RedrawLabel(check_load->getLoad().average15m,offset,4, + resource->label.load15m_textColor); + offset++; } if (resource->show.label_memused) { - RedrawLabel(check_load->getNorm().mem_used,labelnr); - labelnr++; + RedrawLabel(check_load->getNorm().mem_used,offset,6, + resource->label.memUsed_textColor); + offset++; } if (resource->show.label_swapused) { - RedrawLabel(check_load->getNorm().swap_used,labelnr); - labelnr++; + RedrawLabel(check_load->getNorm().swap_used,offset,7, + resource->label.swapUsed_textColor); + offset++; } if (resource->show.label_totalused) { - RedrawLabel(check_load->getNorm().total_used,labelnr); - labelnr++; + RedrawLabel(check_load->getNorm().total_used,offset,8, + resource->label.totalUsed_textColor); + offset++; } if (resource->show.label_usercpu) { - RedrawLabel(check_load->getCpu().user,labelnr); - labelnr++; + RedrawLabel(check_load->getCpu().user,offset,9, + resource->label.seperator_textColor); + offset++; } if (resource->show.label_nicecpu) { - RedrawLabel(check_load->getCpu().nice,labelnr); - labelnr++; + RedrawLabel(check_load->getCpu().nice,offset,10, + resource->label.seperator_textColor); + offset++; } if (resource->show.label_systemcpu) { - RedrawLabel(check_load->getCpu().system,labelnr); - labelnr++; + RedrawLabel(check_load->getCpu().system,offset,11, + resource->label.seperator_textColor); + offset++; } if (resource->show.label_idlecpu) { - RedrawLabel(check_load->getCpu().idle,labelnr); - labelnr++; + RedrawLabel(check_load->getCpu().idle,offset,12, + resource->label.seperator_textColor); + offset++; } }