快速找出php中可能導(dǎo)致cpu飆升問題的代碼行

字號(hào):


    用cpu接近100%時(shí),你如何找到導(dǎo)致cpu飆升的原因?我的思路是,首先找到進(jìn)程正在執(zhí)行的代碼行,從而確定可能有問題的代碼段。然后,再仔細(xì)分析有問題的代碼段,從而找出原因。
    如果你的程序使用的是c、c++編寫,那么你可以很容易的找到正在執(zhí)行的代碼行。但是,程序是php編寫的,如何找到可能有問題的代碼行呢?這個(gè)問題就是本文要解決的問題。
    背景知識(shí):
    大家都知道php是一個(gè)解釋性語言。用戶編寫的php代碼會(huì)生成opcode,由解釋器引擎去解釋執(zhí)行。在解釋執(zhí)行過程中,有一個(gè)全局變量包含了執(zhí)行過 程中用到的各種數(shù)據(jù)。它就是executor_globals。在源碼的Zend/zend_globals.h 文件中可以找到他的類型定義。
    代碼如下:
    struct _zend_executor_globals {
    zval **return_value_ptr_ptr;
    zval uninitialized_zval;
    zval *uninitialized_zval_ptr;
    zval error_zval;
    zval *error_zval_ptr;
    zend_ptr_stack arg_types_stack;
    /* symbol table cache */
    HashTable *symtable_cache[SYMTABLE_CACHE_SIZE];
    HashTable **symtable_cache_limit;
    HashTable **symtable_cache_ptr;
    zend_op **opline_ptr;
    HashTable *active_symbol_table;
    HashTable symbol_table; /* main symbol table */
    HashTable included_files; /* files already included */
    JMP_BUF *bailout;
    int error_reporting;
    int orig_error_reporting;
    int exit_status;
    zend_op_array *active_op_array;
    HashTable *function_table; /* function symbol table */
    HashTable *class_table; /* class table */
    HashTable *zend_constants; /* constants table */
    zend_class_entry *scope;
    zend_class_entry *called_scope; /* Scope of the calling class */
    zval *This;
    long precision;
    int ticks_count;
    zend_bool in_execution;
    HashTable *in_autoload;
    zend_function *autoload_func;
    zend_bool full_tables_cleanup;
    /* for extended information support */
    zend_bool no_extensions;
    #ifdef ZEND_WIN32
    zend_bool timed_out;
    OSVERSIONINFOEX windows_version_info;
    #endif
    HashTable regular_list;
    HashTable persistent_list;
    zend_vm_stack argument_stack;
    int user_error_handler_error_reporting;
    zval *user_error_handler;
    zval *user_exception_handler;
    zend_stack user_error_handlers_error_reporting;
    zend_ptr_stack user_error_handlers;
    zend_ptr_stack user_exception_handlers;
    zend_error_handling_t error_handling;
    zend_class_entry *exception_class;
    /* timeout support */
    int timeout_seconds;
    int lambda_count;
    HashTable *ini_directives;
    HashTable *modified_ini_directives;
    zend_objects_store objects_store;
    zval *exception, *prev_exception;
    zend_op *opline_before_exception;
    zend_op exception_op[3];
    struct _zend_execute_data *current_execute_data;
    struct _zend_module_entry *current_module;
    zend_property_info std_property_info;
    zend_bool active;
    void *saved_fpu_cw;
    void *reserved[ZEND_MAX_RESERVED_RESOURCES];
    };
    這里我們只說兩個(gè)對(duì)我們比較重要的變量,active_op_array 和 current_execute_data。
    active_op_array變量中保存了引擎正在執(zhí)行的op_array(想了解什么是op_array請(qǐng)點(diǎn)擊查看)。在Zend/zend_compile.h中有關(guān)于op_array的數(shù)據(jù)類型的定義。
    代碼如下:
    struct _zend_op_array {
    /* Common elements */
    zend_uchar type;
    char *function_name;
    zend_class_entry *scope;
    zend_uint fn_flags;
    union _zend_function *prototype;
    zend_uint num_args;
    zend_uint required_num_args;
    zend_arg_info *arg_info;
    zend_bool pass_rest_by_reference;
    unsigned char return_reference;
    /* END of common elements */
    zend_bool done_pass_two;
    zend_uint *refcount;
    zend_op *opcodes;
    zend_uint last, size;
    zend_compiled_variable *vars;
    int last_var, size_var;
    zend_uint T;
    zend_brk_cont_element *brk_cont_array;
    int last_brk_cont;
    int current_brk_cont;
    zend_try_catch_element *try_catch_array;
    int last_try_catch;
    /* static variables support */
    HashTable *static_variables;
    zend_op *start_op;
    int backpatch_count;
    zend_uint this_var;
    char *filename;
    zend_uint line_start;
    zend_uint line_end;
    char *doc_comment;
    zend_uint doc_comment_len;
    zend_uint early_binding; /* the linked list of delayed declarations */
    void *reserved[ZEND_MAX_RESERVED_RESOURCES];
    };
    看完定義,就不用我多說了把。定義中,filename和 function_name分別保存了正在執(zhí)行的文件名和方法名。
    current_execute_data保存了正在執(zhí)行的op_array的execute_data。execute_data保存了每個(gè)op_array執(zhí)行過程中的一些數(shù)據(jù)。其定義在,Zend/zend_compile.h:
    代碼如下:
    struct _zend_execute_data {
    struct _zend_op *opline;
    zend_function_state function_state;
    zend_function *fbc; /* Function Being Called */
    zend_class_entry *called_scope;
    zend_op_array *op_array;
    zval *object;
    union _temp_variable *Ts;
    zval ***CVs;
    HashTable *symbol_table;
    struct _zend_execute_data *prev_execute_data;
    zval *old_error_reporting;
    zend_bool nested;
    zval **original_return_value;
    zend_class_entry *current_scope;
    zend_class_entry *current_called_scope;
    zval *current_this;
    zval *current_object;
    struct _zend_op *call_opline;
    };
    定義中的opline就是正在執(zhí)行的opcode。opcode的結(jié)構(gòu)定義如下:
    代碼如下:
    struct _zend_op {
    opcode_handler_t handler;
    znode result;
    znode op1;
    znode op2;
    ulong extended_value;
    uint lineno;
    zend_uchar opcode;
    };
    其中l(wèi)ineno就是opcode所對(duì)應(yīng)的行號(hào)。
    示例說明:
    看完上面的數(shù)據(jù)結(jié)構(gòu)定義,你是否已經(jīng)知道如何找php正在執(zhí)行的文件名,方法名和行號(hào)呢?如果還有疑問的話,那就接著看下面的例子。創(chuàng)建一個(gè)文件test.php,代碼如下:
    代碼如下:
    <?php
    function test1(){
    while(true){
    sleep(1);
    }
    }
    test1();
    ?>
    cli方式執(zhí)行php腳本,加入執(zhí)行的進(jìn)程號(hào)為14973。我們使用gdb命令來調(diào)試進(jìn)程。
    代碼如下:
    $sudo gdb -p 14973
    (gdb) print (char *)executor_globals.active_op_array->filename
    $1 = 0x9853a34 "/home/xinhailong/test/php/test.php"
    (gdb) print (char *)executor_globals.active_op_array->function_name
    $2 = 0x9854db8 "test1"
    (gdb) print executor_globals->current_execute_data->opline->lineno
    $3 = 4
    很顯然,他正在執(zhí)行第四行的sleep方法。
    如果上面的方法你感覺麻煩,那你可以使用.gdbinit文件。這個(gè)文件在php源碼的根目錄下。使用方法如下:
    代碼如下:
    $sudo gdb -p 14973
    (gdb) source /home/xinhailong/.gdbinit
    (gdb) zbacktrace
    [0xa453f34] sleep(1) /home/xinhailong/test/php/test.php:4
    [0xa453ed0] test1() /home/xinhailong/test/php/test.php:7
    (gdb)
    題外話:
    從php5.6開始,php中集成了一個(gè)phpdbg的工具。可以像gdb調(diào)試c語言程序一樣,調(diào)試php程序。感興趣的話,可以打開下面的連接看看