How PHP integrates snappy into xhprof

Source: Internet
Author: User
Introduction to Objects
Xhprof:php's performance analysis tool, a PHP extension developed by Facebook, is able to record the call chain of a function/class method in the course of a PHP script, as well as the amount of time spent on each call, memory usage, and so on;
Snappy:google development of the string compression/decompression tool, the advantage is that the compression speed is fast.

Goal
The xhprof extension provides two PHP functions xhprof_compress ($STR) and xhprof_uncompress ($STR) for compression and decompression.

Specific process
1. Download Xhprof source code to Php-5.6.24/ext/xhprof
2. Follow the normal way./configure, make, ensure the xhprof.so is working properly
3. Download snappy source code to Php-5.6.24/ext/xhprof/snappy
4. Edit the file php-5.6.24/ext/xhprof/php_xhprof.h and add the following line:

...
Php_function (xhprof_compress);
Php_function (xhprof_uncompress);
...

5. Edit the file Php-5.6.24/ext/xhprof/xhprof.c and add the following line:

 ... #include "snappy/snappy-c.h" ... zend_function_entry xhprof_functions[] = {... Php_fe (xhprof_compress, NULL) Php_fe (xhprof_uncompress, null) ...};     Php_function (xhprof_compress) {zval *data;     Char *output;     size_t Output_len; if (Zend_parse_parameters (Zend_num_args () tsrmls_cc, "Z", &data) = = FAILURE) {R     Eturn_false; } if (z_type_p (data)! = is_string) {zend_error (e_warning, "snappy_compress:expects param         Eter to be string. ");     Return_false;     } Output_len = Snappy_max_compressed_length (z_strlen_p (data));     Output = (char *) emalloc (Output_len);         if (!output) {zend_error (e_warning, "snappy_compress:memory error");     Return_false; } if (Snappy_compress (z_strval_p data), Z_strlen_p (data), output, &output_len) = = Snappy_o   K) {#if zend_module_api_no >= 20141001 retval_stringl (output, output_len); #else      Retval_stringl (output, Output_len, 1);     #endif} else {retval_false; } efree (output);     } php_function (xhprof_uncompress) {zval *data;     char *output = NULL;     size_t Output_len; if (Zend_parse_parameters (Zend_num_args () tsrmls_cc, "Z", &data) = = FAILURE) {R     Eturn_false; } if (z_type_p (data)! = is_string) {zend_error (e_warning, "snappy_uncompress:expects par         Ameter to be string. ");     Return_false; } if (Snappy_uncompressed_length (z_strval_p data), Z_strlen_p (data), &output_le         n) = Snappy_ok) {zend_error (e_warning, "Snappy_uncompress:output length error");     Return_false;     } output = (char *) emalloc (Output_len);         if (!output) {zend_error (e_warning, "snappy_uncompress:memory error");     Return_false;              } if (Snappy_uncompress (z_strval_p data), Z_strlen_p (data),             Output, &output_len) = = SNAPPY_OK) {#if zend_module_api_no >= 20141001 retval_stringl (output, Output_len); #else Retval_stringl (output, Output_len, 1);         #endif} else {zend_error (e_warning, "Snappy_uncompress:data error");     Retval_false; } efree (output); }...

The above code is not original, PHP extension from snappy: Php-ext-snappy
6. Edit the Config.m4 file to specify how snappy is compiled
The complete CONFIG.M4 code is as follows and will be edited directly into the xhprof.so. Snappy. The better way should be to check the current host is not installed snappy, if any, specify the link library path, there is no need to compile snappy related functions into the xhprof.so inside.

