Re: type of vm.stats.vm.v_vnodepgsin vm.stats.vm.v_swappgsin, vm.stats.vm.v_vnodepgsout vm.stats.vm.v_swappgsout on AMD64 r320730

From: Konstantin Belousov <kostikbel_at_gmail.com>
Date: Mon, 10 Jul 2017 17:12:37 +0300
On Mon, Jul 10, 2017 at 10:51:43AM -0300, Otac??lio wrote:
> Em 10/07/2017 06:49, Konstantin Belousov escreveu:
> > On Mon, Jul 10, 2017 at 06:45:28PM +0900, Tomoaki AOKI wrote:
> >> Hm, for example, sysutils/lsof (userspace app) depends on kernel
> >> source, and I thought the one Otacilio mentioned is something like it.
> > lsof purpose is to dig into a kernel memory to gather information about
> > the process state and opened files.  To do this, lsof authors have to use
> > internal kernel data structures.
> >
> > The library which is discussed in the thread uses public interfaces,
> > but then tries to pack the returned data into internal kernel structure.
> > If this is true, then the breakage is on the library side.
> 
> Hello
> 
> This following  simple program behaves in the same manner that xosview. 
> It shows corrects answers for pages_in and pages_out on FreeBSD 11 amd64 
> and FreeBSD10.3 i386. On FreeBSD 12 r320730 it shows the error answer 
> that I related previously.  If the lines
> 
> pageinfo[0] = (uint64_t)vm.v_vnodepgsin + (uint64_t)vm.v_swappgsin;
> pageinfo[1] = (uint64_t)vm.v_vnodepgsout + (uint64_t)vm.v_swappgsout;
> 
> are patched to
> 
> pageinfo[0] = (uint32_t)((uint64_t)vm.v_vnodepgsin + 
> (uint64_t)vm.v_swappgsin);
> pageinfo[1] = (uint32_t)((uint64_t)vm.v_vnodepgsout + 
> (uint64_t)vm.v_swappgsout);
> 
> The program works on all FreeBSD version. My doubt is why now this type  
> cast (uint32_t) is necessary?
> 
> []'s
> 
> -Otacilio
> 
> #include <stdio.h>
> #include <sys/types.h>
> #include <sys/sysctl.h>
> 
> #define _WANT_VMMETER
> 
> #include <sys/vmmeter.h>
> 
> /* meminfo[5]  = { active, inactive, wired, cached, free } */
> /* pageinfo[2] = { pages_in, pages_out }                   */
> void BSDGetPageStats(uint64_t *meminfo, uint64_t *pageinfo) {
>      struct vmmeter vm;
>      size_t size = sizeof(unsigned int);
> #define    GET_VM_STATS(name)  sysctlbyname("vm.stats.vm." #name, 
> &vm.name, &size, NULL, 0)
Use of struct vmmeter is wrong, as discussed.  The structure is for kernel
internal interfaces.

The behaviour changed because the structure member was of type uint32_t
but become a pointer.  You initialize the size of the storage to receive
MIB value to sizeof(int), which leaves half of the pointer storage
not filled.

Stop using vmmeter, use correct types for the MIBs values, and pass
correct size to sysctl(3) call.

>      GET_VM_STATS(v_active_count);
>      GET_VM_STATS(v_inactive_count);
>      GET_VM_STATS(v_wire_count);
>      GET_VM_STATS(v_free_count);
>      GET_VM_STATS(v_page_size);
>      GET_VM_STATS(v_vnodepgsin);
>      GET_VM_STATS(v_vnodepgsout);
>      GET_VM_STATS(v_swappgsin);
>      GET_VM_STATS(v_swappgsout);
> 
>      if (meminfo) {
>          meminfo[0] = (uint64_t)vm.v_active_count * vm.v_page_size;
>          meminfo[1] = (uint64_t)vm.v_inactive_count * vm.v_page_size;
>          meminfo[2] = (uint64_t)vm.v_wire_count * vm.v_page_size;
>          meminfo[4] = (uint64_t)vm.v_free_count * vm.v_page_size;
>      }
>      if (pageinfo) {
>          pageinfo[0] = (uint64_t)vm.v_vnodepgsin + (uint64_t)vm.v_swappgsin;
>          pageinfo[1] = (uint64_t)vm.v_vnodepgsout + 
> (uint64_t)vm.v_swappgsout;
>      }
> }
> 
> int main(int argc, char **argv){
>      uint64_t meminfo[5];
>      uint64_t pageinfo[2];
> 
>      BSDGetPageStats(meminfo, pageinfo);
> 
>      printf("active memory     = %lu bytes\n", meminfo[0]);
>      printf("inactive memory = %lu bytes\n", meminfo[1]);
>      printf("wired memory     = %lu bytes\n", meminfo[2]);
>      printf("cached memory     = %lu bytes\n", meminfo[3]);
>      printf("free memory     = %lu bytes\n", meminfo[4]);
> 
>      printf("\npages_in    = %lu bytes\n", pageinfo[0]);
>      printf("pages_out    = %lu bytes\n", pageinfo[1]);
>      return 0;
> }
Received on Mon Jul 10 2017 - 12:12:53 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:41:12 UTC