"Fossies" - the Fresh Open Source Software Archive

Member "dbg-2.15.5/dbg_prof.c" (21 Apr 2007, 11136 Bytes) of package /linux/www/old/dbg-2.15.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "dbg_prof.c" see the Fossies "Dox" file reference documentation.

    1 /***************************************************************************
    2                           dbg_prof.c  -  description
    3                              -------------------
    4     begin                : Sun Sep 24 2000
    5     copyright            : (C) 2001 by Dmitri Dmitrienko
    6                          : (C) 2002, 2007 NuSphere Corp.
    7     www                  : http://dd.cron.ru
    8                          : http://www.nusphere.com/
    9     author               : written by Dmitri Dmitrienko
   10     license              : This source file is subject to version 3.0 of 
   11                            the License,  that is bundled with this package 
   12                            in the file LICENSE, and is available at through 
   13                            the world-wide-web at http://www.nusphere.com/dbg
   14  ***************************************************************************/
   15 
   16 #include "php.h"
   17 #include "php_network.h"
   18 
   19 #include "php_dbg.h"
   20 #include "dbg_cmd.h"
   21 
   22 #ifdef HAVE_DBG_PROFILER
   23 
   24 #ifdef PHP_WIN32
   25 /* Win 32 */
   26 inline dbgint64 dbgTimeTicks() {
   27     LARGE_INTEGER qpc;
   28     QueryPerformanceCounter(&qpc);
   29     return qpc.QuadPart;
   30 }
   31 
   32 inline dbgint64 dbgTimeFreq() {
   33     LARGE_INTEGER qpf;
   34     QueryPerformanceFrequency(&qpf);
   35     return qpf.QuadPart;
   36 }
   37 
   38 #else
   39 
   40 /* Linux / Unix */
   41 inline dbgint64 dbgTimeTicks() {
   42     struct timeval tv;
   43     gettimeofday(&tv, NULL);
   44     return tv.tv_usec + ((dbgint64)tv.tv_sec) * 1000000;
   45 }
   46 
   47 inline dbgint64 dbgTimeFreq() {
   48     return 1000000;
   49 }
   50 
   51 #endif
   52 
   53 
   54 void dbgTESTTIMER() {
   55     dbgint64 v,v2,fv, mv=0, cnt=0, minv=0,maxv=0;
   56     int i;
   57     for (i=0; i<100000; i++) {
   58         v=dbgTimeTicks();
   59         v2=dbgTimeTicks();
   60         v=v2-v;
   61         if (i==0) {
   62             minv=v;
   63             maxv=v;
   64             fv=10*v;
   65         } else {
   66             if (v<minv) minv=v;
   67             if (v>maxv) maxv=v;
   68             fv=((v+fv) * 10) / 11;
   69         }
   70         mv +=v;
   71         cnt++;
   72     }
   73     mv/=cnt;
   74     DBG_TRACE(("minv=%ld\n",minv));
   75     DBG_TRACE(("maxv=%ld\n",maxv));
   76     DBG_TRACE(("mv=%ld\n",mv));
   77     DBG_TRACE(("fv=%ld\n",fv));
   78 }
   79 
   80 #endif
   81 
   82 void init_rslt_array(zval **array, zval **outarray[], int count, char *names[]) {
   83     int i;
   84     zval *tmp;
   85 
   86     zval_dtor(*array);
   87     array_init(*array);
   88     for (i=0; i < count; i++) {
   89         MAKE_STD_ZVAL(tmp);
   90         array_init(tmp);
   91         zend_hash_update((*array)->value.ht, 
   92             names[i], 
   93             strlen(names[i])+1, 
   94             (void *) &tmp, 
   95             sizeof(zval*), 
   96             (void **) &(outarray[i]));
   97     }
   98 }
   99 
  100 #ifdef HAVE_DBG_PROFILER
  101 
  102 #define cprofdata_columns 6
  103 char *profdata_columns[cprofdata_columns] = {
  104     "mod_no", "line_no", "hit_count", 
  105     "tm_max", "tm_min", "tm_sum"};
  106 
  107 /* {{{ proto int dbg_get_profiler_results(array &profdata)
  108         Fetches all profiler results into array 
  109         Returns number of rows in it */
  110 PHP_FUNCTION(dbg_get_profiler_results) {
  111     zval **profdata;
  112     zval **outprofdata[cprofdata_columns];
  113 
  114     zval *element;
  115     profdata_item *it;
  116     int i, cnt = 0;
  117     dbgint64 freq;
  118 
  119     TSRMLS_FETCH1_NOP(DBG);
  120     if (ZEND_NUM_ARGS()!=1 ||
  121         zend_get_parameters_ex(1, &profdata) == FAILURE) {
  122         WRONG_PARAM_COUNT;
  123         RETURN_LONG(0);
  124     }
  125 
  126     freq = dbgTimeFreq();
  127     if (freq <= 1) freq = 1;
  128 
  129     init_rslt_array(profdata, outprofdata, cprofdata_columns, profdata_columns);
  130 
  131     LLIST_FOREACH(DBG(mod_list), mod_item,
  132         for (i=0; i < data->profdata_items; i++) {
  133             it=&(data->profdata_arr[i]);
  134             if (it->hitcount > 0) {
  135 
  136                 /* mod_no */
  137                 MAKE_STD_ZVAL(element);
  138                 ZVAL_LONG(element, data->mod_no);
  139                 zend_hash_index_update((*(outprofdata[0]))->value.ht, cnt, (void *)&element, sizeof(zval*), NULL);
  140 
  141                 /* line_no */
  142                 MAKE_STD_ZVAL(element);
  143                 ZVAL_LONG(element, i);
  144                 zend_hash_index_update((*(outprofdata[1]))->value.ht, cnt, (void *)&element, sizeof(zval*), NULL);
  145 
  146                 /* hitcount */
  147                 MAKE_STD_ZVAL(element);
  148                 ZVAL_LONG(element, it->hitcount);
  149                 zend_hash_index_update((*(outprofdata[2]))->value.ht, cnt, (void *)&element, sizeof(zval*), NULL);
  150 
  151                 /* tm_max */
  152                 MAKE_STD_ZVAL(element);
  153                 ZVAL_DOUBLE(element, (double)it->tm_max / freq);
  154                 zend_hash_index_update((*(outprofdata[3]))->value.ht, cnt, (void *)&element, sizeof(zval*), NULL);
  155 
  156                 /* tm_min */
  157                 MAKE_STD_ZVAL(element);
  158                 ZVAL_DOUBLE(element, (double)it->tm_min / freq);
  159                 zend_hash_index_update((*(outprofdata[4]))->value.ht, cnt, (void *)&element, sizeof(zval*), NULL);
  160 
  161                 /* tm_sum */
  162                 MAKE_STD_ZVAL(element);
  163                 ZVAL_DOUBLE(element, (double)it->tm_sum / freq);
  164                 zend_hash_index_update((*(outprofdata[5]))->value.ht, cnt, (void *)&element, sizeof(zval*), NULL);
  165 
  166                 cnt++;
  167             }
  168         }
  169     );
  170 
  171     RETURN_LONG(cnt);
  172 }
  173 /* }}} */
  174 #endif  
  175 
  176 #define cmodule_columns 2
  177 char *module_columns[cmodule_columns] = {
  178     "mod_no", "mod_name"};
  179 
  180 /* {{{ proto int dbg_get_all_module_names(array &modules)
  181         Fetches list of all modules names and their indexes (mod_no)
  182         Returns number of rows in it */
  183 PHP_FUNCTION(dbg_get_all_module_names) {
  184     zval **modules;
  185     zval **outmodules[cmodule_columns];
  186     zval *element;
  187     int cnt = 0;
  188 
  189     TSRMLS_FETCH1_NOP(DBG);
  190     if (ZEND_NUM_ARGS()!=1 ||
  191         zend_get_parameters_ex(1, &modules) == FAILURE) {
  192         WRONG_PARAM_COUNT;
  193         RETURN_LONG(0);
  194     }
  195     init_rslt_array(modules, outmodules, cmodule_columns, module_columns);
  196     LLIST_FOREACH(DBG(mod_list), mod_item,
  197 
  198         /* mod_no */
  199         MAKE_STD_ZVAL(element);
  200         ZVAL_LONG(element, data->mod_no);
  201         zend_hash_index_update((*(outmodules[0]))->value.ht, cnt, (void *)&element, sizeof(zval*), NULL);
  202 
  203         /* mod_name */
  204         MAKE_STD_ZVAL(element);
  205         ZVAL_STRING(element, (data->mod_name)?data->mod_name:"", 1);
  206         zend_hash_index_update((*(outmodules[1]))->value.ht, cnt, (void *)&element, sizeof(zval*), NULL);
  207         
  208         cnt++;
  209     );
  210     RETURN_LONG(cnt);
  211 }
  212 /* }}} */
  213 
  214 
  215 /* {{{ proto int dbg_get_module_name(int mod_no, string &module_name)
  216    Fetches module name for specified mod_no
  217    Returns 0 in case of error or 1 in success */
  218 PHP_FUNCTION(dbg_get_module_name) {
  219     zval **module_name, **zval_mod_no;
  220     int prm_mod_no;
  221 
  222     TSRMLS_FETCH1_NOP(DBG);
  223 
  224     if (ZEND_NUM_ARGS() != 2 ||
  225         zend_get_parameters_ex(2, &zval_mod_no, &module_name) == FAILURE) {
  226         WRONG_PARAM_COUNT;
  227         RETURN_LONG(0);
  228     }
  229 
  230     convert_to_long(*zval_mod_no);
  231     prm_mod_no = (*zval_mod_no)->value.lval;
  232 
  233     LLIST_FOREACH(DBG(mod_list), mod_item,
  234         if (data->mod_no == prm_mod_no) {
  235             ZVAL_STRING(*module_name, (data->mod_name) ? data->mod_name : "", 1);
  236             RETURN_LONG(1);
  237         }
  238     );
  239 
  240     ZVAL_STRING(*module_name, "", 1);
  241     RETURN_LONG(0);
  242 }
  243 /* }}} */
  244 
  245 #define ccontext_columns 3
  246 char *context_columns[ccontext_columns] = {
  247     "ctx_no", "mod_no", "ctx_name"};
  248 
  249 /* {{{ proto int dbg_get_all_context_names(int mod_no, array &contexts)
  250    Fetches list of contexts (function names) for given mod_no or all contexts if mod_no is zero 
  251    Returns number of resulting rows */
  252 PHP_FUNCTION(dbg_get_all_contexts) {
  253     zval **contexts, **zval_mod_no;
  254     zval **outcontexts[ccontext_columns];
  255     zval *element;
  256     int cnt = 0, prm_mod_no, mod_no;
  257 
  258     TSRMLS_FETCH1_NOP(DBG);
  259 
  260     if (ZEND_NUM_ARGS()!=2 ||
  261         zend_get_parameters_ex(2, &zval_mod_no, &contexts) == FAILURE) {
  262         WRONG_PARAM_COUNT;
  263         RETURN_LONG(0);
  264     }
  265     convert_to_long(*zval_mod_no);
  266     prm_mod_no = (*zval_mod_no)->value.lval;
  267     init_rslt_array(contexts, outcontexts, ccontext_columns, context_columns);
  268     LLIST_FOREACH(DBG(ctx_list), ctx_item,      
  269         mod_no = DBG_FINDMODULE(data->mod_name);
  270         if (prm_mod_no == 0 || prm_mod_no == mod_no) {
  271 
  272             /* mod_no */
  273             MAKE_STD_ZVAL(element);
  274             ZVAL_LONG(element, mod_no);
  275             zend_hash_index_update((*(outcontexts[0]))->value.ht, cnt, (void *)&element, sizeof(zval*), NULL);
  276 
  277             /* ctx_id */
  278             MAKE_STD_ZVAL(element);
  279             ZVAL_LONG(element, data->ctx_id);
  280             zend_hash_index_update((*(outcontexts[1]))->value.ht, cnt, (void *)&element, sizeof(zval*), NULL);
  281 
  282             /* function_name */
  283             MAKE_STD_ZVAL(element);
  284             ZVAL_STRING(element, (data->function_name)?data->function_name : "", 1);
  285             zend_hash_index_update((*(outcontexts[2]))->value.ht, cnt, (void *)&element, sizeof(zval*), NULL);
  286 
  287             cnt++;
  288         }
  289     );
  290     RETURN_LONG(cnt);
  291 }
  292 /* }}} */
  293 
  294 /* {{{ proto int dbg_get_context_name(int ctx_id, string &function_name)
  295    Fetches function name for given ctx_id 
  296    Returns 0 in case of error or 1 in success */
  297 PHP_FUNCTION(dbg_get_context_name) {
  298     zval **zval_ctx_id, **function_name;
  299     int prm_ctx_id;
  300 
  301     TSRMLS_FETCH1_NOP(DBG);
  302 
  303     if (ZEND_NUM_ARGS() != 2 ||
  304         zend_get_parameters_ex(2, &zval_ctx_id, &function_name) == FAILURE) {
  305         WRONG_PARAM_COUNT;
  306         RETURN_LONG(0);
  307     }
  308     
  309     convert_to_long(*zval_ctx_id);
  310     prm_ctx_id = (*zval_ctx_id)->value.lval;
  311 
  312     LLIST_FOREACH(DBG(ctx_list), ctx_item,
  313         if (data->ctx_id == prm_ctx_id) {
  314             ZVAL_STRING(*function_name, (data->function_name) ? data->function_name : "", 1);
  315             RETURN_LONG(1);
  316         }
  317     );
  318     RETURN_LONG(0);
  319 }
  320 /* }}} */
  321 
  322 #define csrcline_columns 3
  323 char *srcline_columns[csrcline_columns] = {
  324     "ctx_no", "mod_no", "line_no"};
  325 
  326 /* {{{ proto int dbg_get_all_source_lines(int mod_no, array &source_lines)
  327         Fetches list of lines for all contexts in given mod_no or all lines for all contexts if mod_no is zero
  328         Returns number of resulting rows */
  329 PHP_FUNCTION(dbg_get_all_source_lines) {
  330     zval **srclines, **zval_mod_no;
  331     zval **outsrclines[csrcline_columns];
  332     zval *element;
  333     int cnt = 0, prm_mod_no, mod_no;
  334 
  335     TSRMLS_FETCH1_NOP(DBG);
  336 
  337     if (ZEND_NUM_ARGS()!=2 ||
  338         zend_get_parameters_ex(2, &zval_mod_no, &srclines) == FAILURE) {
  339         WRONG_PARAM_COUNT;
  340         RETURN_LONG(0);
  341     }
  342     convert_to_long(*zval_mod_no);
  343     prm_mod_no = (*zval_mod_no)->value.lval;
  344     init_rslt_array(srclines, outsrclines, csrcline_columns, srcline_columns);
  345 
  346     LLIST_FOREACH(DBG(ctxlines_list), ctxlines_item,
  347         mod_no = DBG_FINDMODULE(data->mod_name); 
  348         if (prm_mod_no == 0 || prm_mod_no == mod_no) {
  349             int ctx_id = data->ctx_id;
  350             int line_no = data->start_line_no;
  351             int lines_cnt = data->lines_cnt;
  352             int i;
  353             for (i = 0; i < lines_cnt; i++) {
  354 
  355                 /* ctx_id */
  356                 MAKE_STD_ZVAL(element);
  357                 ZVAL_LONG(element, ctx_id);
  358                 zend_hash_index_update((*(outsrclines[0]))->value.ht, cnt, (void *)&element, sizeof(zval*), NULL);
  359 
  360                 /* mod_no */
  361                 MAKE_STD_ZVAL(element);
  362                 ZVAL_LONG(element, mod_no);
  363                 zend_hash_index_update((*(outsrclines[1]))->value.ht, cnt, (void *)&element, sizeof(zval*), NULL);
  364 
  365                 /* line_no */
  366                 MAKE_STD_ZVAL(element);
  367                 ZVAL_LONG(element, line_no + i);
  368                 zend_hash_index_update((*(outsrclines[2]))->value.ht, cnt, (void *)&element, sizeof(zval*), NULL);
  369 
  370                 cnt++;
  371             }           
  372         }
  373     );
  374     RETURN_LONG(cnt);
  375 }
  376 /* }}} */
  377 
  378 /* {{{ proto int dbg_get_source_context(int mod_no, int line_no, int &ctx_id)
  379         Fetches ctx_id for given mod_no, line_no pair
  380         Returns 0 in case of error or 1 in success */
  381 PHP_FUNCTION(dbg_get_source_context) {
  382     zval **zval_mod_no, **zval_line_no;
  383     zval **zval_ctx_id;
  384     int prm_mod_no, prm_line_no;
  385 
  386     TSRMLS_FETCH1_NOP(DBG);
  387 
  388     if (ZEND_NUM_ARGS() != 3 ||
  389         zend_get_parameters_ex(3, &zval_mod_no, &zval_line_no, &zval_ctx_id) == FAILURE) {
  390         WRONG_PARAM_COUNT;
  391         RETURN_LONG(0);
  392     }
  393 
  394     convert_to_long(*zval_mod_no);
  395     prm_mod_no = (*zval_mod_no)->value.lval;
  396     convert_to_long(*zval_line_no);
  397     prm_line_no = (*zval_line_no)->value.lval;
  398     
  399 
  400     LLIST_FOREACH(DBG(ctxlines_list), ctxlines_item,
  401         if (prm_mod_no == DBG_FINDMODULE(data->mod_name) && 
  402             prm_line_no >= data->start_line_no && 
  403             prm_line_no < data->lines_cnt + data->start_line_no) {
  404 
  405             ZVAL_LONG(*zval_ctx_id, data->ctx_id);
  406             RETURN_LONG(1);
  407         }
  408     );
  409 
  410     RETURN_LONG(0);
  411 }
  412 /* }}} */