Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions 04_basic_struct/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Basic structure homework
Implement object with name “MyObject” which is parent of kernel_kobj.
Object should include linked_list structure.
This object should contain sysfs attribute with name “list”.
On read form attribute “list” it should show content of the objects linked list.
On write to attribute “list” it should add new string to the objects linked list.
!! Do not forget properly free all the resources during rmmod.
10 changes: 10 additions & 0 deletions 05_timers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## Homework: Linux Kernel Time Management

1. Implement program which return absolute time in user space.
Use clock_gettime() from time.h. Try different clock id.
Find the difference. Show possible clock resolution provided by clock_getres().

2. Implement kernel module with API in sysfs, which returns relative
time in maximum possible resolution passed since previous read of it.
Implement kernel module with API in sysfs which returns absolute time
of previous reading with maximum resolution like ‘400.123567’ seconds.
22 changes: 22 additions & 0 deletions 06_memory/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Memory management

## Homework
1. Create user-space C or C++ program which tries to allocate buffers
with sizes 2^x for x in range from 0 to maximium possible value
using functions:
**malloc, calloc, alloca, (optional for C++) new **.
Measure time of each allocation/freeing.
2^x means x power of 2 in this task.
Pull request should contains program source code and program output
in text format.

2. Create kernel module and test allocation/freeing time for functions:
**kmalloc, kzmalloc, vmalloc, get_free_pages,
(optional and only for drivers integrated to kernel)alloc_bootmem**.
Measure the time of each allocation/freeing except alloc_bootmem.
The results should be presented in text file table with followed columns:
Buffer size, allocation time, freeing time.
Size unit is 1 byte, time unit is 1 ns.

Pull request should contains source code of developed driver, Makefile
and program output from system log in text format.
11 changes: 11 additions & 0 deletions 06_memory/kmemtime/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
KERNELDIR ?= ../../../buildroot/output/build/linux-5.15/ #WARNING relative path

obj-m := kmemtime.o
CFLAGS_kmemtime.o := -DDEBUG -std=gnu11 -Wno-declaration-after-statement

all:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

clean:
$(MAKE) -C $(KERNELDIR) M=$(PWD) clean

179 changes: 179 additions & 0 deletions 06_memory/kmemtime/kmemtime.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
// SPDX-License-Identifier: GPL-2.0

/**
* @brief Kernel module to measure time spent for different type of memory allocators
*/

#include <linux/math.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/time.h>
#include <linux/ktime.h>
#include <linux/timekeeping.h>
#include <linux/string.h>
#include <asm/bug.h>
#include <asm/page.h>

/* Max n for 2^n to allocate the buffer */
#define KMEMTIME_TESTS_MAX_N 10
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to allocate memory up to allocation error?


enum kmem_type {
e_malloc,
e_calloc,
e_get_free_pages,
e_alloca,
};

struct kmemtime {
enum kmem_type type;
const char *alloc_name;
const char *free_name;
int buff_size[KMEMTIME_TESTS_MAX_N];
u64 time_alloc[KMEMTIME_TESTS_MAX_N];
u64 time_free[KMEMTIME_TESTS_MAX_N];
};

static struct kmemtime kmemtime_test[] = {
{ .type = e_malloc, .alloc_name = "kmalloc ", .free_name = "kfree " },
{ .type = e_calloc, .alloc_name = "vmalloc ", .free_name = "vfree " },
{ .type = e_get_free_pages, .alloc_name = "get_free_pages", .free_name = "free_pages" },
{ .type = e_alloca, .alloc_name = "kzalloc ", .free_name = "kfree " },
};

static char *memtime_print_result(const struct kmemtime *result)
{
const char *size_str = "Buffer size, bytes |";
int buff_pos = 0;
char *table;

/* Estimate buffer size */
const int header_len = snprintf(NULL, 0, "%s %s, ns | %s, ns |\n",
size_str, result->alloc_name, result->free_name);

/* Table columns width for data printing. -2 for '|' and space characters */
const int buff_size_width = strlen(size_str) - 2;
const int alloc_time_width = strlen(result->alloc_name) + strlen(", ns | ") - 2;
const int free_time_width = strlen(result->free_name) + strlen(", ns | ") - 2;
const int row_len = snprintf(NULL, 0, "%*d |%*llu |%*llu |\n",
buff_size_width, result->buff_size[0],
alloc_time_width, result->time_alloc[0],
free_time_width, result->time_free[0]);

/* +1 for '\0' */
const size_t table_len = header_len + row_len * KMEMTIME_TESTS_MAX_N + 1;

table = kmalloc(table_len, GFP_KERNEL);
if (table == NULL)
return NULL;

table[0] = '\0';

buff_pos = snprintf(table, table_len, "%s %s, ns | %s, ns |\n",
size_str, result->alloc_name, result->free_name);

for (int i = 0; i < KMEMTIME_TESTS_MAX_N; ++i) {
buff_pos += snprintf(&table[buff_pos], table_len - buff_pos, "%*d |%*llu |%*llu |\n",
buff_size_width, result->buff_size[i],
alloc_time_width, result->time_alloc[i],
free_time_width, result->time_free[i]);
}

return table;
}

