--- /dev/null
+#include <vnet/vnet.h>
+#include <vnet/plugin/plugin.h>
+#include <vpp/app/version.h>
+#include <vnet/session/session.h>
+#include <vnet/session/segment_manager.h>
+#include <vnet/session/application.h>
+
+#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:
+ */
+
#include <svm/fifo_segment.h>
+static char *fifo_segment_mem_status_strings[] = {
+#define _(sym,str) str,
+ foreach_segment_mem_status
+#undef _
+};
+
/**
* Fifo segment free space
*
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)
{
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;
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;
f->start_chunk = first;
f->end_chunk = last;
fss->n_fl_chunk_bytes -= n_alloc;
+ fsh_cached_bytes_sub (fsh, n_alloc);
return f;
}
}
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;
{
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);
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)
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:
}
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);
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);
}
{
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);
}
/**
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;
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;
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)
{
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)
{
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;
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;
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;