简体   繁体   English

Linux 内核为什么以及如何为核心转储创建稀疏文件?

[英]Why and how does Linux kernel create a sparse file for the coredump?

Currently I'm working on Linux 4.0.6.目前我正在使用 Linux 4.0.6。 After a coredump activation, I observe that the generated corefiles is created as sparse files.核心转储激活后,我观察到生成的核心文件被创建为稀疏文件。 For example, the ls command shows the size of my corefile is 42 MB.例如, ls命令显示我的核心文件的大小为 42 MB。 However, the du command shows that it allocates only 26.3 MB.但是, du命令显示它只分配了 26.3 MB。

My questions regarding this observations:我对这个观察的问题:

  1. Why does Linux kernel create a sparse file for the coredump?为什么 Linux 内核会为 coredump 创建一个稀疏文件?
  2. How does it work?它是如何工作的? Does it depend on the filesystem where the coredump is placed?它是否取决于放置核心转储的文件系统?
  3. Can I configure the system/ kernel to prevent the coredump as a sparse file?我可以配置系统/内核以防止核心转储作为稀疏文件吗?

You should just think of what a coredump file is: a mere sequential write of the memory of the process.您应该只考虑核心转储文件是什么:进程内存的顺序写入。 On modern OS, the memory is not a simple sequential byte range, but can be made of multiple segments mapped at different addresses.在现代操作系统上,内存不是一个简单的顺序字节范围,而是可以由映射到不同地址的多个段组成。 That explains that if you try to read or write at an address outside of a mapped segment you get a Segment Violation Signal (SIGSEGV).这解释了如果您尝试在映射段之外的地址读取或写入,您会收到段违规信号 (SIGSEGV)。

So at dump time, the system writes the segments in ascending order and just lseek to the beginning of each new segment thus building a sparse file.因此,在倾倒时,系统按升序排列,只是lseek的,从而构建一个稀疏文件中的每个新段的开头写的段。

Now for your questions:现在回答您的问题:

  1. Why does Linux kernel create a sparse file for the coredump?为什么 Linux 内核会为 coredump 创建一个稀疏文件?

The explanation is just above.解释就在上面。

  1. How does it work?它是如何工作的? Does it depend on the filesystem where the coredump is placed?它是否取决于放置核心转储的文件系统?

Not really, unless the underlying file system does not allow sparse files.不是真的,除非底层文件系统不允许稀疏文件。

  1. Can I configure the system/ kernel to prevent the coredump as a sparse file?我可以配置系统/内核以防止核心转储作为稀疏文件吗?

IMHO you cannot, and more you do not want to.恕我直言,你不能,而且你更不想。 The ls command gives you the higher memory address used by the program, while the du command gives you the total memory size used by the program, because unused addresses are not mapped and do not consume memory at all. ls命令为您提供程序使用的较高内存地址,而du命令为您提供程序使用的总内存大小,因为未使用的地址不会被映射并且根本不消耗内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM