Postgresql/Greenplum內核參數配置手冊

memory overcommit

vm.overcommit_memory = 2
vm.overcommit_ratio = 95 # **See [Note](https://gpdb.docs.pivotal.io/6-0/install_guide/prep_os.html#topic4__sysctl_conf) 2**

GP相關說明

When vm.overcommit_memory is 2, you specify a value for vm.overcommit_ratio. For information about calculating the value for vm.overcommit_ratio when using resource queue-based resource management, see the Greenplum Database server configuration parameter gp_vmem_protect_limit in the Greenplum Database Reference Guide. If you are using resource group-based resource management, tune the operating system vm.overcommit_ratio as necessary. If your memory utilization is too low, increase the vm.overcommit_ratio value; if your memory or swap usage is too high, decrease the value.

linux內核解釋
http://linuxperf.com/?p=102
Memory Overcommit的意思是操作系統承諾給進程的內存大小超過了實際可用的內存。一個保守的操作系統不會允許memory overcommit,有多少就分配多少,再申請就沒有了,這其實有些浪費內存,因爲進程實際使用到的內存往往比申請的內存要少,比如某個進程malloc()了200MB內存,但實際上只用到了100MB,按照UNIX/Linux的算法,物理內存頁的分配發生在使用的瞬間,而不是在申請的瞬間,也就是說未用到的100MB內存根本就沒有分配,這100MB內存就閒置了。下面這個概念很重要,是理解memory overcommit的關鍵:commit(或overcommit)針對的是內存申請,內存申請不等於內存分配,內存只在實際用到的時候才分配。

Linux是允許memory overcommit的,只要你來申請內存我就給你,寄希望於進程實際上用不到那麼多內存,但萬一用到那麼多了呢?那就會發生類似“銀行擠兌”的危機,現金(內存)不足了。Linux設計了一個OOM killer機制(OOM = out-of-memory)來處理這種危機:挑選一個進程出來殺死,以騰出部分內存,如果還不夠就繼續殺…也可通過設置內核參數 vm.panic_on_oom 使得發生OOM時自動重啓系統。這都是有風險的機制,重啓有可能造成業務中斷,殺死進程也有可能導致業務中斷,我自己的這個小網站就碰到過這種問題,參見前文。所以Linux 2.6之後允許通過內核參數 vm.overcommit_memory 禁止memory overcommit。

內核參數 vm.overcommit_memory 接受三種取值:

  • 0 – Heuristic overcommit handling. 這是缺省值,它允許overcommit,但過於明目張膽的overcommit會被拒絕,比如malloc一次性申請的內存大小就超過了系統總內存。Heuristic的意思是“試探式的”,內核利用某種算法(對該算法的詳細解釋請看文末)猜測你的內存申請是否合理,它認爲不合理就會拒絕overcommit。
  • 1 – Always overcommit. 允許overcommit,對內存申請來者不拒。
  • 2 – Don’t overcommit. 禁止overcommit。

關於禁止overcommit (vm.overcommit_memory=2) ,需要知道的是,怎樣纔算是overcommit呢?kernel設有一個閾值,申請的內存總數超過這個閾值就算overcommit,在/proc/meminfo中可以看到這個閾值的大小:

# grep -i commit /proc/meminfo
CommitLimit:     5967744 kB
Committed_AS:    5363236 kB

CommitLimit 就是overcommit的閾值,申請的內存總數超過CommitLimit的話就算是overcommit。
這個閾值是如何計算出來的呢?它既不是物理內存的大小,也不是free memory的大小,它是通過內核參數vm.overcommit_ratio或vm.overcommit_kbytes間接設置的,公式如下:
【CommitLimit = (Physical RAM * vm.overcommit_ratio / 100) + Swap】

注:
vm.overcommit_ratio 是內核參數,缺省值是50,表示物理內存的50%。如果你不想使用比率,也可以直接指定內存的字節數大小,通過另一個內核參數 vm.overcommit_kbytes 即可;
如果使用了huge pages,那麼需要從物理內存中減去,公式變成:
CommitLimit = ([total RAM] – [total huge TLB RAM]) * vm.overcommit_ratio / 100 + swap
參見https://access.redhat.com/solutions/665023

