您好,登錄后才能下訂單哦!
這篇文章主要介紹“分析PostgreSQL SetupLockInTable方法中與OOM相關的代碼”,在日常操作中,相信很多人在分析PostgreSQL SetupLockInTable方法中與OOM相關的代碼問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”分析PostgreSQL SetupLockInTable方法中與OOM相關的代碼”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
有時候我們可能會在PG的日志發現如下信息:
2020-01-09 16:29:19.062 CST,"pg12","testdb",6193,"[local]",5e16dccd.1831,1,"CREATE TABLE",2020-01-09 15:57:01 CST,2/34,1512004206,ERROR,53200,"out of shared memory",,"You might need to increase max_locks_per_transaction.",,,,"CREATE TABLE a13030 (id int);",,,"psql" 2020-01-09 16:29:19.379 CST,"pg12","testdb",6193,"[local]",5e16dccd.1831,2,"CREATE TABLE",2020-01-09 15:57:01 CST,2/0,1512004206,ERROR,25P02,"current transaction is aborted, commands ignored until end of transaction block",,,,,,"CREATE TABLE a13031 (id int);",,,"psql"
直觀上來看,OOM似乎與max_locks_per_transaction扯不上什么關系,為什么PG會提示增加max_locks_per_transaction的值呢?
測試腳本
\pset footer off \pset tuples_only \o /tmp/drop.sql SELECT 'drop table if exists tbl' || id || ' ;' as "--" FROM generate_series(1, 20000) AS id; \i /tmp/drop.sql \pset footer off \pset tuples_only \o /tmp/create.sql SELECT 'CREATE TABLE tbl' || id || ' (id int);' as "--" FROM generate_series(1, 20000) AS id; \o /tmp/ret.txt begin; \i /tmp/create.sql
數據結構
HTAB
/* * Top control structure for a hashtable --- in a shared table, each backend * has its own copy (OK since no fields change at runtime) * 哈希表的頂層控制結構. * 在這個共享哈希表中,每一個后臺進程都有自己的拷貝 * (之所以沒有問題是因為fork出來后,在運行期沒有字段會變化) */ struct HTAB { //指向共享的控制信息 HASHHDR *hctl; /* => shared control information */ //段開始目錄 HASHSEGMENT *dir; /* directory of segment starts */ //哈希函數 HashValueFunc hash; /* hash function */ //哈希鍵比較函數 HashCompareFunc match; /* key comparison function */ //哈希鍵拷貝函數 HashCopyFunc keycopy; /* key copying function */ //內存分配器 HashAllocFunc alloc; /* memory allocator */ //內存上下文 MemoryContext hcxt; /* memory context if default allocator used */ //表名(用于錯誤信息) char *tabname; /* table name (for error messages) */ //如在共享內存中,則為T bool isshared; /* true if table is in shared memory */ //如為T,則固定大小不能擴展 bool isfixed; /* if true, don't enlarge */ /* freezing a shared table isn't allowed, so we can keep state here */ //不允許凍結共享表,因此這里會保存相關狀態 bool frozen; /* true = no more inserts allowed */ /* We keep local copies of these fixed values to reduce contention */ //保存這些固定值的本地拷貝,以減少沖突 //哈希鍵長度(以字節為單位) Size keysize; /* hash key length in bytes */ //段大小,必須為2的冪 long ssize; /* segment size --- must be power of 2 */ //段偏移,ssize的對數 int sshift; /* segment shift = log2(ssize) */ }; /* * Header structure for a hash table --- contains all changeable info * 哈希表的頭部結構 -- 存儲所有可變信息 * * In a shared-memory hash table, the HASHHDR is in shared memory, while * each backend has a local HTAB struct. For a non-shared table, there isn't * any functional difference between HASHHDR and HTAB, but we separate them * anyway to share code between shared and non-shared tables. * 在共享內存哈希表中,HASHHDR位于共享內存中,每一個后臺進程都有一個本地HTAB結構. * 對于非共享哈希表,HASHHDR和HTAB沒有任何功能性的不同, * 但無論如何,我們還是把它們區分為共享和非共享表. */ struct HASHHDR { /* * The freelist can become a point of contention in high-concurrency hash * tables, so we use an array of freelists, each with its own mutex and * nentries count, instead of just a single one. Although the freelists * normally operate independently, we will scavenge entries from freelists * other than a hashcode's default freelist when necessary. * 在高并發的哈希表中,空閑鏈表會成為競爭熱點,因此我們使用空閑鏈表數組, * 數組中的每一個元素都有自己的mutex和條目統計,而不是使用一個. * * If the hash table is not partitioned, only freeList[0] is used and its * spinlock is not used at all; callers' locking is assumed sufficient. * 如果哈希表沒有分區,那么只有freelist[0]元素是有用的,自旋鎖沒有任何用處; * 調用者鎖定被認為已足夠OK. */ /* Number of freelists to be used for a partitioned hash table. */ //#define NUM_FREELISTS 32 FreeListData freeList[NUM_FREELISTS]; /* These fields can change, but not in a partitioned table */ //這些域字段可以改變,但不適用于分區表 /* Also, dsize can't change in a shared table, even if unpartitioned */ //同時,就算是非分區表,共享表的dsize也不能改變 //目錄大小 long dsize; /* directory size */ //已分配的段大小(<= dbsize) long nsegs; /* number of allocated segments (<= dsize) */ //正在使用的最大桶ID uint32 max_bucket; /* ID of maximum bucket in use */ //進入整個哈希表的模掩碼 uint32 high_mask; /* mask to modulo into entire table */ //進入低于半個哈希表的模掩碼 uint32 low_mask; /* mask to modulo into lower half of table */ /* These fields are fixed at hashtable creation */ //下面這些字段在哈希表創建時已固定 //哈希鍵大小(以字節為單位) Size keysize; /* hash key length in bytes */ //所有用戶元素大小(以字節為單位) Size entrysize; /* total user element size in bytes */ //分區個數(2的冪),或者為0 long num_partitions; /* # partitions (must be power of 2), or 0 */ //目標的填充因子 long ffactor; /* target fill factor */ //如目錄是固定大小,則該值為dsize的上限值 long max_dsize; /* 'dsize' limit if directory is fixed size */ //段大小,必須是2的冪 long ssize; /* segment size --- must be power of 2 */ //端偏移,ssize的對數 int sshift; /* segment shift = log2(ssize) */ //一次性分配的條目個數 int nelem_alloc; /* number of entries to allocate at once */ #ifdef HASH_STATISTICS /* * Count statistics here. NB: stats code doesn't bother with mutex, so * counts could be corrupted a bit in a partitioned table. * 統計信息. * 注意:統計相關的代碼不會影響mutex,因此對于分區表,統計可能有一點點問題 */ long accesses; long collisions; #endif }; /* * Per-freelist data. * 空閑鏈表數據. * * In a partitioned hash table, each freelist is associated with a specific * set of hashcodes, as determined by the FREELIST_IDX() macro below. * nentries tracks the number of live hashtable entries having those hashcodes * (NOT the number of entries in the freelist, as you might expect). * 在一個分區哈希表中,每一個空閑鏈表與特定的hashcodes集合相關,通過下面的FREELIST_IDX()宏進行定義. * nentries跟蹤有這些hashcodes的仍存活的hashtable條目個數. * (注意不要搞錯,不是空閑的條目個數) * * The coverage of a freelist might be more or less than one partition, so it * needs its own lock rather than relying on caller locking. Relying on that * wouldn't work even if the coverage was the same, because of the occasional * need to "borrow" entries from another freelist; see get_hash_entry(). * 空閑鏈表的覆蓋范圍可能比一個分區多或少,因此需要自己的鎖而不能僅僅依賴調用者的鎖. * 依賴調用者鎖在覆蓋面一樣的情況下也不會起效,因為偶爾需要從另一個自由列表“借用”條目,詳細參見get_hash_entry() * * Using an array of FreeListData instead of separate arrays of mutexes, * nentries and freeLists helps to reduce sharing of cache lines between * different mutexes. * 使用FreeListData數組而不是一個獨立的mutexes,nentries和freelists數組有助于減少不同mutexes之間的緩存線共享. */ typedef struct { //該空閑鏈表的自旋鎖 slock_t mutex; /* spinlock for this freelist */ //相關桶中的條目個數 long nentries; /* number of entries in associated buckets */ //空閑元素鏈 HASHELEMENT *freeList; /* chain of free elements */ } FreeListData; /* * HASHELEMENT is the private part of a hashtable entry. The caller's data * follows the HASHELEMENT structure (on a MAXALIGN'd boundary). The hash key * is expected to be at the start of the caller's hash entry data structure. * HASHELEMENT是哈希表條目的私有部分. * 調用者的數據按照HASHELEMENT結構組織(位于MAXALIGN的邊界). * 哈希鍵應位于調用者hash條目數據結構的開始位置. */ typedef struct HASHELEMENT { //鏈接到相同桶中的下一個條目 struct HASHELEMENT *link; /* link to next entry in same bucket */ //該條目的哈希函數結果 uint32 hashvalue; /* hash function result for this entry */ } HASHELEMENT; /* Hash table header struct is an opaque type known only within dynahash.c */ //哈希表頭部結構,非透明類型,用于dynahash.c typedef struct HASHHDR HASHHDR; /* Hash table control struct is an opaque type known only within dynahash.c */ //哈希表控制結構,非透明類型,用于dynahash.c typedef struct HTAB HTAB; /* Parameter data structure for hash_create */ //hash_create使用的參數數據結構 /* Only those fields indicated by hash_flags need be set */ //根據hash_flags標記設置相應的字段 typedef struct HASHCTL { //分區個數(必須是2的冪) long num_partitions; /* # partitions (must be power of 2) */ //段大小 long ssize; /* segment size */ //初始化目錄大小 long dsize; /* (initial) directory size */ //dsize上限 long max_dsize; /* limit to dsize if dir size is limited */ //填充因子 long ffactor; /* fill factor */ //哈希鍵大小(字節為單位) Size keysize; /* hash key length in bytes */ //參見上述數據結構注釋 Size entrysize; /* total user element size in bytes */ // HashValueFunc hash; /* hash function */ HashCompareFunc match; /* key comparison function */ HashCopyFunc keycopy; /* key copying function */ HashAllocFunc alloc; /* memory allocator */ MemoryContext hcxt; /* memory context to use for allocations */ //共享內存中的哈希頭部結構地址 HASHHDR *hctl; /* location of header in shared mem */ } HASHCTL; /* A hash bucket is a linked list of HASHELEMENTs */ //哈希桶是HASHELEMENTs鏈表 typedef HASHELEMENT *HASHBUCKET; /* A hash segment is an array of bucket headers */ //hash segment是桶數組 typedef HASHBUCKET *HASHSEGMENT; /* * Hash functions must have this signature. * Hash函數必須有它自己的標識 */ typedef uint32 (*HashValueFunc) (const void *key, Size keysize); /* * Key comparison functions must have this signature. Comparison functions * return zero for match, nonzero for no match. (The comparison function * definition is designed to allow memcmp() and strncmp() to be used directly * as key comparison functions.) * 哈希鍵對比函數必須有自己的標識. * 如匹配則對比函數返回0,不匹配返回非0. * (對比函數定義被設計為允許在對比鍵值時可直接使用memcmp()和strncmp()) */ typedef int (*HashCompareFunc) (const void *key1, const void *key2, Size keysize); /* * Key copying functions must have this signature. The return value is not * used. (The definition is set up to allow memcpy() and strlcpy() to be * used directly.) * 鍵拷貝函數必須有自己的標識. * 返回值無用. */ typedef void *(*HashCopyFunc) (void *dest, const void *src, Size keysize); /* * Space allocation function for a hashtable --- designed to match malloc(). * Note: there is no free function API; can't destroy a hashtable unless you * use the default allocator. * 哈希表的恐懼分配函數 -- 被設計為與malloc()函數匹配. * 注意:這里沒有釋放函數API;不能銷毀哈希表,除非使用默認的分配器. */ typedef void *(*HashAllocFunc) (Size request);
get_hash_entry
分配一個新的哈希表條目.如內存溢出則返回NULL.
/* * Allocate a new hashtable entry if possible; return NULL if out of memory. * (Or, if the underlying space allocator throws error for out-of-memory, * we won't return at all.) * 如可能,分配一個新的哈希表條目.如內存溢出則返回NULL. * (或者,如果依賴的空間分配器因為內存溢出拋出錯誤,則不會返回任何信息) */ static HASHBUCKET get_hash_entry(HTAB *hashp, int freelist_idx) { HASHHDR *hctl = hashp->hctl; HASHBUCKET newElement; for (;;) { //循環 /* if partitioned, must lock to touch nentries and freeList */ //如為分區哈希表,在訪問條目和空閑鏈表時,必須鎖定 if (IS_PARTITIONED(hctl)) SpinLockAcquire(&hctl->freeList[freelist_idx].mutex); /* try to get an entry from the freelist */ //從空閑鏈表中嘗試獲取一個條目 newElement = hctl->freeList[freelist_idx].freeList; if (newElement != NULL) break; if (IS_PARTITIONED(hctl)) SpinLockRelease(&hctl->freeList[freelist_idx].mutex); /* * No free elements in this freelist. In a partitioned table, there * might be entries in other freelists, but to reduce contention we * prefer to first try to get another chunk of buckets from the main * shmem allocator. If that fails, though, we *MUST* root through all * the other freelists before giving up. There are multiple callers * that assume that they can allocate every element in the initially * requested table size, or that deleting an element guarantees they * can insert a new element, even if shared memory is entirely full. * Failing because the needed element is in a different freelist is * not acceptable. * 在空閑鏈表中沒有空閑條目.在分區哈希表中,在其他空閑鏈表中可能存在條目, * 但為了減少爭用,我們期望首先嘗試從主shmem分配器中獲取桶中的其他chunk. * 如果失敗,我們必須在放棄之前從根節點開始遍歷所有其他空閑鏈表. * 存在多個調用者假定它們可以在初始的請求哈希表大小內分配每一個元素, * 或者甚至在共享內存全滿的情況下刪除元素可以保證它們可以插入一個新元素. * 之所以失敗是因為所需要的元素在不同的空閑鏈表中是不可接受的. */ if (!element_alloc(hashp, hctl->nelem_alloc, freelist_idx)) { //本空閑鏈表不能分配內存 int borrow_from_idx; if (!IS_PARTITIONED(hctl)) //非分區哈希表,返回NULL,意味著內存溢出了. return NULL; /* out of memory */ /* try to borrow element from another freelist */ //嘗試從其他空閑鏈表瀏覽元素 borrow_from_idx = freelist_idx; for (;;) { //------- 開始遍歷其他空閑鏈表 borrow_from_idx = (borrow_from_idx + 1) % NUM_FREELISTS; if (borrow_from_idx == freelist_idx) //已經完成整個空閑鏈表的遍歷,退出 break; /* examined all freelists, fail */ //獲取自旋鎖 SpinLockAcquire(&(hctl->freeList[borrow_from_idx].mutex)); newElement = hctl->freeList[borrow_from_idx].freeList; if (newElement != NULL) { hctl->freeList[borrow_from_idx].freeList = newElement->link; SpinLockRelease(&(hctl->freeList[borrow_from_idx].mutex)); /* careful: count the new element in its proper freelist */ //小心:在合適的空閑鏈表上統計新的元素 SpinLockAcquire(&hctl->freeList[freelist_idx].mutex); hctl->freeList[freelist_idx].nentries++; SpinLockRelease(&hctl->freeList[freelist_idx].mutex); return newElement; } SpinLockRelease(&(hctl->freeList[borrow_from_idx].mutex)); } /* no elements available to borrow either, so out of memory */ //已無可用空間,內存溢出 return NULL; } } /* remove entry from freelist, bump nentries */ //從空閑鏈表中移除條目,nentries+1 hctl->freeList[freelist_idx].freeList = newElement->link; hctl->freeList[freelist_idx].nentries++; if (IS_PARTITIONED(hctl)) SpinLockRelease(&hctl->freeList[freelist_idx].mutex); return newElement; }
跟蹤SetupLockInTable,進入hash_search_with_hash_value
(gdb) b SetupLockInTable if lockmode == 8 Breakpoint 2 at 0x8ccf4e: file lock.c, line 1131. (gdb) c Continuing. Breakpoint 2, SetupLockInTable (lockMethodTable=0xc8dba0 <default_lockmethod>, proc=0x7f293e800b70, locktag=0x7fff167a9250, hashcode=1823181291, lockmode=8) at lock.c:1131 1131 lock = (LOCK *) hash_search_with_hash_value(LockMethodLockHash, (gdb) step hash_search_with_hash_value (hashp=0x1210160, keyPtr=0x7fff167a9250, hashvalue=1823181291, action=HASH_ENTER_NULL, foundPtr=0x7fff167a90bf) at dynahash.c:925 925 HASHHDR *hctl = hashp->hctl;
查看HTAB(hashp)和HASHHDR(hctl)
(gdb) n 926 int freelist_idx = FREELIST_IDX(hctl, hashvalue); (gdb) p *hashp $8 = {hctl = 0x7f293e443980, dir = 0x7f293e443cd8, hash = 0xa79ac6 <tag_hash>, match = 0x47cb70 <memcmp@plt>, keycopy = 0x47d0a0 <memcpy@plt>, alloc = 0x8c3419 <ShmemAllocNoError>, hcxt = 0x0, tabname = 0x12101c0 "LOCK hash", isshared = true, isfixed = false, frozen = false, keysize = 16, ssize = 256, sshift = 8} (gdb) p *hctl $9 = {freeList = {{mutex = 0 '\000', nentries = 389, freeList = 0x0}, {mutex = 0 '\000', nentries = 392, freeList = 0x0}, {mutex = 0 '\000', nentries = 400, freeList = 0x0}, {mutex = 0 '\000', nentries = 382, freeList = 0x7f293ebd1b00}, {mutex = 0 '\000', nentries = 439, freeList = 0x7f293ebc89a0}, { mutex = 0 '\000', nentries = 391, freeList = 0x7f293ebc1b20}, {mutex = 0 '\000', nentries = 411, freeList = 0x7f293eb91680}, {mutex = 0 '\000', nentries = 395, freeList = 0x0}, {mutex = 0 '\000', nentries = 402, freeList = 0x0}, {mutex = 0 '\000', nentries = 416, freeList = 0x7f293ebcb338}, { mutex = 0 '\000', nentries = 431, freeList = 0x7f293ebc3170}, {mutex = 0 '\000', nentries = 421, freeList = 0x0}, {mutex = 0 '\000', nentries = 406, freeList = 0x7f293eb995a8}, {mutex = 0 '\000', nentries = 409, freeList = 0x7f293ebd2238}, {mutex = 0 '\000', nentries = 411, freeList = 0x7f293ebd1f98}, {mutex = 0 '\000', nentries = 386, freeList = 0x0}, {mutex = 0 '\000', nentries = 412, freeList = 0x0}, { mutex = 0 '\000', nentries = 424, freeList = 0x0}, {mutex = 0 '\000', nentries = 394, freeList = 0x7f293ebd1da0}, {mutex = 0 '\000', nentries = 401, freeList = 0x0}, {mutex = 0 '\000', nentries = 408, freeList = 0x7f293eb99500}, {mutex = 0 '\000', nentries = 437, freeList = 0x0}, { mutex = 0 '\000', nentries = 429, freeList = 0x7f293ebd2778}, {mutex = 0 '\000', nentries = 386, freeList = 0x0}, {mutex = 0 '\000', nentries = 410, freeList = 0x7f293ebd2d60}, {mutex = 0 '\000', nentries = 416, freeList = 0x0}, {mutex = 0 '\000', nentries = 412, freeList = 0x7f293ebd1c50}, { mutex = 0 '\000', nentries = 436, freeList = 0x7f293ebd1ba8}, {mutex = 0 '\000', nentries = 380, freeList = 0x7f293ebd1cf8}, {mutex = 0 '\000', nentries = 428, freeList = 0x0}, {mutex = 0 '\000', nentries = 405, freeList = 0x0}, {mutex = 0 '\000', nentries = 372, freeList = 0x0}}, dsize = 256, nsegs = 16, max_bucket = 4095, high_mask = 8191, low_mask = 4095, keysize = 16, entrysize = 152, num_partitions = 16, ffactor = 1, max_dsize = 256, ssize = 256, sshift = 8, nelem_alloc = 48}
可以看到,hctl中的freelist數組,數組中每個元素的freeList,如不為0x0(NULL),則說明還有空閑空間,否則說明已無空間.
(gdb) n 949 if (action == HASH_ENTER || action == HASH_ENTER_NULL) (gdb) p freelist_idx $10 = 11 (gdb) p hctl->freeList[11] $11 = {mutex = 0 '\000', nentries = 421, freeList = 0x0} (gdb) n 956 if (!IS_PARTITIONED(hctl) && !hashp->frozen && (gdb) 965 bucket = calc_bucket(hctl, hashvalue); --> hash桶 (gdb) 967 segment_num = bucket >> hashp->sshift; --> 根據hash桶獲取段 (gdb) 968 segment_ndx = MOD(bucket, hashp->ssize);--> 根據hash桶獲取段內偏移 (gdb) 970 segp = hashp->dir[segment_num];--> 獲取hash段指針 (gdb) 972 if (segp == NULL) (gdb) p bucket $12 = 2539 (gdb) p segment_num $13 = 9 (gdb) p segment_ndx $14 = 235 (gdb) p *segp $15 = (HASHBUCKET) 0x7f293e44de98 (gdb) p **segp $16 = {link = 0x0, hashvalue = 3817199872} (gdb) n 975 prevBucketPtr = &segp[segment_ndx]; --> HASHBUCKET指針的指針 (gdb) 976 currBucket = *prevBucketPtr; --> HASHBUCKET指針 (gdb) 981 match = hashp->match; /* save one fetch in inner loop */ (gdb) 982 keysize = hashp->keysize; /* ditto */ (gdb) p match $17 = (HashCompareFunc) 0x47cb70 <memcmp@plt> --> hash函數 (gdb) n 984 while (currBucket != NULL) --> 沿著碰撞鏈循環獲取hash桶 (gdb) 986 if (currBucket->hashvalue == hashvalue && --> 退出條件:找到匹配的元素 (gdb) 989 prevBucketPtr = &(currBucket->link); (gdb) 990 currBucket = *prevBucketPtr; (gdb) 984 while (currBucket != NULL) --> 退出條件:hash桶指針為NULL,沒有找到元素 (gdb) 997 if (foundPtr) (gdb) p foundPtr $18 = (_Bool *) 0x7fff167a90bf (gdb) n 998 *foundPtr = (bool) (currBucket != NULL); (gdb) 1003 switch (action) (gdb) 1042 Assert(hashp->alloc != DynaHashAlloc); (gdb) 1047 if (currBucket != NULL) (gdb) 1051 if (hashp->frozen) (gdb) 1055 currBucket = get_hash_entry(hashp, freelist_idx); (gdb) step get_hash_entry (hashp=0x1210160, freelist_idx=11) at dynahash.c:1252 1252 HASHHDR *hctl = hashp->hctl; (gdb) n 注 : #define IS_PARTITIONED(hctl) ((hctl)->num_partitions != 0) 1258 if (IS_PARTITIONED(hctl)) (gdb) p *hctl $19 = {freeList = {{mutex = 0 '\000', nentries = 389, freeList = 0x0}, {mutex = 0 '\000', nentries = 392, freeList = 0x0}, {mutex = 0 '\000', nentries = 400, freeList = 0x0}, {mutex = 0 '\000', nentries = 382, freeList = 0x7f293ebd1b00}, {mutex = 0 '\000', nentries = 439, freeList = 0x7f293ebc89a0}, { mutex = 0 '\000', nentries = 391, freeList = 0x7f293ebc1b20}, {mutex = 0 '\000', nentries = 411, freeList = 0x7f293eb91680}, {mutex = 0 '\000', nentries = 395, freeList = 0x0}, {mutex = 0 '\000', nentries = 402, freeList = 0x0}, {mutex = 0 '\000', nentries = 416, freeList = 0x7f293ebcb338}, { mutex = 0 '\000', nentries = 431, freeList = 0x7f293ebc3170}, {mutex = 0 '\000', nentries = 421, freeList = 0x0}, {mutex = 0 '\000', nentries = 406, freeList = 0x7f293eb995a8}, {mutex = 0 '\000', nentries = 409, freeList = 0x7f293ebd2238}, {mutex = 0 '\000', nentries = 411, freeList = 0x7f293ebd1f98}, {mutex = 0 '\000', nentries = 386, freeList = 0x0}, {mutex = 0 '\000', nentries = 412, freeList = 0x0}, { mutex = 0 '\000', nentries = 424, freeList = 0x0}, {mutex = 0 '\000', nentries = 394, freeList = 0x7f293ebd1da0}, {mutex = 0 '\000', nentries = 401, freeList = 0x0}, {mutex = 0 '\000', nentries = 408, freeList = 0x7f293eb99500}, {mutex = 0 '\000', nentries = 437, freeList = 0x0}, { mutex = 0 '\000', nentries = 429, freeList = 0x7f293ebd2778}, {mutex = 0 '\000', nentries = 386, freeList = 0x0}, {mutex = 0 '\000', nentries = 410, freeList = 0x7f293ebd2d60}, {mutex = 0 '\000', nentries = 416, freeList = 0x0}, {mutex = 0 '\000', nentries = 412, freeList = 0x7f293ebd1c50}, { mutex = 0 '\000', nentries = 436, freeList = 0x7f293ebd1ba8}, {mutex = 0 '\000', nentries = 380, freeList = 0x7f293ebd1cf8}, {mutex = 0 '\000', nentries = 428, freeList = 0x0}, {mutex = 0 '\000', nentries = 405, freeList = 0x0}, {mutex = 0 '\000', nentries = 372, freeList = 0x0}}, dsize = 256, nsegs = 16, max_bucket = 4095, high_mask = 8191, low_mask = 4095, keysize = 16, entrysize = 152, num_partitions = 16, ffactor = 1, max_dsize = 256, ssize = 256, sshift = 8, nelem_alloc = 48} (gdb) n 1259 SpinLockAcquire(&hctl->freeList[freelist_idx].mutex); (gdb) 1262 newElement = hctl->freeList[freelist_idx].freeList; --> 嘗試從空閑鏈表中獲取新元素 (gdb) 1264 if (newElement != NULL) --> 不為NULL,則返回,否則,嘗試擴展 (gdb) p newElement $20 = (HASHBUCKET) 0x0 (gdb) n 1267 if (IS_PARTITIONED(hctl)) (gdb) 1268 SpinLockRelease(&hctl->freeList[freelist_idx].mutex); (gdb) 1282 if (!element_alloc(hashp, hctl->nelem_alloc, freelist_idx)) --> 擴展 (gdb) step element_alloc (hashp=0x1210160, nelem=48, freelist_idx=11) at dynahash.c:1659 --> 進入element_alloc 1659 HASHHDR *hctl = hashp->hctl; (gdb) n 1666 if (hashp->isfixed) (gdb) 1670 elementSize = MAXALIGN(sizeof(HASHELEMENT)) + MAXALIGN(hctl->entrysize); --> 確保地址對齊 (gdb) 1672 CurrentDynaHashCxt = hashp->hcxt; (gdb) p elementSize $21 = 168 (gdb) n 1673 firstElement = (HASHELEMENT *) hashp->alloc(nelem * elementSize); --> 分配空間 (gdb) 1675 if (!firstElement) (gdb) p firstElement $22 = (HASHELEMENT *) 0x0 (gdb) p nelem * elementSize $23 = 8064 (gdb) n 1676 return false;--> 擴展失敗,返回false (gdb) 1700 } (gdb) get_hash_entry (hashp=0x1210160, freelist_idx=11) at dynahash.c:1286 1286 if (!IS_PARTITIONED(hctl)) (gdb) 1290 borrow_from_idx = freelist_idx; --> 該空閑鏈表無法擴展,尋找下一個空閑鏈表 (gdb) 1293 borrow_from_idx = (borrow_from_idx + 1) % NUM_FREELISTS; --> 簡單的+1后取模 (gdb) p freelist_idx $24 = 11 (gdb) n 1294 if (borrow_from_idx == freelist_idx) --> 找了一圈,回到原點,內存不足,這時候會報錯 (gdb) p NUM_FREELISTS $25 = 32 (gdb) p borrow_from_idx $26 = 12 (gdb) n 1297 SpinLockAcquire(&(hctl->freeList[borrow_from_idx].mutex)); (gdb) 1298 newElement = hctl->freeList[borrow_from_idx].freeList; (gdb) 1300 if (newElement != NULL) (gdb) 1302 hctl->freeList[borrow_from_idx].freeList = newElement->link; (gdb) 1303 SpinLockRelease(&(hctl->freeList[borrow_from_idx].mutex)); (gdb) 1306 SpinLockAcquire(&hctl->freeList[freelist_idx].mutex); (gdb) 1307 hctl->freeList[freelist_idx].nentries++; (gdb) 1308 SpinLockRelease(&hctl->freeList[freelist_idx].mutex); (gdb) p newElement $27 = (HASHBUCKET) 0x7f293eb995a8 (gdb) n 1310 return newElement; --> 找到了空閑元素,返回 (gdb) 1329 } (gdb) hash_search_with_hash_value (hashp=0x1210160, keyPtr=0x7fff167a9250, hashvalue=1823181291, action=HASH_ENTER_NULL, foundPtr=0x7fff167a90bf) at dynahash.c:1056 1056 if (currBucket == NULL) (gdb) 1073 *prevBucketPtr = currBucket; (gdb) 1074 currBucket->link = NULL; (gdb) 1077 currBucket->hashvalue = hashvalue; (gdb) 1078 hashp->keycopy(ELEMENTKEY(currBucket), keyPtr, keysize); (gdb) 1087 return (void *) ELEMENTKEY(currBucket); (gdb) 1093 } (gdb) SetupLockInTable (lockMethodTable=0xc8dba0 <default_lockmethod>, proc=0x7f293e800b70, locktag=0x7fff167a9250, hashcode=1823181291, lockmode=8) at lock.c:1136 1136 if (!lock) (gdb) 1142 if (!found) (gdb) 1144 lock->grantMask = 0; (gdb) 1145 lock->waitMask = 0; (gdb) 1146 SHMQueueInit(&(lock->procLocks)); (gdb) 1147 ProcQueueInit(&(lock->waitProcs)); (gdb) 1148 lock->nRequested = 0; (gdb) 1149 lock->nGranted = 0; (gdb) 1150 MemSet(lock->requested, 0, sizeof(int) * MAX_LOCKMODES); (gdb) 1151 MemSet(lock->granted, 0, sizeof(int) * MAX_LOCKMODES); (gdb) 1165 proclocktag.myLock = lock; (gdb) 1166 proclocktag.myProc = proc; (gdb) 1168 proclock_hashcode = ProcLockHashCode(&proclocktag, hashcode); (gdb) 1173 proclock = (PROCLOCK *) hash_search_with_hash_value(LockMethodProcLockHash, (gdb) 1178 if (!proclock) (gdb) p proclock $28 = (PROCLOCK *) 0x7f293ebc6ec0 (gdb) n 1203 if (!found) (gdb) 1205 uint32 partition = LockHashPartition(hashcode); (gdb) 1217 proclock->groupLeader = proc->lockGroupLeader != NULL ? (gdb) 1218 proc->lockGroupLeader : proc; (gdb) 1217 proclock->groupLeader = proc->lockGroupLeader != NULL ? (gdb) 1219 proclock->holdMask = 0; (gdb) 1220 proclock->releaseMask = 0; (gdb) 1222 SHMQueueInsertBefore(&lock->procLocks, &proclock->lockLink); (gdb) 1223 SHMQueueInsertBefore(&(proc->myProcLocks[partition]), (gdb) 1275 lock->nRequested++; (gdb) 1276 lock->requested[lockmode]++; (gdb) 1277 Assert((lock->nRequested > 0) && (lock->requested[lockmode] > 0)); (gdb) 1283 if (proclock->holdMask & LOCKBIT_ON(lockmode)) (gdb) 1289 return proclock; (gdb) 1290 } (gdb) LockAcquireExtended (locktag=0x7fff167a9250, lockmode=8, sessionLock=false, dontWait=false, reportMemoryError=true, locallockp=0x7fff167a9248) at lock.c:956 956 if (!proclock) (gdb) c Continuing.
到此,關于“分析PostgreSQL SetupLockInTable方法中與OOM相關的代碼”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。