Linux kernel version: 5.0.1

arm64

  1.将物理内存划分为若干页,每页的大小为4KiB(可以为8KiB或16KiB),那么如何知道每个页当前是什么情况呢?

  那就需要一个结构体来描述每一页的情况,那么就出现了结构体struct page.

  2.有若干页,意味着需要若干个struct page这样的结构体来描述若干页的状态;

  3.这些struct page存放在哪里呢?肯定是存放在物理内存里;

  4.存放在物理内存里,那么假设物理内存有4MiB,指定页面大小为4KiB,那么这些物理内存能被划分为多少个页面呢?又需要多少物理内存来存放struct page结构体呢?

  页面个数=4MiB/4KiB=4*1024 KiB/4KiB=1024个;

  那么就需要1024个struct page来描述这1024个页面的情况,这么多结构体需要多少内存呢?

  1024 * sizeof(struct page)

  5.如何获取sizeof(struct page)的大小呢?

  struct page结构体(结构体定义在include/linux/mm_types.h)如下:

   struct page {
unsigned long flags; /* Atomic flags, some possibly
* updated asynchronously */
/*
* Five words (20/40 bytes) are available in this union.
* WARNING: bit 0 of the first word is used for PageTail(). That
* means the other users of this union MUST NOT use the bit to
* avoid collision and false-positive PageTail().
*/
union {
struct { /* Page cache and anonymous pages */
/**
* @lru: Pageout list, eg. active_list protected by
* zone_lru_lock. Sometimes used as a generic list
* by the page owner.
*/
struct list_head lru;
/* See page-flags.h for PAGE_MAPPING_FLAGS */
struct address_space *mapping;
pgoff_t index; /* Our offset within mapping. */
/**
* @private: Mapping-private opaque data.
* Usually used for buffer_heads if PagePrivate.
* Used for swp_entry_t if PageSwapCache.
* Indicates order in the buddy system if PageBuddy.
*/
unsigned long private;
};
struct { /* slab, slob and slub */
union {
struct list_head slab_list; /* uses lru */
struct { /* Partial pages */
struct page *next;
#ifdef CONFIG_64BIT
int pages; /* Nr of pages left */
int pobjects; /* Approximate count */
#else
short int pages;
short int pobjects;
#endif
};
};
struct kmem_cache *slab_cache; /* not slob */
/* Double-word boundary */
void *freelist; /* first free object */
union {
void *s_mem; /* slab: first object */
unsigned long counters; /* SLUB */
struct { /* SLUB */
unsigned inuse:;
unsigned objects:;
unsigned frozen:;
};
};
};
struct { /* Tail pages of compound page */
unsigned long compound_head; /* Bit zero is set */ /* First tail page only */
unsigned char compound_dtor;
unsigned char compound_order;
atomic_t compound_mapcount;
};
struct { /* Second tail page of compound page */
unsigned long _compound_pad_1; /* compound_head */
unsigned long _compound_pad_2;
struct list_head deferred_list;
};
struct { /* Page table pages */
unsigned long _pt_pad_1; /* compound_head */
pgtable_t pmd_huge_pte; /* protected by page->ptl */
unsigned long _pt_pad_2; /* mapping */
union {
struct mm_struct *pt_mm; /* x86 pgds only */
atomic_t pt_frag_refcount; /* powerpc */
};
#if ALLOC_SPLIT_PTLOCKS
spinlock_t *ptl;
#else
spinlock_t ptl;
#endif
};
struct { /* ZONE_DEVICE pages */
/** @pgmap: Points to the hosting device page map. */
struct dev_pagemap *pgmap;
unsigned long hmm_data;
unsigned long _zd_pad_1; /* uses mapping */
}; /** @rcu_head: You can use this to free a page by RCU. */
struct rcu_head rcu_head;
}; union { /* This union is 4 bytes in size. */
/*
* If the page can be mapped to userspace, encodes the number
* of times this page is referenced by a page table.
*/
atomic_t _mapcount; /*
* If the page is neither PageSlab nor mappable to userspace,
* the value stored here may help determine what this page
* is used for. See page-flags.h for a list of page types
* which are currently stored here.
*/
unsigned int page_type; unsigned int active; /* SLAB */
int units; /* SLOB */
}; /* Usage count. *DO NOT USE DIRECTLY*. See page_ref.h */
atomic_t _refcount; #ifdef CONFIG_MEMCG
struct mem_cgroup *mem_cgroup;
#endif /*
* On machines where all RAM is mapped into kernel address space,
* we can simply calculate the virtual address. On machines with
* highmem some memory is mapped into kernel virtual memory
* dynamically, so we need a place to store that address.
* Note that this field could be 16 bits on x86 ... ;)
*
* Architectures with slow multiplication can define
* WANT_PAGE_VIRTUAL in asm/page.h
*/
#if defined(WANT_PAGE_VIRTUAL)
void *virtual; /* Kernel virtual address (NULL if
not kmapped, ie. highmem) */
#endif /* WANT_PAGE_VIRTUAL */ #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
int _last_cpupid;
#endif
} _struct_page_alignment;

  使用此内核模块获取,编译方法为:make CROSS_COMPILE=1 KDIR=<linux kernel source code path>

  所以:

  1024 * sizeof(struct page) = 1024 * 64 = 64 KiB = 16 个页面

  6.既然有部分物理内存用来存储每个页面的情况,那么可用的物理内存必然少于4MiB,那么具体是多少呢?

  1024 - 16 = 1008 个页面 = 1008 * 4 KiB = 4032 KiB

  

