PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/opcache - zend_persist_calc.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 113 180 62.8 %
Date: 2014-09-19 Functions: 8 9 88.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | Zend OPcache                                                         |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1998-2014 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP license,      |
       8             :    | that is bundled with this package in the file LICENSE, and is        |
       9             :    | available through the world-wide-web at the following url:           |
      10             :    | http://www.php.net/license/3_01.txt                                  |
      11             :    | If you did not receive a copy of the PHP license and are unable to   |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@php.net so we can mail you a copy immediately.               |
      14             :    +----------------------------------------------------------------------+
      15             :    | Authors: Andi Gutmans <andi@zend.com>                                |
      16             :    |          Zeev Suraski <zeev@zend.com>                                |
      17             :    |          Stanislav Malyshev <stas@zend.com>                          |
      18             :    |          Dmitry Stogov <dmitry@zend.com>                             |
      19             :    +----------------------------------------------------------------------+
      20             : */
      21             : 
      22             : #include "zend.h"
      23             : #include "ZendAccelerator.h"
      24             : #include "zend_persist.h"
      25             : #include "zend_extensions.h"
      26             : #include "zend_shared_alloc.h"
      27             : #include "zend_operators.h"
      28             : 
      29             : #define START_SIZE()       uint memory_used = 0
      30             : #define ADD_DUP_SIZE(m,s)  memory_used += zend_shared_memdup_size((void*)m, s)
      31             : #define ADD_SIZE(m)        memory_used += ZEND_ALIGNED_SIZE(m)
      32             : #define RETURN_SIZE()      return memory_used
      33             : 
      34             : # define ADD_STRING(str) \
      35             :                 ADD_DUP_SIZE((str), _STR_HEADER_SIZE + (str)->len + 1)
      36             : # define ADD_INTERNED_STRING(str, do_free) do { \
      37             :                 if (!IS_ACCEL_INTERNED(str)) { \
      38             :                         zend_string *tmp = accel_new_interned_string(str TSRMLS_CC); \
      39             :                         if (tmp != (str)) { \
      40             :                                 if (do_free) { \
      41             :                                         /*zend_string_release(str);*/ \
      42             :                                 } \
      43             :                                 (str) = tmp; \
      44             :                         } else { \
      45             :                                 ADD_STRING(str); \
      46             :                         } \
      47             :                 } \
      48             :         } while (0)
      49             : 
      50             : static uint zend_persist_zval_calc(zval *z TSRMLS_DC);
      51             : 
      52         927 : static uint zend_hash_persist_calc(HashTable *ht, uint (*pPersistElement)(zval *pElement TSRMLS_DC) TSRMLS_DC)
      53             : {
      54             :         uint idx;
      55             :         Bucket *p;
      56         927 :         START_SIZE();
      57             : 
      58         927 :         if (!ht->nTableMask) {
      59         848 :                 RETURN_SIZE();
      60             :         }
      61          79 :         if (ht->u.flags & HASH_FLAG_PACKED) {
      62          10 :                 ADD_SIZE(sizeof(Bucket) * ht->nNumUsed);
      63             :         } else {
      64          69 :                 ADD_SIZE(sizeof(Bucket) * ht->nNumUsed + sizeof(uint32_t) * ht->nTableSize);
      65             :         }
      66             : 
      67         236 :         for (idx = 0; idx < ht->nNumUsed; idx++) {
      68         157 :                 p = ht->arData + idx;
      69         157 :                 if (Z_TYPE(p->val) == IS_UNDEF) continue;
      70             : 
      71             :                 /* persist bucket and key */
      72         157 :                 if (p->key) {
      73         124 :                         zend_uchar flags = GC_FLAGS(p->key) & ~ (IS_STR_PERSISTENT | IS_STR_INTERNED | IS_STR_PERMANENT);
      74         124 :                         ADD_INTERNED_STRING(p->key, 1);
      75         124 :                         GC_FLAGS(p->key) |= flags;
      76             :                 }
      77             : 
      78         157 :                 ADD_SIZE(pPersistElement(&p->val TSRMLS_CC));
      79             :         }
      80             : 
      81          79 :         RETURN_SIZE();
      82             : }
      83             : 
      84           0 : static uint zend_persist_ast_calc(zend_ast *ast TSRMLS_DC)
      85             : {
      86             :         uint32_t i;
      87           0 :         START_SIZE();
      88             : 
      89           0 :         if (ast->kind == ZEND_AST_ZVAL) {
      90           0 :                 ADD_SIZE(sizeof(zend_ast_zval));
      91           0 :                 ADD_SIZE(zend_persist_zval_calc(zend_ast_get_zval(ast) TSRMLS_CC));
      92           0 :         } else if (zend_ast_is_list(ast)) {
      93           0 :                 zend_ast_list *list = zend_ast_get_list(ast);
      94           0 :                 ADD_SIZE(sizeof(zend_ast_list) - sizeof(zend_ast *) + sizeof(zend_ast *) * list->children);
      95           0 :                 for (i = 0; i < list->children; i++) {
      96           0 :                         if (list->child[i]) {
      97           0 :                                 ADD_SIZE(zend_persist_ast_calc(list->child[i] TSRMLS_CC));
      98             :                         }
      99             :                 }
     100             :         } else {
     101           0 :                 uint32_t children = zend_ast_get_num_children(ast);
     102           0 :                 ADD_SIZE(sizeof(zend_ast) - sizeof(zend_ast *) + sizeof(zend_ast *) * children);
     103           0 :                 for (i = 0; i < children; i++) {
     104           0 :                         if (ast->child[i]) {
     105           0 :                                 ADD_SIZE(zend_persist_ast_calc(ast->child[i] TSRMLS_CC));
     106             :                         }
     107             :                 }
     108             :         }
     109           0 :         RETURN_SIZE();
     110             : }
     111             : 
     112        3073 : static uint zend_persist_zval_calc(zval *z TSRMLS_DC)
     113             : {
     114             :         zend_uchar flags;
     115             :         uint size;
     116        3073 :         START_SIZE();
     117             : 
     118        3073 :         switch (Z_TYPE_P(z)) {
     119             :                 case IS_STRING:
     120             :                 case IS_CONSTANT:
     121        2374 :                         flags = Z_GC_FLAGS_P(z) & ~ (IS_STR_PERSISTENT | IS_STR_INTERNED | IS_STR_PERMANENT);
     122        2374 :                         ADD_INTERNED_STRING(Z_STR_P(z), 0);
     123        2374 :                         if (IS_INTERNED(Z_STR_P(z))) {
     124        2374 :                                 Z_TYPE_FLAGS_P(z) &= ~ (IS_TYPE_REFCOUNTED | IS_TYPE_COPYABLE);
     125             :                         }
     126        2374 :                         Z_GC_FLAGS_P(z) |= flags;
     127        2374 :                         break;
     128             :                 case IS_ARRAY:
     129          67 :                         size = zend_shared_memdup_size(Z_ARR_P(z), sizeof(zend_array));
     130          67 :                         if (size) {
     131          67 :                                 ADD_SIZE(size);
     132          67 :                                 ADD_SIZE(zend_hash_persist_calc(Z_ARRVAL_P(z), zend_persist_zval_calc TSRMLS_CC));
     133             :                         }
     134          67 :                         break;
     135             :                 case IS_REFERENCE:
     136           0 :                         size = zend_shared_memdup_size(Z_REF_P(z), sizeof(zend_reference));
     137           0 :                         if (size) {
     138           0 :                                 ADD_SIZE(size);
     139           0 :                                 ADD_SIZE(zend_persist_zval_calc(Z_REFVAL_P(z) TSRMLS_CC));
     140             :                         }
     141           0 :                         break;
     142             :                 case IS_CONSTANT_AST:
     143           0 :                         size = zend_shared_memdup_size(Z_AST_P(z), sizeof(zend_ast_ref));
     144           0 :                         if (size) {
     145           0 :                                 ADD_SIZE(size);
     146           0 :                                 ADD_SIZE(zend_persist_ast_calc(Z_ASTVAL_P(z) TSRMLS_CC));
     147             :                         }
     148             :                         break;
     149             :         }
     150        3073 :         RETURN_SIZE();
     151             : }
     152             : 
     153         451 : static uint zend_persist_op_array_calc_ex(zend_op_array *op_array TSRMLS_DC)
     154             : {
     155         451 :         START_SIZE();
     156             : 
     157         451 :         if (op_array->type != ZEND_USER_FUNCTION) {
     158           0 :                 return 0;
     159             :         }
     160             : 
     161         451 :         if (op_array->static_variables) {
     162          13 :                 ADD_DUP_SIZE(op_array->static_variables, sizeof(HashTable));
     163          13 :                 ADD_SIZE(zend_hash_persist_calc(op_array->static_variables, zend_persist_zval_calc TSRMLS_CC));
     164             :         }
     165             : 
     166         451 :         if (zend_shared_alloc_get_xlat_entry(op_array->opcodes)) {
     167             :                 /* already stored */
     168           0 :                 if (op_array->function_name) {
     169           0 :                         zend_string *new_name = zend_shared_alloc_get_xlat_entry(op_array->function_name);
     170           0 :                         if (IS_ACCEL_INTERNED(new_name)) {
     171           0 :                                 op_array->function_name = new_name;
     172             :                         }
     173             :                 }
     174           0 :                 RETURN_SIZE();
     175             :         }
     176             : 
     177         451 :         if (op_array->literals) {
     178         451 :                 zval *p = op_array->literals;
     179         451 :                 zval *end = p + op_array->last_literal;
     180         451 :                 ADD_DUP_SIZE(op_array->literals, sizeof(zval) * op_array->last_literal);
     181        3848 :                 while (p < end) {
     182        2946 :                         ADD_SIZE(zend_persist_zval_calc(p TSRMLS_CC));
     183        2946 :                         p++;
     184             :                 }
     185             :         }
     186             : 
     187         451 :         ADD_DUP_SIZE(op_array->opcodes, sizeof(zend_op) * op_array->last);
     188             : 
     189         451 :         if (op_array->function_name) {
     190          29 :                 zend_string *old_name = op_array->function_name;
     191          29 :                 zend_string *new_name = zend_shared_alloc_get_xlat_entry(old_name);
     192             : 
     193          29 :                 if (new_name) {
     194           0 :                         op_array->function_name = new_name;
     195             :                 } else {
     196          29 :                         ADD_INTERNED_STRING(op_array->function_name, 0);
     197          29 :                         zend_shared_alloc_register_xlat_entry(old_name, op_array->function_name);
     198             :                 }
     199             :     }
     200             : 
     201         451 :         if (op_array->filename) {
     202         451 :                 ADD_STRING(op_array->filename);
     203             :         }
     204             : 
     205         451 :         if (op_array->arg_info) {
     206             :                 uint32_t i;
     207             : 
     208          25 :                 ADD_DUP_SIZE(op_array->arg_info, sizeof(zend_arg_info) * op_array->num_args);
     209          55 :                 for (i = 0; i < op_array->num_args; i++) {
     210          30 :                         if (op_array->arg_info[i].name) {
     211             : //???                           ADD_INTERNED_STRING(op_array->arg_info[i].name, op_array->arg_info[i].name_len + 1);
     212          30 :                                 ADD_SIZE(op_array->arg_info[i].name_len + 1);
     213             :                         }
     214          30 :                         if (op_array->arg_info[i].class_name) {
     215             : //???                           ADD_INTERNED_STRING(op_array->arg_info[i].class_name, op_array->arg_info[i].class_name_len + 1);
     216           0 :                                 ADD_SIZE(op_array->arg_info[i].class_name_len + 1);
     217             :                         }
     218             : 
     219             :                 }
     220             :         }
     221             : 
     222         451 :         if (op_array->brk_cont_array) {
     223           2 :                 ADD_DUP_SIZE(op_array->brk_cont_array, sizeof(zend_brk_cont_element) * op_array->last_brk_cont);
     224             :         }
     225             : 
     226         451 :         if (ZCG(accel_directives).save_comments && op_array->doc_comment) {
     227           0 :                 ADD_STRING(op_array->doc_comment);
     228             :         }
     229             : 
     230         451 :         if (op_array->try_catch_array) {
     231           8 :                 ADD_DUP_SIZE(op_array->try_catch_array, sizeof(zend_try_catch_element) * op_array->last_try_catch);
     232             :         }
     233             : 
     234         451 :         if (op_array->vars) {
     235             :                 int i;
     236             : 
     237          71 :                 ADD_DUP_SIZE(op_array->vars, sizeof(zend_string*) * op_array->last_var);
     238         239 :                 for (i = 0; i < op_array->last_var; i++) {
     239         168 :                         ADD_INTERNED_STRING(op_array->vars[i], 0);
     240             :                 }
     241             :         }
     242             : 
     243         451 :         RETURN_SIZE();
     244             : }
     245             : 
     246          29 : static uint zend_persist_op_array_calc(zval *zv TSRMLS_DC)
     247             : {
     248          29 :         START_SIZE();
     249          29 :         ADD_SIZE(sizeof(zend_op_array));
     250          29 :         ADD_SIZE(zend_persist_op_array_calc_ex(Z_PTR_P(zv) TSRMLS_CC));
     251          29 :         RETURN_SIZE();
     252             : }
     253             : 
     254           2 : static uint zend_persist_property_info_calc(zval *zv TSRMLS_DC)
     255             : {
     256           2 :         zend_property_info *prop = Z_PTR_P(zv);
     257           2 :         START_SIZE();
     258             : 
     259           2 :         ADD_SIZE(sizeof(zend_property_info));
     260           2 :         ADD_INTERNED_STRING(prop->name, 0);
     261           2 :         if (ZCG(accel_directives).save_comments && prop->doc_comment) {
     262           0 :                 ADD_STRING(prop->doc_comment);
     263             :         }
     264           2 :         RETURN_SIZE();
     265             : }
     266             : 
     267           1 : static uint zend_persist_class_entry_calc(zval *zv TSRMLS_DC)
     268             : {
     269           1 :         zend_class_entry *ce = Z_PTR_P(zv);
     270           1 :         START_SIZE();
     271             : 
     272           1 :         if (ce->type == ZEND_USER_CLASS) {
     273           1 :                 ADD_DUP_SIZE(ce, sizeof(zend_class_entry));
     274           1 :                 ADD_INTERNED_STRING(ce->name, 0);
     275           1 :                 ADD_SIZE(zend_hash_persist_calc(&ce->function_table, zend_persist_op_array_calc TSRMLS_CC));
     276           1 :                 if (ce->default_properties_table) {
     277             :                     int i;
     278             : 
     279           0 :                         ADD_SIZE(sizeof(zval) * ce->default_properties_count);
     280           0 :                         for (i = 0; i < ce->default_properties_count; i++) {
     281           0 :                                 ADD_SIZE(zend_persist_zval_calc(&ce->default_properties_table[i] TSRMLS_CC));
     282             :                         }
     283             :                 }
     284           1 :                 if (ce->default_static_members_table) {
     285             :                     int i;
     286             : 
     287           1 :                         ADD_SIZE(sizeof(zval) * ce->default_static_members_count);
     288           3 :                         for (i = 0; i < ce->default_static_members_count; i++) {
     289           2 :                                 ADD_SIZE(zend_persist_zval_calc(&ce->default_static_members_table[i] TSRMLS_CC));
     290             :                         }
     291             :                 }
     292           1 :                 ADD_SIZE(zend_hash_persist_calc(&ce->constants_table, zend_persist_zval_calc TSRMLS_CC));
     293             : 
     294           1 :                 if (ZEND_CE_FILENAME(ce)) {
     295           1 :                         ADD_STRING(ZEND_CE_FILENAME(ce));
     296             :                 }
     297           1 :                 if (ZCG(accel_directives).save_comments && ZEND_CE_DOC_COMMENT(ce)) {
     298           0 :                         ADD_STRING(ZEND_CE_DOC_COMMENT(ce));
     299             :                 }
     300             : 
     301           1 :                 ADD_SIZE(zend_hash_persist_calc(&ce->properties_info, zend_persist_property_info_calc TSRMLS_CC));
     302             : 
     303           1 :                 if (ce->trait_aliases) {
     304           0 :                         int i = 0;
     305           0 :                         while (ce->trait_aliases[i]) {
     306           0 :                                 if (ce->trait_aliases[i]->trait_method) {
     307           0 :                                         if (ce->trait_aliases[i]->trait_method->method_name) {
     308           0 :                                                 ADD_INTERNED_STRING(ce->trait_aliases[i]->trait_method->method_name, 0);
     309             :                                         }
     310           0 :                                         if (ce->trait_aliases[i]->trait_method->class_name) {
     311           0 :                                                 ADD_INTERNED_STRING(ce->trait_aliases[i]->trait_method->class_name, 0);
     312             :                                         }
     313           0 :                                         ADD_SIZE(sizeof(zend_trait_method_reference));
     314             :                                 }
     315             : 
     316           0 :                                 if (ce->trait_aliases[i]->alias) {
     317           0 :                                         ADD_INTERNED_STRING(ce->trait_aliases[i]->alias, 0);
     318             :                                 }
     319           0 :                                 ADD_SIZE(sizeof(zend_trait_alias));
     320           0 :                                 i++;
     321             :                         }
     322           0 :                         ADD_SIZE(sizeof(zend_trait_alias*) * (i + 1));
     323             :                 }
     324             : 
     325           1 :                 if (ce->trait_precedences) {
     326           0 :                         int i = 0;
     327             : 
     328           0 :                         while (ce->trait_precedences[i]) {
     329           0 :                                 ADD_INTERNED_STRING(ce->trait_precedences[i]->trait_method->method_name, 0);
     330           0 :                                 ADD_INTERNED_STRING(ce->trait_precedences[i]->trait_method->class_name, 0);
     331           0 :                                 ADD_SIZE(sizeof(zend_trait_method_reference));
     332             : 
     333           0 :                                 if (ce->trait_precedences[i]->exclude_from_classes) {
     334           0 :                                         int j = 0;
     335             : 
     336           0 :                                         while (ce->trait_precedences[i]->exclude_from_classes[j].class_name) {
     337           0 :                                                 ADD_INTERNED_STRING(ce->trait_precedences[i]->exclude_from_classes[j].class_name, 0);
     338           0 :                                                 j++;
     339             :                                         }
     340           0 :                                         ADD_SIZE(sizeof(zend_class_entry*) * (j + 1));
     341             :                                 }
     342           0 :                                 ADD_SIZE(sizeof(zend_trait_precedence));
     343           0 :                                 i++;
     344             :                         }
     345           0 :                         ADD_SIZE(sizeof(zend_trait_precedence*) * (i + 1));
     346             :                 }
     347             :         }
     348           1 :         RETURN_SIZE();
     349             : }
     350             : 
     351         422 : static uint zend_accel_persist_class_table_calc(HashTable *class_table TSRMLS_DC)
     352             : {
     353         422 :         return zend_hash_persist_calc(class_table, zend_persist_class_entry_calc TSRMLS_CC);
     354             : }
     355             : 
     356         422 : uint zend_accel_script_persist_calc(zend_persistent_script *new_persistent_script, char *key, unsigned int key_length TSRMLS_DC)
     357             : {
     358         422 :         START_SIZE();
     359             : 
     360         422 :         ADD_SIZE(zend_hash_persist_calc(&new_persistent_script->function_table, zend_persist_op_array_calc TSRMLS_CC));
     361         422 :         ADD_SIZE(zend_accel_persist_class_table_calc(&new_persistent_script->class_table TSRMLS_CC));
     362         422 :         ADD_SIZE(zend_persist_op_array_calc_ex(&new_persistent_script->main_op_array TSRMLS_CC));
     363         422 :         ADD_DUP_SIZE(key, key_length + 1);
     364         422 :         ADD_STRING(new_persistent_script->full_path);
     365         422 :         ADD_DUP_SIZE(new_persistent_script, sizeof(zend_persistent_script));
     366             : 
     367         422 :         RETURN_SIZE();
     368             : }

Generated by: LCOV version 1.10

Generated at Fri, 19 Sep 2014 17:11:11 +0000 (3 days ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.