Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:46249 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 83960 invoked from network); 30 Nov 2009 13:32:31 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 30 Nov 2009 13:32:31 -0000 Authentication-Results: pb1.pair.com smtp.mail=yoarvi@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=yoarvi@gmail.com; sender-id=pass; domainkeys=bad Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.222.176 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: yoarvi@gmail.com X-Host-Fingerprint: 209.85.222.176 mail-pz0-f176.google.com Received: from [209.85.222.176] ([209.85.222.176:43391] helo=mail-pz0-f176.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 31/37-43487-E69C31B4 for ; Mon, 30 Nov 2009 08:32:31 -0500 Received: by pzk6 with SMTP id 6so2411420pzk.29 for ; Mon, 30 Nov 2009 05:32:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:date:message-id:subject :from:to:content-type; bh=i912FBcbDB4unlTSDlYf9Fc2S+0xEL3++AcqbssRMCQ=; b=Js7HTN+JRBOZV9zmm5slsRlWlAxO3aMNaFGSrMEhWcdDtYOL14ep+w1Xc5cnLR57ic ZP+Xp6Uo9v7Jb5Ggu3TAj9Kp0msY9+V6ZvRRjTbC+063+neIvREz4ripeWNccv+4UD6D ueiKEw9elfi7eNSVe6FRnnZH5CfWNI8tL3frc= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; b=jSb3XZ9qWWhmq21Cq/Shmw71/F392JJpgIsssU1EU8nTmfJl6YfbChedsieeTN2guj 9zkowRLd/aFuwhxJDfLvKCmsDV3wJVsY65quHCRBCjiJvAZ/7pNIFx6BYjen/6FmwicR Ivm6uwoTFT3b6xH/8uctMAEw9+T0yJo7fcRo0= MIME-Version: 1.0 Received: by 10.142.4.41 with SMTP id 41mr420706wfd.123.1259587948008; Mon, 30 Nov 2009 05:32:28 -0800 (PST) Date: Mon, 30 Nov 2009 19:02:27 +0530 Message-ID: To: internals@lists.php.net Content-Type: text/plain; charset=ISO-8859-1 Subject: [PATCH] - Improving multi-threaded performance by propagating TSRMLS_C From: yoarvi@gmail.com (Arvind Srinivasan) When running a benchmarking workload on PHP that was configured with multi-threading support (--enable-maintainer-zts) I noticed that pthread_get_specific is invoked many times during the processing of a request. PHP code invokes TSRMLS_FETCH() (which ends up invoking ts_resource_ex) in a number of places. Caller/callee data from Sun Studio's collector/analyzer showed the following: Attr. Excl. Incl. Name User CPU User CPU User CPU sec. sec. sec. 178.105 185.460 363.564 _emalloc 96.568 114.320 210.888 _efree 27.960 89.232 343.901 _zval_ptr_dtor 19.544 6.685 26.228 php_body_write_wrapper 6.925 36.806 224.617 _zval_dtor_func 4.263 2.902 7.165 safe_free_zval_ptr_rel 4.163 11.898 16.061 zend_get_parameters_ex 4.013 14.690 174.682 my_copy_zval 3.963 6.775 10.738 _erealloc 3.502 12.399 978.444 apc_copy_function_for_execution 2.732 4.143 9.647 do_inherit_method_check 2.592 21.565 225.137 _zval_copy_ctor_func 0.881 22.095 106.535 virtual_file_ex 0.600 1.961 6.855 list_entry_destructor 0.470 1.301 24.397 zend_file_handle_dtor 0.410 1.781 2.712 zend_function_dtor 0.270 0.350 0.620 convert_to_array 0.220 0.991 15.831 apc_search_paths 0.150 0.490 3.362 zend_register_resource 0.140 1.581 10.137 zend_alter_ini_entry 0.130 4.833 9023.272 php5_execute 0.110 0.500 3.502 zend_ini_long 0.070 0.530 0.600 _zend_bailout 0.050 0.320 4.513 zend_error 0.040 0.690 3.913 php_error_cb 0.040 0.560 2.852 zend_alter_ini_entry_ex 0. 3.202 584.369 php_request_shutdown 274.252 274.252 357.910 *ts_resource_ex 83.659 84.749 84.749 pthread_getspecific Propagating the value of TSRMLS_CC will avoid the overhead of having to invoke tsrm_tls_get/pthread_get_specific. The following patch (against trunk) does this: http://bitbucket.org/arvi/arviq/src/tip/svn-TSRM-patch.txt The above patch is mostly the result of making zend_alloc.[ch] TSRMLS_C-aware. While modifying the APIs to add propagate TSRMLS_C, I didn't quite know the best/preferred way to make the changes so I tried to use the following guidelines: (1) If the number of occurrences of the API to be modified are relatively small, then make the change directly. e.g. -int fcgi_accept_request(fcgi_request *req); -int fcgi_finish_request(fcgi_request *req, int force_close); +int fcgi_accept_request(fcgi_request *req TSRMLS_DC); +int fcgi_finish_request(fcgi_request *req, int force_close TSRMLS_DC); (2) If the number of occurrences is large, then rename the function (prefix with _) and add a macro that passes TSRMLS_C e.g. -ZEND_API void zend_hash_destroy(HashTable *ht); +ZEND_API void _zend_hash_destroy(HashTable *ht TSRMLS_DC); +#define zend_hash_destroy(ht) _zend_hash_destroy((ht) TSRMLS_CC) (3) For functions that have varargs, pass TSRMLS_C as the last but one parameter e.g. -ZEND_API int zend_get_parameters(int ht, int param_count, ...); +ZEND_API int zend_get_parameters(int ht TSRMLS_DC, int param_count, ...); (4) For single argument functions that have varags, pass TSRMLS_C as the first parameter e.g. -ZEND_API int zend_get_parameters_ex(int param_count, ...) /* {{{ */ +ZEND_API int zend_get_parameters_ex(TSRMLS_DC1 int param_count, ...) /* {{{ */ where +#define TSRMLS_DC1 TSRMLS_D, +#define TSRMLS_CC1 TSRMLS_C, Is (2) an acceptable thing to do or should I avoid renaming functions and do a mongo search-replace? With respect to (4), I wasn't sure how else to tackle varargs functions that take a single parameter. Any direction on what I should do for (3), (4) would be much appreciated. Out of curiosity, I counted the number of times thread_resources = tsrm_tls_get(); in ts_resource_ex is invoked for a simple command line invocation of hello.php (). Before patch: 22125 times After patch: 119 times Most of this is probably in one-time initialization stuff. I hope to have my trunk tree hooked up to my benchmarking setup soon and then I'll have more meaningful numbers. Thanks, Arvi