ports/167970: [patch] sysutils/htop miscalculates memory usage

Sayetsky Anton vsjcfm at gmail.com
Wed May 16 15:20:02 UTC 2012


>Number:         167970
>Category:       ports
>Synopsis:       [patch] sysutils/htop miscalculates memory usage
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    freebsd-ports-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          update
>Submitter-Id:   current-users
>Arrival-Date:   Wed May 16 15:20:01 UTC 2012
>Closed-Date:
>Last-Modified:
>Originator:     Sayetsky Anton <vsjcfm at gmail.com>
>Release:        FreeBSD 8.3-RELEASE-p1 amd64
>Organization:
>Environment:
System: FreeBSD j.local 8.3-RELEASE-p1 FreeBSD 8.3-RELEASE-p1 #0
r235504: Wed May 16 14:33:18 EEST 2012
root at j.local:/tmp/obj/mnt/garbage/build/src/sys/JASONW amd64
>Description:
htop uses linux compability layer for memory calculation, but this
incorrect for FreeBSD.
>How-To-Repeat:
Install htop & compare memory values reported by htop and top from base system
>Fix:
Patch attached

--- patch-Makefile begins here ---
--- Makefile    2012-03-05 21:02:17.000000000 +0200
+++ /root/Makefile      2012-05-16 17:40:49.000000000 +0300
@@ -7,6 +7,7 @@

 PORTNAME=      htop
 PORTVERSION=   1.0.1
+PORTREVISION=  1
 CATEGORIES=    sysutils
 MASTER_SITES=  SF

--- patch-Makefile ends here ---

--- patch-configure.ac begins here ---
--- ./configure.ac.orig 2011-12-26 23:46:57.000000000 +0200
+++ ./configure.ac      2012-05-16 17:39:50.000000000 +0300
@@ -23,11 +23,12 @@

 # Checks for libraries.
 AC_CHECK_LIB([m], [ceil], [], [missing_libraries="$missing_libraries libm"])
+AC_CHECK_LIB([kvm], [kvm_open], [],
[missing_libraries="$missing_libraries libkvm"])

 # Checks for header files.
 AC_HEADER_DIRENT
 AC_HEADER_STDC
-AC_CHECK_HEADERS([stdlib.h string.h strings.h sys/param.h sys/time.h
unistd.h curses.h],[:],[
+AC_CHECK_HEADERS([stdlib.h string.h strings.h sys/param.h sys/time.h
unistd.h curses.h kvm.h paths.h fcntl.h sys/sysctl.h],[:],[
   missing_headers="$missing_headers $ac_header"
 ])
 AC_CHECK_HEADERS([execinfo.h],[:],[:])
--- patch-configure.ac ends here ---

--- patch-ProcessList.c begins here ---
--- ./ProcessList.c.orig        2012-02-03 01:45:11.000000000 +0200
+++ ./ProcessList.c     2012-05-16 17:39:50.000000000 +0300
@@ -25,6 +25,19 @@
 #include <time.h>
 #include <assert.h>

+#ifndef PAGE_SIZE
+#define PAGE_SIZE sysconf(_SC_PAGESIZE)
+#endif
+
+#ifdef __FreeBSD__
+#define KB 1024
+#define SYSCTLBYNAME(name, var, len) sysctlbyname(name, &(var),
&(len), NULL, 0)
+#include <kvm.h>
+#include <paths.h>
+#include <fcntl.h>
+#include <sys/sysctl.h>
+#endif
+
 /*{
 #include "Vector.h"
 #include "Hashtable.h"
@@ -749,11 +762,13 @@

 void ProcessList_scan(ProcessList* this) {
    unsigned long long int usertime, nicetime, systemtime,
systemalltime, idlealltime, idletime, totaltime, virtalltime;
-   unsigned long long int swapFree = 0;
+   int cpus = this->cpuCount;
+   FILE* file = NULL;

-   FILE* file = fopen(PROCMEMINFOFILE, "r");
+   #ifndef __FreeBSD__
+   unsigned long long int swapFree = 0;
+   file = fopen(PROCMEMINFOFILE, "r");
    assert(file != NULL);
-   int cpus = this->cpuCount;
    {
       char buffer[128];
       while (fgets(buffer, 128, file)) {
@@ -788,6 +803,33 @@
    this->usedMem = this->totalMem - this->freeMem;
    this->usedSwap = this->totalSwap - swapFree;
    fclose(file);
+   #endif
+
+   #ifdef __FreeBSD__
+   kvm_t *kd = NULL;
+   struct kvm_swap kvmswapinfo[1];
+   size_t len = 0;
+
+   kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, NULL);
+   assert(kd != NULL);
+   kvm_getswapinfo(kd, kvmswapinfo, 1, 0);
+   this->totalSwap = kvmswapinfo[0].ksw_total * PAGE_SIZE / KB;
+   this->usedSwap = kvmswapinfo[0].ksw_used * PAGE_SIZE / KB;
+   kvm_close(kd);
+   len = sizeof(this->totalMem);
+   SYSCTLBYNAME("vm.stats.vm.v_page_count", this->totalMem, len);
+   this->totalMem *= PAGE_SIZE / KB;
+   len = sizeof(this->cachedMem);
+   SYSCTLBYNAME("vm.stats.vm.v_cache_count", this->cachedMem, len);
+   this->cachedMem *= PAGE_SIZE / KB;
+   len = sizeof(this->buffersMem);
+   SYSCTLBYNAME("vfs.bufspace", this->buffersMem, len);
+   this->buffersMem /= KB;
+   len = sizeof(this->usedMem);
+   SYSCTLBYNAME("vm.stats.vm.v_active_count", this->usedMem, len);
+   this->usedMem = this->usedMem * PAGE_SIZE / KB + this->cachedMem +
this->buffersMem;
+   this->freeMem = this->totalMem - this->usedMem;
+   #endif

    file = fopen(PROCSTATFILE, "r");
    assert(file != NULL);
--- patch-ProcessList.c ends here ---
>Release-Note:
>Audit-Trail:
>Unformatted:



More information about the freebsd-ports-bugs mailing list