[asterisk-dev] expensive call - ast_ouraddrfor()

Luigi Rizzo rizzo at icir.org
Tue Jul 17 12:01:24 CDT 2007


just noticed that chan_sip() does a number of calls to
ast_ouraddrfor(), which is used to determine the local
address to use when talking to a remote peer.

This is an extremely expensive function because involves
4 system calls (socket(), connect(), getsockname(), close()).

Local profiling shows approx 60000 ticks, on a 2 GHz box,
which is a significant amount of time (30 microseconds).
This probably should be reconsidered and maybe replaced
with a cache of previous results.

A patch is below if you want to measure it for yourself

	cheers
	luigi

Index: main/acl.c
===================================================================
--- main/acl.c	(revision 75381)
+++ main/acl.c	(working copy)
@@ -332,7 +332,10 @@
 	int s;
 	struct sockaddr_in sin;
 	socklen_t slen;
-
+	static int prof = -1;
+	if (prof == -1)
+		prof = ast_add_profile("ast_ouraddrfor", 0); 
+	ast_mark(prof, 1);
 	s = socket(PF_INET, SOCK_DGRAM, 0);
 	if (s < 0) {
 		ast_log(LOG_WARNING, "Cannot create socket\n");
@@ -354,6 +357,7 @@
 	}
 	close(s);
 	*us = sin.sin_addr;
+	ast_mark(prof, 0);
 	return 0;
 }
 
	cheers	
	luigi

*CLI> core show profile
profile values (21, allocated 30)
-------------------
    ID      Scale      Events        Value      Average  Name
     0: [       0]        104        82548          793  ext_match
     1: [       0]         29      3784700       130506  sip_alloc
     2: [       0]         29       568692        19610  sip_alloc-alloc
     3: [       0]         29      3199456       110326  sip_alloc-init
     4: [       0]          5        73000        14600  sip_alloc-tail
     5: [       0]          6       723484       120580  sip_alloc-need_rtp
     6: [       0]         29       166680         5747  ao2_link
     7: [       0]         33      2162632        65534  ast_ouraddrfor
     8: [       0]        116       510928         4404  sip_read-memset
     9: [       0]        116      1195772        10308  sip_read-recvfrom
    10: [       0]        116       992632         8557  sip_read-parse
    11: [       0]         75      4353748        58049  sip_read-find_call
    12: [       0]        116       275732         2377  sip_read-ast_calloc sip_request
    13: [       0]        116       159140         1371  sip_read-malloc sip_request
    14: [       0]        116       179444         1546  sip_read-copy body
    15: [       0]        116      1072924         9249  sip_read-mtx_init
    16: [       0]        116       317364         2735  sip_read-mtx_destroy
    17: [       0]         75       313172         4175  find_call-headers
    18: [       0]         75       706668         9422  find_call-find
    19: [       0]         17      3218172       189304  find_call-tail
    20: [       0]        177       152932          864  find_call_cb




More information about the asterisk-dev mailing list