TA的每日心情 | 奋斗 昨天 09:33 |
---|
签到天数: 208 天 连续签到: 2 天 [LV.7]常住居民III
|
本帖最后由 andeyqi 于 2023-11-24 11:09 编辑
简介:
Linux 内核对每个物理页面采用struct page数据结构来描述,每个page 的大下通常是4K的方式来管理的,并将struct page 存储到一个全局的数组mem_map[],他们的对应关系是1:1的线性映射,内核里提供了很多page信息获取的api函数,我们基于此来读取下当前内存的状态信息。
使用如下测试代码:- #include <linux/version.h>
- #include <linux/module.h>
- #include <linux/init.h>
- #include <linux/mm.h>
- #define PRT(a, b) pr_info("%-15s=%10d %10ld %8ld\n", \
- a, b, (PAGE_SIZE*b)/1024, (PAGE_SIZE*b)/1024/1024)
- static int __init my_init(void)
- {
- struct page *p;
- unsigned long i, pfn, valid = 0;
- int free = 0, locked = 0, reserved = 0, swapcache = 0,
- referenced = 0, slab = 0, private = 0, uptodate = 0,
- dirty = 0, active = 0, writeback = 0, mappedtodisk = 0;
- unsigned long num_physpages;
- num_physpages = get_num_physpages();
- for (i = 0; i < num_physpages; i++) {
- /* Most of ARM systems have ARCH_PFN_OFFSET */
- pfn = i + ARCH_PFN_OFFSET;
- /* may be holes due to remapping */
- if (!pfn_valid(pfn))
- continue;
- valid++;
- p = pfn_to_page(pfn);
- if (!p)
- continue;
- /* page_count(page) == 0 is a free page. */
- if (!page_count(p)) {
- free++;
- continue;
- }
- if (PageLocked(p))
- locked++;
- if (PageReserved(p))
- reserved++;
- if (PageSwapCache(p))
- swapcache++;
- if (PageReferenced(p))
- referenced++;
- if (PageSlab(p))
- slab++;
- if (PagePrivate(p))
- private++;
- if (PageUptodate(p))
- uptodate++;
- if (PageDirty(p))
- dirty++;
- if (PageActive(p))
- active++;
- if (PageWriteback(p))
- writeback++;
- if (PageMappedToDisk(p))
- mappedtodisk++;
- }
- pr_info("\nExamining %ld pages (num_phys_pages) = %ld MB\n",
- num_physpages, num_physpages * PAGE_SIZE / 1024 / 1024);
- pr_info("Pages with valid PFN's=%ld, = %ld MB\n", valid,
- valid * PAGE_SIZE / 1024 / 1024);
- pr_info("\n Pages KB MB\n\n");
- PRT("free", free);
- PRT("locked", locked);
- PRT("reserved", reserved);
- PRT("swapcache", swapcache);
- PRT("referenced", referenced);
- PRT("slab", slab);
- PRT("private", private);
- PRT("uptodate", uptodate);
- PRT("dirty", dirty);
- PRT("active", active);
- PRT("writeback", writeback);
- PRT("mappedtodisk", mappedtodisk);
- return 0;
- }
- static void __exit my_exit(void)
- {
- pr_info("Module exit\n");
- }
- module_init(my_init);
- module_exit(my_exit);
复制代码
测试代码编译通过后拷贝至开发板运行结果如下:
从输出的信息可知- 共有page 262144*4k = 1024M
- 空闲page 188200*4k = 735M
- LOCK page = 0
- 保留的 page 24434*4k = 95M
- 交换分区的page = 0
- slab page 8855*4k = 34M
- private page 2890*4k = 11M
- dirty page 2274*4k = 8M
- mappedtodisk 31252 * 4k = 122M
输入下cat /proc/meminfo 查看下进行对比
因为内存信息是事实动态变化的,可以对比下两个数据基本都是在一个数量级的。
|
|