From owner-freebsd-ports-bugs@FreeBSD.ORG Fri May 20 00:10:10 2011 Return-Path: Delivered-To: freebsd-ports-bugs@hub.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 5CCBA1065674 for ; Fri, 20 May 2011 00:10:10 +0000 (UTC) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (freefall.freebsd.org [IPv6:2001:4f8:fff6::28]) by mx1.freebsd.org (Postfix) with ESMTP id 2D58D8FC13 for ; Fri, 20 May 2011 00:10:10 +0000 (UTC) Received: from freefall.freebsd.org (localhost [127.0.0.1]) by freefall.freebsd.org (8.14.4/8.14.4) with ESMTP id p4K0AAtL029404 for ; Fri, 20 May 2011 00:10:10 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.14.4/8.14.4/Submit) id p4K0AAZC029403; Fri, 20 May 2011 00:10:10 GMT (envelope-from gnats) Resent-Date: Fri, 20 May 2011 00:10:10 GMT Resent-Message-Id: <201105200010.p4K0AAZC029403@freefall.freebsd.org> Resent-From: FreeBSD-gnats-submit@FreeBSD.org (GNATS Filer) Resent-To: freebsd-ports-bugs@FreeBSD.org Resent-Reply-To: FreeBSD-gnats-submit@FreeBSD.org, Bartosz Fabianowski Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 6F931106564A for ; Fri, 20 May 2011 00:02:29 +0000 (UTC) (envelope-from nobody@FreeBSD.org) Received: from red.freebsd.org (red.freebsd.org [IPv6:2001:4f8:fff6::22]) by mx1.freebsd.org (Postfix) with ESMTP id 44E688FC12 for ; Fri, 20 May 2011 00:02:29 +0000 (UTC) Received: from red.freebsd.org (localhost [127.0.0.1]) by red.freebsd.org (8.14.4/8.14.4) with ESMTP id p4K02SZL013851 for ; Fri, 20 May 2011 00:02:28 GMT (envelope-from nobody@red.freebsd.org) Received: (from nobody@localhost) by red.freebsd.org (8.14.4/8.14.4/Submit) id p4K02SvN013850; Fri, 20 May 2011 00:02:28 GMT (envelope-from nobody) Message-Id: <201105200002.p4K02SvN013850@red.freebsd.org> Date: Fri, 20 May 2011 00:02:28 GMT From: Bartosz Fabianowski To: freebsd-gnats-submit@FreeBSD.org X-Send-Pr-Version: www-3.1 Cc: Subject: ports/157195: [PATCH] sysutils/htop miscalculates available memory X-BeenThere: freebsd-ports-bugs@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Ports bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 20 May 2011 00:10:10 -0000 >Number: 157195 >Category: ports >Synopsis: [PATCH] sysutils/htop miscalculates available memory >Confidential: no >Severity: non-critical >Priority: low >Responsible: freebsd-ports-bugs >State: open >Quarter: >Keywords: >Date-Required: >Class: sw-bug >Submitter-Id: current-users >Arrival-Date: Fri May 20 00:10:09 UTC 2011 >Closed-Date: >Last-Modified: >Originator: Bartosz Fabianowski >Release: 8.2-STABLE >Organization: >Environment: >Description: The sysutils/htop port reads /compat/linux/proc/meminfo to calculate the amount of memory used. The code that generates this file resides in sys/compat/linprocfs/linprocfs.c. Here, the amount of available memory is intentionally misreported: /* * The correct thing here would be: * memfree = cnt.v_free_count * PAGE_SIZE; memused = memtotal - memfree; * * but it might mislead linux binaries into thinking there * is very little memory left, so we cheat and tell them that * all memory that isn't wired down is free. */ memused = cnt.v_wire_count * PAGE_SIZE; memfree = memtotal - memused; >How-To-Repeat: Run top and htop simultaneously. Notice that htop reports very low memory usage while top reports the correct value. >Fix: The attached patch uses sysctl to read the actual amount of available memory, just as top does. Patch attached with submission follows: --- ProcessList.c.orig 2009-03-11 14:03:42.000000000 +0100 +++ ProcessList.c 2011-05-20 01:48:22.000000000 +0200 @@ -31,6 +31,11 @@ #include "debug.h" #include +#ifdef __FreeBSD__ +#include +#include +#endif + /*{ #ifndef PROCDIR #define PROCDIR "/proc" @@ -313,7 +318,7 @@ unsigned int pid = p->pid; int index = Vector_indexOf(this->processes, p, Process_pidCompare); assert(index != -1); - Vector_remove(this->processes, index); + if (index >= 0) Vector_remove(this->processes, index); assert(Hashtable_get(this->processTable, pid) == NULL); (void)pid; assert(Hashtable_count(this->processTable) == Vector_count(this->processes)); } @@ -730,7 +735,7 @@ void ProcessList_scan(ProcessList* this) { unsigned long long int usertime, nicetime, systemtime, systemalltime, idlealltime, idletime, totaltime; - unsigned long long int swapFree; + unsigned long long int swapFree = 0; FILE* status; char buffer[128]; @@ -766,6 +771,12 @@ } } + #ifdef __FreeBSD__ + size_t len = sizeof(this->freeMem); + if (!sysctlbyname("vm.stats.vm.v_free_count", &this->freeMem, &len, NULL, 0)) + this->freeMem *= getpagesize() / 1024; + #endif + this->usedMem = this->totalMem - this->freeMem; this->usedSwap = this->totalSwap - swapFree; fclose(status); >Release-Note: >Audit-Trail: >Unformatted: