快速找出php中可能導致cpu飆升問題的代碼行
來源:易賢網(wǎng) 閱讀:570 次 日期:2014-12-10 14:12:18
溫馨提示:易賢網(wǎng)小編為您整理了“快速找出php中可能導致cpu飆升問題的代碼行”,方便廣大網(wǎng)友查閱!

用cpu接近100%時,你如何找到導致cpu飆升的原因?我的思路是,首先找到進程正在執(zhí)行的代碼行,從而確定可能有問題的代碼段。然后,再仔細分析有問題的代碼段,從而找出原因。

如果你的程序使用的是c、c++編寫,那么你可以很容易的找到正在執(zhí)行的代碼行。但是,程序是php編寫的,如何找到可能有問題的代碼行呢?這個問題就是本文要解決的問題。

背景知識:

大家都知道php是一個解釋性語言。用戶編寫的php代碼會生成opcode,由解釋器引擎去解釋執(zhí)行。在解釋執(zhí)行過程中,有一個全局變量包含了執(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];

};

這里我們只說兩個對我們比較重要的變量,active_op_array 和 current_execute_data。

active_op_array變量中保存了引擎正在執(zhí)行的op_array(想了解什么是op_array請點擊查看)。在Zend/zend_compile.h中有關于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保存了每個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的結構定義如下:

代碼如下:

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所對應的行號。

示例說明:

看完上面的數(shù)據(jù)結構定義,你是否已經(jīng)知道如何找php正在執(zhí)行的文件名,方法名和行號呢?如果還有疑問的話,那就接著看下面的例子。創(chuàng)建一個文件test.php,代碼如下:

代碼如下:

<?php

function test1(){

while(true){

sleep(1);

}

}

test1();

?>

cli方式執(zhí)行php腳本,加入執(zhí)行的進程號為14973。我們使用gdb命令來調試進程。

代碼如下:

$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文件。這個文件在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中集成了一個phpdbg的工具??梢韵駁db調試c語言程序一樣,調試php程序。感興趣的話,可以打開下面的連接看看

更多信息請查看IT技術專欄

更多信息請查看網(wǎng)絡編程
易賢網(wǎng)手機網(wǎng)站地址:快速找出php中可能導致cpu飆升問題的代碼行

2025國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機號
  • 驗證碼
關于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務許可證:(云)人服證字(2023)第0102001523號
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關注公眾號:hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權所有:易賢網(wǎng)