Linux

如何設置 shmall、shmmax、shmmin 等…

  • May 12, 2021

我使用PostgreSQL 的文件來設置它,例如這個配置:

>>> cat /proc/meminfo 
MemTotal:       16345480 kB
MemFree:         1770128 kB
Buffers:          382184 kB
Cached:         10432632 kB
SwapCached:            0 kB
Active:          9228324 kB
Inactive:        4621264 kB
Active(anon):    7019996 kB
Inactive(anon):   548528 kB
Active(file):    2208328 kB
Inactive(file):  4072736 kB
Unevictable:           0 kB
Mlocked:               0 kB
SwapTotal:             0 kB
SwapFree:              0 kB
Dirty:              3432 kB
Writeback:             0 kB
AnonPages:       3034588 kB
Mapped:          4243720 kB
Shmem:           4533752 kB
Slab:             481728 kB
SReclaimable:     440712 kB
SUnreclaim:        41016 kB
KernelStack:        1776 kB
PageTables:        39208 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:     8172740 kB
Committed_AS:   14935216 kB
VmallocTotal:   34359738367 kB
VmallocUsed:      399340 kB
VmallocChunk:   34359334908 kB
HardwareCorrupted:     0 kB
AnonHugePages:    456704 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
DirectMap4k:       12288 kB
DirectMap2M:    16680960 kB

>>> ipcs -l          

------ Shared Memory Limits --------
max number of segments = 4096
max seg size (kbytes) = 4316816
max total shared memory (kbytes) = 4316816
min seg size (bytes) = 1

------ Semaphore Limits --------
max number of arrays = 128
max semaphores per array = 250
max semaphores system wide = 32000
max ops per semop call = 32
semaphore max value = 32767

------ Messages Limits --------
max queues system wide = 31918
max size of message (bytes) = 8192
default max size of queue (bytes) = 16384

sysctl.conf extract,由我計算:

kernel.shmall = 1079204
kernel.shmmax = 4420419584

postgresql.conf 非預設值,由我計算:

max_connections = 60            # (change requires restart)
shared_buffers = 4GB            # min 128kB
work_mem = 4MB              # min 64kB
wal_sync_method = open_sync     # the default is the first option
checkpoint_segments = 16        # in logfile segments, min 1, 16MB each
checkpoint_completion_target = 0.9  # checkpoint target duration, 0.0 - 1.0
effective_cache_size = 6GB

這合適嗎?如果不是(或不一定),在哪種情況下合適?

我們確實注意到此配置帶來了不錯的性能改進,您將如何改進它?

如何計算核心記憶體管理參數?

任何人都可以解釋如何真正從頭開始設置它們嗎?

我在這裡回答了另一個問題:

Git 無法推送錯誤“記憶體不足”

我不是在這裡回答你所有的問題,而是你標題中的問題:

如何設置 shmall、shmmax、shmmni 等…

對於某些核心發行版,有一些設置會阻止核心將最大記憶體分配給單個程序:

設置核心參數

修改/etc/sysctl.conf文件以包含適合您的作業系統的行:

# Red Hat Enterprise Linux 3.0 and CentOS 3.x 
kernel.shmmax = 2147483648
kernel.shmmni = 4096
kernel.shmall = 2097152
kernel.shmmin = 1
kernel.shmseg = 10

# semaphores: 
semmsl, semmns, semopm, semmni kernel.sem = 250 32000 100 128
fs.file-max = 65536

# Red Hat Enterprise Linux 4.0 and CentOS 4.x 
kernel.shmmax = 536870912
kernel.shmmni = 4096
kernel.shmall = 2097152

如果您的程序超出限制,儘管系統上報告的最大記憶體可用,核心仍將終止該程序。

注意:小心這些設置。您可能不想使用該範例中的設置,因為我從我們環境中的伺服器中提取了它們。

需要提及的一些額外注意事項:

要使用 sysctl 更新和測試核心設置,請使用以下命令:

列出目前設置:

sysctl -A | grep shm
sysctl -w kernel.shmmax=<value> to write in sysctl.conf
sysctl -p /etc/sysctl.conf to read/reload the values from sysctl.conf

通過編輯文件禁用安全 linux /etc/selinux/config,確保 SELINUX 標誌設置如下。

SELINUX=disabled

這合適嗎?如果不是(或不一定),在哪種情況下合適?

當 ISP 提供商不希望單個客戶程序佔用共享伺服器上的所有資源時,核心設置通常在數據中心環境中進行更嚴格的定義。

您通常不必設置核心記憶體參數,除非您有一個因資源不足而被核心殺死的程序。

在某些情況下,postgres 還可以為特定頁面大小分配比共享記憶體中可用的記憶體更多的記憶體:

* The PostgreSQL server failed to start. Please check the log output:
2011-11-04 05:06:26 UTC FATAL: could not create shared memory segment: Invalid
argument
2011-11-04 05:06:26 UTC DETAIL: Failed system call was shmget(key=5432001, size
=161849344, 03600).
2011-11-04 05:06:26 UTC HINT: This error usually means that PostgreSQL’s reques
t for a shared memory segment exceeded your kernel’s SHMMAX parameter. You can
either reduce the request size or reconfigure the kernel with larger SHMMAX. To
reduce the request size (currently 161849344 bytes), reduce PostgreSQL’s shared
_buffers parameter (currently 19200) and/or its max_connections parameter (curre
ntly 53).
If the request size is already small, it’s possible that it is less than
your kernel’s SHMMIN parameter, in which case raising the request size or recon
figuring SHMMIN is called for.
The PostgreSQL documentation contains more information about shared memo
ry configuration.
…fail!

可以通過調整核心資源設置來解決上述範例中的錯誤。此處詳細描述了用於確定資源設置的推薦設置和方法:

http://www.postgresql.org/docs/9.1/static/kernel-resources.html

但是,除非您遇到與 postgres 程序相關的資源匱乏情況,否則您實際上不必觸摸這些設置。這些情況最常發生在共享環境或分配給它們的資源很少的伺服器中。

任何人都可以解釋如何真正從頭開始設置它們嗎?

至於 Postgres 調整,您應該閱讀以下內容:

http://wiki.postgresql.org/wiki/Tuning_Your_PostgreSQL_Server

引用自:https://serverfault.com/questions/373375