Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

PostgreSQL VFD mechanism

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

1. Structure

The VFD mechanism is maintained by the structure struct vfd. The meaning of each member variable is shown in the following table:

Fd

The actual physical file descriptor corresponding to vfd

Fdstate

FD_DELETE_AT_CLOSE: indicates that the file needs to be deleted when it is closed

FD_TEMP_FILE_LIMIT: marking temporary fil

FD_CLOSE_AT_EOXACT:

These are all for temporary files.

Resowner

Owner, for automatic cleanup

NextFree

The free linked list of VFD is actually the subscript of the array.

LruMoreRecently

Recently, the least use of linked lists in VFD is bi-directional. It's actually the subscript of the array.

LruLe***ecently

LruLe***ecently is positive, and the head is inserted every time it is inserted.

FileSize

File size

FileName

File name

FileFlags

The label when opening the file, such as O_CREATE, etc.

FileMode

Attributes when opening a file, such as read and write permissions, etc.

2. Initialize

Initialization at startup, using malloc, is only valid in this process, that is, each process maintains its own VfdCache instead of shared memory. Only the first array is applied for at initialization and its fd is set to VFD_CLOSED.

PostgresMain- > BaseInit- > InitFileAccess: VfdCache = (Vfd *) malloc (sizeof (Vfd)); MemSet ((char *) & (VfdCache [0]), 0, sizeof (Vfd)); VfdCache- > fd = VFD_CLOSED; SizeVfdCache = 1TX 2, open

1) when Open, AllocateVfd is called first, an idle slot is found in the VfdCache array, and then returned. For the flow of this function, see AllocateVfd call.

2) then call ReleaseLruFiles to determine whether the fd with the maximum limit is open. If the limit is exceeded, the fd close of the last VFD in the LRU list will be dropped.

3) open file and insert the VFD into the LRU linked list. The detailed flow of the function Insert inserted into LRU is shown in the function analysis below.

4) then assign values to the vfdP member variables.

PathNameOpenFilePerm- > file = AllocateVfd (); vfdP = & VfdCacheFile; ReleaseLruFiles (); vfdP- > fd = BasicOpenFilePerm (fileName, fileFlags, fileMode); Insert (file); vfdP- > fileName = fnamecopy; / * Saved flags are adjusted to be OK for re-opening file * / vfdP- > fileFlags = fileFlags & ~ (O_CREAT | O_TRUNC | O_EXCL); vfdP- > fileMode = fileMode; vfdP- > fileSize = 0; vfdP- > fdstate = 0x0; vfdP- > resowner = NULL;AllocateVfd

1) before each call to the BasicOpenFilePerm open file, AllocateVfd is called to get a free vfd from the VfdCache.

2) it will first determine whether the free linked list is empty. At the initial time, when SizeVfdCache is 1, the VfdCache is initialized into an array of size 32 and concatenated through nextFree to form a free linked list. Note that the free linked list is a loop.

3) VfdCache [0] is not used. The first 32, that is, after the first expansion of the free linked list as shown in the following figure, skip VfdCache [1], 1 will be returned. That is to say, every time VFD is fetched, it is VfdCache [0] .nextFree.

4) when it is expanded again, it will be doubled and expanded.

AllocateVfd- > if (VfdCache [0] .nextFree = = 0) {Size newCacheSize = SizeVfdCache * 2; if (newCacheSize

< 32) newCacheSize = 32; newVfdCache = (Vfd *) realloc(VfdCache, sizeof(Vfd) * newCacheSize); VfdCache = newVfdCache; for (i = SizeVfdCache; i < newCacheSize; i++){ MemSet((char *) &(VfdCache[i]), 0, sizeof(Vfd)); VfdCache[i].nextFree = i + 1; VfdCache[i].fd = VFD_CLOSED; } VfdCache[newCacheSize - 1].nextFree = 0; VfdCache[0].nextFree = SizeVfdCache; SizeVfdCache = newCacheSize; } file = VfdCache[0].nextFree; VfdCache[0].nextFree = VfdCache[file].nextFree; return file; ReleaseLruFiles 1)nfile为open打开的文件数,numAllocatedDescs为fopen打开的文件数,max_safe_fds为操作系统计算得出的值。 2)一旦超出max_safe_fds值,就会调用ReleaseLruFile从LRU链表删除一个,注意删除的是VfdCache[0].lruMoreRecently,即链表的尾部,最近最少使用的。 3)首先将该fd关闭,然后将之置为VFD_CLOSED。调用Delete函数将VFD从LRU链表删除。注意这里只是从LRU链表删除,不会释放回收到free链表,也不会修改vfd数据结构的其他成员变量值。因为后续可能还会用到该物理文件,会重新open并将之重新insert到LRU链表。 ReleaseLruFiles->

While (nfile + numAllocatedDescs > = max_safe_fds) {if (! ReleaseLruFile () break;} ReleaseLruFile- > LruDelete (VfdCache [0] .lruMoreRecently);-> vfdP = & VfdCache [file]; close (vfdP- > fd); vfdP- > fd = VFD_CLOSED;-- nfile; Delete (file);-> vfdP = & VfdCache[ file] VfdCache [vfdP-> lruLe***ecently]. LruMoreRecently = vfdP- > lruMoreRecently; VfdCache [vfdP-> lruMoreRecently]. LruLe***ecently = vfdP- > lruLe***ecently;3, InsertInsert- > vfdP = & VfdCache [file]; vfdP- > lruMoreRecently = 0; vfdP- > lruLe***ecently = VfdCache [0] .lruLe * ecently; VfdCache [0] .lrule * * ecently = file; VfdCache[ vfdP-> lruLe***ecently]. LruMoreRecently = file

The form of the LRU linked list is as follows:

When Insert a VFD:

4. DeleteDelete (file);-- > vfdP = & VfdCache.VfdCache [vfdP-> lruLe***ecently]. LruMoreRecently = vfdP- > lruMoreRecently; VfdCache [vfdP-> lruMoreRecently]. LruLe***ecently = vfdP- > lruLe***ecently

For example, delete VfdCache [1]:

5. Recycle VFD

1) every time FileClose is called, the vfd is recycled to the free linked list.

2) call the close function first

3) then remove it from the LRU linked list

4) if it is a temporary file, the temporary file will also be deleted

5) call FreeVfd to recycle vfd to free linked list

FileClose- > close (vfdP- > fd);-- nfile; vfdP- > fd = VFD_CLOSED; Delete (file); FreeVfd (file); FreeVfd

Call the function FreeVfd to recycle and note the modification of several member variables. When recycling, insert it into the header of the free linked list. Take it from the head every time you take it.

FreeVfd- > free (vfdP- > fileName); / / Note that fileName needs to be released. It is another malloc vfdP- > fileName = NULL; vfdP- > fdstate = 0x0; vfdP- > nextFree = VfdCache [0] .nextFree; VfdCache [0] .nextFree = file

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report