diff --git a/memory/README.md b/memory/README.md new file mode 100644 index 0000000..4c22613 --- /dev/null +++ b/memory/README.md @@ -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. diff --git a/memory/uapi_log.txt b/memory/uapi_log.txt new file mode 100644 index 0000000..e03f483 --- /dev/null +++ b/memory/uapi_log.txt @@ -0,0 +1,103 @@ + +*--------------------------*malloc()*--------------------------* +Power Buffer size (bytes) Allocation (ns) Freeing (ns) +0 1 649 223 +1 2 110 120 +2 4 53 58 +3 8 52 82 +4 16 108 58 +5 32 183 57 +6 64 101 58 +7 128 143 58 +8 256 100 57 +9 512 146 55 +10 1024 206 58 +11 2048 2970 431 +12 4096 160 100 +13 8192 2826 68 +14 16384 2754 68 +15 32768 2724 68 +16 65536 2852 67 +17 131072 6841 8657 +18 262144 4490 3839 +19 524288 4180 3779 +20 1048576 4142 4555 +21 2097152 6224 6032 +22 4194304 6688 4598 +23 8388608 5313 4679 +24 16777216 5107 4498 +25 33554432 5370 4641 +26 67108864 6076 5578 +27 134217728 5591 5623 +28 268435456 5488 6688 +29 536870912 5458 8778 +30 1073741824 5500 13123 +31 2147483648 5468 13146 +32 4294967296 5468 13356 + +MEAN 3137 3432 + +*--------------------------*calloc()*--------------------------* +Power Buffer size (bytes) Allocation (ns) Freeing (ns) +0 1 509 183 +1 2 173 83 +2 4 98 60 +3 8 98 57 +4 16 160 58 +5 32 163 58 +6 64 161 55 +7 128 311 63 +8 256 3869 67 +9 512 221 65 +10 1024 276 65 +11 2048 263 243 +12 4096 441 115 +13 8192 3664 133 +14 16384 3736 75 +15 32768 10795 100 +16 65536 25071 108 +17 131072 63415 193 +18 262144 156469 248 +19 524288 47117 238 +20 1048576 398067 323 +21 2097152 498210 121 +22 4194304 1010692 193 +23 8388608 2510674 1403 +24 16777216 6655518 880 +25 33554432 16141 14827 +26 67108864 5996 4857 +27 134217728 4162 4464 +28 268435456 4043 4852 +29 536870912 4058 6525 +30 1073741824 4071 9636 +31 2147483648 4052 9662 +32 4294967296 4147 9730 + +MEAN 346570 2113 + +*--------------------------*alloca()*--------------------------* +Power Buffer size (bytes) Allocation (ns) +0 1 80 +1 2 54 +2 4 54 +3 8 54 +4 16 54 +5 32 56 +6 64 54 +7 128 54 +8 256 54 +9 512 56 +10 1024 54 +11 2048 52 +12 4096 2646 +13 8192 2107 +14 16384 3320 +15 32768 2153 +16 65536 3300 +17 131072 2331 +18 262144 2336 +19 524288 2285 +20 1048576 3049 +21 2097152 2777 + +MEAN 1226 diff --git a/memory/uapi_memory.c b/memory/uapi_memory.c new file mode 100644 index 0000000..857169e --- /dev/null +++ b/memory/uapi_memory.c @@ -0,0 +1,140 @@ +#include +#include +#include +#include + +#define ALLOC_COEF .9F //For controling the stack memory allocation + +long get_interval(struct timespec *ts1, struct timespec *ts2) +{ + return 1000000000 * (ts2->tv_sec - ts1->tv_sec) + + (ts2->tv_nsec - ts1->tv_nsec); +} + +static inline void malloc_test(void) +{ + struct timespec ts1, ts2; + void *restrict pdata = NULL; + long mean_alloc = 0; + long mean_free = 0; + long curr_alloc = 0; + long curr_free = 0; + long inter_count = 0; + + printf("\n*--------------------------*malloc()*--------------------------*\n"); + printf("%-8s%-25s%-20s%-20s\n", "Power", "Buffer size (bytes)", "Allocation (ns)", "Freeing (ns)"); + size_t alloc_size = 1; + do { + clock_gettime(CLOCK_REALTIME, &ts1); + pdata = malloc(alloc_size); + clock_gettime(CLOCK_REALTIME, &ts2); + + if (!pdata) + break; + + curr_alloc = get_interval(&ts1, &ts2); + mean_alloc += curr_alloc; + + clock_gettime(CLOCK_REALTIME, &ts1); + free(pdata); + clock_gettime(CLOCK_REALTIME, &ts2); + + curr_free = get_interval(&ts1, &ts2); + mean_free += curr_free; + + printf("%-8ld%-25lu%-20ld%-20ld\n", inter_count, alloc_size, curr_alloc, curr_free); + + ++inter_count; + } while(alloc_size *= 2); + + mean_alloc /= inter_count; + mean_free /= inter_count; + printf("\n%-33s%-20ld%-20ld\n", "MEAN", mean_alloc, mean_free); +} + +static inline void calloc_test(void) +{ + struct timespec ts1, ts2; + void *restrict pdata = NULL; + long mean_alloc = 0; + long mean_free = 0; + long curr_alloc = 0; + long curr_free = 0; + long inter_count = 0; + + printf("\n*--------------------------*calloc()*--------------------------*\n"); + printf("%-8s%-25s%-20s%-20s\n", "Power", "Buffer size (bytes)", "Allocation (ns)", "Freeing (ns)"); + size_t alloc_size = 1; + do { + clock_gettime(CLOCK_REALTIME, &ts1); + pdata = calloc(alloc_size, 1); + clock_gettime(CLOCK_REALTIME, &ts2); + + if (!pdata) + break; + + curr_alloc = get_interval(&ts1, &ts2); + mean_alloc += curr_alloc; + + clock_gettime(CLOCK_REALTIME, &ts1); + free(pdata); + clock_gettime(CLOCK_REALTIME, &ts2); + + curr_free = get_interval(&ts1, &ts2); + mean_free += curr_free; + + printf("%-8ld%-25lu%-20ld%-20ld\n", inter_count, alloc_size, curr_alloc, curr_free); + + ++inter_count; + } while(alloc_size *= 2); + + mean_alloc /= inter_count; + mean_free /= inter_count; + printf("\n%-33s%-20ld%-20ld\n", "MEAN", mean_alloc, mean_free); +} + +static inline void alloca_test(void) +{ + struct timespec ts1, ts2; + void *restrict pdata = NULL; + long mean_alloc = 0; + long curr_alloc = 0; + long inter_count = 0; + ssize_t stack_rem = 0; + pthread_attr_t pth_attr; + + pthread_attr_init(&pth_attr); + pthread_attr_getstacksize(&pth_attr, &stack_rem); + stack_rem *= ALLOC_COEF; + + printf("\n*--------------------------*alloca()*--------------------------*\n"); + printf("%-8s%-25s%-20s\n", "Power", "Buffer size (bytes)", "Allocation (ns)"); + size_t alloc_size = 1; + do { + stack_rem -= alloc_size; + if (stack_rem < 0) + break; + + clock_gettime(CLOCK_REALTIME, &ts1); + pdata = alloca(alloc_size); + clock_gettime(CLOCK_REALTIME, &ts2); + + curr_alloc = get_interval(&ts1, &ts2); + mean_alloc += curr_alloc; + + printf("%-8ld%-25lu%-20ld\n", inter_count, alloc_size, curr_alloc); + + ++inter_count; + } while(alloc_size *= 2); + + mean_alloc /= inter_count; + printf("\n%-33s%-20ld\n", "MEAN", mean_alloc); +} + +int main(void) +{ + malloc_test(); + calloc_test(); + alloca_test(); + return 0; +} diff --git a/timer/README.md b/timer/README.md new file mode 100644 index 0000000..d7fa872 --- /dev/null +++ b/timer/README.md @@ -0,0 +1,19 @@ +# Internal Kernel API (Time Management) + +## Homework + +1. User space. Implement program which shows absolute time in user space. +Pull request should contain the commit with source code and +generated output in text format. + +2. Kernel space. + a) Implement kernel module with API in sysfs, + which show relation time in maximum possible resolution + passed since previous read of it. + b) Implement kernel module with API in sysfs which shows absolute time of + previous reading with maximum resolution like ‘400.123567’ seconds. + c) (optional) Implement kernel module with API in sysfs which shows + average processor load updated once in a second. +Pull request should contain the commit with source code and +text output from sysfs. + \ No newline at end of file