From 234fe894d80a6ebc7e457bc86f2eea80d5ef01ea Mon Sep 17 00:00:00 2001 From: Ryujiro Shibuya Date: Wed, 25 Dec 2019 07:40:54 +0000 Subject: session svm: tracking segment memory usage 1. segment manager would attempt to balance the usages across the segments, when it allocate fifos 2. the memory presure level is determined per fifo-segment 3. updated unit test 4. updated cli output for segments Type: feature Signed-off-by: Ryujiro Shibuya Change-Id: I2923f3e0a43dd919196a0cb2cd55e098fde6cf66 --- src/plugins/unittest/CMakeLists.txt | 1 + src/plugins/unittest/segment_manager_test.c | 757 ++++++++++++++++++++++++++++ src/svm/fifo_segment.c | 154 +++++- src/svm/fifo_segment.h | 64 +++ src/svm/fifo_types.h | 3 + src/vnet/session/application.c | 7 +- src/vnet/session/application_interface.h | 2 + src/vnet/session/segment_manager.c | 89 +++- src/vnet/session/segment_manager.h | 11 +- 9 files changed, 1056 insertions(+), 32 deletions(-) create mode 100644 src/plugins/unittest/segment_manager_test.c diff --git a/src/plugins/unittest/CMakeLists.txt b/src/plugins/unittest/CMakeLists.txt index a029e0e3259..e75b96ff265 100644 --- a/src/plugins/unittest/CMakeLists.txt +++ b/src/plugins/unittest/CMakeLists.txt @@ -36,6 +36,7 @@ add_vpp_plugin(unittest sparse_vec_test.c string_test.c svm_fifo_test.c + segment_manager_test.c tcp_test.c test_buffer.c unittest.c diff --git a/src/plugins/unittest/segment_manager_test.c b/src/plugins/unittest/segment_manager_test.c new file mode 100644 index 00000000000..8f362f40cb6 --- /dev/null +++ b/src/plugins/unittest/segment_manager_test.c @@ -0,0 +1,757 @@ +#include +#include +#include +#include +#include +#include + +#define SEG_MGR_TEST_I(_cond, _comment, _args...) \ +({ \ + int _evald = (_cond); \ + if (!(_evald)) { \ + fformat(stderr, "FAIL:%d: " _comment "\n", \ + __LINE__, ##_args); \ + } else { \ + fformat(stderr, "PASS:%d: " _comment "\n", \ + __LINE__, ##_args); \ + } \ + _evald; \ +}) + +#define SEG_MGR_TEST(_cond, _comment, _args...) \ +{ \ + if (!SEG_MGR_TEST_I(_cond, _comment, ##_args)) { \ + return 1; \ + } \ +} + +#define ST_DBG(_comment, _args...) \ + fformat(stderr, _comment "\n", ##_args); \ + +#define SEGMENT_MANAGER_GET_INDEX_FROM_HANDLE(x) (x >> 32) + +/* dummy callback functions */ +static void +dummy_session_reset_callback (session_t * s) +{ + clib_warning ("called..."); +} + +static int +dummy_session_connected_callback (u32 app_index, u32 api_context, + session_t * s, u8 is_fail) +{ + clib_warning ("called..."); + return 0; +} + +static int +dummy_add_segment_callback (u32 client_index, u64 segment_handle) +{ + clib_warning ("called..."); + return 0; +} + +static int +dummy_del_segment_callback (u32 client_index, u64 segment_handle) +{ + clib_warning ("called..."); + return 0; +} + +static void +dummy_session_disconnect_callback (session_t * s) +{ + clib_warning ("called..."); +} + +static int +dummy_session_accept_callback (session_t * s) +{ + clib_warning ("called..."); + return 0; +} + +static int +dummy_server_rx_callback (session_t * s) +{ + clib_warning ("called..."); + return -1; +} + +/* *INDENT-OFF* */ +static session_cb_vft_t dummy_session_cbs = { + .session_reset_callback = dummy_session_reset_callback, + .session_connected_callback = dummy_session_connected_callback, + .session_accept_callback = dummy_session_accept_callback, + .session_disconnect_callback = dummy_session_disconnect_callback, + .builtin_app_rx_callback = dummy_server_rx_callback, + .add_segment_callback = dummy_add_segment_callback, + .del_segment_callback = dummy_del_segment_callback, +}; +/* *INDENT-ON* */ + +static char *states_str[] = { +#define _(sym,str) str, + foreach_segment_mem_status +#undef _ +}; + +static u32 size_4KB = 4 << 10; +static u32 size_8KB = 8 << 10; +static u32 size_12KB = 12 << 10; +static u32 size_16KB = 16 << 10; +static u32 size_20KB = 20 << 10; +static u32 size_32KB = 32 << 10; +static u32 size_52KB = 52 << 10; +static u32 size_64KB = 64 << 10; +static u32 size_128KB = 128 << 10; +static u32 size_1MB = 1 << 20; +static u32 size_2MB = 2 << 20; + + +static int +segment_manager_test_pressure_1 (vlib_main_t * vm, unformat_input_t * input) +{ + int rv; + segment_manager_t *sm; + fifo_segment_t *fs0, *fs; + svm_fifo_t *rx_fifo, *tx_fifo; + uword app_seg_size = size_2MB; + u32 fifo_size = size_128KB; + u64 options[APP_OPTIONS_N_OPTIONS]; + u8 data[size_128KB]; + + memset (&options, 0, sizeof (options)); + + vnet_app_attach_args_t attach_args = { + .api_client_index = ~0, + .options = options, + .namespace_id = 0, + .session_cb_vft = &dummy_session_cbs, + .name = format (0, "segment_manager_test_pressure_1"), + }; + + attach_args.options[APP_OPTIONS_SEGMENT_SIZE] = app_seg_size; + attach_args.options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN; + attach_args.options[APP_OPTIONS_RX_FIFO_SIZE] = fifo_size; + attach_args.options[APP_OPTIONS_TX_FIFO_SIZE] = fifo_size; + rv = vnet_application_attach (&attach_args); + SEG_MGR_TEST ((rv == 0), "vnet_application_attach %d", rv); + + sm = + segment_manager_get (SEGMENT_MANAGER_GET_INDEX_FROM_HANDLE + (attach_args.segment_handle)); + SEG_MGR_TEST ((sm != 0), "segment_manager_get %p", sm); + + /* initial status : (0 / 2MB) */ + fs0 = segment_manager_get_segment (sm, 0); + rv = fifo_segment_get_mem_status (fs0); + SEG_MGR_TEST ((rv == MEMORY_PRESSURE_NO_PRESSURE), + "fifo_segment_get_mem_status %s", states_str[rv]); + + + /* allocate a fifo : 128KB x2 */ + rv = segment_manager_alloc_session_fifos (sm, + vlib_get_thread_index (), + &rx_fifo, &tx_fifo); + SEG_MGR_TEST ((rv == 0), "segment_manager_alloc_session_fifos %d", rv); + + svm_fifo_set_size (rx_fifo, size_1MB); + svm_fifo_set_size (tx_fifo, size_1MB); + + fs = segment_manager_get_segment (sm, rx_fifo->segment_index); + SEG_MGR_TEST ((fs == fs0), "fs %p", fs); + + /* fill fifos (but not add chunks) */ + svm_fifo_enqueue (rx_fifo, fifo_size - 1, data); + svm_fifo_enqueue (tx_fifo, fifo_size - 1, data); + + /* 256KB+ / 2048KB+ => ~12% */ + fifo_segment_update_free_bytes (fs); + rv = fifo_segment_get_mem_status (fs); + SEG_MGR_TEST ((rv == MEMORY_PRESSURE_NO_PRESSURE), + "fifo_segment_get_mem_status %s", states_str[rv]); + + /* grow fifos */ + svm_fifo_enqueue (rx_fifo, fifo_size, data); + svm_fifo_enqueue (rx_fifo, fifo_size, data); + svm_fifo_enqueue (rx_fifo, fifo_size, data); + svm_fifo_enqueue (tx_fifo, fifo_size, data); + svm_fifo_enqueue (tx_fifo, fifo_size, data); + svm_fifo_enqueue (tx_fifo, fifo_size, data); + + /* 8 chunks : 49% */ + fifo_segment_update_free_bytes (fs); + rv = fifo_segment_get_mem_status (fs); + SEG_MGR_TEST ((rv == MEMORY_PRESSURE_NO_PRESSURE), + "fifo_segment_get_mem_status %s", states_str[rv]); + + /* grow fifos */ + svm_fifo_enqueue (rx_fifo, fifo_size, data); + svm_fifo_enqueue (tx_fifo, fifo_size, data); + + /* 10 chunks : 61% */ + fifo_segment_update_free_bytes (fs); + rv = fifo_segment_get_mem_status (fs); + SEG_MGR_TEST ((rv == MEMORY_PRESSURE_LOW_PRESSURE), + "fifo_segment_get_mem_status %s", states_str[rv]); + + /* grow fifos */ + svm_fifo_enqueue (rx_fifo, fifo_size, data); + svm_fifo_enqueue (rx_fifo, fifo_size, data); + svm_fifo_enqueue (tx_fifo, fifo_size, data); + svm_fifo_enqueue (tx_fifo, fifo_size, data); + + /* 14 chunks : 85% */ + fifo_segment_update_free_bytes (fs); + rv = fifo_segment_get_mem_status (fs); + SEG_MGR_TEST ((rv == MEMORY_PRESSURE_HIGH_PRESSURE), + "fifo_segment_get_mem_status %s", states_str[rv]); + + + /* shrink fifos */ + svm_fifo_dequeue_drop (rx_fifo, fifo_size); + svm_fifo_dequeue_drop (rx_fifo, fifo_size); + svm_fifo_dequeue_drop (tx_fifo, fifo_size); + svm_fifo_dequeue_drop (tx_fifo, fifo_size); + + /* 10 chunks : 61% */ + fifo_segment_update_free_bytes (fs); + rv = fifo_segment_get_mem_status (fs); + SEG_MGR_TEST ((rv == MEMORY_PRESSURE_LOW_PRESSURE), + "fifo_segment_get_mem_status %s", states_str[rv]); + + + /* grow fifos */ + svm_fifo_enqueue (rx_fifo, fifo_size, data); + svm_fifo_enqueue (rx_fifo, fifo_size, data); + svm_fifo_enqueue (tx_fifo, fifo_size, data); + svm_fifo_enqueue (tx_fifo, fifo_size, data); + + /* 14 chunks : 85% */ + fifo_segment_update_free_bytes (fs); + rv = fifo_segment_get_mem_status (fs); + SEG_MGR_TEST ((rv == MEMORY_PRESSURE_HIGH_PRESSURE), + "fifo_segment_get_mem_status %s", states_str[rv]); + + svm_fifo_dequeue_drop (rx_fifo, fifo_size); + svm_fifo_dequeue_drop (rx_fifo, fifo_size); + svm_fifo_dequeue_drop (tx_fifo, fifo_size); + svm_fifo_dequeue_drop (tx_fifo, fifo_size); + + + /* 10 chunks : 61% */ + fifo_segment_update_free_bytes (fs); + rv = fifo_segment_get_mem_status (fs); + SEG_MGR_TEST ((rv == MEMORY_PRESSURE_LOW_PRESSURE), + "fifo_segment_get_mem_status %s", states_str[rv]); + + /* shrink fifos */ + svm_fifo_dequeue_drop (rx_fifo, fifo_size); + svm_fifo_dequeue_drop (rx_fifo, fifo_size); + svm_fifo_dequeue_drop (rx_fifo, fifo_size); + svm_fifo_dequeue_drop (rx_fifo, fifo_size); + svm_fifo_dequeue_drop (tx_fifo, fifo_size); + svm_fifo_dequeue_drop (tx_fifo, fifo_size); + svm_fifo_dequeue_drop (tx_fifo, fifo_size); + svm_fifo_dequeue_drop (tx_fifo, fifo_size); + + /* 2 chunks : 12% */ + fifo_segment_update_free_bytes (fs); + rv = fifo_segment_get_mem_status (fs); + SEG_MGR_TEST ((rv == MEMORY_PRESSURE_NO_PRESSURE), + "fifo_segment_get_mem_status %s", states_str[rv]); + + + vnet_app_detach_args_t detach_args = { + .app_index = attach_args.app_index, + .api_client_index = ~0, + }; + rv = vnet_application_detach (&detach_args); + SEG_MGR_TEST ((rv == 0), "vnet_application_detach %d", rv); + + return 0; +} + +static int +segment_manager_test_pressure_2 (vlib_main_t * vm, unformat_input_t * input) +{ + int rv, i; + segment_manager_t *sm; + fifo_segment_t *fs0, *fs; + svm_fifo_t *rx_fifo, *tx_fifo; + uword app_seg_size = size_2MB; + u32 fifo_size = size_4KB; + u64 options[APP_OPTIONS_N_OPTIONS]; + u8 data[size_4KB]; + + memset (&options, 0, sizeof (options)); + + vnet_app_attach_args_t attach_args = { + .api_client_index = ~0, + .options = options, + .namespace_id = 0, + .session_cb_vft = &dummy_session_cbs, + .name = format (0, "segment_manager_test_pressure_1"), + }; + + attach_args.options[APP_OPTIONS_SEGMENT_SIZE] = app_seg_size; + attach_args.options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN; + attach_args.options[APP_OPTIONS_RX_FIFO_SIZE] = fifo_size; + attach_args.options[APP_OPTIONS_TX_FIFO_SIZE] = fifo_size; + rv = vnet_application_attach (&attach_args); + SEG_MGR_TEST ((rv == 0), "vnet_application_attach %d", rv); + + sm = + segment_manager_get (SEGMENT_MANAGER_GET_INDEX_FROM_HANDLE + (attach_args.segment_handle)); + SEG_MGR_TEST ((sm != 0), "segment_manager_get %p", sm); + + /* initial status : (0 / 2MB) */ + fs0 = segment_manager_get_segment (sm, 0); + fifo_segment_update_free_bytes (fs0); + rv = fifo_segment_get_mem_status (fs0); + SEG_MGR_TEST ((rv == MEMORY_PRESSURE_NO_PRESSURE), + "fifo_segment_get_mem_status %s", states_str[rv]); + + + /* allocate fifos : 4KB x2 */ + rv = segment_manager_alloc_session_fifos (sm, + vlib_get_thread_index (), + &rx_fifo, &tx_fifo); + SEG_MGR_TEST ((rv == 0), "segment_manager_alloc_session_fifos %d", rv); + + svm_fifo_set_size (rx_fifo, size_2MB); + svm_fifo_set_size (tx_fifo, size_2MB); + + /* fill fifos (but not add chunks) */ + svm_fifo_enqueue (rx_fifo, fifo_size - 1, data); + svm_fifo_enqueue (tx_fifo, fifo_size - 1, data); + + fs = segment_manager_get_segment (sm, rx_fifo->segment_index); + + /* grow fifos */ + for (i = 0; i < 509; ++i) + { + svm_fifo_enqueue (rx_fifo, fifo_size, data); + } + + /* 510 chunks : 100% of 2MB */ + fifo_segment_update_free_bytes (fs); + rv = fifo_segment_get_mem_status (fs); + SEG_MGR_TEST ((rv == MEMORY_PRESSURE_HIGH_PRESSURE), + "fifo_segment_get_mem_status %s", states_str[rv]); + + /* this fifo growth is expected to fail */ + rv = svm_fifo_enqueue (rx_fifo, fifo_size, data); + SEG_MGR_TEST ((rv == SVM_FIFO_EGROW), "svm_fifo_enqueue %d", rv); + + /* then, no-memory is detected */ + fifo_segment_update_free_bytes (fs); + rv = fifo_segment_get_mem_status (fs); + SEG_MGR_TEST ((rv == MEMORY_PRESSURE_NO_MEMORY), + "fifo_segment_get_mem_status %s", states_str[rv]); + + /* shrink fifos */ + for (i = 0; i < 20; ++i) + { + svm_fifo_dequeue_drop (rx_fifo, fifo_size); + } + + /* 489 chunks : 96%, it is high-pressure level + * but the reached-mem-limit record is not reset + * so the no-memory state lasts. + */ + fifo_segment_update_free_bytes (fs); + rv = fifo_segment_get_mem_status (fs); + SEG_MGR_TEST ((rv == MEMORY_PRESSURE_NO_MEMORY), + "fifo_segment_get_mem_status %s", states_str[rv]); + + /* shrink fifos */ + for (i = 0; i < 133; ++i) + { + svm_fifo_dequeue_drop (rx_fifo, fifo_size); + } + + /* 356 chunks : 70% of 2MB */ + fifo_segment_update_free_bytes (fs); + rv = fifo_segment_get_mem_status (fs); + SEG_MGR_TEST ((rv == MEMORY_PRESSURE_LOW_PRESSURE), + "fifo_segment_get_mem_status %s", states_str[rv]); + + /* shrink fifos */ + for (i = 0; i < 354; ++i) + { + svm_fifo_dequeue_drop (rx_fifo, fifo_size); + } + + /* 2 chunks : 3% of 2MB */ + fifo_segment_update_free_bytes (fs); + rv = fifo_segment_get_mem_status (fs); + SEG_MGR_TEST ((rv == MEMORY_PRESSURE_NO_PRESSURE), + "fifo_segment_get_mem_status %s", states_str[rv]); + + + vnet_app_detach_args_t detach_args = { + .app_index = attach_args.app_index, + .api_client_index = ~0, + }; + rv = vnet_application_detach (&detach_args); + SEG_MGR_TEST ((rv == 0), "vnet_application_detach %d", rv); + + return 0; +} + +static int +segment_manager_test_fifo_balanced_alloc (vlib_main_t * vm, + unformat_input_t * input) +{ + int rv, i, fs_index; + segment_manager_t *sm; + fifo_segment_t *fs[4]; + svm_fifo_t *rx_fifo[4], *tx_fifo[4]; + uword app_seg_size = size_2MB; + u32 fifo_size = size_4KB; + u64 options[APP_OPTIONS_N_OPTIONS]; + u8 data[size_4KB]; + + memset (&options, 0, sizeof (options)); + + vnet_app_attach_args_t attach_args = { + .api_client_index = ~0, + .options = options, + .namespace_id = 0, + .session_cb_vft = &dummy_session_cbs, + .name = format (0, "segment_manager_test_pressure_1"), + }; + + attach_args.options[APP_OPTIONS_SEGMENT_SIZE] = app_seg_size; + attach_args.options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN; + attach_args.options[APP_OPTIONS_RX_FIFO_SIZE] = fifo_size; + attach_args.options[APP_OPTIONS_TX_FIFO_SIZE] = fifo_size; + rv = vnet_application_attach (&attach_args); + SEG_MGR_TEST ((rv == 0), "vnet_application_attach %d", rv); + + sm = + segment_manager_get (SEGMENT_MANAGER_GET_INDEX_FROM_HANDLE + (attach_args.segment_handle)); + SEG_MGR_TEST ((sm != 0), "segment_manager_get %p", sm); + + /* initial status : (0 / 2MB) */ + fs[0] = segment_manager_get_segment (sm, 0); + rv = fifo_segment_get_mem_status (fs[0]); + SEG_MGR_TEST ((rv == MEMORY_PRESSURE_NO_PRESSURE), + "fifo_segment_get_mem_status %s", states_str[rv]); + + /* allocate fifos : 4KB x2 */ + rv = segment_manager_alloc_session_fifos (sm, + vlib_get_thread_index (), + &rx_fifo[0], &tx_fifo[0]); + SEG_MGR_TEST ((rv == 0), "segment_manager_alloc_session_fifos %d", rv); + SEG_MGR_TEST ((rx_fifo[0]->segment_index == 0), + "segment_index %d", rx_fifo[0]->segment_index); + SEG_MGR_TEST ((tx_fifo[0]->segment_index == 0), + "segment_index %d", tx_fifo[0]->segment_index); + + /* grow fifos */ + svm_fifo_set_size (rx_fifo[0], size_1MB); + for (i = 0; i < 200; ++i) + { + svm_fifo_enqueue (rx_fifo[0], fifo_size, data); + } + + /* add another 2MB segment */ + fs_index = segment_manager_add_segment (sm, size_2MB); + SEG_MGR_TEST ((fs_index == 1), "fs_index %d", fs_index); + + /* allocate fifos : 4KB x2 + * expected to be allocated on the newer segment, + * because the usage of the first segment is high. + */ + rv = segment_manager_alloc_session_fifos (sm, + vlib_get_thread_index (), + &rx_fifo[1], &tx_fifo[1]); + SEG_MGR_TEST ((rv == 0), "segment_manager_alloc_session_fifos %d", rv); + SEG_MGR_TEST ((rx_fifo[1]->segment_index == 1), + "segment_index %d", rx_fifo[1]->segment_index); + SEG_MGR_TEST ((tx_fifo[1]->segment_index == 1), + "segment_index %d", tx_fifo[1]->segment_index); + + /* allocate fifos : 4KB x2 + * expected to be allocated on the newer segment. + */ + rv = segment_manager_alloc_session_fifos (sm, + vlib_get_thread_index (), + &rx_fifo[2], &tx_fifo[2]); + SEG_MGR_TEST ((rv == 0), "segment_manager_alloc_session_fifos %d", rv); + SEG_MGR_TEST ((rx_fifo[2]->segment_index == 1), + "segment_index %d", rx_fifo[2]->segment_index); + SEG_MGR_TEST ((tx_fifo[2]->segment_index == 1), + "segment_index %d", tx_fifo[2]->segment_index); + + /* grow fifos, so the usage of the secong segment becomes + * higher than the first one. + */ + svm_fifo_set_size (rx_fifo[1], size_1MB); + for (i = 0; i < 400; ++i) + { + svm_fifo_enqueue (rx_fifo[1], fifo_size, data); + } + + /* allocate fifos : 4KB x2 + * expected to be allocated on the first segment. + */ + rv = segment_manager_alloc_session_fifos (sm, + vlib_get_thread_index (), + &rx_fifo[3], &tx_fifo[3]); + SEG_MGR_TEST ((rv == 0), "segment_manager_alloc_session_fifos %d", rv); + SEG_MGR_TEST ((rx_fifo[3]->segment_index == 0), + "segment_index %d", rx_fifo[3]->segment_index); + SEG_MGR_TEST ((tx_fifo[3]->segment_index == 0), + "segment_index %d", tx_fifo[3]->segment_index); + + + + vnet_app_detach_args_t detach_args = { + .app_index = attach_args.app_index, + .api_client_index = ~0, + }; + rv = vnet_application_detach (&detach_args); + SEG_MGR_TEST ((rv == 0), "vnet_application_detach %d", rv); + + return 0; +} + +static int +segment_manager_test_fifo_ops (vlib_main_t * vm, unformat_input_t * input) +{ + int rv, i; + segment_manager_t *sm; + fifo_segment_t *fs; + svm_fifo_t *rx_fifo, *tx_fifo; + uword app_seg_size = size_2MB, most_grown = 0; + u32 fifo_size = size_4KB; + u32 max_dequeue = 0; + u64 options[APP_OPTIONS_N_OPTIONS]; + u8 data[size_128KB]; + + memset (&options, 0, sizeof (options)); + + vnet_app_attach_args_t attach_args = { + .api_client_index = ~0, + .options = options, + .namespace_id = 0, + .session_cb_vft = &dummy_session_cbs, + .name = format (0, "segment_manager_test_pressure_1"), + }; + + attach_args.options[APP_OPTIONS_SEGMENT_SIZE] = app_seg_size; + attach_args.options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN; + attach_args.options[APP_OPTIONS_RX_FIFO_SIZE] = fifo_size; + attach_args.options[APP_OPTIONS_TX_FIFO_SIZE] = fifo_size; + rv = vnet_application_attach (&attach_args); + SEG_MGR_TEST ((rv == 0), "vnet_application_attach %d", rv); + + sm = + segment_manager_get (SEGMENT_MANAGER_GET_INDEX_FROM_HANDLE + (attach_args.segment_handle)); + SEG_MGR_TEST ((sm != 0), "segment_manager_get %p", sm); + + /* initial status : (0 / 2MB) */ + fs = segment_manager_get_segment (sm, 0); + rv = fifo_segment_get_mem_status (fs); + SEG_MGR_TEST ((rv == MEMORY_PRESSURE_NO_PRESSURE), + "fifo_segment_get_mem_status %s", states_str[rv]); + + /* allocate fifos : 4KB x2 */ + rv = segment_manager_alloc_session_fifos (sm, + vlib_get_thread_index (), + &rx_fifo, &tx_fifo); + SEG_MGR_TEST ((rv == 0), "segment_manager_alloc_session_fifos %d", rv); + + /* check the initial fifo size : 4KB */ + rv = svm_fifo_size (rx_fifo); + SEG_MGR_TEST ((rv == size_4KB), "svm_fifo_size %d", rv); + + /* fill 4KB */ + rv = svm_fifo_enqueue (rx_fifo, size_4KB, data); + SEG_MGR_TEST ((rv == size_4KB), "svm_fifo_enqueue %d", rv); + max_dequeue = svm_fifo_max_dequeue (rx_fifo); + SEG_MGR_TEST ((max_dequeue == size_4KB), "max_dequeue %u", max_dequeue); + + /* grow the fifo size : 4KB -> 8KB */ + svm_fifo_set_size (rx_fifo, size_8KB); + rv = svm_fifo_size (rx_fifo); + SEG_MGR_TEST ((rv == size_8KB), "svm_fifo_size %d", rv); + + /* verify that fifo cannot grow larger than the fifo size */ + /* 4KB + 8KB > 8KB, so only 4KB is queued */ + rv = svm_fifo_enqueue (rx_fifo, size_8KB, data); + SEG_MGR_TEST ((rv == size_4KB), "svm_fifo_enqueue %d", rv); + max_dequeue = svm_fifo_max_dequeue (rx_fifo); + SEG_MGR_TEST ((max_dequeue == size_8KB), "max_dequeue %u", max_dequeue); + + /* grow the fifo size : 8KB -> 16KB */ + svm_fifo_set_size (rx_fifo, size_16KB); + + /* 8KB + 4KB = 12KB */ + svm_fifo_enqueue (rx_fifo, size_4KB, data); + max_dequeue = svm_fifo_max_dequeue (rx_fifo); + SEG_MGR_TEST ((max_dequeue == size_12KB), "max_dequeue %u", max_dequeue); + + /* grow the fifo size : 16KB -> 32KB */ + svm_fifo_set_size (rx_fifo, size_32KB); + + /* 12KB + 8KB = 20KB */ + svm_fifo_enqueue (rx_fifo, size_8KB, data); + max_dequeue = svm_fifo_max_dequeue (rx_fifo); + SEG_MGR_TEST ((max_dequeue == size_20KB), "max_dequeue %u", max_dequeue); + + /* grow the fifo size : 32KB -> 64KB */ + svm_fifo_set_size (rx_fifo, size_64KB); + + /* 20KB + 32KB = 52KB */ + svm_fifo_enqueue (rx_fifo, size_32KB, data); + max_dequeue = svm_fifo_max_dequeue (rx_fifo); + SEG_MGR_TEST ((max_dequeue == size_52KB), "max_dequeue %u", max_dequeue); + + /* bulk enqueue */ + for (i = 0; i < 55; ++i) + { + svm_fifo_set_size (rx_fifo, svm_fifo_size (rx_fifo) + size_32KB); + svm_fifo_enqueue (rx_fifo, size_32KB, data); + } + max_dequeue = svm_fifo_max_dequeue (rx_fifo); + SEG_MGR_TEST ((max_dequeue == (size_52KB + size_32KB * 55)), + "max_dequeue %u", max_dequeue); + rv = fifo_segment_get_mem_status (fs); + SEG_MGR_TEST ((rv == MEMORY_PRESSURE_HIGH_PRESSURE), + "fifo_segment_get_mem_status %s", states_str[rv]); + most_grown = svm_fifo_size (rx_fifo); + + /* dequeue */ + svm_fifo_dequeue_drop (rx_fifo, size_20KB); + svm_fifo_dequeue_drop (rx_fifo, size_32KB); + + /* bulk dequeue */ + for (i = 0; i < 20; ++i) + svm_fifo_dequeue_drop (rx_fifo, size_32KB); + max_dequeue = svm_fifo_max_dequeue (rx_fifo); + SEG_MGR_TEST ((max_dequeue == (size_32KB * 35)), "max_dequeue %u", + max_dequeue); + rv = fifo_segment_get_mem_status (fs); + SEG_MGR_TEST ((rv == MEMORY_PRESSURE_LOW_PRESSURE), + "fifo_segment_get_mem_status %s", states_str[rv]); + + /* bulk dequeue */ + for (i = 0; i < 10; ++i) + svm_fifo_dequeue_drop (rx_fifo, size_32KB); + max_dequeue = svm_fifo_max_dequeue (rx_fifo); + SEG_MGR_TEST ((max_dequeue == (size_32KB * 25)), "max_dequeue %u", + max_dequeue); + rv = fifo_segment_get_mem_status (fs); + SEG_MGR_TEST ((rv == MEMORY_PRESSURE_NO_PRESSURE), + "fifo_segment_get_mem_status %s", states_str[rv]); + + /* bulk enqueue */ + for (i = 0; i < 30; ++i) + svm_fifo_enqueue (rx_fifo, size_32KB, data); + max_dequeue = svm_fifo_max_dequeue (rx_fifo); + SEG_MGR_TEST ((max_dequeue == (size_32KB * 55)), "max_dequeue %u", + max_dequeue); + rv = fifo_segment_get_mem_status (fs); + SEG_MGR_TEST ((rv == MEMORY_PRESSURE_HIGH_PRESSURE), + "fifo_segment_get_mem_status %s", states_str[rv]); + + /* bulk dequeue */ + for (i = 0; i < 20; ++i) + svm_fifo_dequeue_drop (rx_fifo, size_32KB); + max_dequeue = svm_fifo_max_dequeue (rx_fifo); + SEG_MGR_TEST ((max_dequeue == (size_32KB * 35)), "max_dequeue %u", + max_dequeue); + rv = fifo_segment_get_mem_status (fs); + SEG_MGR_TEST ((rv == MEMORY_PRESSURE_LOW_PRESSURE), + "fifo_segment_get_mem_status %s", states_str[rv]); + + /* bulk dequeue */ + for (i = 0; i < 35; ++i) + svm_fifo_dequeue_drop (rx_fifo, size_32KB); + max_dequeue = svm_fifo_max_dequeue (rx_fifo); + SEG_MGR_TEST ((max_dequeue == 0), "max_dequeue %u", max_dequeue); + rv = fifo_segment_get_mem_status (fs); + SEG_MGR_TEST ((rv == MEMORY_PRESSURE_NO_PRESSURE), + "fifo_segment_get_mem_status %s", states_str[rv]); + + /* (virtual) fifo size is still large as it is not updated */ + SEG_MGR_TEST ((rx_fifo->size == most_grown), "rx_fifo->size %u", + rx_fifo->size); + + vnet_app_detach_args_t detach_args = { + .app_index = attach_args.app_index, + .api_client_index = ~0, + }; + rv = vnet_application_detach (&detach_args); + SEG_MGR_TEST ((rv == 0), "vnet_application_detach %d", rv); + + return 0; +} + + +static clib_error_t * +segment_manager_test (vlib_main_t * vm, + unformat_input_t * input, vlib_cli_command_t * cmd_arg) +{ + int res = 0; + + vnet_session_enable_disable (vm, 1); + + while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) + { + if (unformat (input, "pressure_levels_1")) + res = segment_manager_test_pressure_1 (vm, input); + else if (unformat (input, "pressure_levels_2")) + res = segment_manager_test_pressure_2 (vm, input); + else if (unformat (input, "alloc")) + res = segment_manager_test_fifo_balanced_alloc (vm, input); + else if (unformat (input, "fifo_ops")) + res = segment_manager_test_fifo_ops (vm, input); + + else if (unformat (input, "all")) + { + if ((res = segment_manager_test_pressure_1 (vm, input))) + goto done; + if ((res = segment_manager_test_pressure_2 (vm, input))) + goto done; + if ((res = segment_manager_test_fifo_balanced_alloc (vm, input))) + goto done; + if ((res = segment_manager_test_fifo_ops (vm, input))) + goto done; + } + else + break; + } + +done: + if (res) + return clib_error_return (0, "Segment manager unit test failed."); + return 0; +} + +/* *INDENT-OFF* */ +VLIB_CLI_COMMAND (tcp_test_command, static) = +{ + .path = "test segment-manager", + .short_help = "test segment manager [pressure_levels_1]" + "[pressure_level_2][alloc][fifo_ops][all]", + .function = segment_manager_test, +}; + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ + diff --git a/src/svm/fifo_segment.c b/src/svm/fifo_segment.c index 49cd1161a4b..88600b84e36 100644 --- a/src/svm/fifo_segment.c +++ b/src/svm/fifo_segment.c @@ -15,6 +15,12 @@ #include +static char *fifo_segment_mem_status_strings[] = { +#define _(sym,str) str, + foreach_segment_mem_status +#undef _ +}; + /** * Fifo segment free space * @@ -53,6 +59,26 @@ fsh_update_free_bytes (fifo_segment_header_t * fsh) clib_atomic_store_rel_n (&fsh->n_free_bytes, fsh_free_space (fsh)); } +static inline void +fsh_cached_bytes_add (fifo_segment_header_t * fsh, int size) +{ + clib_atomic_fetch_add_rel (&fsh->n_cached_bytes, size); +} + +static inline void +fsh_cached_bytes_sub (fifo_segment_header_t * fsh, int size) +{ + clib_atomic_fetch_sub_rel (&fsh->n_cached_bytes, size); +} + +static inline uword +fsh_n_cached_bytes (fifo_segment_header_t * fsh) +{ + uword n_cached = clib_atomic_load_relax_n (&fsh->n_cached_bytes); + ASSERT (n_cached >= 0); + return n_cached; +} + static void fsh_check_mem (fifo_segment_header_t * fsh) { @@ -133,6 +159,7 @@ fifo_segment_init (fifo_segment_t * fs) ssvm_pop_heap (oldheap); fsh->n_free_bytes = fsh_free_space (fsh); + fsh->n_cached_bytes = 0; max_chunks = fsh->n_free_bytes / FIFO_SEGMENT_MIN_FIFO_SIZE; fsh->n_reserved_bytes = (max_chunks / 4) * sizeof (rb_node_t); sh->ready = 1; @@ -349,6 +376,7 @@ fs_try_alloc_fifo_freelist_multi_chunk (fifo_segment_header_t * fsh, c->next = fss->free_chunks[fl_index]; fss->free_chunks[fl_index] = c; fss->n_fl_chunk_bytes += fl_size; + n_alloc -= fl_size; data_bytes += fl_size; } first = last = 0; @@ -372,6 +400,7 @@ fs_try_alloc_fifo_freelist_multi_chunk (fifo_segment_header_t * fsh, f->start_chunk = first; f->end_chunk = last; fss->n_fl_chunk_bytes -= n_alloc; + fsh_cached_bytes_sub (fsh, n_alloc); return f; } @@ -420,6 +449,7 @@ fs_try_alloc_fifo_batch (fifo_segment_header_t * fsh, } fss->n_fl_chunk_bytes += batch_size * rounded_data_size; + fsh_cached_bytes_add (fsh, batch_size * rounded_data_size); fsh_free_bytes_sub (fsh, size); return 0; @@ -450,7 +480,10 @@ fs_try_alloc_fifo (fifo_segment_header_t * fsh, fifo_segment_slice_t * fss, { f = fs_try_alloc_fifo_freelist (fss, fl_index, data_bytes); if (f) - goto done; + { + fsh_cached_bytes_sub (fsh, fs_freelist_index_to_size (fl_index)); + goto done; + } } fsh_check_mem (fsh); @@ -462,6 +495,8 @@ fs_try_alloc_fifo (fifo_segment_header_t * fsh, fifo_segment_slice_t * fss, goto done; f = fs_try_alloc_fifo_freelist (fss, fl_index, data_bytes); + if (f) + fsh_cached_bytes_sub (fsh, fs_freelist_index_to_size (fl_index)); goto done; } if (fifo_sz <= n_free_bytes) @@ -520,6 +555,7 @@ fsh_alloc_chunk (fifo_segment_header_t * fsh, u32 slice_index, u32 chunk_size) fss->free_chunks[fl_index] = c->next; c->next = 0; fss->n_fl_chunk_bytes -= fs_freelist_index_to_size (fl_index); + fsh_cached_bytes_sub (fsh, fs_freelist_index_to_size (fl_index)); } done: @@ -530,10 +566,12 @@ done: } static void -fsh_slice_collect_chunks (fifo_segment_slice_t * fss, svm_fifo_chunk_t * cur) +fsh_slice_collect_chunks (fifo_segment_header_t * fsh, + fifo_segment_slice_t * fss, svm_fifo_chunk_t * cur) { svm_fifo_chunk_t *next; int fl_index; + u32 n_collect = 0; clib_spinlock_lock (&fss->chunk_lock); @@ -545,10 +583,13 @@ fsh_slice_collect_chunks (fifo_segment_slice_t * fss, svm_fifo_chunk_t * cur) cur->enq_rb_index = RBTREE_TNIL_INDEX; cur->deq_rb_index = RBTREE_TNIL_INDEX; fss->free_chunks[fl_index] = cur; - fss->n_fl_chunk_bytes += fs_freelist_index_to_size (fl_index); + n_collect += fs_freelist_index_to_size (fl_index); cur = next; } + fss->n_fl_chunk_bytes += n_collect; + fsh_cached_bytes_add (fsh, n_collect); + clib_spinlock_unlock (&fss->chunk_lock); } @@ -558,7 +599,7 @@ fsh_collect_chunks (fifo_segment_header_t * fsh, u32 slice_index, { fifo_segment_slice_t *fss; fss = fsh_slice_get (fsh, slice_index); - fsh_slice_collect_chunks (fss, cur); + fsh_slice_collect_chunks (fsh, fss, cur); } /** @@ -644,7 +685,7 @@ fifo_segment_free_fifo (fifo_segment_t * fs, svm_fifo_t * f) fss->free_fifos = f; /* Free fifo chunks */ - fsh_slice_collect_chunks (fss, f->start_chunk); + fsh_slice_collect_chunks (fsh, fss, f->start_chunk); f->start_chunk = f->end_chunk = 0; f->head_chunk = f->tail_chunk = f->ooo_enq = f->ooo_deq = 0; @@ -746,6 +787,7 @@ fifo_segment_prealloc_fifo_chunks (fifo_segment_t * fs, u32 slice_index, c->next = fss->free_chunks[fl_index]; fss->free_chunks[fl_index] = c; cmem += sizeof (*c) + rounded_data_size; + fsh_cached_bytes_add (fsh, rounded_data_size); } fss->n_fl_chunk_bytes += batch_size * rounded_data_size; @@ -925,12 +967,36 @@ fifo_segment_update_free_bytes (fifo_segment_t * fs) fsh_update_free_bytes (fs->h); } +uword +fifo_segment_size (fifo_segment_t * fs) +{ + return fs->ssvm.ssvm_size; +} + +u8 +fsh_has_reached_mem_limit (fifo_segment_header_t * fsh) +{ + return (fsh->flags & FIFO_SEGMENT_F_MEM_LIMIT) ? 1 : 0; +} + +void +fsh_reset_mem_limit (fifo_segment_header_t * fsh) +{ + fsh->flags &= ~FIFO_SEGMENT_F_MEM_LIMIT; +} + uword fifo_segment_free_bytes (fifo_segment_t * fs) { return fsh_n_free_bytes (fs->h); } +uword +fifo_segment_cached_bytes (fifo_segment_t * fs) +{ + return fsh_n_cached_bytes (fs->h); +} + uword fifo_segment_fl_chunk_bytes (fifo_segment_t * fs) { @@ -974,6 +1040,52 @@ fifo_segment_get_slice_fifo_list (fifo_segment_t * fs, u32 slice_index) return fss->fifos; } +u8 +fifo_segment_get_mem_usage (fifo_segment_t * fs) +{ + uword size, in_use; + + size = fifo_segment_size (fs); + in_use = + size - fifo_segment_free_bytes (fs) - fifo_segment_cached_bytes (fs); + return (in_use * 100) / size; +} + +fifo_segment_mem_status_t +fifo_segment_determine_status (fifo_segment_header_t * fsh, u8 usage) +{ + if (!fsh->high_watermark || !fsh->low_watermark) + return MEMORY_PRESSURE_NO_PRESSURE; + + /* once the no-memory is detected, the status continues + * until memory usage gets below the high watermark + */ + if (fsh_has_reached_mem_limit (fsh)) + { + if (usage >= fsh->high_watermark) + return MEMORY_PRESSURE_NO_MEMORY; + else + fsh_reset_mem_limit (fsh); + } + + if (usage >= fsh->high_watermark) + return MEMORY_PRESSURE_HIGH_PRESSURE; + + else if (usage >= fsh->low_watermark) + return MEMORY_PRESSURE_LOW_PRESSURE; + + return MEMORY_PRESSURE_NO_PRESSURE; +} + +fifo_segment_mem_status_t +fifo_segment_get_mem_status (fifo_segment_t * fs) +{ + fifo_segment_header_t *fsh = fs->h; + u8 usage = fifo_segment_get_mem_usage (fs); + + return fifo_segment_determine_status (fsh, usage); +} + u8 * format_fifo_segment_type (u8 * s, va_list * args) { @@ -1003,6 +1115,7 @@ format_fifo_segment (u8 * s, va_list * args) int verbose __attribute__ ((unused)) = va_arg (*args, int); uword est_chunk_bytes, est_free_seg_bytes, free_chunks; uword chunk_bytes = 0, free_seg_bytes, chunk_size; + uword tracked_cached_bytes; fifo_segment_header_t *fsh; fifo_segment_slice_t *fss; svm_fifo_chunk_t *c; @@ -1010,6 +1123,9 @@ format_fifo_segment (u8 * s, va_list * args) char *address; size_t size; int i; + uword allocated, in_use; + f64 usage; + fifo_segment_mem_status_t mem_st; indent = format_get_indent (s) + 2; @@ -1068,19 +1184,31 @@ format_fifo_segment (u8 * s, va_list * args) est_free_seg_bytes = fifo_segment_free_bytes (fs); fifo_segment_update_free_bytes (fs); free_seg_bytes = fifo_segment_free_bytes (fs); + tracked_cached_bytes = fifo_segment_cached_bytes (fs); + allocated = fifo_segment_size (fs); + in_use = fifo_segment_size (fs) - est_free_seg_bytes - tracked_cached_bytes; + usage = (100.0 * in_use) / allocated; + mem_st = fifo_segment_get_mem_status (fs); s = format (s, "\n%Useg free bytes: %U (%lu) estimated: %U (%lu)\n", format_white_space, indent + 2, format_memory_size, free_seg_bytes, free_seg_bytes, format_memory_size, est_free_seg_bytes, est_free_seg_bytes); - s = format (s, "%Uchunk free bytes: %U (%lu) estimated: %U (%lu)\n", - format_white_space, indent + 2, format_memory_size, chunk_bytes, - chunk_bytes, format_memory_size, est_chunk_bytes, - est_chunk_bytes); - s = format (s, "%Ufifo hdr free bytes: %U (%u) reserved %U (%lu)\n", - format_white_space, indent + 2, format_memory_size, fifo_hdr, - fifo_hdr, format_memory_size, fsh->n_reserved_bytes, - fsh->n_reserved_bytes); + s = + format (s, + "%Uchunk free bytes: %U (%lu) estimated: %U (%lu) tracked: %U (%lu)\n", + format_white_space, indent + 2, format_memory_size, chunk_bytes, + chunk_bytes, format_memory_size, est_chunk_bytes, est_chunk_bytes, + format_memory_size, tracked_cached_bytes, tracked_cached_bytes); + s = + format (s, "%Ufifo hdr free bytes: %U (%u) reserved %U (%lu)\n", + format_white_space, indent + 2, format_memory_size, fifo_hdr, + fifo_hdr, format_memory_size, fsh->n_reserved_bytes, + fsh->n_reserved_bytes); + s = + format (s, "%Usegment usage: %.2f%% (%U / %U) %s\n", format_white_space, + indent + 2, usage, format_memory_size, in_use, format_memory_size, + allocated, fifo_segment_mem_status_strings[mem_st]); s = format (s, "\n"); return s; diff --git a/src/svm/fifo_segment.h b/src/svm/fifo_segment.h index 85548063972..4e950f6b4f0 100644 --- a/src/svm/fifo_segment.h +++ b/src/svm/fifo_segment.h @@ -39,6 +39,30 @@ typedef enum fifo_segment_flags_ FIFO_SEGMENT_F_MEM_LIMIT = 1 << 2, } fifo_segment_flags_t; +#define foreach_segment_mem_status \ +_(NO_PRESSURE, "No pressure") \ +_(LOW_PRESSURE, "Low pressure") \ +_(HIGH_PRESSURE, "High pressure") \ +_(NO_MEMORY, "No memory") + +typedef enum +{ +#define _(sym,str) MEMORY_PRESSURE_##sym, + foreach_segment_mem_status +#undef _ + MEMORY_N_PRESSURE, +} fifo_segment_mem_status_t; + +#if 0 +typedef enum fifo_segment_mem_status_ +{ + MEMORY_PRESSURE_NO_PRESSURE, + MEMORY_PRESSURE_LOW_PRESSURE, + MEMORY_PRESSURE_HIGH_PRESSURE, + MEMORY_PRESSURE_NO_MEMORY, +} fifo_segment_mem_status_t; +#endif + typedef struct { ssvm_private_t ssvm; /**< ssvm segment data */ @@ -146,6 +170,31 @@ svm_fifo_chunk_t *fsh_alloc_chunk (fifo_segment_header_t * fsh, void fsh_collect_chunks (fifo_segment_header_t * fsh, u32 slice_index, svm_fifo_chunk_t * cur); +/** + * Fifo segment has reached mem limit + * + * @param fsh fifo segment header + * @return 1 (if reached) or 0 (otherwise) + */ +u8 fsh_has_reached_mem_limit (fifo_segment_header_t * fsh); + +/** + * Fifo segment reset mem limit flag + * + * @param fs fifo segment + */ +void fsh_reset_mem_limit (fifo_segment_header_t * fsh); + +/** + * Fifo segment allocated size + * + * Returns fifo segment's allocated size + * + * @param fs fifo segment + * @return allocated size in bytes + */ +uword fifo_segment_size (fifo_segment_t * fs); + /** * Fifo segment estimate of number of free bytes * @@ -168,6 +217,16 @@ uword fifo_segment_free_bytes (fifo_segment_t * fs); */ void fifo_segment_update_free_bytes (fifo_segment_t * fs); +/** + * Fifo segment number of cached bytes + * + * Returns fifo segment's number of cached bytes. + * + * @param fs fifo segment + * @return cached bytes + */ +uword fifo_segment_cached_bytes (fifo_segment_t * fs); + /** * Number of bytes on chunk free lists * @@ -189,6 +248,11 @@ u32 fifo_segment_num_free_fifos (fifo_segment_t * fs); */ u32 fifo_segment_num_free_chunks (fifo_segment_t * fs, u32 size); +u8 fifo_segment_get_mem_usage (fifo_segment_t * fs); +fifo_segment_mem_status_t fifo_segment_determine_status + (fifo_segment_header_t * fsh, u8 usage); +fifo_segment_mem_status_t fifo_segment_get_mem_status (fifo_segment_t * fs); + void fifo_segment_main_init (fifo_segment_main_t * sm, u64 baseva, u32 timeout_in_seconds); diff --git a/src/svm/fifo_types.h b/src/svm/fifo_types.h index 3e6a14eea7d..f0a286d46a0 100644 --- a/src/svm/fifo_types.h +++ b/src/svm/fifo_types.h @@ -112,11 +112,14 @@ struct fifo_segment_header_ fifo_segment_slice_t *slices; /** Fixed array of slices */ ssvm_shared_header_t *ssvm_sh; /**< Pointer to fs ssvm shared hdr */ uword n_free_bytes; /**< Segment free bytes */ + uword n_cached_bytes; /**< Cached bytes */ u32 n_active_fifos; /**< Number of active fifos */ u32 n_reserved_bytes; /**< Bytes not to be allocated */ u32 max_log2_chunk_size; /**< Max log2(chunk size) for fs */ u8 flags; /**< Segment flags */ u8 n_slices; /**< Number of slices */ + u8 high_watermark; /**< Memory pressure watermark high */ + u8 low_watermark; /**< Memory pressure watermark low */ }; #endif /* SRC_SVM_FIFO_TYPES_H_ */ diff --git a/src/vnet/session/application.c b/src/vnet/session/application.c index 646d6b6de2c..0dcb3e6ad94 100644 --- a/src/vnet/session/application.c +++ b/src/vnet/session/application.c @@ -550,6 +550,10 @@ application_alloc_and_init (app_init_args_t * a) props->use_mq_eventfd = 1; if (options[APP_OPTIONS_TLS_ENGINE]) app->tls_engine = options[APP_OPTIONS_TLS_ENGINE]; + if (options[APP_OPTIONS_HIGH_WATERMARK]) + props->high_watermark = options[APP_OPTIONS_HIGH_WATERMARK]; + if (options[APP_OPTIONS_LOW_WATERMARK]) + props->low_watermark = options[APP_OPTIONS_LOW_WATERMARK]; props->segment_type = seg_type; /* Add app to lookup by api_client_index table */ @@ -698,8 +702,7 @@ application_alloc_worker_and_init (application_t * app, app_worker_t ** wrk) sm = segment_manager_alloc (); sm->app_wrk_index = app_wrk->wrk_index; - if ((rv = segment_manager_init (sm, app->sm_properties.segment_size, - app->sm_properties.prealloc_fifos))) + if ((rv = segment_manager_init (sm))) { app_worker_free (app_wrk); return rv; diff --git a/src/vnet/session/application_interface.h b/src/vnet/session/application_interface.h index 552fec1b6ef..d03994fc1f9 100644 --- a/src/vnet/session/application_interface.h +++ b/src/vnet/session/application_interface.h @@ -201,6 +201,8 @@ typedef enum APP_OPTIONS_PROXY_TRANSPORT, APP_OPTIONS_ACCEPT_COOKIE, APP_OPTIONS_TLS_ENGINE, + APP_OPTIONS_HIGH_WATERMARK, + APP_OPTIONS_LOW_WATERMARK, APP_OPTIONS_N_OPTIONS } app_attach_options_index_t; diff --git a/src/vnet/session/segment_manager.c b/src/vnet/session/segment_manager.c index 0a54b96a125..25fbd6f2c04 100644 --- a/src/vnet/session/segment_manager.c +++ b/src/vnet/session/segment_manager.c @@ -29,6 +29,8 @@ typedef struct segment_manager_main_ u32 default_fifo_size; /**< default rx/tx fifo size */ u32 default_segment_size; /**< default fifo segment size */ u32 default_app_mq_size; /**< default app msg q size */ + u8 default_high_watermark; /**< default high watermark % */ + u8 default_low_watermark; /**< default low watermark % */ } segment_manager_main_t; static segment_manager_main_t sm_main; @@ -54,6 +56,8 @@ segment_manager_props_init (segment_manager_props_t * props) props->rx_fifo_size = sm_main.default_fifo_size; props->tx_fifo_size = sm_main.default_fifo_size; props->evt_q_size = sm_main.default_app_mq_size; + props->high_watermark = sm_main.default_high_watermark; + props->low_watermark = sm_main.default_low_watermark; props->n_slices = vlib_num_workers () + 1; return props; } @@ -160,6 +164,13 @@ segment_manager_add_segment (segment_manager_t * sm, uword segment_size) */ fs_index = fs - sm->segments; + /* + * Set watermarks in segment + */ + fs->h->high_watermark = sm->high_watermark; + fs->h->low_watermark = sm->low_watermark; + fs->h->flags &= ~FIFO_SEGMENT_F_MEM_LIMIT; + done: if (vlib_num_workers ()) @@ -307,11 +318,12 @@ segment_manager_alloc (void) * Returns error if ssvm segment(s) allocation fails. */ int -segment_manager_init (segment_manager_t * sm, uword first_seg_size, - u32 prealloc_fifo_pairs) +segment_manager_init (segment_manager_t * sm) { u32 rx_fifo_size, tx_fifo_size, pair_size; u32 rx_rounded_data_size, tx_rounded_data_size; + uword first_seg_size; + u32 prealloc_fifo_pairs; u64 approx_total_size, max_seg_size = ((u64) 1 << 32) - (128 << 10); segment_manager_props_t *props; fifo_segment_t *segment; @@ -319,7 +331,13 @@ segment_manager_init (segment_manager_t * sm, uword first_seg_size, int seg_index, i; props = segment_manager_properties_get (sm); - first_seg_size = clib_max (first_seg_size, sm_main.default_segment_size); + first_seg_size = clib_max (props->segment_size, + sm_main.default_segment_size); + prealloc_fifo_pairs = props->prealloc_fifos; + + segment_manager_set_watermarks (sm, + props->high_watermark, + props->low_watermark); if (prealloc_fifo_pairs) { @@ -434,6 +452,27 @@ segment_manager_get_if_valid (u32 index) return pool_elt_at_index (sm_main.segment_managers, index); } +static fifo_segment_t * +find_max_free_segment (segment_manager_t * sm, u32 thread_index) +{ + fifo_segment_t *cur, *fs = 0; + uword free_bytes, max_free_bytes = 0; + + clib_rwlock_reader_lock (&sm->segments_rwlock); + /* *INDENT-OFF* */ + pool_foreach (cur, sm->segments, ({ + if ((free_bytes = fifo_segment_free_bytes (cur)) > max_free_bytes) + { + max_free_bytes = free_bytes; + fs = cur; + } + })); + /* *INDENT-ON* */ + clib_rwlock_reader_unlock (&sm->segments_rwlock); + + return fs; +} + u32 segment_manager_index (segment_manager_t * sm) { @@ -569,19 +608,25 @@ segment_manager_alloc_session_fifos (segment_manager_t * sm, /* * Find the first free segment to allocate the fifos in */ + fs = find_max_free_segment (sm, thread_index); - /* *INDENT-OFF* */ - segment_manager_foreach_segment_w_lock (fs, sm, ({ - alloc_fail = segment_manager_try_alloc_fifos (fs, - thread_index, - props->rx_fifo_size, - props->tx_fifo_size, - rx_fifo, tx_fifo); - /* Exit with lock held, drop it after notifying app */ - if (!alloc_fail) - goto alloc_success; - })); - /* *INDENT-ON* */ + if (fs) + { + clib_rwlock_reader_lock (&sm->segments_rwlock); + alloc_fail = segment_manager_try_alloc_fifos (fs, thread_index, + props->rx_fifo_size, + props->tx_fifo_size, + rx_fifo, tx_fifo); + /* On success, keep lock until fifos are initialized */ + if (!alloc_fail) + goto alloc_success; + + segment_manager_segment_reader_unlock (sm); + } + else + { + alloc_fail = 1; + } alloc_check: @@ -787,6 +832,8 @@ segment_manager_main_init (segment_manager_main_init_args_t * a) sm->default_fifo_size = 1 << 12; sm->default_segment_size = 1 << 20; sm->default_app_mq_size = 128; + sm->default_high_watermark = 80; + sm->default_low_watermark = 50; } static clib_error_t * @@ -910,6 +957,18 @@ segment_manager_format_sessions (segment_manager_t * sm, int verbose) clib_rwlock_reader_unlock (&sm->segments_rwlock); } +void +segment_manager_set_watermarks (segment_manager_t * sm, + u8 high_watermark, u8 low_watermark) +{ + ASSERT (high_watermark >= 0 && high_watermark <= 100 && + low_watermark >= 0 && low_watermark <= 100 && + low_watermark <= high_watermark); + + sm->high_watermark = high_watermark; + sm->low_watermark = low_watermark; +} + /* * fd.io coding-style-patch-verification: ON * diff --git a/src/vnet/session/segment_manager.h b/src/vnet/session/segment_manager.h index 52f89eef171..cad95509b2e 100644 --- a/src/vnet/session/segment_manager.h +++ b/src/vnet/session/segment_manager.h @@ -34,6 +34,8 @@ typedef struct _segment_manager_props u8 n_slices; /**< number of fs slices/threads */ ssvm_segment_type_t segment_type; /**< seg type: if set to SSVM_N_TYPES, private segments are used */ + u8 high_watermark; /**< memory usage high watermark % */ + u8 low_watermark; /**< memory usage low watermark % */ } segment_manager_props_t; typedef struct _segment_manager @@ -58,6 +60,9 @@ typedef struct _segment_manager * App event queue allocated in first segment */ svm_msg_q_t *event_queue; + + u8 high_watermark; + u8 low_watermark; } segment_manager_t; typedef struct segment_manager_main_init_args_ @@ -69,8 +74,7 @@ typedef struct segment_manager_main_init_args_ #define SEGMENT_MANAGER_INVALID_APP_INDEX ((u32) ~0) segment_manager_t *segment_manager_alloc (void); -int segment_manager_init (segment_manager_t * sm, uword first_seg_size, - u32 prealloc_fifo_pairs); +int segment_manager_init (segment_manager_t * sm); /** * Cleanup segment manager @@ -118,6 +122,9 @@ int segment_manager_try_alloc_fifos (fifo_segment_t * fs, void segment_manager_dealloc_fifos (svm_fifo_t * rx_fifo, svm_fifo_t * tx_fifo); +void segment_manager_set_watermarks (segment_manager_t * sm, + u8 high_watermark, u8 low_watermark); + u8 segment_manager_has_fifos (segment_manager_t * sm); svm_msg_q_t *segment_manager_alloc_queue (fifo_segment_t * fs, -- cgit 1.2.3-korg