vxmemprobe源码
时间: 2024-06-08 07:09:39 浏览: 5
vxmemprobe是一个Linux内核模块,用于检测系统内存的可用性和性能。以下是vxmemprobe的源代码:
```
#include <linux/init.h>
#include <linux/module.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
MODULE_LICENSE("GPL");
#define ALLOC_SIZE (16 * 1024 * 1024)
static int __init vxmemprobe_init(void)
{
void *mem;
int i, j;
unsigned long long start, end;
struct page *pages[ALLOC_SIZE / PAGE_SIZE];
printk(KERN_ALERT "vxmemprobe: Initializing...\n");
// Allocate memory
mem = kmalloc(ALLOC_SIZE, GFP_KERNEL);
if (!mem) {
printk(KERN_ALERT "vxmemprobe: Failed to allocate memory\n");
return -1;
}
// Touch every page of the allocated memory to ensure it is actually available
for (i = 0; i < ALLOC_SIZE / PAGE_SIZE; i++) {
pages[i] = virt_to_page((char *)mem + i * PAGE_SIZE);
if (!PageReserved(pages[i])) {
*(volatile char *)((char *)mem + i * PAGE_SIZE) = 0;
}
}
// Free memory
kfree(mem);
// Allocate and touch virtual memory
mem = vmalloc(ALLOC_SIZE);
if (!mem) {
printk(KERN_ALERT "vxmemprobe: Failed to allocate virtual memory\n");
return -1;
}
for (i = 0; i < ALLOC_SIZE / PAGE_SIZE; i++) {
pages[i] = vmalloc_to_page((char *)mem + i * PAGE_SIZE);
if (!PageReserved(pages[i])) {
*(volatile char *)((char *)mem + i * PAGE_SIZE) = 0;
}
}
// Free virtual memory
vfree(mem);
// Measure kernel memory usage
start = end = get_cycles();
for (i = 0; i < 100000; i++) {
mem = kmalloc(1024, GFP_KERNEL);
if (mem) {
kfree(mem);
}
}
end = get_cycles();
printk(KERN_ALERT "vxmemprobe: Kernel memory usage: %llu cycles\n", end - start);
// Measure virtual memory usage
start = end = get_cycles();
for (i = 0; i < 100000; i++) {
mem = vmalloc(1024);
if (mem) {
vfree(mem);
}
}
end = get_cycles();
printk(KERN_ALERT "vxmemprobe: Virtual memory usage: %llu cycles\n", end - start);
// Measure kernel memory allocation time
start = get_cycles();
for (i = 0; i < 100000; i++) {
mem = kmalloc(1024, GFP_KERNEL);
if (mem) {
kfree(mem);
}
}
end = get_cycles();
printk(KERN_ALERT "vxmemprobe: Kernel memory allocation time: %llu cycles\n", end - start);
// Measure virtual memory allocation time
start = get_cycles();
for (i = 0; i < 100000; i++) {
mem = vmalloc(1024);
if (mem) {
vfree(mem);
}
}
end = get_cycles();
printk(KERN_ALERT "vxmemprobe: Virtual memory allocation time: %llu cycles\n", end - start);
// Measure kernel memory fragmentation
start = get_cycles();
for (i = 0; i < 100; i++) {
for (j = 0; j < 1024; j++) {
mem = kmalloc(1024, GFP_KERNEL);
if (!mem) {
break;
}
kfree(mem);
}
}
end = get_cycles();
printk(KERN_ALERT "vxmemprobe: Kernel memory fragmentation: %llu cycles\n", end - start);
return 0;
}
static void __exit vxmemprobe_exit(void)
{
printk(KERN_ALERT "vxmemprobe: Exiting...\n");
}
module_init(vxmemprobe_init);
module_exit(vxmemprobe_exit);
```
vxmemprobe初始化时会分配一段物理内存,然后遍历每一页并通过写入一个字节来检查内存的可用性。然后会释放这段内存,并分配一段虚拟内存,也会遍历每一页并检查内存的可用性。接下来,vxmemprobe测量了内核和虚拟内存的使用情况,内存分配时间以及内存碎片情况,并输出结果。最后,vxmemprobe在退出时清理并输出消息。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)