/proc/meminfo中的 Committed_AS 表示所有進程已經申請的內存總大小,(注意是已經申請的,不是已經分配的),如果 Committed_AS 超過 CommitLimit 就表示發生了 overcommit,超出越多表示 overcommit 越嚴重。Committed_AS 的含義換一種說法就是,如果要絕對保證不發生OOM (out of memory) 需要多少物理內存。

ip port

net.ipv4.ip_local_port_range = 10000 65535

GP相關說明

To avoid port conflicts between Greenplum Database and other applications when initializing Greenplum Database, do not specify Greenplum Database ports in the range specified by the operating system parameter net.ipv4.ip_local_port_range. For example, if net.ipv4.ip_local_port_range = 10000 65535, you could set the Greenplum Database base port numbers to these values.
PORT_BASE = 6000
MIRROR_PORT_BASE = 7000
For information about the port ranges that are used by Greenplum Database, see gpinitsystem.

linux內核解釋
On Linux, there is a sysctl parameter calledip_local_port_rangethat defines the minimum and maximum port a networking connection can use as its source (local) port. This applies to both TCP and UDP connections.

cat /proc/sys/net/ipv4/ip_local_port_range

shared memory

# kernel.shmall = _PHYS_PAGES / 2 # See Note 1
kernel.shmall = 4000000000
# kernel.shmmax = kernel.shmall * PAGE_SIZE # See Note 1
kernel.shmmax = 500000000
kernel.shmmni = 4096

查看限制、查看使用
ipcs -lmipcs -u

  • shmall: This parameter sets the total amount of shared memory pages that can be used system wide. Hence, SHMALL should always be at least ceil(shmmax/PAGE_SIZE).
    共享內存能使用的總頁數
    echo $(expr $(getconf _PHYS_PAGES) / 2)
  • shmmax: This parameter defines the maximum size in bytes of a single shared memory segment that a Linux process can allocate in its virtual address space.
    共享內存的總大小
    echo $(expr $(getconf _PHYS_PAGES) / 2 \* $(getconf PAGE_SIZE))
  • shmmin: This parameter sets the system wide maximum number of shared memory segments.

semaphores

cat /proc/sys/kernel/sem
500 2048000 200 40960

SEMMSL, SEMMNS, SEMOPM, SEMMNI

kernel.sem = 500 2048000 200 40960

SEMMSL
含義:每個信號量set中信號量最大個數 設置:最小250;對於processes參數設置較大的系統建議設置爲processes+10

SEMMNS
含義:linux系統中信號量最大個數 設置:至少32000;SEMMSL * SEMMNI

SEMOPM
含義:semop系統調用允許的信號量最大個數設置:至少100;或者等於SEMMSL

SEMMNI
含義:linux系統信號量set最大個數 設置:最少128

link

管理IPC資源

分別查詢IPC資源:

$ipcs -m 查看系統使用的IPC共享內存資源
$ipcs -q 查看系統使用的IPC隊列資源
$ipcs -s 查看系統使用的IPC信號量資源

查看IPC資源被誰佔用

示例:有個IPCKEY(51036),需要查詢其是否被佔用;

首先通過計算器將其轉爲十六進制:

51036 -> c75c
如果知道是被共享內存佔用:

$ipcs -m | grep c75c
0x0000c75c 40403197   tdea3    666        536870912  2
如果不確定,則直接查找:

$ipcs | grep c75c
0x0000c75c 40403197   tdea3    666        536870912  2
0x0000c75c 5079070    tdea3    666        4

系統IPC參數查詢

ipcs -l

清除IPC資源

ipcrm -M shmkey  移除用shmkey創建的共享內存段
ipcrm -m shmid    移除用shmid標識的共享內存段
ipcrm -Q msgkey  移除用msqkey創建的消息隊列
ipcrm -q msqid  移除用msqid標識的消息隊列
ipcrm -S semkey  移除用semkey創建的信號
ipcrm -s semid  移除用semid標識的信號

清除當前用戶創建的所有的IPC資源:

ipcs -q | awk '{ print "ipcrm -q "$2}' | sh > /dev/null 2>&1;
ipcs -m | awk '{ print "ipcrm -m "$2}' | sh > /dev/null 2>&1;
ipcs -s | awk '{ print "ipcrm -s "$2}' | sh > /dev/null 2>&1;

link

https://gpdb.docs.pivotal.io/6-0/install_guide/prep_os.html#topic_sqj_lt1_nfb~~~~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章