Benoît Ganne | ec4749a | 2020-09-18 10:05:37 +0200 | [diff] [blame] | 1 | .. _memleak: |
| 2 | |
| 3 | ***************** |
| 4 | Memory leaks |
| 5 | ***************** |
| 6 | |
| 7 | Memory traces |
| 8 | ============= |
| 9 | |
| 10 | VPP supports memory traces to help debug (suspected) memory leaks. Each |
| 11 | allocation/deallocation is instrumented so that the number of allocations and |
| 12 | current global allocated size is maintained for each unique allocation stack |
| 13 | trace. |
| 14 | |
| 15 | Looking at a memory trace can help diagnose where memory is (over-)used, and |
| 16 | comparing memory traces at different point in time can help diagnose if and |
| 17 | where memory leaks happen. |
| 18 | |
| 19 | To enable memory traces on main-heap: |
| 20 | |
| 21 | .. code-block:: console |
Benoît Ganne | 34a19b2 | 2020-11-13 18:43:30 +0100 | [diff] [blame] | 22 | |
Benoît Ganne | ec4749a | 2020-09-18 10:05:37 +0200 | [diff] [blame] | 23 | $ vppctl memory-trace on main-heap |
| 24 | |
| 25 | To dump memory traces for analysis: |
| 26 | |
| 27 | .. code-block:: console |
Benoît Ganne | 34a19b2 | 2020-11-13 18:43:30 +0100 | [diff] [blame] | 28 | |
Benoît Ganne | 70892fc | 2022-10-03 20:00:45 +0200 | [diff] [blame] | 29 | $ vppctl show memory main-heap verbose |
Benoît Ganne | ec4749a | 2020-09-18 10:05:37 +0200 | [diff] [blame] | 30 | Thread 0 vpp_main |
| 31 | base 0x7fffb6422000, size 1g, locked, unmap-on-destroy, name 'main heap' |
| 32 | page stats: page-size 4K, total 262144, mapped 30343, not-mapped 231801 |
| 33 | numa 0: 30343 pages, 118.53m bytes |
| 34 | total: 1023.99M, used: 115.49M, free: 908.50M, trimmable: 908.48M |
| 35 | free chunks 451 free fastbin blks 0 |
| 36 | max total allocated 1023.99M |
| 37 | |
| 38 | Bytes Count Sample Traceback |
| 39 | 31457440 1 0x7fffbb31ad00 clib_mem_alloc_aligned_at_offset + 0x80 |
| 40 | clib_mem_alloc_aligned + 0x26 |
| 41 | alloc_aligned_8_8 + 0xe1 |
| 42 | clib_bihash_instantiate_8_8 + 0x76 |
| 43 | clib_bihash_init2_8_8 + 0x2ec |
| 44 | clib_bihash_init_8_8 + 0x6a |
| 45 | l2fib_table_init + 0x54 |
| 46 | set_int_l2_mode + 0x89 |
| 47 | int_l3 + 0xb4 |
| 48 | vlib_cli_dispatch_sub_commands + 0xeee |
| 49 | vlib_cli_dispatch_sub_commands + 0xc62 |
| 50 | vlib_cli_dispatch_sub_commands + 0xc62 |
| 51 | 266768 5222 0x7fffbd79f978 clib_mem_alloc_aligned_at_offset + 0x80 |
| 52 | vec_resize_allocate_memory + 0xa8 |
| 53 | _vec_resize_inline + 0x240 |
| 54 | unix_cli_file_add + 0x83d |
| 55 | unix_cli_listen_read_ready + 0x10b |
| 56 | linux_epoll_input_inline + 0x943 |
| 57 | linux_epoll_input + 0x39 |
| 58 | dispatch_node + 0x336 |
| 59 | vlib_main_or_worker_loop + 0xbf1 |
| 60 | vlib_main_loop + 0x1a |
| 61 | vlib_main + 0xae7 |
| 62 | thread0 + 0x3e |
| 63 | .... |
| 64 | |
| 65 | libc memory traces |
| 66 | ================== |
| 67 | |
| 68 | Internal VPP memory allocations rely on VPP main-heap, however when using |
Nathan Skrzypczak | 9ad39c0 | 2021-08-19 11:38:06 +0200 | [diff] [blame] | 69 | external libraries, esp. in plugins (e.g. OpenSSL library used by the IKEv2 |
Benoît Ganne | ec4749a | 2020-09-18 10:05:37 +0200 | [diff] [blame] | 70 | plugin), those external libraries usually manages memory using the standard |
| 71 | libc malloc()/free()/... calls. This, in turn, makes use of the default |
| 72 | libc heap. |
| 73 | |
| 74 | VPP has no knowledge of this heap and tools such as memory traces cannot be |
| 75 | used. |
| 76 | |
| 77 | In order to enable the use of standard VPP debugging tools, this library |
| 78 | replaces standard libc memory management calls with version using VPP |
| 79 | main-heap. |
| 80 | |
Nathan Skrzypczak | 9ad39c0 | 2021-08-19 11:38:06 +0200 | [diff] [blame] | 81 | To use it, you need to use the `LD_PRELOAD` mechanism, e.g. |
Benoît Ganne | ec4749a | 2020-09-18 10:05:37 +0200 | [diff] [blame] | 82 | |
| 83 | .. code-block:: console |
Benoît Ganne | 34a19b2 | 2020-11-13 18:43:30 +0100 | [diff] [blame] | 84 | |
Benoît Ganne | ec4749a | 2020-09-18 10:05:37 +0200 | [diff] [blame] | 85 | ~# LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libvppmem_preload.so /usr/bin/vpp -c /etc/vpp/startup.conf |
| 86 | |
| 87 | You can then use tools such as memory traces as usual. |