Improve "show segment-manager segments"

Clean up private-segment fifo preallocation

Change-Id: I53c630ed81d48f2832a204992d73635536926983
Signed-off-by: Dave Barach <dave@barachs.net>
diff --git a/src/vnet/session/segment_manager.c b/src/vnet/session/segment_manager.c
index 7cf66ee..c23e4c0 100644
--- a/src/vnet/session/segment_manager.c
+++ b/src/vnet/session/segment_manager.c
@@ -74,6 +74,11 @@
     }
   else
     {
+      u32 rx_fifo_size, tx_fifo_size, rx_rounded_data_size,
+	tx_rounded_data_size;
+      u32 approx_segment_count;
+      u64 approx_total_size;
+
       ca->segment_name = "process-private-segment";
       ca->segment_size = ~0;
       ca->rx_fifo_size = sm->properties->rx_fifo_size;
@@ -82,6 +87,36 @@
       ca->private_segment_count = sm->properties->private_segment_count;
       ca->private_segment_size = sm->properties->private_segment_size;
 
+      /* Default to a small private segment */
+      if (ca->private_segment_size == 0)
+	ca->private_segment_size = 128 << 20;
+
+      /* Calculate space requirements */
+      rx_rounded_data_size = (1 << (max_log2 (ca->rx_fifo_size)));
+      tx_rounded_data_size = (1 << (max_log2 (ca->tx_fifo_size)));
+
+      rx_fifo_size = sizeof (svm_fifo_t) + rx_rounded_data_size;
+      tx_fifo_size = sizeof (svm_fifo_t) + tx_rounded_data_size;
+
+      approx_total_size = (u64) ca->preallocated_fifo_pairs
+	* (rx_fifo_size + tx_fifo_size);
+      approx_segment_count =
+	(approx_total_size +
+	 (ca->private_segment_size - 1)) / (u64) ca->private_segment_size;
+
+      /* The user asked us to figure it out... */
+      if (ca->private_segment_count == 0)
+	{
+	  ca->private_segment_count = approx_segment_count;
+	}
+      /* Follow directions, but issue a warning */
+      else if (approx_segment_count != ca->private_segment_count)
+	{
+	  clib_warning
+	    ("Honoring segment count %u, but calculated count was %u",
+	     ca->private_segment_count, approx_segment_count);
+	}
+
       if (svm_fifo_segment_create_process_private (ca))
 	clib_warning ("Failed to create process private segment");
 
@@ -503,7 +538,9 @@
   u8 show_segments = 0, verbose = 0, *name;
   uword address;
   u64 size;
-  u32 fifos;
+  u32 active_fifos;
+  u32 free_fifos;
+
   mheap_t *heap_header;
 
   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
@@ -536,8 +573,8 @@
       segments = svm_fifo_segment_segments_pool ();
       vlib_cli_output (vm, "%d svm fifo segments allocated",
 		       pool_elts (segments));
-      vlib_cli_output (vm, "%-20s%=12s%=12s%=15s", "Name", "Size (M)",
-		       "Fifos", "Address");
+      vlib_cli_output (vm, "%-20s%=12s%=16s%=16s%=16s", "Name",
+		       "HeapSize (M)", "ActiveFifos", "FreeFifos", "Address");
 
       /* *INDENT-OFF* */
       pool_foreach (seg, segments, ({
@@ -557,9 +594,14 @@
 	    size = seg->ssvm.ssvm_size;
 	    name = seg->ssvm.sh->name;
 	  }
-	fifos = svm_fifo_segment_num_fifos (seg);
-	vlib_cli_output (vm, "%-20s%=12u%=12u%=15x", name, size << 20, fifos,
+	active_fifos = svm_fifo_segment_num_fifos (seg);
+        free_fifos = svm_fifo_segment_num_free_fifos (seg, ~0 /* size */);
+	vlib_cli_output (vm, "%-20v%=16llu%=16u%=16u%16llx",
+                         name, size >> 20ULL, active_fifos, free_fifos,
 			 address);
+        if (verbose)
+          vlib_cli_output (vm, "%U",
+                           format_svm_fifo_segment, seg, verbose);
 	if (seg->h->flags & FIFO_SEGMENT_F_IS_PRIVATE)
 	  vec_free (name);
       }));
@@ -573,7 +615,7 @@
 VLIB_CLI_COMMAND (segment_manager_show_command, static) =
 {
   .path = "show segment-manager",
-  .short_help = "show segment-manager [segments]",
+  .short_help = "show segment-manager [segments][verbose]",
   .function = segment_manager_show_fn,
 };
 /* *INDENT-ON* */
diff --git a/src/vnet/tcp/builtin_client.c b/src/vnet/tcp/builtin_client.c
index 44b1f70..94e6b4a 100644
--- a/src/vnet/tcp/builtin_client.c
+++ b/src/vnet/tcp/builtin_client.c
@@ -563,12 +563,14 @@
       else if (unformat (input, "private-segment-count %d",
 			 &tm->private_segment_count))
 	;
-      else if (unformat (input, "private-segment-size %dm", &tmp))
-	tm->private_segment_size = tmp << 20;
-      else if (unformat (input, "private-segment-size %dg", &tmp))
-	tm->private_segment_size = tmp << 30;
-      else if (unformat (input, "private-segment-size %d", &tmp))
-	tm->private_segment_size = tmp;
+      else if (unformat (input, "private-segment-size %U",
+			 unformat_memory_size, &tmp))
+	{
+	  if (tmp >= 0x100000000ULL)
+	    return clib_error_return
+	      (0, "private segment size %lld (%llu) too large", tmp, tmp);
+	  tm->private_segment_size = tmp;
+	}
       else if (unformat (input, "preallocate-fifos"))
 	tm->prealloc_fifos = 1;
       else if (unformat (input, "preallocate-sessions"))
diff --git a/src/vnet/tcp/builtin_proxy.c b/src/vnet/tcp/builtin_proxy.c
index d8cfb11..91377e7 100644
--- a/src/vnet/tcp/builtin_proxy.c
+++ b/src/vnet/tcp/builtin_proxy.c
@@ -523,7 +523,7 @@
 {
   builtin_proxy_main_t *bpm = &builtin_proxy_main;
   int rv;
-  u32 tmp;
+  u64 tmp;
 
   bpm->fifo_size = 64 << 10;
   bpm->rcv_buffer_size = 1024;
@@ -542,12 +542,14 @@
       else if (unformat (input, "private-segment-count %d",
 			 &bpm->private_segment_count))
 	;
-      else if (unformat (input, "private-segment-size %dm", &tmp))
-	bpm->private_segment_size = tmp << 20;
-      else if (unformat (input, "private-segment-size %dg", &tmp))
-	bpm->private_segment_size = tmp << 30;
-      else if (unformat (input, "private-segment-size %d", &tmp))
-	bpm->private_segment_size = tmp;
+      else if (unformat (input, "private-segment-size %U",
+			 unformat_memory_size, &tmp))
+	{
+	  if (tmp >= 0x100000000ULL)
+	    return clib_error_return
+	      (0, "private segment size %lld (%llu) too large", tmp, tmp);
+	  bpm->private_segment_size = tmp;
+	}
       else
 	return clib_error_return (0, "unknown input `%U'",
 				  format_unformat_error, input);
diff --git a/src/vnet/tcp/builtin_server.c b/src/vnet/tcp/builtin_server.c
index 295c189..9331452 100644
--- a/src/vnet/tcp/builtin_server.c
+++ b/src/vnet/tcp/builtin_server.c
@@ -369,7 +369,7 @@
   builtin_server_main_t *bsm = &builtin_server_main;
   u8 server_uri_set = 0;
   int rv;
-  u32 tmp;
+  u64 tmp;
 
   bsm->no_echo = 0;
   bsm->fifo_size = 64 << 10;
@@ -392,12 +392,14 @@
       else if (unformat (input, "private-segment-count %d",
 			 &bsm->private_segment_count))
 	;
-      else if (unformat (input, "private-segment-size %dm", &tmp))
-	bsm->private_segment_size = tmp << 20;
-      else if (unformat (input, "private-segment-size %dg", &tmp))
-	bsm->private_segment_size = tmp << 30;
-      else if (unformat (input, "private-segment-size %d", &tmp))
-	bsm->private_segment_size = tmp;
+      else if (unformat (input, "private-segment-size %U",
+			 unformat_memory_size, &tmp))
+	{
+	  if (tmp >= 0x100000000ULL)
+	    return clib_error_return
+	      (0, "private segment size %lld (%llu) too large", tmp, tmp);
+	  bsm->private_segment_size = tmp;
+	}
       else if (unformat (input, "uri %s", &bsm->server_uri))
 	server_uri_set = 1;
       else