Home > System Tutorial > LINUX > body text

How the Linux file system works: Everything in the index node and directory entry is a file

WBOY
Release: 2024-04-03 09:16:01
forward
442 people have browsed it

linux檔案系統工作原理索引節點和目錄項目

linux中一切皆文件,普通文件、目錄、區塊設備、套接字、管道也要透過統一的檔案系統來管理。

linux為每位檔案都指派兩個資料結構,索引節點和目錄項,主要拿來記錄檔案的元資訊和目錄結構。

索引節點是每位檔案的惟一標誌,目錄項目所維護的正是檔案系統的樹狀結構,目錄項目和索引節點關係是多對一,可以簡單理解為:一個檔案可以有多某些名。

透過硬連結為文件建立的別稱,會對應不同的目錄項,這種目錄項本質還是連結同一個文件,所以其索引節點相同。

c碟最小單位是磁軌(512B),然而這樣每次讀寫那麼小,效率很低。所以檔案系統又把連續的磁軌組成了邏輯區塊,每次都以邏輯區塊為最小單元,來管理數據,每次以邏輯區塊為最小單元,來管理數據,常見邏輯區塊大小為4KB,由連續的8個磁軌組成。

兩個注意點:

linux 文件系统优化_优化文件系统的管理_优化文件系统NTFS的管理

#虛擬檔案系統

目錄項目、索引節點、邏輯區塊以及超級區塊構成linux檔案系統四大要素。為了支援各類不同檔案系統,linux在使用者流程和檔案系統中間,又引進了一個具象層,即虛擬檔案系統VFS.

VFS定義了一組所有檔案系統都支援的資料結構和標準插口。

檔案系統I/O

I/O分類:緩衝與非緩衝I/O,直接與非直接I/O,阻塞與非阻塞I/O,同步與非同步I/O。

优化文件系统NTFS的管理_优化文件系统的管理_linux 文件系统优化

#空間不足,df查看c盤,發覺剩餘空間有好多

雖然不只文件數據,索引節點也佔用c盤空間,使用以下指令:

df-i

當發覺inode不足,c盤空間充足,可能是過多小檔案造成的。刪掉那些小文件,或則把它們聯通到索引節點充足的其他c盤中,就可以解決這個問題。

核心使用Slab機制,管理目錄項目和索引節點的快取。 /proc/meminfo只給了Slab的整體大小,具體到每一種Slab緩存,還要查看/proc/slabinfo。

优化文件系统的管理_优化文件系统NTFS的管理_linux 文件系统优化

#儲存系統I/O運作原理:

儲存系統的I/O,一般是整個系統中最慢的一環。所以,Linux透過多種快取機制來優化I/O效率。比方說,為了優化檔案存取的效能,會使用頁快取、索引節點快取、目錄項目快取等多種快取機制,以降低對上層區塊裝置的直接呼叫。同樣,為了優化區塊設備的存取效率,會使用緩衝區,來快取區塊設備的資料。

c磁碟效能指標

使用率只考慮有沒有I/O,而不考慮I/O的大小。換句話說,當使用率是100%的時侯,c盤仍然有可能接受新的I/O懇求

不能孤立地去比較某一指標,而要結合讀寫比列、I/O類型(隨機還是連續)以及I/O的大小,綜合來剖析;在資料庫、大量小檔案等這類隨機讀寫比較多的場景中嵌入式linux,IOPS更能反映系統的整體效能;而在多媒體等次序讀寫較多的場景中,吞吐量才更能反映系統的整體效能

优化文件系统的管理_优化文件系统NTFS的管理_linux 文件系统优化

#當你遇見這些「狂打日誌」的場景時,你可以用iostat、strace、lsof等工具來定位狂打日誌的進程,找出對應的日誌文件,再透過應用程式的插口,調整日誌等級來解決問題。假如應用程式不能動態調整日誌級別,你可能還必須更改應用程式的配置,並重啟應用程式讓配置生效。

為何strace追蹤這個進程,卻沒有發覺任何write系統呼叫?

由於寫檔案是由子執行緒執行的,所有strace追蹤進程沒有見到write系統調用,可以透過pstree查看行程的執行緒信息,再用strace追蹤;或則透過strace-fppid追蹤所有執行緒

慢查詢剖析

top、iostat剖析了系統的CPU和c盤使用情況,發覺了c盤的I/O困局。接著,我們利用pidstat,發覺困局是mysqld造成的。緊接著,我們又透過strace、lsoflinux 檔案系統優化,找出了mysqld正在讀的檔案。同時,依照檔案的名子和路徑,我們找出了mysqld正在操作的資料庫和資料表。綜合這種信息,我們判定,這是一個沒有借助索引造成的慢查詢問題。

优化文件系统的管理_优化文件系统NTFS的管理_linux 文件系统优化

#停止dataservice後,IO問題也會消失,為何?

案例應用程式存取的資料表,基於MyISAM引擎,而MyISAM的一個特徵,就是只在顯存中快取索引,並不緩存資料。所以,在查詢句子未能使用索引時,就須要資料表從資料庫檔案讀入顯存,之後再進行處理。

dataservice會不停的釋放檔案緩存,致使mysql不會借助c盤緩存。

redis慢先用top、iostat剖析了系統的CPU、記憶體和c盤使用情況,不過卻發覺,系統資源並沒有出現困局。為了進一步剖析,就須要你對系統和應用程式的工作原理有一定的了解。例如,在明天的案例中,儘管c盤I/O並沒有出現困局,但從Redis的原理來說,查詢快取時不應當出現大量的c盤I/O寫入操作。沿著這個思路,我們繼續利用pidstat、strace、lsof、nsenter等一系列的工具,找出了兩個潛在問題,一個是Redis的不合理配置,另一個是Python應用對Redis的濫用I/O基準測試工具

fio(flexibleI/OTester)

I/O performance optimization

Optimization of application

Replace random writes with append writes to reduce polling expenses and increase the rate of I/O writes. Use cached I/OLinux file system optimization and make full use of the system cache to increase the number of actual I/Os. Create your own cache internally within the application, or use an external caching system such as Redis. In this way, on the one hand, the cached data and life cycle can be controlled within the application; on the other hand, it can also reduce the impact of other applications using cache on themselves. Library functions such as fopen and fread provided by the C standard library will use the cache of the standard library to reduce the operation of the C drive. When you directly use system calls such as open and read, you can only use the page cache and buffer provided by the operating system, and there is no cache of library functions available. If you need to frequently read and write the same C disk space, you can use mmap instead of read. /write, reduce the number of copies of video memory. In scenarios where synchronous writing is required, try to merge write requests instead of having each request be written to the c drive synchronously. That is, you can use fsync() instead of O_SYNC to share the same file in multiple applications. When using Linux memory management on the c drive, in order to ensure that the I/O is not fully occupied by an application, it is recommended that you use the I/O subsystem of cgroups to limit the IOPS and throughput of the process/process group. When using the CFQ scheduler, you can Use ionice to adjust the I/O scheduling priority of the process, especially to improve the I/O priority of core applications. ionice supports three priority classes: Idle, Best-effort and Realtime. Among them, Best-effort and Realtime also support levels from 0 to 7 respectively. The smaller the value, the higher the priority level.

The above is the detailed content of How the Linux file system works: Everything in the index node and directory entry is a file. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:itcool.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!