memory pools
cache memory에 이어 memory pools를 실습했다. 역시 책만 보면 잘 모른다. nvme 드라이버- drivers/nvme/target/io-cmd-file.c – 중 mempool 관련 코드를 찾을 수 있다.
#include<linux/kernel.h>
#include<linux/init.h>
#include<linux/module.h>
#include <linux/slab.h>
#include <linux/mempool.h>
#define scullc_quantum 16
/* declare one cache pointer: use it for all devices */
/* kmem_cache_t 가 kmem_cache로 변경됨*/
struct kmem_cache *scullc_cache;
static const char test[scullc_quantum]="123456789abcde";
static char* data;
mempool_t *mem_pool1;
static int __init hello_world_init(void) /* Constructor */
{
int i;
printk(KERN_INFO "hello, pool memory\n");
/* memory 할당*/
scullc_cache = kmem_cache_create("scullc", scullc_quantum*sizeof(char),
0, SLAB_HWCACHE_ALIGN, NULL); /* no ctor */
if (!scullc_cache) {
printk(KERN_ERR "no memory. out.\n");
return -ENOMEM;
}
/* memory pool로 allocate*/
mem_pool1 = mempool_create(16, mempool_alloc_slab, mempool_free_slab,
scullc_cache);
//data = kmem_cache_alloc(scullc_cache, GFP_KERNEL);
data = mempool_alloc(mem_pool1, GFP_KERNEL);
for (i=0;i<scullc_quantum;i++){
data[i] = test[i];
}
printk(KERN_INFO "cache: pos: %p, size: %d\n",scullc_cache, sizeof(scullc_cache));
printk(KERN_INFO "data: pos:%p, size: %d, %s\n",data, sizeof(data), data);
return 0;
}
void __exit hello_world_exit(void)
{
//kmem_cache_free(scullc_cache, data);
mempool_free(data, mem_pool1);
mempool_destroy(mem_pool1);
kmem_cache_destroy(scullc_cache);
printk(KERN_INFO "good bye, pool memory\n");
}
module_init(hello_world_init);
module_exit(hello_world_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("now0930");
MODULE_DESCRIPTION("cache memory test");
pi@raspberrypi:~/rasp/advancedMemory/mempool $ make #make -C /lib/modules/5.10.63-v7l+/build V=1 M=/home/pi/rasp/advancedMemory/mempool modules make -C /lib/modules/`uname -r`/build M=`pwd` modules make[1]: 디렉터리 '/usr/src/linux-headers-5.10.63-v7l+' 들어감 make[1]: 디렉터리 '/usr/src/linux-headers-5.10.63-v7l+' 나감 pi@raspberrypi:~/rasp/advancedMemory/mempool $ sudo insmod memory_pool.ko pi@raspberrypi:~/rasp/advancedMemory/mempool $ sudo rmmod memory_pool pi@raspberrypi:~/rasp/advancedMemory/mempool $ dmesg | tail -10 [52590.309504] hello, pool memory [52590.309579] cache: pos: 0c17faff, size: 4 [52590.309598] data: pos:f97bc0b2, size: 4, 123456789abcde [52601.784069] good bye, pool memory