操作系统内存管理的一些杂记
- 在 /proc/pid/maps 文件存储了对应进程内的内存分布,其 layout 示例如下:
其中,每一行都在 kernel 中对应一个vm_area_struct
结构,定义如下(源码):
struct vm_area_struct {
/* The first cache line has the info for VMA tree walking. */
unsigned long vm_start; /* Our start address within vm_mm. */
unsigned long vm_end; /* The first byte after our end address
within vm_mm. */
/* linked list of VM areas per task, sorted by address */
struct vm_area_struct *vm_next, *vm_prev;
struct rb_node vm_rb;
/*
* Largest free memory gap in bytes to the left of this VMA.
* Either between this VMA and vma->vm_prev, or between one of the
* VMAs below us in the VMA rbtree and its ->vm_prev. This helps
* get_unmapped_area find a free area of the right size.
*/
unsigned long rb_subtree_gap;
/* Second cache line starts here. */
struct mm_struct *vm_mm; /* The address space we belong to. */
pgprot_t vm_page_prot; /* Access permissions of this VMA. */
unsigned long vm_flags; /* Flags, see mm.h. */
/*
* For areas with an address space and backing store,
* linkage into the address_space->i_mmap interval tree.
*/
struct {
struct rb_node rb;
unsigned long rb_subtree_last;
} shared;
/*
* A file's MAP_PRIVATE vma can be in both i_mmap tree and anon_vma
* list, after a COW of one of the file pages. A MAP_SHARED vma
* can only be in the i_mmap tree. An anonymous MAP_PRIVATE, stack
* or brk vma (with NULL file) can only be in an anon_vma list.
*/
struct list_head anon_vma_chain; /* Serialized by mmap_sem &
* page_table_lock */
struct anon_vma *anon_vma; /* Serialized by page_table_lock */
/* Function pointers to deal with this struct. */
const struct vm_operations_struct *vm_ops;
/* Information about our backing store: */
unsigned long vm_pgoff; /* Offset (within vm_file) in PAGE_SIZE
units */
struct file * vm_file; /* File we map to (can be NULL). */
void * vm_private_data; /* was vm_pte (shared mem) */
atomic_long_t swap_readahead_info;
#ifndef CONFIG_MMU
struct vm_region *vm_region; /* NOMMU mapping region */
#endif
#ifdef CONFIG_NUMA
struct mempolicy *vm_policy; /* NUMA policy for the VMA */
#endif
struct vm_userfaultfd_ctx vm_userfaultfd_ctx;
} __randomize_layout;
上面 maps 输出的格式说明:
maps中的列数 | maps 输出的值 | 对应 vm_area_struct 结构中的字段 | 意义 |
---|---|---|---|
1 | 00400000-00403000 | vm_start 和 vm_end | 虚拟地址空间的起始和终止地址 |
2 | r-xp | vm_flags | 此段 VMA 的属性,r可读,w可写,x可执行,p和s公用一个字段,表示私有段和共享段 |
3 | 00000000 | vm_pgoff | 有名映射中表示 vm_start 在文件中的偏移量(以页为单位),匿名映射中等于0或者vm_start/PAGE_SIZE |
4 | fd:01 | vm_file->f_dentry->d_inode->i_sb->s_dev | 映射文件的所属设备号,匿名映射始终是 fd:00 |
5 | 917515 | vm_file->f_dentry->d_inode->i_ino | 映射文件所属的节点号,对匿名映射来说始终是 fd:00 |
6 | /home/Ashitaka/Codes/cpp_study/program_mem_layout/layout.out | 无对应 | 映射文件名,匿名映射来说则是此段内存在进程中的作用,例如[heap]或者[stack] |
此文章主要参考(或者说“复述”合适一点。。。)
https://blog.csdn.net/lijzheng/article/details/23618365
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】博客园携手 AI 驱动开发工具商 Chat2DB 推出联合终身会员
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET 依赖注入中的 Captive Dependency
· .NET Core 对象分配(Alloc)底层原理浅谈
· 聊一聊 C#异步 任务延续的三种底层玩法
· 敏捷开发:如何高效开每日站会
· 为什么 .NET8线程池 容易引发线程饥饿
· 一个适用于 .NET 的开源整洁架构项目模板
· API 风格选对了,文档写好了,项目就成功了一半!
· 【开源】C#上位机必备高效数据转换助手
· .NET 9.0 使用 Vulkan API 编写跨平台图形应用
· MyBatis中的 10 个宝藏技巧!