#include #include #include #include #include /* Meta Information */ MODULE_LICENSE("GPL"); // declare variables static unsigned int i = 0; static char ** buffer; static int loop_cnt; static int alloc_size; // declare module parameters module_param(loop_cnt, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); module_param(alloc_size, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); static int __init initmodule(void) { // get size of void pointer later returned by kmalloc(...) int size = sizeof(void*); /* init buffer array for loop_cnt elements with size of void* |-|-|-| */ buffer = kmalloc_array(loop_cnt, size, GFP_KERNEL); printk(KERN_DEBUG "Allocating %d times the memory size of %d\n", loop_cnt, alloc_size); // stop total time long long totalStart; long long totalStop; totalStart = rdtsc(); // allocate loop_ctn times the memory of size alloc_size while (i < loop_cnt) { long long start; long long stop; start = rdtsc(); // allocate size of alloc_size and assign void* to buffer[i] buffer[i] = kmalloc(alloc_size, GFP_KERNEL); stop = rdtsc(); printk(KERN_DEBUG "%i: It took %lld ticks to allocate the memory\n", i, stop - start); i++; } totalStop = rdtsc(); printk(KERN_DEBUG "Total: It took %lld ticks to allocate the memory\n", totalStop - totalStart); return 0; } static void __exit moduleExit(void) { while (i > 0) { kfree(buffer[i]); printk(KERN_DEBUG "Cleared\n"); i--; } } module_init(initmodule); module_exit(moduleExit);