Skip to content

Instantly share code, notes, and snippets.

@masami256
Last active September 22, 2015 04:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masami256/7f23144b0ec68118fe72 to your computer and use it in GitHub Desktop.
Save masami256/7f23144b0ec68118fe72 to your computer and use it in GitHub Desktop.
kmem_cache_alloc_bulk() test
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/string.h>
MODULE_DESCRIPTION("bult test module");
MODULE_AUTHOR("masami256");
MODULE_LICENSE("GFP");
#define BULK_ALLOC_TEST_COUNT 100
#define BULK_ALLOC_TEST_ARRAY_SIZE 16
static int test_num;
module_param(test_num, int, 0);
MODULE_PARM_DESC(test_num, "Test number");
static char *test_name = "bulk";
module_param(test_name, charp, 0000);
MODULE_PARM_DESC(test_type, "Test name");
struct bulk_cache {
int n;
char msg[8];
};
static struct kmem_cache *bulk_cachep;
static inline u64 bulk_alloc_test_total_clock(u64 start, u64 end)
{
return end - start;
}
static void bulk_alloc_test_bulk_alloc(void)
{
struct bulk_cache *p[BULK_ALLOC_TEST_ARRAY_SIZE];
bool ret;
u64 start, end;
rdtscll(start);
ret = kmem_cache_alloc_bulk(bulk_cachep, GFP_KERNEL, BULK_ALLOC_TEST_ARRAY_SIZE, (void **) &p);
if (!ret) {
pr_warn("%s:%d: faild to allocate memory\n", __func__, __LINE__);
return ;
}
rdtscll(end);
pr_info("bulk_test: test %d: kmem_cache_alloc_bulk() %lld\n", test_num, bulk_alloc_test_total_clock(start, end));
kmem_cache_free_bulk(bulk_cachep, BULK_ALLOC_TEST_ARRAY_SIZE, (void **) &p);
}
static void bulk_alloc_test_slab_alloc(void)
{
int i, j;
struct bulk_cache *p[BULK_ALLOC_TEST_ARRAY_SIZE];
u64 start, end;
rdtscll(start);
for (i = 0; i < BULK_ALLOC_TEST_ARRAY_SIZE; i++) {
p[i] = kmem_cache_alloc(bulk_cachep, GFP_KERNEL);
if (!p[i]) {
pr_warn("%s:%d: faild to allocate memory\n", __func__, __LINE__);
goto out;
}
}
rdtscll(end);
pr_info("bulk_test: test %d: kmem_cache_alloc() %lld\n", test_num, bulk_alloc_test_total_clock(start, end));
out:
for (j = 0; j < i; j++)
kmem_cache_free(bulk_cachep, p[j]);
}
static int bulk_alloc_init(void)
{
bulk_cachep = KMEM_CACHE(bulk_cache, SLAB_PANIC);
pr_info("cache created: 0x%p\n", bulk_cachep);
if (!strcmp(test_name, "bulk"))
bulk_alloc_test_bulk_alloc();
else
bulk_alloc_test_slab_alloc();
return 0;
}
static void bulk_alloc_cleanup(void)
{
kmem_cache_destroy(bulk_cachep);
pr_info("cache freed\n");
}
module_init(bulk_alloc_init);
module_exit(bulk_alloc_cleanup);
KERNDIR := /lib/modules/`uname -r`/build
BUILD_DIR := $(shell pwd)
VERBOSE = 0
obj-m := bulk_alloc.o
smallmod-objs := bulk_alloc.o
all:
make -C $(KERNDIR) SUBDIRS=$(BUILD_DIR) KBUILD_VERBOSE=$(VERBOSE) modules
clean:
rm -f *.o
rm -f *.ko
rm -f *.mod.c
rm -f *~
#!/bin/bash
test_name="bulk"
if [ $# -eq 1 ]; then
test_name=$1
fi
for ((i=0; i<100; i++));
do
echo "run test ${i}"
insmod ./bulk_alloc.ko test_num=${i} test_name=${test_name}
rmmod bulk_alloc
done
dmesg | grep bulk_test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment