Skip to content

Commit

Permalink
add slab test code
Browse files Browse the repository at this point in the history
  • Loading branch information
redstar9451 committed Feb 11, 2018
1 parent 0bf8324 commit 3bb1b16
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,9 @@ vexpress_ca9x4:busybox vexpress_kernel
samples:
make -C samples/compile-module
make -C samples/slab
-mkdir $(KBUILD_OUTPUT_BUSYBOX) -p
-rm -rf $(KBUILD_OUTPUT_BUSYBOX)/_install
make -C $(KBUILD_OUTPUT_BUSYBOX) install
find $(CURDIR)/build/samples -name "*.ko" | xargs cp -v -t $(KBUILD_OUTPUT_BUSYBOX)/_install
fakeroot ./make-rootfs.sh $(KBUILD_OUTPUT_BUSYBOX) $(CURDIR)/busybox-1.28.0

53 changes: 46 additions & 7 deletions samples/slab/samples-slab.c
Original file line number Diff line number Diff line change
@@ -1,18 +1,57 @@
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>

MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)

struct kmem_cache *test_cache = NULL;
#define TEST_CACHE_SIZE 128
char *str[16] = {0};

void slab_ctor(void *data)
{
static int index = 0;
char *str = data;
memset(str, 0, TEST_CACHE_SIZE);
printk(KERN_ERR "slab actor, index=%d\n", index);
sprintf(str, "Hello, this is a cache entry, index = %d", index++);
return;
}

static __init int slab_init(void)
{
printk(KERN_ALERT "Hello World enter\n");
test_cache = kmem_cache_create("test cache", TEST_CACHE_SIZE, 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, slab_ctor);
if (!test_cache)
{
printk(KERN_ERR "fail to create slab cache\n");
return 0;
}
printk (KERN_ERR "before kmem_cache_alloc\n");
str[0] = kmem_cache_alloc(test_cache, GFP_KERNEL); /* 首次分配时,会预分配出多个,目前测试的情况为21个 */
printk(KERN_ERR "after kmem_cache_alloc\n");
printk(KERN_ERR "str: %s\n", str[0]);

printk (KERN_ERR "before kmem_cache_alloc\n");
str[1] = kmem_cache_alloc(test_cache, GFP_KERNEL);
printk(KERN_ERR "after kmem_cache_alloc\n");
printk(KERN_ERR "str: %s\n", str[1]);
return 0;
}
static void hello_exit(void)

static __exit void slab_exit(void)
{
printk(KERN_ALERT "Hello World exit\n");
/* kmem_cache_destroy can handle NULL pointer, no need to validate the param*/
kmem_cache_destroy(NULL);
/* before destroy kmem cache, the cache will be destroyed must has no allocated entries,
* or you will get a stack dump, and fail to destroy */
kmem_cache_free(test_cache, str[0]);
kmem_cache_free(test_cache, str[1]);
kmem_cache_destroy(test_cache);
printk(KERN_ERR "slab exit\n");
}

module_init(hello_init);
module_exit(hello_exit);
module_init(slab_init);
module_exit(slab_exit);
MODULE_AUTHOR("redstar");
MODULE_DESCRIPTION("A Sample Hello World Module");
MODULE_DESCRIPTION("Slab test module");
MODULE_ALIAS("A Sample module");

0 comments on commit 3bb1b16

Please sign in to comment.