freebsd的memory简介

Posted by 刘勇 on 2020-03-03

FreeBSD的memory简介

  • 线上很多业务还跑在了freebsd这个古老的系统上,而freebsd系统上的内存指标和Linux的有些不同。此文主要对freebsd下的内存指标分类、含义和查看做一些简单的说明。

基本指标

  • 我们man一下top查看一下具体的内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Physical Memory Stats
Active:
number of bytes active

Inact: number of bytes inactive

Wired: number of bytes wired down, including BIO-level cached file data
pages

Cache: number of clean bytes caching data that are available for imme‐
diate reallocation

Buf: number of bytes used for BIO-level disk caching

Free: number of bytes free
  • 每个指标具体包括的内容,参考官方的wiki,显示如下。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
## Active
1. Contains pages "actively" (recently) referenced by userland
2. Contains a mix of clean and dirty pages
3. Pages are regularly scanned by the page daemon (each page is visited once every vm.pageout_update_period seconds)
4. Scans check to see if the page has been referenced since the last scan
5. If enough scans complete without seeing a reference, the page is moved to the inactive queue
6. Implements pseudo-LRU

## Inactive
1. Contains pages aged out of the active queue
2. Contains pages evicted from the buffer cache
3. Contains a mix of clean and dirty pages
4. Pages are scanned by the page daemon (starting from the head of the queue) when there is a memory shortage:
1) Pages which have been referenced are moved back to the active queue or the tail of the inactive queue
2) Pages which are dirty are moved to the tail of the laundry queue
3) Unreferenced, clean pages may be freed and reused immediately
5. Implements second-chance LRU

## Laundry
1. Queue for managing dirty inactive pages, which must be cleaned ("laundered") before they can be reused
2. Managed by a separate thread, the laundry thread, instead of the page daemon
3. Laundry thread launders a small number of pages to balance the inactive and laundry queues
4. Frequency of laundering depends on:
1)How many clean pages the page daemon is freeing; more frees contributes to a higher frequency of laundering
2)The size of the laundry queue relative to the inactive queue; if the laundry queue is growing, we will launder more frequently
5. Pages are scanned by the laundry thread (starting from the head of the queue):
1)Pages which have been referenced are moved back to the active queue or the tail of the laundry queue
2)Dirty pages are laundered and then moved close to the head of the inactive queue

## Free
1. Memory available for use by the rest of the system.

## Wired
1. Non-pageable memory: cannot be freed until explicitly released by the owner
2. Userland memory can be wired by mlock(2) (subject to system and per-user limits)
3. Kernel memory allocators return wired memory
4. Contents of the ARC and the buffer cache are wired
5. Some memory is permanently wired and is never freed (e.g., the kernel file itself)

查看内存分布

sysctl命令查看

1
2
3
$ sysctl hw.physmem
$ sysctl hw | egrep 'hw.(phys|user|real)'
$ grep memory /var/run/dmesg.boot

image

top命令查看

  • $ top

image

perl脚本查看

1
2
3
4
5
$ fetch http://www.cyberciti.biz/files/scripts/freebsd-memory.pl.txt
$ sudo mv freebsd-memory.pl.txt /usr/local/bin/free
$ sudo chmod +x /usr/local/bin/free
$ pkg search perl5
$ pkg install perl5-5.20.2_5

image

shell脚本查看

1
2
$ fetch https://raw.githubusercontent.com/ocochard/myscripts/master/FreeBSD/freebsd-memory.sh
$ sh freebsd-memory.sh

image

freecolor命令查看

1
2
3
$ pkg install freecolor
$ freecolor -m -o
$ freecolor -t -m -o
  • 详细可以man freecolor

image



支付宝打赏 微信打赏

赞赏一下