straceMalloc

stracing malloc
git clone https://noulin.net/git/straceMalloc.git
Log | Files | Refs | README

README.md (6075B)


      1 malloc.c is a program allocating blocks of memory with malloc.
      2 
      3 strace logs the system calls to the linux kernel, __mmap__ is the syscall used by malloc to allocate large blocks of RAM.
      4 
      5 The time spend in each malloc is measured, as you can see in the log, there is only a small variation.
      6 
      7 malloc.c allocates buffers increasingly big starting with 1KB and ending with 32MB.
      8 
      9 On my system, glibc allocates 128KB initially, so the first 6 allocations dont trigger a syscall.
     10 
     11 For buffers larger than 128KB, malloc calls the mmap syscall and the time spent in malloc is on average the same.
     12 
     13 When allocating buffers with alloca, the time spent in alloca depends on the buffer size, it takes more time for larger buffers. alloca never triggers a syscall as seen in the strace log.
     14 
     15 Compiling and running:
     16 
     17 ```
     18 sheepy -c malloc.c
     19 ./malloc
     20 strace ./malloc
     21 ```
     22 
     23 malloc output:
     24 ```
     25 alloc
     26 0 0x55949cb3a420 size: 1KB, time: 560ns
     27 1 0x55949cb3a830 size: 2KB, time: 1612ns
     28 2 0x55949cb3b040 size: 4KB, time: 1568ns
     29 3 0x55949cb3c050 size: 8KB, time: 1303ns
     30 4 0x55949cb3e060 size: 16KB, time: 1211ns
     31 5 0x55949cb42070 size: 32KB, time: 1206ns
     32 6 0x55949cb4a080 size: 64KB, time: 1217ns
     33 7 0x7f6f87048010 size: 128KB, time: 2899ns
     34 8 0x7f6f87007010 size: 256KB, time: 2574ns
     35 9 0x7f6f86f86010 size: 512KB, time: 1868ns
     36 10 0x7f6f867a8010 size: 1024KB, time: 2894ns
     37 11 0x7f6f865a7010 size: 2048KB, time: 2189ns
     38 12 0x7f6f861a6010 size: 4096KB, time: 2329ns
     39 13 0x7f6f859a5010 size: 8192KB, time: 2323ns
     40 14 0x7f6f849a4010 size: 16384KB, time: 1973ns
     41 15 0x7f6f829a3010 size: 32768KB, time: 2191ns
     42 s
     43 alloca 0 0x7fff5b02ce70 size: 1KB, time: 39ns
     44 alloca 1 0x7fff5b02c660 size: 2KB, time: 32ns
     45 alloca 2 0x7fff5b02b650 size: 4KB, time: 31ns
     46 alloca 3 0x7fff5b029640 size: 8KB, time: 1078ns
     47 alloca 4 0x7fff5b025630 size: 16KB, time: 970ns
     48 alloca 5 0x7fff5b01d620 size: 32KB, time: 1162ns
     49 alloca 6 0x7fff5b00d610 size: 64KB, time: 978ns
     50 alloca 7 0x7fff5afed600 size: 128KB, time: 1425ns
     51 alloca 8 0x7fff5afad5f0 size: 256KB, time: 1044ns
     52 alloca 9 0x7fff5af2d5e0 size: 512KB, time: 1148ns
     53 alloca 10 0x7fff5ae2d5d0 size: 1024KB, time: 961ns
     54 alloca 11 0x7fff5ac2d5c0 size: 2048KB, time: 1241ns
     55 ```
     56 
     57 strace log on debian stretch:
     58 
     59 ```
     60 write(1, "alloc\n", 6alloc
     61 )                  = 6
     62 write(1, "0 0x55e346706420 size: 1024, tim"..., 410 0x55e346706420 size: 1024, time: 837ns
     63 ) = 41
     64 write(1, "1 0x55e346706830 size: 2048, tim"..., 421 0x55e346706830 size: 2048, time: 1960ns
     65 ) = 42
     66 write(1, "2 0x55e346707040 size: 4096, tim"..., 422 0x55e346707040 size: 4096, time: 1712ns
     67 ) = 42
     68 write(1, "3 0x55e346708050 size: 8192, tim"..., 423 0x55e346708050 size: 8192, time: 1588ns
     69 ) = 42
     70 write(1, "4 0x55e34670a060 size: 16384, ti"..., 434 0x55e34670a060 size: 16384, time: 1573ns
     71 ) = 43
     72 write(1, "5 0x55e34670e070 size: 32768, ti"..., 435 0x55e34670e070 size: 32768, time: 1486ns
     73 ) = 43
     74 write(1, "6 0x55e346716080 size: 65536, ti"..., 436 0x55e346716080 size: 65536, time: 1487ns
     75 ) = 43
     76 mmap(NULL, 135168, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f475dc2f000
     77 write(1, "7 0x7f475dc2f010 size: 131072, t"..., 457 0x7f475dc2f010 size: 131072, time: 20558ns
     78 ) = 45
     79 mmap(NULL, 266240, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f475dbee000
     80 write(1, "8 0x7f475dbee010 size: 262144, t"..., 458 0x7f475dbee010 size: 262144, time: 19708ns
     81 ) = 45
     82 mmap(NULL, 528384, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f475db6d000
     83 write(1, "9 0x7f475db6d010 size: 524288, t"..., 459 0x7f475db6d010 size: 524288, time: 16711ns
     84 ) = 45
     85 mmap(NULL, 1052672, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f475d38f000
     86 write(1, "10 0x7f475d38f010 size: 1048576,"..., 4710 0x7f475d38f010 size: 1048576, time: 19502ns
     87 ) = 47
     88 mmap(NULL, 2101248, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f475d18e000
     89 write(1, "11 0x7f475d18e010 size: 2097152,"..., 4711 0x7f475d18e010 size: 2097152, time: 18659ns
     90 ) = 47
     91 mmap(NULL, 4198400, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f475cd8d000
     92 write(1, "12 0x7f475cd8d010 size: 4194304,"..., 4712 0x7f475cd8d010 size: 4194304, time: 17346ns
     93 ) = 47
     94 mmap(NULL, 8392704, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f475c58c000
     95 write(1, "13 0x7f475c58c010 size: 8388608,"..., 4713 0x7f475c58c010 size: 8388608, time: 18613ns
     96 ) = 47
     97 mmap(NULL, 16781312, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f475b58b000
     98 write(1, "14 0x7f475b58b010 size: 16777216"..., 4814 0x7f475b58b010 size: 16777216, time: 17792ns
     99 ) = 48
    100 mmap(NULL, 33558528, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f475958a000
    101 write(1, "15 0x7f475958a010 size: 33554432"..., 4815 0x7f475958a010 size: 33554432, time: 16998ns
    102 ) = 48
    103 write(1, "s\n", 2s
    104 )                      = 2
    105 write(1, "alloca 0 0x7ffd31053480 size: 1K"..., 46alloca 0 0x7ffd31053480 size: 1KB, time: 51ns
    106 ) = 46
    107 write(1, "alloca 1 0x7ffd31052c70 size: 2K"..., 46alloca 1 0x7ffd31052c70 size: 2KB, time: 37ns
    108 ) = 46
    109 write(1, "alloca 2 0x7ffd31051c60 size: 4K"..., 46alloca 2 0x7ffd31051c60 size: 4KB, time: 60ns
    110 ) = 46
    111 write(1, "alloca 3 0x7ffd3104fc50 size: 8K"..., 48alloca 3 0x7ffd3104fc50 size: 8KB, time: 1897ns
    112 ) = 48
    113 write(1, "alloca 4 0x7ffd3104bc40 size: 16"..., 49alloca 4 0x7ffd3104bc40 size: 16KB, time: 1224ns
    114 ) = 49
    115 write(1, "alloca 5 0x7ffd31043c30 size: 32"..., 49alloca 5 0x7ffd31043c30 size: 32KB, time: 1266ns
    116 ) = 49
    117 write(1, "alloca 6 0x7ffd31033c20 size: 64"..., 49alloca 6 0x7ffd31033c20 size: 64KB, time: 1318ns
    118 ) = 49
    119 write(1, "alloca 7 0x7ffd31013c10 size: 12"..., 50alloca 7 0x7ffd31013c10 size: 128KB, time: 1908ns
    120 ) = 50
    121 write(1, "alloca 8 0x7ffd30fd3c00 size: 25"..., 50alloca 8 0x7ffd30fd3c00 size: 256KB, time: 2673ns
    122 ) = 50
    123 write(1, "alloca 9 0x7ffd30f53bf0 size: 51"..., 50alloca 9 0x7ffd30f53bf0 size: 512KB, time: 2582ns
    124 ) = 50
    125 write(1, "alloca 10 0x7ffd30e53be0 size: 1"..., 52alloca 10 0x7ffd30e53be0 size: 1024KB, time: 1391ns
    126 ) = 52
    127 write(1, "alloca 11 0x7ffd30c53bd0 size: 2"..., 52alloca 11 0x7ffd30c53bd0 size: 2048KB, time: 2118ns
    128 ) = 52
    129 exit_group(0)                           = ?
    130 ```