Php_arg_enable (xhprof, whether to enable xhprof support, [--enable-xhprof ENABLE xhprof support]) DNL compiler C + +: Php_require_cxx () dnl snappy snappy_major= "1" snappy_minor= "1" snappy_patchlevel= "3" Ac_prog_cxx Ac_lang ([C + +]) AC_C_  Bigendian ac_check_headers ([stdint.h stddef.h sys/mman.h sys/resource.h windows.h byteswap.h sys/byteswap. H sys/endian.h sys/time.h]) ac_check_func ([mmap]) ac_msg_checking ([if the compiler supports __builtin_expect]) Ac_try_ COMPILE (, [Return __builtin_expect (1, 1)? 1:0], [Snappy_have_builtin_expect=yes ac_msg_result ([yes])], [Snappy_hav E_builtin_expect=no Ac_msg_result ([No])]) if test x$snappy_have_builtin_expect = Xyes; Then Ac_define ([Have_builtin_expect], [1], [DEFINE to 1 if the compiler supports __builtin_expect.]) fi ac_msg_checking ([i f the compiler supports __BUILTIN_CTZLL]) ac_try_compile (, [Return (__builtin_ctzll (0X100000000LL) = =)? 1:0], [SN Appy_have_builtin_ctz=yes Ac_msg_result ([yes])], [Snappy_have_builtin_ctz=no AC_msg_result ([No])]) if test x$snappy_have_builtin_ctz = Xyes; Then Ac_define ([Have_builtin_ctz], [1], [DEFINE to 1 if the compiler supports __BUILTIN_CTZ and friends.]) fi if Te St "$ac _cv_header_stdint_h" = "yes"; Then Ac_subst ([Ac_cv_have_stdint_h], [1]) Else AC_SUBST ([Ac_cv_have_stdint_h], [0]) fi if test "$ac _cv_header_stddef_h" = "Yes"; Then Ac_subst ([Ac_cv_have_stddef_h], [1]) Else AC_SUBST ([ac_cv_have_stddef_h], [0]) fi if test "$ac _cv_header_sys_uio_h "=" yes "; Then Ac_subst ([Ac_cv_have_sys_uio_h], [1]) Else AC_SUBST ([Ac_cv_have_sys_uio_h], [0]) fi ac_subst ([snappy_major]) AC_ SUBST ([Snappy_minor]) Ac_subst ([Snappy_patchlevel]) ac_config_files ([snappy/snappy-stubs-public.h]) AC_OUTPUTDNL Check for stdc++ libname=stdc++ ac_msg_checking ([for stdc++]) ac_lang_save ac_lang_cplusplus ac_try_compile ([#include & Lt;string> using namespace std; ],[string Dummy;],[ac_msg_result (yes) php_add_library ($LIBNAME, Xhprof_shared_libadd)],[ac_msg_error ([Wrong stdc+ + Library NOT found]) Ac_lang_restore Php_subst (xhprof_shared_libadd) dnl Sources snappy_sources= "snappy/snappy-c.cc snappy/ snappy.cc snappy/snappy-stubs-internal.cc snappy/snappy-sinksource.cc "if test" $PHP _xhprof "! =" no "; Then Php_new_extension (xhprof, XHPROF.C $SNAPPY _sources, $ext _shared) fi

The above code is not original, from snappy PHP extension: Php-ext-snappy
7. After re-executing phpize, execute make to generate a new xhprof.so
8. Execute command NM xhprof.so See if snappy related functions have been successfully compiled, such as the following, _snappy_compress,_snappy_uncompress and other memory addresses are non-null, the instructions are compiled into the xhprof.so.

➜xhprof nm modules/xhprof.so0000000000008a98 s Gcc_except_table100000000000008ad8 s Gcc_except_ Table110000000000008b24 s gcc_except_table120000000000008b70 s gcc_except_table130000000000008bb0 s GCC_except_ TABLE140000000000008BFC s gcc_except_table160000000000008c48 s GCC_EXCEPT_TABLE180000000000008CBC s GCC_except_ table190000000000008d4c s Gcc_except_table2300000000000088f4 s gcc_except_table50000000000008934 s GCC_except_ table70000000000008a0c s gcc_except_table80000000000008a4c s gcc_except_table9 ... 0000000000002510 t _restore_cpu_affinity0000000000003f00 t _snappy_compress0000000000003f60 t _snappy_max_compressed _length0000000000003f70 t _snappy_uncompress0000000000003fe0 t _snappy_uncompressed_length0000000000004000 t _snappy                 _validate_compressed_buffer ... U _zend_register_long_constant u _zend_unregister_ini_entries00000000000013a0 T _zif_xhprof_compress000000 0000001090 t _zif_xhprof_disable0000000000001350 t _zif_xhprof_dump0000000000001040 t _zif_xhprof_enable0000000000001200 T _zif_xhprof_sample_disable00000000000011e0 t _zif_xhprof_sample_ enable0000000000001470 t _zif_xhprof_uncompress0000000000001700 t _zm_activate_xhprof0000000000001720 t _zm_ deactivate_xhprof0000000000001960 t _zm_info_xhprof0000000000001660 t _zm_shutdown_xhprof0000000000001560 t _zm_ startup_xhprof0000000000001f00 T _zval_dump U _zval_used_for_init

9. Xhprof_compress () and xhrof_uncompress () can be used in PHP scripts.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.