nicolargo / glances

Glances an Eye on your system. A top/htop alternative for GNU/Linux, BSD, Mac OS and Windows operating systems.
http://nicolargo.github.io/glances/
Other
27.04k stars 1.54k forks source link

API: How to calculate "VERT" & "RES" values shown in Glances webpage? #2426

Closed mkanet closed 1 year ago

mkanet commented 1 year ago

Assuming Glances webpage displays this: image

...and Glances API returns the respective json data below.

QUESTION: How do I calculate the VIRT and RES values in the screenshot above using the sample API JSON response below? I'm assuming that they should be calculated from values in memory_info below and also maybe time_since_update? There are several values listed in memory_info which makes it confusing.

{
    ....
    "processlist": [
        {
            "pid": 8368,
            "cpu_times": [
                541.640625,
                47074.07812499999,
                0,
                0
            ],
            "num_threads": 4,
            "cpu_percent": 21.9,
            "memory_info": [
                418680832,
                414109696,
                793086,
                418738176,
                418680832,
                202112,
                185488,
                536500,
                72048,
                414109696,
                414224384,
                414109696
            ],
            "status": "R",
            "io_counters": [
                13246611,
                2059106,
                13246273,
                2058987,
                1
            ],
            "nice": 32,
            "name": "python.exe",
            "memory_percent": 1.2282917219576388,
            "num_handles": 587,
            "cpu_affinity": [
                16,
                17,
                18,
                19,
                20,
                21,
                22,
                23
            ],
            "ionice": 2,
            "num_ctx_switches": [
                28432048,
                0
            ],
            "tcp": 7,
            "udp": 0,
            "extended_stats": true,
            "key": "pid",
            "time_since_update": 18.005251169204712,
            "cmdline": [
                "C:\\Users\\MKANET\\AppData\\Local\\Programs\\Python\\Python310\\python.exe",
                "C:\\Users\\MKANET\\AppData\\Local\\Programs\\Python\\Python310\\Scripts\\glances.exe",
                "-w",
                "-0",
                "--disable-autodiscover",
                "--disable-plugin",
                "docker",
                "--config",
                "C:\\Users\\MKANET\\AppData\\Local\\Programs\\Python\\Python310\\scripts\\glances.conf",
                "-q"
            ],
            "username": "TITAN\\MKANET"
        },
        {
            "pid": 14356,
            "cpu_times": [
                26585.765625,
                1160.375,
                0,
                0
            ],
            "num_threads": 15,
            "cpu_percent": 1.9,
            "memory_info": [
                2189692928,
                47104000,
                33705847,
                2194300928,
                2189692928,
                5029616,
                5020480,
                348216,
                33272,
                47104000,
                73142272,
                47104000
            ],
            "status": "R",
            "io_counters": [
                225275600261,
                70859192596,
                225268137218,
                70858657010,
                1
            ],
            "nice": 32,
            "name": "vmware-vmx.exe",
            "memory_percent": 6.4239427543021215,
            "key": "pid",
            "time_since_update": 18.005251169204712,
            "cmdline": [
                "C:\\Program Files (x86)\\VMware\\VMware Player\\x64\\vmware-vmx.exe",
                "-s",
                "vmx.noUIBuildNumberCheck=TRUE",
                "-#",
                "product=4;name=VMware Player;version=17.0.1;buildnumber=21139696;licensename=VMware Player;licenseversion=17.0;",
                "-@",
                "pipe=\\\\.\\pipe\\vmx55defce9cfb8f059;msgs=ui",
                "C:\\Users\\MKANET\\VMware VMs\\HASSIO\\HASSIO.vmx"
            ],
            "username": "TITAN\\MKANET"
            ....
        }
    ]
}

UPDATE:

Presuming:

        "memory_info": [
            418680832,        #<---RES
            414109696,        #<---VERT
            793086,
            418738176,
            418680832,
            202112,
            185488,
            536500,
            72048,
            414109696,
            414224384,
            414109696
        ]

python.exe glances process RES = ((418680832 / 1024) / 1024) = 399.28515625 399M

VIRT = ((414109696 / 1024) / 1024) = 394.92578125 385M


Presuming:

            "memory_info": [
                2189692928,        #<---RES
                47104000,            #<---VERT
                33705847,
                2194300928,
                2189692928,
                5029616,
                5020480,
                348216,
                33272,
                47104000,
                73142272,
                47104000
            ]

vmware-vmx.exe RES = ((2189692928 / 1024) / 1024) = 2,088.25390625 2,088M also 2.04G

VIRT = ((414109696 / 1024) / 1024) = 44.921875 44.9M

nicolargo commented 1 year ago

Hi @mkanet

your assumption are ok. The VIRT and RES memory are computed as following:

        "memory_info": [
            418680832,        #<---RES
            414109696,        #<---VERT
            793086,
            418738176,
            418680832,
            202112,
            185488,
            536500,
            72048,
            414109696,
            414224384,
            414109696
        ]

Stat are given in bytes and are converted by Glances in a user friendly unit. using the auto_unit internal function (https://github.com/nicolargo/glances/blob/develop/glances/plugins/plugin/model.py#L1040).

Under the wood, the stats are grabbed from the PSUtil memory_info function (https://psutil.readthedocs.io/en/latest/#psutil.Process.memory_info).

Good point for the new version of the API (Glances v4) to clarify the data !

Example of new data mode:

{ 
  '<pid>': {
    'cpu_percent': ...,
    'mem_percent': ...,
    'mem_virt': ...,
    'mem_res': ...,
    ...
  }
}
mkanet commented 1 year ago

@nicolargo thank you for confirming!

PS: Is Glances version 4.0.0 API backward-compatible with 3rd party applications that access Glances 3.x API?