[태그:] memory

  • linux pages

    Pages

    연속된 메모리를 할당을 테스트 했다. dmes로 보면 어느 정도 출력하다 끊어 버린다. 마지막 데이터를 확인 했다.

    #include<linux/kernel.h>
    #include<linux/init.h>
    #include<linux/module.h>
    #include <linux/slab.h>
    #include <linux/mempool.h>
    
    #define PAGES_ORDER 2
    
    char* data;
    static int __init hello_world_init(void) /* Constructor */
    {
    	printk(KERN_INFO "hello, pages\n");
    	/* memory 할당*/
    	data = (char*)__get_free_pages(GFP_KERNEL, PAGES_ORDER);
    	if (!data){
    		printk("no pages allocated\n");
    		return ENOMEM;
    	}
    	/* 0으로 초기화*/
    	memset(data, 'c', PAGE_SIZE << PAGES_ORDER); 
    	printk(KERN_INFO "data: pos:%p, size: %0xd, %d\n", data, (int)(PAGE_SIZE << PAGES_ORDER), data[(PAGE_SIZE << PAGES_ORDER)-1]);
    	return 0;
    }
    
    void __exit hello_world_exit(void)
    {
    	printk(KERN_INFO "good bye, pages\n");
    	free_pages((unsigned long)data, PAGES_ORDER);
    
    }
    module_init(hello_world_init);
    module_exit(hello_world_exit);
    
    MODULE_LICENSE("GPL");
    MODULE_AUTHOR("now0930");
    MODULE_DESCRIPTION("pages test");
    pi@raspberrypi:~/rasp/advancedMemory/pages $ dmesg | tail -10
    [ 1104.594762] hello, pages
    [ 1104.594802] data: pos:c32289cc, size: 4000d, 99
    [ 1122.403587] good bye, pages
  • linux memory pools

    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
  • linux cache memory

    cache memroy 실습

    linux device driver, 8장을 실습 했다. 컴파일 하여 실행하기 전에는 어떻게 돌아가는지 잘 알 수 없다. scullc을 보고 따라하려 했으나 잘 모르겠다. 메모리를 cache로 선언하고 일반 메모리를 쓰듯이 하면 된다.

    #include<linux/kernel.h>
    #include<linux/init.h>
    #include<linux/module.h>
    #include <linux/slab.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;
    static int __init hello_world_init(void) /* Constructor */
    {
    	int i;
    	printk(KERN_INFO "hello, cache 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;
    
    	}
    	/* allocate*/
    	data = kmem_cache_alloc(scullc_cache, 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);
    	kmem_cache_destroy(scullc_cache);
    	printk(KERN_INFO "good bye, cache memory\n");
    
    }
    module_init(hello_world_init);
    module_exit(hello_world_exit);
    MODULE_LICENSE("GPL");
    MODULE_AUTHOR("now0930");
    MODULE_DESCRIPTION("cache memory test");

    cache 메모리를 얼마나 사용하고 있는지 알아보는 유틸리티가 있을 것 같은데 못 찾았다.

    참조 사이트