static void memtime_get_result(struct kmemtime *result)
{
int *test_ptr;
int buff_size;
unsigned long page_addr;
u64 alloc_start;
u64 alloc_end;
u64 free_end;

for (int i = 0; i < KMEMTIME_TESTS_MAX_N; ++i) {
buff_size = int_pow(2, i);
result->buff_size[i] = buff_size;

switch (result->type) {
case e_malloc:
alloc_start = ktime_get_ns();
test_ptr = kmalloc(buff_size, GFP_KERNEL);
BUG_ON(test_ptr == NULL);
alloc_end = ktime_get_ns();
kfree(test_ptr);
free_end = ktime_get_ns();
break;

case e_alloca:
alloc_start = ktime_get_ns();
test_ptr = kzalloc(buff_size, GFP_KERNEL);
BUG_ON(test_ptr == NULL);
alloc_end = ktime_get_ns();
kfree(test_ptr);
free_end = ktime_get_ns();
break;

case e_calloc:
alloc_start = ktime_get_ns();
test_ptr = vmalloc(buff_size);
BUG_ON(test_ptr == NULL);
alloc_end = ktime_get_ns();
vfree(test_ptr);
free_end = ktime_get_ns();
break;

case e_get_free_pages:
/* Overwrite buff size for __get_free_pages */
result->buff_size[i] = int_pow(2, i) * PAGE_SIZE;
alloc_start = ktime_get_ns();
page_addr = __get_free_pages(GFP_KERNEL, i);
alloc_end = ktime_get_ns();
free_pages(page_addr, i);
free_end = ktime_get_ns();
break;

default:
BUG();
break;
}

result->time_alloc[i] = alloc_end - alloc_start;
result->time_free[i] = free_end - alloc_end;
pr_debug("time %lld nanoseconds for %s\n", result->time_alloc[i], result->alloc_name);
pr_debug("Free time %lld nanoseconds for %s\n", result->time_free[i], result->free_name);
}
}

int kmemtime_init(void)
{
char *res;

for (int i = 0; i < ARRAY_SIZE(kmemtime_test); ++i) {
memtime_get_result(&kmemtime_test[i]);
res = memtime_print_result(&kmemtime_test[i]);
if (res == NULL) {
BUG();
return -ENOMEM;
}

pr_info("%s\n", res);
kfree(res);
}

return 0;
}

void kmemtime_exit(void)
{

}

module_init(kmemtime_init);
module_exit(kmemtime_exit);

MODULE_DESCRIPTION("kmemtime kernel module");
MODULE_AUTHOR("Sergey D.");
MODULE_LICENSE("GPL");
47 changes: 47 additions & 0 deletions 06_memory/kmemtime/results.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
Buffer size, bytes | kmalloc , ns | kfree , ns |
1 | 15824 | 6420 |
2 | 564 | 288 |
4 | 242 | 213 |
8 | 235 | 191 |
16 | 270 | 258 |
32 | 231 | 199 |
64 | 231 | 196 |
128 | 615 | 257 |
256 | 751 | 569 |
512 | 601 | 251 |

Buffer size, bytes | vmalloc , ns | vfree , ns |
1 | 41907 | 13412 |
2 | 5914 | 2487 |
4 | 2933 | 2030 |
8 | 2656375 | 19768 |
16 | 12440 | 3048 |
32 | 2856 | 1675 |
64 | 2144 | 1430 |
128 | 1857 | 1668 |
256 | 1794 | 1336 |
512 | 1660 | 1361 |

Buffer size, bytes | get_free_pages, ns | free_pages, ns |
4096 | 12029 | 59528 |
8192 | 5866 | 3687 |
16384 | 960 | 680 |
32768 | 1649 | 617 |
65536 | 10293 | 17811 |
131072 | 1827 | 887 |
262144 | 847 | 786 |
524288 | 12730 | 1472 |
1048576 | 6729 | 1938 |
2097152 | 31621 | 5070 |

Buffer size, bytes | kzalloc , ns | kfree , ns |
1 | 25354 | 13805 |
2 | 432 | 234 |
4 | 245 | 174 |
8 | 214 | 175 |
16 | 5389 | 597 |
32 | 3468 | 1812 |
64 | 846 | 562 |
128 | 1819 | 415 |
256 | 1266 | 226 |
512 | 1562 | 431 |
14 changes: 14 additions & 0 deletions 06_memory/memtime/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CC=gcc
CC_FLAGS=-std=gnu11 -Wall

CC_SOURCES=memtime.c
TARGET_NAME=memtime

$(TARGET_NAME):
$(CC) $(CC_FLAGS) -o $@ $(CC_SOURCES) -lm

all: clean $(TARGET_NAME)

clean:
-$(RM) -rf *.o $(TARGET_NAME)

Loading