diff --git a/06_memory/dump_part01.txt b/06_memory/dump_part01.txt new file mode 100644 index 0000000..894e3d0 --- /dev/null +++ b/06_memory/dump_part01.txt @@ -0,0 +1,70 @@ +../bin/test_mem +malloc: It took 0 s, 0 ms, 0 µs, 675 ns, size 1 +calloc: It took 0 s, 0 ms, 0 µs, 878 ns, size 1 +alloca: It took 0 s, 0 ms, 0 µs, 101 ns, size 1 +malloc: It took 0 s, 0 ms, 0 µs, 158 ns, size 2 +calloc: It took 0 s, 0 ms, 0 µs, 192 ns, size 2 +alloca: It took 0 s, 0 ms, 0 µs, 65 ns, size 2 +malloc: It took 0 s, 0 ms, 0 µs, 66 ns, size 4 +calloc: It took 0 s, 0 ms, 0 µs, 181 ns, size 4 +alloca: It took 0 s, 0 ms, 0 µs, 68 ns, size 4 +malloc: It took 0 s, 0 ms, 0 µs, 68 ns, size 8 +calloc: It took 0 s, 0 ms, 0 µs, 174 ns, size 8 +alloca: It took 0 s, 0 ms, 0 µs, 67 ns, size 8 +malloc: It took 0 s, 0 ms, 0 µs, 138 ns, size 16 +calloc: It took 0 s, 0 ms, 0 µs, 258 ns, size 16 +alloca: It took 0 s, 0 ms, 0 µs, 67 ns, size 16 +malloc: It took 0 s, 0 ms, 0 µs, 126 ns, size 32 +calloc: It took 0 s, 0 ms, 0 µs, 228 ns, size 32 +alloca: It took 0 s, 0 ms, 0 µs, 68 ns, size 32 +malloc: It took 0 s, 0 ms, 0 µs, 85 ns, size 64 +calloc: It took 0 s, 0 ms, 0 µs, 214 ns, size 64 +alloca: It took 0 s, 0 ms, 0 µs, 67 ns, size 64 +malloc: It took 0 s, 0 ms, 0 µs, 105 ns, size 128 +calloc: It took 0 s, 0 ms, 0 µs, 548 ns, size 128 +alloca: It took 0 s, 0 ms, 0 µs, 67 ns, size 128 +malloc: It took 0 s, 0 ms, 0 µs, 90 ns, size 256 +calloc: It took 0 s, 0 ms, 0 µs, 225 ns, size 256 +alloca: It took 0 s, 0 ms, 0 µs, 66 ns, size 256 +malloc: It took 0 s, 0 ms, 0 µs, 127 ns, size 512 +calloc: It took 0 s, 0 ms, 0 µs, 286 ns, size 512 +alloca: It took 0 s, 0 ms, 0 µs, 69 ns, size 512 +malloc: It took 0 s, 0 ms, 0 µs, 165 ns, size 1024 +calloc: It took 0 s, 0 ms, 3 µs, 3081 ns, size 1024 +alloca: It took 0 s, 0 ms, 0 µs, 69 ns, size 1024 +malloc: It took 0 s, 0 ms, 0 µs, 136 ns, size 2048 +calloc: It took 0 s, 0 ms, 0 µs, 286 ns, size 2048 +alloca: It took 0 s, 0 ms, 0 µs, 68 ns, size 2048 +malloc: It took 0 s, 0 ms, 2 µs, 2773 ns, size 4096 +calloc: It took 0 s, 0 ms, 0 µs, 346 ns, size 4096 +alloca: It took 0 s, 0 ms, 2 µs, 2853 ns, size 4096 +malloc: It took 0 s, 0 ms, 4 µs, 4773 ns, size 8192 +calloc: It took 0 s, 0 ms, 0 µs, 431 ns, size 8192 +alloca: It took 0 s, 0 ms, 2 µs, 2678 ns, size 8192 +malloc: It took 0 s, 0 ms, 2 µs, 2552 ns, size 16384 +calloc: It took 0 s, 0 ms, 3 µs, 3564 ns, size 16384 +alloca: It took 0 s, 0 ms, 2 µs, 2747 ns, size 16384 +malloc: It took 0 s, 0 ms, 2 µs, 2739 ns, size 32768 +calloc: It took 0 s, 0 ms, 10 µs, 10208 ns, size 32768 +alloca: It took 0 s, 0 ms, 2 µs, 2680 ns, size 32768 +malloc: It took 0 s, 0 ms, 6 µs, 6609 ns, size 65536 +calloc: It took 0 s, 0 ms, 22 µs, 22841 ns, size 65536 +alloca: It took 0 s, 0 ms, 3 µs, 3782 ns, size 65536 +malloc: It took 0 s, 0 ms, 2 µs, 2604 ns, size 131072 +calloc: It took 0 s, 0 ms, 49 µs, 49530 ns, size 131072 +alloca: It took 0 s, 0 ms, 3 µs, 3044 ns, size 131072 +malloc: It took 0 s, 0 ms, 6 µs, 6497 ns, size 262144 +calloc: It took 0 s, 0 ms, 17 µs, 17151 ns, size 262144 +alloca: It took 0 s, 0 ms, 3 µs, 3739 ns, size 262144 +malloc: It took 0 s, 0 ms, 7 µs, 7814 ns, size 524288 +calloc: It took 0 s, 0 ms, 253 µs, 253306 ns, size 524288 +alloca: It took 0 s, 0 ms, 4 µs, 4085 ns, size 524288 +malloc: It took 0 s, 0 ms, 7 µs, 7261 ns, size 1048576 +calloc: It took 0 s, 0 ms, 267 µs, 267541 ns, size 1048576 +alloca: It took 0 s, 0 ms, 4 µs, 4989 ns, size 1048576 +malloc: It took 0 s, 0 ms, 7 µs, 7156 ns, size 2097152 +calloc: It took 0 s, 0 ms, 547 µs, 547159 ns, size 2097152 +alloca: It took 0 s, 0 ms, 5 µs, 5332 ns, size 2097152 +malloc: It took 0 s, 0 ms, 7 µs, 7051 ns, size 4194304 +calloc: It took 0 s, 1 ms, 1093 µs, 1093337 ns, size 4194304 +make: *** [Makefile:21: test] Segmentation fault (core dumped) diff --git a/06_memory/dump_part02.txt b/06_memory/dump_part02.txt new file mode 100644 index 0000000..bfba844 --- /dev/null +++ b/06_memory/dump_part02.txt @@ -0,0 +1,6 @@ +[ 1579.250693] test_module loaded +[ 1579.250694] al_name buf_size all_time fr_time +[ 1579.250695] kmalloc: 100, 59 ns, 35 ns +[ 1579.250695] kzalloc: 100, 50 ns, 26 ns +[ 1579.250705] vmalloc: 4096, 9280 ns, 17 ns +[ 1579.250705] get_free_pages: 0, 79 ns, 68 ns diff --git a/06_memory/part01/Makefile b/06_memory/part01/Makefile new file mode 100644 index 0000000..9f63ff5 --- /dev/null +++ b/06_memory/part01/Makefile @@ -0,0 +1,21 @@ +TARGET=test_mem +BIN=../bin + +CC=g++ +AM_CXXFLAGS = -std=c++11 + +all: bin $(TARGET) + +bin: + mkdir -p $(BIN) + +$(TARGET): $(TARGET).c + $(CC) -o $(BIN)/$@ $< + +clean: + @rm -vf $(BIN)/$(TARGET) *.o + +clear: clean + +test: + $(BIN)/$(TARGET) diff --git a/06_memory/part01/test_mem.c b/06_memory/part01/test_mem.c new file mode 100644 index 0000000..3ae9572 --- /dev/null +++ b/06_memory/part01/test_mem.c @@ -0,0 +1,87 @@ +//#include +//#include +#include +#include +#include +#include + +using namespace std::chrono; + +static size_t size = 1; + +static std::chrono::time_point start = + std::chrono::steady_clock::now(); + +void StartTimer() +{ + start = std::chrono::steady_clock::now(); +} + +void StopTimer(const char * name_timer) +{ + const auto end = std::chrono::steady_clock::now(); + std::cout + << name_timer << ": It took " + << (end - start) / 1s << " s, " + << (end - start) / 1ms << " ms, " + << std::chrono::duration_cast(end - start).count() << " µs, " + << std::chrono::duration_cast(end - start).count() << " ns, " + << " size " << size << "\n"; +} + + +int main(void) +{ + int counter=1000; + + while(1) + { + StartTimer(); + char *buf = (char*)malloc(size); + StopTimer("malloc"); + + if(!buf) + { + std::cout << "Error: size - " << size << std::endl; + break; + } + else { + //printf( "malloc size - %ld\n" , size); + free(buf); + } + + + StartTimer(); + buf = (char*)calloc(1, size); + StopTimer("calloc"); + + if(!buf) + { + std::cout << "Error: size - " << size << std::endl; + break; + } + else { + //printf( "calloc size - %ld\n" , size); + free(buf); + } + + StartTimer(); + buf = (char *)alloca(size); + StopTimer("alloca"); + + if(!buf) + { + std::cout << "Error: size - " << size << std::endl; + break; + } + else { + //printf( "alloca size - %ld\n" , size); + } + + size*=2; + + if(!counter--) + break; + } + return 0; +} diff --git a/06_memory/part02/Makefile b/06_memory/part02/Makefile new file mode 100644 index 0000000..b0bb16e --- /dev/null +++ b/06_memory/part02/Makefile @@ -0,0 +1,16 @@ +TARGET=test_mem +BIN=../bin + +obj-m = $(TARGET).o + +all: + make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules +clean: + make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean + +test: #all + sudo dmesg -C + - sudo insmod ./$(TARGET).ko + #sudo rmmod $(TARGET) + sudo dmesg + diff --git a/06_memory/part02/check.sh b/06_memory/part02/check.sh new file mode 100755 index 0000000..b30d7c3 --- /dev/null +++ b/06_memory/part02/check.sh @@ -0,0 +1,7 @@ +#!/bin/bash +set et ts=4 sts=4 sw=4 + +CURRENT=$(uname -r) +#BUILD_KERNEL=/lib/modules/$CURRENT/build +BUILD_KERNEL=~/projects/LinuxKernelCoursePro/kernel/buildroot-2021.02.7/output/build/linux-5.10.10/ +$BUILD_KERNEL/scripts/checkpatch.pl -f --no-tree ./test_mem.c #--fix diff --git a/06_memory/part02/test_mem.c b/06_memory/part02/test_mem.c new file mode 100644 index 0000000..1024e87 --- /dev/null +++ b/06_memory/part02/test_mem.c @@ -0,0 +1,160 @@ +#include +#include +#include +#include +#include +#include + +#define MODULE_TAG "test_module " +#define BUFFER_SIZE 100 + +static char *proc_buffer; +u64 start1, start2; +u64 end1, end2; +size_t size; + +static const char * const mem_cr_names[] = { + "kmalloc", + "kzalloc", + "vmalloc", + "get_free_pages", +}; + +enum mem_cr_type { + mem_cr_kmalloc = 0, + mem_cr_kzalloc = 1, + mem_cr_vmalloc = 2, + mem_cr_get_free_pages = 3, +}; + +void ShowHeader(void) +{ + pr_info("al_name \t\t buf_size \t all_time \t fr_time"); +} + +void ShowReport(const char *name_timer) +{ + pr_info("%s: \t %ld, \t\t %lld ns, \t %lld ns\n", + name_timer, size, end1-start1, end2-start2); +} + +void StartTimer1(void) +{ + start1 = ktime_get_ns(); +} + +void StopTimer1StartTimer2(void) +{ + end1 = ktime_get_ns(); + start2 = ktime_get_ns(); +} + +void StopTimer(void) +{ + end2 = ktime_get_ns(); +} + +static int create_buffer(int value) +{ + int ret = 0; + + switch (value) { + case mem_cr_kmalloc: + size = BUFFER_SIZE; + proc_buffer = kmalloc(size, GFP_KERNEL); + break; + case mem_cr_kzalloc: + size = BUFFER_SIZE; + proc_buffer = kzalloc(size, GFP_KERNEL); + break; + case mem_cr_vmalloc: + size = PAGE_SIZE; + proc_buffer = vmalloc(size); + break; + case mem_cr_get_free_pages: + size = 0; + proc_buffer = (char *)__get_free_pages(GFP_KERNEL, 0); + break; + } + if (proc_buffer == NULL) + ret = -ENOMEM; + return ret; +} + +static void cleanup_buffer(int value) +{ + if (proc_buffer) { + switch (value) { + case mem_cr_kmalloc: + kfree(proc_buffer); + break; + case mem_cr_kzalloc: + kfree(proc_buffer); + break; + case mem_cr_vmalloc: + vfree(proc_buffer); + break; + case mem_cr_get_free_pages: + free_pages((unsigned long)proc_buffer, 0); + break; + } + proc_buffer = NULL; + } +} + + +static int __init example_init(void) +{ + int err; + + pr_notice(MODULE_TAG "loaded\n"); + + ShowHeader(); + + StartTimer1(); + err = create_buffer(mem_cr_kmalloc); + StopTimer1StartTimer2(); + cleanup_buffer(mem_cr_kmalloc); + StopTimer(); + ShowReport(mem_cr_names[mem_cr_kmalloc]); + + StartTimer1(); + err = create_buffer(mem_cr_kzalloc); + StopTimer1StartTimer2(); + cleanup_buffer(mem_cr_kzalloc); + StopTimer(); + ShowReport(mem_cr_names[mem_cr_kzalloc]); + + StartTimer1(); + err = create_buffer(mem_cr_vmalloc); + StopTimer1StartTimer2(); + StopTimer(); + ShowReport(mem_cr_names[mem_cr_vmalloc]); + + StartTimer1(); + err = create_buffer(mem_cr_get_free_pages); + StopTimer1StartTimer2(); + cleanup_buffer(mem_cr_get_free_pages); + StopTimer(); + ShowReport(mem_cr_names[mem_cr_get_free_pages]); + + return -1; + +error: + pr_err(MODULE_TAG "failed to load\n"); + return err; +} + +static void __exit example_exit(void) +{ + pr_notice(MODULE_TAG "exited\n"); +} + +module_init(example_init); +module_exit(example_exit); + + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Vitaliy Zhyrko vitaliyzh@gmail.com"); +MODULE_DESCRIPTION("A simple example Linux module."); +MODULE_VERSION("0.01");