Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

memory: user-space #13

Open
wants to merge 8 commits into
base: Matvii.Zorin
Choose a base branch
from
163 changes: 95 additions & 68 deletions memory/log.txt
Original file line number Diff line number Diff line change
@@ -1,76 +1,103 @@

*--------------------------*malloc()*--------------------------*
Power Buffer size (bytes) Allocation (ns) Freeing (ns)
1 1 639 250
2 2 136 76
3 4 62 52
4 8 65 50
5 16 118 53
6 32 133 56
7 64 173 60
8 128 125 57
9 256 88 60
10 512 123 55
11 1024 161 55
12 2048 2814 503
13 4096 136 111
14 8192 2667 65
15 16384 2964 68
16 32768 2709 68
17 65536 2649 65
18 131072 7214 11243
19 262144 4328 3641
20 524288 3932 3511
21 1048576 3922 3563
22 2097152 6590 6440
23 4194304 6199 4696
24 8388608 5362 4700
25 16777216 5014 4485
26 33554432 5010 4580
27 67108864 4996 4713
28 134217728 4969 5114
29 268435456 4954 8277
30 536870912 4851 8161
31 1073741824 5969 12895
32 2147483648 5312 12504
33 4294967296 5297 12424
0 1 562 188
1 2 126 142
2 4 87 92
3 8 62 66
4 16 114 80
5 32 201 100
6 64 180 100
7 128 216 99
8 256 161 82
9 512 182 94
10 1024 257 98
11 2048 3071 427
12 4096 202 144
13 8192 2991 118
14 16384 2922 130
15 32768 2956 127
16 65536 3157 115
17 131072 7894 11804
18 262144 6035 5208
19 524288 4953 4626
20 1048576 4590 4788
21 2097152 7491 7109
22 4194304 7855 6362
23 8388608 6855 6457
24 16777216 7230 4132
25 33554432 4343 3841
26 67108864 4183 4081
27 134217728 4370 4466
28 268435456 4272 5380
29 536870912 6116 11445
30 1073741824 7452 17836
31 2147483648 8581 18082
32 4294967296 7534 17259

MEAN 3020 3413
MEAN 3551 4093

*--------------------------*calloc()*--------------------------*
Power Buffer size (bytes) Allocation (ns) Freeing (ns)
1 1 506 185
2 2 115 53
3 4 96 53
4 8 92 55
5 16 146 53
6 32 158 52
7 64 158 55
8 128 271 57
9 256 196 60
10 512 248 65
11 1024 225 73
12 2048 320 225
13 4096 411 100
14 8192 3250 73
15 16384 3569 85
16 32768 10327 146
17 65536 25198 160
18 131072 58739 163
19 262144 139834 236
20 524288 39028 190
21 1048576 346880 186
22 2097152 508630 300
23 4194304 1477771 6869
24 8388608 4135254 734
25 16777216 5359725 968
26 33554432 15250 13000
27 67108864 5334 4553
28 134217728 4238 4544
29 268435456 4176 5120
30 536870912 4059 6818
31 1073741824 5032 10706
32 2147483648 4465 10362
33 4294967296 4402 10506
0 1 473 163
1 2 200 115
2 4 164 105
3 8 122 80
4 16 170 78
5 32 172 87
6 64 200 100
7 128 385 70
8 256 248 82
9 512 317 102
10 1024 381 96
11 2048 343 341
12 4096 785 112
13 8192 4993 152
14 16384 3966 112
15 32768 11941 136
16 65536 26086 204
17 131072 69981 187
18 262144 150670 423
19 524288 53354 1525
20 1048576 866863 2804
21 2097152 899034 483
22 4194304 1189385 1004
23 8388608 2389343 839
24 16777216 5158120 1421
25 33554432 26193 17083
26 67108864 10045 9273
Copy link
Contributor

@yekovalyov yekovalyov Feb 13, 2020

Choose a reason for hiding this comment

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

Can You explain why allocation of 2^25 bytes faster than allocation of 2^24 bytes?

Copy link
Author

@matthewzorin matthewzorin Feb 13, 2020

Choose a reason for hiding this comment

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

I think maybe in linux calloc function uses simple allocation and memset in cases, where we need lesser than the certain amount of bytes. So, in another cases it switches to the more efficient way for getting zero-initialized memory. For example - zeroed pages (2^24 bytes - 4096 pages, 1 page - 4096 bytes), but this method needs more time than I measured. I'd glad to receive Your opinion about this result

27 134217728 8189 10461
28 268435456 8123 9737
29 536870912 7246 12217
30 1073741824 9096 20275
31 2147483648 9524 19847
32 4294967296 8271 19839

MEAN 368427 2327
MEAN 330738 3925

*--------------------------*alloca()*--------------------------*
Power Buffer size (bytes) Allocation (ns)
0 1 108
1 2 115
2 4 92
3 8 80
4 16 103
5 32 85
6 64 90
7 128 96
8 256 98
9 512 95
10 1024 80
11 2048 95
12 4096 4182
13 8192 3719
14 16384 3736
15 32768 3407
16 65536 5579
17 131072 4074
18 262144 4447
19 524288 5853
20 1048576 4167
21 2097152 5172

MEAN 2066
36 changes: 26 additions & 10 deletions memory/uapi_memory.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>

#define ALLOC_COEF .9F //For controling the stack memory allocation

long get_interval(struct timespec *ts1, struct timespec *ts2)
{
Expand All @@ -11,7 +14,7 @@ long get_interval(struct timespec *ts1, struct timespec *ts2)
static inline void malloc_test(void)
{
struct timespec ts1, ts2;
void *pdata = NULL;
void *restrict pdata = NULL;
long mean_alloc = 0;
long mean_free = 0;
long curr_alloc = 0;
Expand All @@ -38,8 +41,10 @@ static inline void malloc_test(void)

curr_free = get_interval(&ts1, &ts2);
mean_free += curr_free;
++inter_count;

printf("%-8ld%-25lu%-20ld%-20ld\n", inter_count, alloc_size, curr_alloc, curr_free);

++inter_count;
} while(alloc_size *= 2);

mean_alloc /= inter_count;
Expand All @@ -50,7 +55,7 @@ static inline void malloc_test(void)
static inline void calloc_test(void)
{
struct timespec ts1, ts2;
void *pdata = NULL;
void *restrict pdata = NULL;
long mean_alloc = 0;
long mean_free = 0;
long curr_alloc = 0;
Expand All @@ -77,8 +82,10 @@ static inline void calloc_test(void)

curr_free = get_interval(&ts1, &ts2);
mean_free += curr_free;
++inter_count;

printf("%-8ld%-25lu%-20ld%-20ld\n", inter_count, alloc_size, curr_alloc, curr_free);

++inter_count;
} while(alloc_size *= 2);

mean_alloc /= inter_count;
Expand All @@ -89,26 +96,35 @@ static inline void calloc_test(void)
static inline void alloca_test(void)
{
struct timespec ts1, ts2;
void *pdata = NULL;
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);

if (!pdata)
break;

curr_alloc = get_interval(&ts1, &ts2);
mean_alloc += curr_alloc;
++inter_count;

printf("%-8ld%-25lu%-20ld\n", inter_count, alloc_size, curr_alloc);

++inter_count;
} while(alloc_size *= 2);

mean_alloc /= inter_count;
Expand All @@ -119,6 +135,6 @@ int main(void)
{
malloc_test();
calloc_test();
//alloca_test();
alloca_test();
return 0;
}