Prechádzať zdrojové kódy

rtpengine: kamctl fifo nh_show_hash_total

Print the total number of hash entries in the hash table, at the given moment.
Updated doku.
Stefan Mititelu 10 rokov pred
rodič
commit
74fdbe2248

+ 17 - 0
modules/rtpengine/doc/rtpengine_admin.xml

@@ -1041,6 +1041,23 @@ $ &ctltool; fifo nh_ping_rtpp all
 			</programlisting>
 			</example>
 		</section>
+
+	    <section id="rtpengine.m.nh_show_hash_total">
+			<title><function moreinfo="none">nh_show_hash_total</function></title>
+			<para>
+				Print the total number of hash entries in the hash table at a given moment.
+			</para>
+			<example>
+			<title>
+				<function moreinfo="none">nh_show_hash_total</function> usage</title>
+			<programlisting format="linespecific">
+...
+$ &ctltool; fifo nh_show_hash_total
+...
+			</programlisting>
+			</example>
+		</section>
+
 	</section>
 
 </chapter>

+ 54 - 10
modules/rtpengine/rtpengine.c

@@ -109,6 +109,7 @@ MODULE_VERSION
 #define MI_ENABLE_RTP_PROXY			"nh_enable_rtpp"
 #define MI_SHOW_RTP_PROXIES			"nh_show_rtpp"
 #define MI_PING_RTP_PROXY           "nh_ping_rtpp"
+#define MI_SHOW_HASH_TOTAL          "nh_show_hash_total"
 
 #define MI_RTP_PROXY_NOT_FOUND		"RTP proxy not found"
 #define MI_RTP_PROXY_NOT_FOUND_LEN	(sizeof(MI_RTP_PROXY_NOT_FOUND)-1)
@@ -143,6 +144,10 @@ MODULE_VERSION
 #define MI_SUCCESS_LEN         		(sizeof(MI_SUCCESS)-1)
 #define MI_FAIL                     "fail"
 #define MI_FAIL_LEN         		(sizeof(MI_FAIL)-1)
+#define MI_HASH_ENTRIES				"entries"
+#define MI_HASH_ENTRIES_LEN			(sizeof(MI_HASH_ENTRIES)-1)
+#define MI_HASH_ENTRIES_FAIL		"Fail to get entry details"
+#define MI_HASH_ENTRIES_FAIL_LEN	(sizeof(MI_HASH_ENTRIES_FAIL)-1)
 
 #define MI_FOUND_ALL                   2
 #define MI_FOUND_ONE                   1
@@ -215,12 +220,10 @@ static int rtpp_test_ping(struct rtpp_node *node);
 static int pv_get_rtpstat_f(struct sip_msg *, pv_param_t *, pv_value_t *);
 
 /*mi commands*/
-static struct mi_root* mi_enable_rtp_proxy(struct mi_root* cmd_tree,
-		void* param );
-static struct mi_root* mi_show_rtp_proxy(struct mi_root* cmd_tree,
-		void* param);
-static struct mi_root* mi_ping_rtp_proxy(struct mi_root* cmd_tree,
-        void* param);
+static struct mi_root* mi_enable_rtp_proxy(struct mi_root* cmd_tree, void* param);
+static struct mi_root* mi_show_rtp_proxy(struct mi_root* cmd_tree, void* param);
+static struct mi_root* mi_ping_rtp_proxy(struct mi_root* cmd_tree, void* param);
+static struct mi_root* mi_show_hash_total(struct mi_root* cmd_tree, void* param);
 
 
 static int rtpengine_disable_tout = 60;
@@ -350,6 +353,7 @@ static mi_export_t mi_cmds[] = {
 	{MI_ENABLE_RTP_PROXY,     mi_enable_rtp_proxy,  0,  0,  0},
 	{MI_SHOW_RTP_PROXIES,     mi_show_rtp_proxy,    0,  0,  0},
 	{MI_PING_RTP_PROXY,       mi_ping_rtp_proxy,    0,  0,  0},
+	{MI_SHOW_HASH_TOTAL,      mi_show_hash_total,    0,  0,  0},
 	{ 0, 0, 0, 0, 0}
 };
 
@@ -1106,8 +1110,7 @@ error:
 	return -1;
 }
 
-static struct mi_root* mi_show_rtp_proxy(struct mi_root* cmd_tree,
-												void* param)
+static struct mi_root* mi_show_rtp_proxy(struct mi_root* cmd_tree, void* param)
 {
 	struct mi_node *node;
 	struct mi_root *root = NULL;
@@ -1196,8 +1199,7 @@ error:
 	return init_mi_tree(404, MI_ERROR, MI_ERROR_LEN);
 }
 
-static struct mi_root* mi_ping_rtp_proxy(struct mi_root* cmd_tree,
-												void* param)
+static struct mi_root* mi_ping_rtp_proxy(struct mi_root* cmd_tree, void* param)
 {
 	struct mi_node *node, *crt_node;
 	struct mi_attr *attr;
@@ -1322,6 +1324,48 @@ error:
 }
 
 
