Changeset 204 in svn for trunk/coverager.c


Ignore:
Timestamp:
2006-10-01T08:57:51Z (9 years ago)
Author:
Xuefer
Message:

coverager: new api xcache_coverager_start/xcache_coverager_stop/xcache_coverager_get(), coverager ini changed

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/coverager.c

    r201 r204  
    1616#define PCOV_HEADER_MAGIC 0x564f4350
    1717
    18 char *xc_coveragedump_dir = NULL;
     18static char *xc_coveragedump_dir = NULL;
    1919static zend_compile_file_t *origin_compile_file;
    2020
     
    193193}
    194194/* }}} */
    195 void xc_coverager_request_init(TSRMLS_D) /* {{{ */
    196 {
    197     if (XG(coveragedumper)) {
     195
     196static void xc_coverager_initenv(TSRMLS_D) /* {{{ */
     197{
     198    if (!XG(coverages)) {
    198199        XG(coverages) = emalloc(sizeof(HashTable));
    199200        zend_hash_init(XG(coverages), 0, NULL, xc_destroy_coverage, 0);
     
    201202}
    202203/* }}} */
    203 void xc_coverager_request_shutdown(TSRMLS_D) /* {{{ */
     204static void xc_coverager_clean(TSRMLS_D) /* {{{ */
     205{
     206    if (XG(coverages)) {
     207        zend_hash_destroy(XG(coverages));
     208        efree(XG(coverages));
     209        XG(coverages) = NULL;
     210    }
     211}
     212/* }}} */
     213
     214static void xc_coverager_enable(TSRMLS_D) /* {{{ */
     215{
     216    XG(coverage_enabled) = 1;
     217}
     218/* }}} */
     219static void xc_coverager_disable(TSRMLS_D) /* {{{ */
     220{
     221    XG(coverage_enabled) = 0;
     222}
     223/* }}} */
     224
     225void xc_coverager_request_init(TSRMLS_D) /* {{{ */
     226{
     227    if (XG(coveragedumper)) {
     228        xc_coverager_enable(TSRMLS_C);
     229        xc_coverager_initenv(TSRMLS_C);
     230        CG(extended_info) = 1;
     231    }
     232    else {
     233        XG(coverage_enabled) = 0;
     234    }
     235}
     236/* }}} */
     237static void xc_coverager_autodump(TSRMLS_D) /* {{{ */
    204238{
    205239    coverager_t *pcov;
     
    208242    int dumpdir_len, outfilelen, alloc_len = 0;
    209243    uint size;
    210 
    211     if (!XG(coverages)) {
    212         return;
    213     }
    214     if (XG(coveragedumper)) {   
     244    HashPosition pos;
     245
     246    if (XG(coverages) && xc_coveragedump_dir) {
    215247        dumpdir_len = strlen(xc_coveragedump_dir);
    216248        alloc_len = dumpdir_len + 1 + 128;
     
    218250        strcpy(outfilename, xc_coveragedump_dir);
    219251
    220         zend_hash_internal_pointer_reset(XG(coverages));
    221         while (zend_hash_get_current_data(XG(coverages), (void **) &pcov) == SUCCESS) {
    222             zend_hash_get_current_key_ex(XG(coverages), &s, &size, NULL, 0, NULL);
     252        zend_hash_internal_pointer_reset_ex(XG(coverages), &pos);
     253        while (zend_hash_get_current_data_ex(XG(coverages), (void **) &pcov, &pos) == SUCCESS) {
     254            zend_hash_get_current_key_ex(XG(coverages), &s, &size, NULL, 0, &pos);
    223255            outfilelen = dumpdir_len + size + 5;
    224256            if (alloc_len < outfilelen) {
     
    233265#endif
    234266            xc_coverager_save_cov(ZSTR_S(s), outfilename, *pcov TSRMLS_CC);
    235             zend_hash_move_forward(XG(coverages));
     267            zend_hash_move_forward_ex(XG(coverages), &pos);
    236268        }
    237269        efree(outfilename);
    238270    }
    239 
    240     zend_hash_destroy(XG(coverages));
    241     efree(XG(coverages));
    242     XG(coverages) = NULL;
     271}
     272/* }}} */
     273static void xc_coverager_dump(zval *return_value TSRMLS_DC) /* {{{ */
     274{
     275    coverager_t *pcov;
     276    HashPosition pos;
     277
     278    if (XG(coverages)) {
     279        array_init(return_value);
     280
     281        zend_hash_internal_pointer_reset_ex(XG(coverages), &pos);
     282        while (zend_hash_get_current_data_ex(XG(coverages), (void **) &pcov, &pos) == SUCCESS) {
     283            zval *lines;
     284            long *phits;
     285            coverager_t cov;
     286            HashPosition pos2;
     287            zstr filename;
     288            uint size;
     289
     290            cov = *pcov;
     291            zend_hash_get_current_key_ex(XG(coverages), &filename, &size, NULL, 0, &pos);
     292
     293            MAKE_STD_ZVAL(lines);
     294            array_init(lines);
     295            zend_hash_internal_pointer_reset_ex(cov, &pos2);
     296            while (zend_hash_get_current_data_ex(cov, (void**)&phits, &pos2) == SUCCESS) {
     297                long hits = *phits;
     298                add_index_long(lines, pos2->h, hits >= 0 ? hits : 0);
     299                zend_hash_move_forward_ex(cov, &pos2);
     300            }
     301            add_assoc_zval_ex(return_value, ZSTR_S(filename), strlen(ZSTR_S(filename)) + 1, lines);
     302
     303            zend_hash_move_forward_ex(XG(coverages), &pos);
     304        }
     305    }
     306    else {
     307        RETVAL_NULL();
     308    }
     309}
     310/* }}} */
     311void xc_coverager_request_shutdown(TSRMLS_D) /* {{{ */
     312{
     313    if (XG(coveragedumper)) {
     314        xc_coverager_autodump(TSRMLS_C);
     315        xc_coverager_clean(TSRMLS_C);
     316    }
    243317}
    244318/* }}} */
     
    351425
    352426    op_array = origin_compile_file(h, type TSRMLS_CC);
    353     if (XG(coveragedumper) && XG(coverages) && op_array) {
    354         xc_coverager_init_compile_result(op_array TSRMLS_CC);
     427    if (op_array) {
     428        if (XG(coveragedumper)) {
     429            xc_coverager_initenv(TSRMLS_C);
     430            xc_coverager_init_compile_result(op_array TSRMLS_CC);
     431        }
    355432    }
    356433    return op_array;
     
    363440    TSRMLS_FETCH();
    364441
    365     if (XG(coveragedumper) && XG(coverages)) {
     442    if (XG(coverages) && XG(coverage_enabled)) {
    366443        int size = xc_coverager_get_op_array_size_no_tail(op_array);
    367444        int oplineno = (*EG(opline_ptr)) - op_array->opcodes;
     
    376453int xc_coverager_init(int module_number TSRMLS_DC) /* {{{ */
    377454{
    378     if (xc_coveragedump_dir) {
     455    origin_compile_file = zend_compile_file;
     456    zend_compile_file = xc_compile_file_for_coverage;
     457
     458    if (cfg_get_string("xcache.coveragedump_directory", &xc_coveragedump_dir) == SUCCESS && xc_coveragedump_dir) {
    379459        int len = strlen(xc_coveragedump_dir);
    380460        if (len) {
     
    383463            }
    384464        }
    385     }
    386     if (xc_coveragedump_dir && xc_coveragedump_dir[0]) {
    387         origin_compile_file = zend_compile_file;
    388         zend_compile_file = xc_compile_file_for_coverage;
    389         CG(extended_info) = 1;
    390     }
     465        if (!strlen(xc_coveragedump_dir)) {
     466            xc_coveragedump_dir = NULL;
     467        }
     468    }
     469
    391470    return SUCCESS;
    392471}
     
    398477    }
    399478    if (xc_coveragedump_dir) {
    400         pefree(xc_coveragedump_dir, 1);
    401479        xc_coveragedump_dir = NULL;
    402480    }
     
    405483
    406484/* user api */
    407 PHP_FUNCTION(xcache_coverager_decode) /* {{{ */
     485/* {{{ proto array xcache_coverager_decode(string data)
     486 * decode specified data which is saved by auto dumper to array
     487 */
     488PHP_FUNCTION(xcache_coverager_decode)
    408489{
    409490    char *str;
     
    434515}
    435516/* }}} */
    436 
     517/* {{{ proto void xcache_coverager_start([bool clean = true])
     518 * starts coverager data collecting
     519 */
     520PHP_FUNCTION(xcache_coverager_start)
     521{
     522    zend_bool clean = 1;
     523
     524    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &clean) == FAILURE) {
     525        return;
     526    }
     527
     528    if (clean) {
     529        xc_coverager_clean(TSRMLS_C);
     530    }
     531
     532    if (XG(coveragedumper)) {
     533        xc_coverager_enable(TSRMLS_C);
     534    }
     535    else {
     536        php_error(E_WARNING, "You can only start coverager after you set 'xcache.coveragedumper' to 'On' in ini");
     537    }
     538}
     539/* }}} */
     540/* {{{ proto void xcache_coverager_stop([bool clean = false])
     541 * stop coverager data collecting
     542 */
     543PHP_FUNCTION(xcache_coverager_stop)
     544{
     545    zend_bool clean = 0;
     546
     547    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &clean) == FAILURE) {
     548        return;
     549    }
     550
     551    xc_coverager_disable(TSRMLS_C);
     552    if (clean) {
     553        xc_coverager_clean(TSRMLS_C);
     554    }
     555}
     556/* }}} */
     557/* {{{ proto array xcache_coverager_get([bool clean = false])
     558 * get coverager data collected
     559 */
     560PHP_FUNCTION(xcache_coverager_get)
     561{
     562    zend_bool clean = 0;
     563    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &clean) == FAILURE) {
     564        return;
     565    }
     566
     567    xc_coverager_dump(return_value TSRMLS_CC);
     568    if (clean) {
     569        xc_coverager_clean(TSRMLS_C);
     570    }
     571}
     572/* }}} */
Note: See TracChangeset for help on using the changeset viewer.