linux如何管理物理内存?的更多相关文章

  1. Linux内存管理--物理内存分配【转】

    转自:http://blog.csdn.net/myarrow/article/details/8682819 1. First Fit分配器 First Fit分配器是最基本的内存分配器,它使用bi ...

  2. Linux内存管理 【转】

    转自:http://blog.chinaunix.net/uid-25909619-id-4491368.html Linux内存管理 摘要:本章首先以应用程序开发者的角度审视Linux的进程内存管理 ...

  3. Linux内存管理【转】

    转自:http://www.cnblogs.com/wuchanming/p/4360264.html 转载:http://www.kerneltravel.net/journal/v/mem.htm ...

  4. Linux内存管理(最透彻的一篇)【转】

    转自:https://www.cnblogs.com/ralap7/p/9184773.html 摘要:本章首先以应用程序开发者的角度审视Linux的进程内存管理,在此基础上逐步深入到内核中讨论系统物 ...

  5. 【转】Linux内存管理(最透彻的一篇)

    摘要:本章首先以应用程序开发者的角度审视Linux的进程内存管理,在此基础上逐步深入到内核中讨论系统物理内存管理和内核内存的使用方法.力求从外到内.水到渠成地引导网友分析Linux的内存管理与使用.在 ...

  6. Linux内存管理—详细讲解

    摘要:本章首先以应用程序开发者的角度审视Linux的进程内存管理,在此基础上逐步深入到内核中讨论系统物理内存管理和内核内存的使用方法.力求从外到内.水到渠成地引导网友分析Linux的内存管理与使用.在 ...

  7. Linux内存管理 (1)物理内存初始化

    专题:Linux内存管理专题 关键词:用户内核空间划分.Node/Zone/Page.memblock.PGD/PUD/PMD/PTE.lowmem/highmem.ZONE_DMA/ZONE_NOR ...

  8. Linux下的物理内存管理2-slab缓存的管理

    2017-03-02 在Linux下的物理内存管理中,对SLAB机制大致做了介绍,对SLAB管理结构对象也做了介绍,但是对于小内存块的分配没有介绍,本节重点介绍下slab对小内存块的管理. 内核中使用 ...

  9. 浅谈Linux内存管理机制

    经常遇到一些刚接触Linux的新手会问内存占用怎么那么多?在Linux中经常发现空闲内存很少,似乎所有的内存都被系统占用了,表面感觉是内存不够用了,其实不然.这是Linux内存管理的一个优秀特性,在这 ...

随机推荐

  1. gispro发布vectortile笔记

    1.https://www.cnblogs.com/escage/p/6387529.html 矢量切片的作用.对于地图中的基础数据图层,或者数据量比较大的矢量图层,只是作渲染用.则需要制作矢量切片, ...

  2. workerman程序调试

    现象1 启动后报错类似如下: php start.php start PHP Warning: stream_socket_server(): unable to connect to tcp://x ...

  3. 正确把mysql数据库从windows迁移到linux系统上的方法

    (一)用mysqldump命令导出数据库文件: 在windows下cd到Mysql的bin目录: c:/data.txt这个目录和导出的文本名可以自己随便取,-B 后面的是表名,我要导出的表明叫use ...

  4. 【Hadoop学习之二】Hadoop伪分布式安装

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4       jdk8       hadoop-3.1.1 伪分布式就 ...

  5. Linux基础命令---查找进程id

    pidof pidof可以查找指定名称的进程的pid,将结果送到标准输出.pidof有两种返回值:0,找到至少一个进程:1,没有找到进程.pidof实际上与killall5相同:程序根据调用它的名称进 ...

  6. 典型 python 小练习

    #格式化输出 3方式a=input('user:').strip()print('%s'%a) #%s 占位符a1=[1,2,3]print(f'333{a1}早') #法二print('ss{0}k ...

  7. POJ 1018 Communication System (动态规划)

    We have received an order from Pizoor Communications Inc. for a special communication system. The sy ...

  8. python shutil模块简单介绍

    python shutil模块简单介绍 简介 shutil模块提供了大量的文件的高级操作.特别针对文件拷贝和删除,主要功能为目录和文件操作以及压缩操作. shutil 模块方法: copy(src, ...

  9. python+selenium win32gui实现文件上传 enumerate()

    upload = dr.find_element_by_id('exampleInputFile0') upload.click() time.sleep(1) # win32gui dialog = ...

  10. NFS客户端阻塞睡眠问题与配置调研

    Linux NFS客户端需要很小心地配置,否则在NFS服务器崩溃时,访问NFS的程序会被挂起,用ps查看,进程状态(STAT)处于D,意为(由于IO阻塞而进入)不可中断睡眠(如果是D+,+号表示程序运 ...