+static struct mi_root* mi_show_hash_total(struct mi_root* cmd_tree, void* param)
+{
+	struct mi_node *node, *crt_node;
+	struct mi_attr *attr;
+	struct mi_root *root = NULL;
+	unsigned int total;
+	str total_str;
+
+	// Init print tree
+	root = init_mi_tree(200, MI_OK_S, MI_OK_LEN);
+	if (!root) {
+		LM_ERR("the MI tree cannot be initialized!\n");
+		return 0;
+	}
+	node = &root->node;
+
+	// Create new node and add it to the roots's kids
+	if(!(crt_node = add_mi_node_child(node, MI_DUP_NAME, "total", strlen("total"), 0, 0))) {
+		LM_ERR("cannot add the child node to the tree\n");
+		goto error;
+	}
+
+	// Get total number of entries
+	total = rtpengine_hash_table_total();
+	total_str.s = int2str(total, &total_str.len);
+
+	// Add node attributes
+	if ((attr = add_mi_attr(crt_node, MI_DUP_VALUE, MI_HASH_ENTRIES, MI_HASH_ENTRIES_LEN, total_str.s, total_str.len)) == 0) {
+		LM_ERR("cannot add attributes to the node\n");
+		goto error;
+	}
+
+	return root;
+
+error:
+	if (root) {
+	    free_mi_tree(root);
+	}
+
+	return init_mi_tree(404, MI_HASH_ENTRIES_FAIL, MI_HASH_ENTRIES_FAIL_LEN);
+}
+
 
 static int
 mod_init(void)

+ 30 - 0
modules/rtpengine/rtpengine_hash.c

@@ -74,6 +74,7 @@ int rtpengine_hash_table_init(int size) {
 		// never expire the head of the hashtable index lists
 		rtpengine_hash_table->entry_list[i]->tout = -1;
 		rtpengine_hash_table->entry_list[i]->next = NULL;
+		rtpengine_hash_table->total = 0;
 	}
 
 	// init lock
@@ -165,6 +166,9 @@ int rtpengine_hash_table_insert(void *key, void *value) {
 
 			// set pointers
 			entry = last_entry;
+
+			// update total
+			rtpengine_hash_table->total--;
 		}
 
 		// next entry in the list
@@ -174,6 +178,9 @@ int rtpengine_hash_table_insert(void *key, void *value) {
 
 	last_entry->next = new_entry;
 
+	// update total
+	rtpengine_hash_table->total++;
+
 	// unlock
 	lock_release(rtpengine_hash_lock);
 
@@ -205,6 +212,9 @@ int rtpengine_hash_table_remove(void *key) {
 			shm_free(entry->callid.s);
 			shm_free(entry);
 
+			// update total
+			rtpengine_hash_table->total--;
+
 			// unlock
 			lock_release(rtpengine_hash_lock);
 
@@ -222,6 +232,9 @@ int rtpengine_hash_table_remove(void *key) {
 
 			// set pointers
 			entry = last_entry;
+
+			// update total
+			rtpengine_hash_table->total--;
 		}
 
 		last_entry = entry;
@@ -271,6 +284,9 @@ void* rtpengine_hash_table_lookup(void *key) {
 
 			// set pointers
 			entry = last_entry;
+
+			// update total
+			rtpengine_hash_table->total--;
 		}
 
 		last_entry = entry;
@@ -314,6 +330,9 @@ void rtpengine_hash_table_print() {
 
 				// set pointers
 				entry = last_entry;
+
+				// update total
+				rtpengine_hash_table->total--;
 			} else {
 				LM_DBG("hash_index=%d callid=%.*s tout=%u\n",
 					i, entry->callid.len, entry->callid.s, entry->tout - get_ticks());
@@ -327,3 +346,14 @@ void rtpengine_hash_table_print() {
 	// unlock
 	lock_release(rtpengine_hash_lock);
 }
+
+unsigned int rtpengine_hash_table_total() {
+
+	// check rtpengine hashtable
+	if (!rtpengine_hash_table) {
+		LM_ERR("NULL rtpengine_hash_table");
+		return 0;
+	}
+
+	return rtpengine_hash_table->total;
+}

+ 4 - 2
modules/rtpengine/rtpengine_hash.h

@@ -15,7 +15,8 @@ struct rtpengine_hash_entry {
 
 /* table */
 struct rtpengine_hash_table {
-	struct rtpengine_hash_entry **entry_list;
+	struct rtpengine_hash_entry **entry_list;	// hastable
+	unsigned int total;				// total number of entries in the hashtable
 };
 
 
@@ -24,6 +25,7 @@ int rtpengine_hash_table_destroy();
 int rtpengine_hash_table_insert(void *key, void *value);
 int rtpengine_hash_table_remove(void *key);
 void* rtpengine_hash_table_lookup(void *key);
-void rtpengine_hash_table_print() ;
+void rtpengine_hash_table_print();
+unsigned int rtpengine_hash_table_total();
 
 #endif