Ricerca nel sito web

Linux vmstat Command Tutorial per principianti (5 esempi)


Su questa pagina

  1. Comando Linux vmstat
  2. D1. Come usare vmstat?
  3. Q2. Come fare in modo che vmstat aggiorni automaticamente il suo output?
  4. Q3. Come fare in modo che vmstat visualizzi slabinfo?
  5. Q4. Come fare in modo che vmstat riporti le statistiche di riepilogo dell'attività del disco?
  6. D5. Come fare in modo che vmstat aggiunga il timestamp a ciascuna riga di output?
  7. Conclusione

Se stai cercando un'utilità della riga di comando che puoi utilizzare per accedere alle informazioni su processi, attività della CPU, memoria e altro, sarai felice di sapere che vmstat lo fa per te.

In questo articolo, discuteremo le basi di questo strumento utilizzando alcuni esempi di facile comprensione. Ma prima di farlo, vale la pena ricordare che tutti gli esempi in questo articolo sono stati testati su una macchina Ubuntu 20.04 LTS, ma funzionerà anche su CentOS e Debian.

Comando Linux vmstat

Il comando vmstat in Linux riporta le statistiche della memoria virtuale. Di seguito la sua sintassi:

vmstat [options] [delay [count]]

Ed ecco come lo spiega la pagina man degli strumenti:

       vmstat  reports  information about processes, memory, paging, block IO,
       traps, disks and cpu activity.

       The first report produced gives averages since the last reboot.   Addi?
       tional  reports  give information on a sampling period of length delay.
       The process and memory reports are instantaneous in either case.

Di seguito sono riportati alcuni esempi in stile Q&A che dovrebbero darti un'idea migliore di come funziona il comando vmstat:

Q1. Come usare vmstat?

Per un utilizzo di base, basta eseguire vmstat sans qualsiasi opzione.

vmstat

Ad esempio, nel mio caso, il comando precedente ha prodotto il seguente output:

procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0 401160 100252 1307468    0    0     5    17   49   70  0  0 100  0  0

Come indica la prima riga, l'output è suddiviso in sei sezioni. La pagina man degli strumenti contiene informazioni dettagliate su questi valori. Ecco l'estratto:

Procs
       r: The number of runnable processes (running or waiting for run time).
       b: The number of processes in uninterruptible sleep.

   Memory
       swpd: the amount of virtual memory used.
       free: the amount of idle memory.
       buff: the amount of memory used as buffers.
       cache: the amount of memory used as cache.
       inact: the amount of inactive memory.  (-a option)
       active: the amount of active memory.  (-a option)

   Swap
       si: Amount of memory swapped in from disk (/s).
       so: Amount of memory swapped to disk (/s).

   IO
       bi: Blocks received from a block device (blocks/s).
       bo: Blocks sent to a block device (blocks/s).

   System
       in: The number of interrupts per second, including the clock.
       cs: The number of context switches per second.

   CPU
       These are percentages of total CPU time.
       us: Time spent running non-kernel code.  (user time, including nice time)
       sy: Time spent running kernel code.  (system time)
       id: Time spent idle.  Prior to Linux 2.5.41, this includes IO-wait time.
       wa: Time spent waiting for IO.  Prior to Linux 2.5.41, included in idle.
       st: Time stolen from a virtual machine.  Prior to Linux 2.6.11, unknown.

D2. Come fare in modo che vmstat aggiorni automaticamente il suo output?

Per impostazione predefinita, vmstat produce l'output una volta. Tuttavia, se si desidera che vmstat aggiorni automaticamente il proprio output, è possibile farlo specificando un valore numerico come input per il comando.

Questo valore numerico funge da ritardo (in secondi) dopo il quale l'output viene aggiornato. Per esempio:

vmstat 3

In questo modo, l'output di vmstat verrà aggiornato ogni 3 secondi.

procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0 410928 100276 1307480    0    0     5    17   49   70  0  0 100  0  0
 0  0      0 410920 100284 1307480    0    0     0   181  108  143  0  0 100  0  0
 0  0      0 410920 100292 1307480    0    0     0     7   92  132  0  0 100  0  0
 0  0      0 410920 100292 1307480    0    0     0     0   87  124  0  0 100  0  0
...
...
...

D3. Come fare in modo che vmstat visualizzi slabinfo?

Per slabinfo, è necessario utilizzare l'opzione della riga di comando -m. Si noti che il kernel delle distribuzioni Linux deve supportare slabinfo affinché questa opzione funzioni. Se il supporto è presente, devi eseguire il comando con le autorizzazioni sudo.

sudo vmstat -m

Ad esempio, nel mio caso, ecco un estratto dell'output prodotto:

Cache                       Num  Total   Size  Pages
ufs_inode_cache               0      0    808     40
qnx4_inode_cache              0      0    680     48
hfsplus_attr_cache            0      0   3840      8
hfsplus_icache                0      0    896     36
hfs_inode_cache               0      0    832     39
minix_inode_cache             0      0    672     48
ntfs_big_inode_cache          0      0    960     34
ntfs_inode_cache              0      0    296     55
jfs_ip                        0      0   1280     25
xfs_dqtrx                     0      0    528     31
xfs_dquot                     0      0    496     33
xfs_buf                       0      0    384     42
xfs_rui_item                  0      0    696     47
xfs_rud_item                  0      0    176     46
xfs_inode                     0      0   1024     32
xfs_efd_item                  0      0    440     37
xfs_buf_item                  0      0    272     30
xfs_trans                     0      0    232     35
xfs_da_state                  0      0    480     34
xfs_btree_cur                 0      0    224     36
...
...
...

D4. Come fare in modo che vmstat riporti le statistiche di riepilogo dell'attività del disco?

Utilizzare l'opzione della riga di comando -D per questo.

vmstat -D

Ecco l'output prodotto da questo comando sul mio sistema:

           10 disks
            2 partitions
        15066 total reads
         5937 merged reads
      1272794 read sectors
        20063 milli reading
       155026 writes
       103687 merged writes
      4602472 written sectors
       105279 milli writing
            0 inprogress IO
          126 milli spent IO

Q5. Come fare in modo che vmstat aggiunga il timestamp a ciascuna riga di output?

Per assicurarti che il comando vmstat aggiunga il timestamp a ciascuna riga di output, utilizza la riga di comando -t:

vmstat -t
$ vmstat 2 -t
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- -----timestamp-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st                 UTC
 2  0      0 410116 100432 1308160    0    0     5    17   49   70  0  0 100  0  0 2020-05-08 09:40:05
 0  0      0 410116 100432 1308160    0    0     0    88  100  136  0  0 100  0  0 2020-05-08 09:40:07
 0  0      0 410116 100440 1308160    0    0     0    18   90  139  0  0 100  0  0 2020-05-08 09:40:09
 0  0      0 410116 100440 1308160    0    0     0     0   93  126  0  0 100  0  0 2020-05-08 09:40:11
 0  0      0 410368 100448 1308160    0    0     0    10  107  144  0  0 100  0  0 2020-05-08 09:40:13
 0  0      0 410368 100448 1308160    0    0     0     0   86  122  0  0 100  0  0 2020-05-08 09:40:15
 0  0      0 410368 100448 1308160    0    0     0     0  101  135  0  0 100  0  0 2020-05-08 09:40:17

Le voci evidenziate mostrano il timestamp incluso in ciascuna riga.

Conclusione

A seconda del tipo di lavoro svolto sulla riga di comando di Linux, il comando vmstat può rivelarsi davvero utile. Oltre alle opzioni discusse qui, ci sono molte altre opzioni della riga di comando offerte da questo strumento. Vai qui per saperne di più.