From owner-freebsd-hackers@FreeBSD.ORG Fri Mar 29 00:27:39 2013 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.FreeBSD.org [8.8.178.115]) by hub.freebsd.org (Postfix) with ESMTP id C5533A95 for ; Fri, 29 Mar 2013 00:27:39 +0000 (UTC) (envelope-from rodperson@rodperson.com) Received: from www6.pairlite.com (www6.pairlite.com [64.130.10.16]) by mx1.freebsd.org (Postfix) with ESMTP id A6D90D41 for ; Fri, 29 Mar 2013 00:27:39 +0000 (UTC) Received: from [192.168.1.10] (c-71-60-224-178.hsd1.pa.comcast.net [71.60.224.178]) by www6.pairlite.com (Postfix) with ESMTPSA id 1B20D67AEE for ; Thu, 28 Mar 2013 20:27:29 -0400 (EDT) Message-ID: <5156316F.9050202@rodperson.com> Date: Fri, 29 Mar 2013 20:27:27 -0400 From: Rod Person User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:17.0) Gecko/17.0 Thunderbird/17.0 MIME-Version: 1.0 To: freebsd-hackers@freebsd.org Subject: Help porting Linux app - getting Free Memory and Real Memory Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Mailman-Approved-At: Fri, 29 Mar 2013 01:44:45 +0000 X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 29 Mar 2013 00:27:39 -0000 Hi All, I'm working on a port I maintain and the code has drastically changed. Everything is going we except that the program gives warnings that there isn't enough free memory on the system to perform certain actions. The program uses Linux call to /proc/meminfo to get this information. I've worked it out that I should be using the sysctl function to get this information. I wrote this code that seems to give the information that I need, but it does not seem to match up with calling [CMD=""]sysctl[/CMD] from the command line. #include #include #include #include #include #include struct vmtotal getVMinfo(){ struct vmtotal vm_info; int mib[2]; mib[0] = CTL_VM; mib[1] = VM_TOTAL; size_t len = sizeof(vm_info); sysctl(mib, 2, &vm_info, &len, NULL, 0); return vm_info; } int getSysCtl(int top_level, int next_level){ int mib[2], ctlvalue; size_t len; mib[0] = top_level; mib[1] = next_level; len = sizeof(ctlvalue); sysctl(mib, 2, &ctlvalue, &len, NULL, 0); return ctlvalue; } int main(void){ int realmem = getSysCtl(CTL_HW, HW_REALMEM); int usermem = getSysCtl(CTL_HW, HW_USERMEM); int pagesize = getSysCtl(CTL_HW, HW_PAGESIZE); printf("Real Memory: %i\n",realmem); printf("User Memory: %i\n",usermem); printf("Page Size: %i\n",pagesize); struct vmtotal vmsize = getVMinfo(); printf("Total VM Memory: %i\n",vmsize.t_vm); printf("Total Real Memory: %i\n",vmsize.t_rm); printf("shared real memory: %i\n",vmsize.t_rmshr); printf("active shared real memory: %i\n",vmsize.t_armshr); printf("Total Free Memory pages: %i\n",vmsize.t_free); } gives me the following output: Real Memory: 1006632960 User Memory: -1004396544 Page Size: 4096 Total VM Memory: 269489480 Total Real Memory: 153927 shared real memory: 6015 active shared real memory: 5736 Total Free Memory pages: 1497516 But the command sysctl vm.vmtotal outputs: vm.vmtotal: System wide totals computed every five seconds: (values in kilobytes) =============================================== Processes: (RUNQ: 1 Disk Wait: 0 Page Wait: 0 Sleep: 89) Virtual Memory: (Total: 1078017644K Active: 4167380K) Real Memory: (Total: 617380K Active: 516236K) Shared Virtual Memory: (Total: 38944K Active: 28844K) Shared Real Memory: (Total: 24652K Active: 23536K) Free Memory: 5987760K I see that if I use the formula [B](Total Free Pages * Page Size)/1024[/B], I get a number extremely close to the Free Memory number of the command line output, but I'm not sure if that is correct. Any suggestions? Thanks Rod