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: 111 178 62.4 %
Date: 2014-11-22 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 ADD_DUP_SIZE(m,s)  ZCG(current_persistent_script)->size += zend_shared_memdup_size((void*)m, s)
      30             : #define ADD_SIZE(m)        ZCG(current_persistent_script)->size += ZEND_ALIGNED_SIZE(m)
      31             : 
      32             : #define ADD_ARENA_SIZE(m)        ZCG(current_persistent_script)->arena_size += ZEND_ALIGNED_SIZE(m)
      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 void zend_persist_zval_calc(zval *z TSRMLS_DC);
      51             : 
      52         927 : static void zend_hash_persist_calc(HashTable *ht, void (*pPersistElement)(zval *pElement TSRMLS_DC) TSRMLS_DC)
      53             : {
      54             :         uint idx;
      55             :         Bucket *p;
      56             : 
      57         927 :         if (!ht->nTableMask) {
      58         848 :                 return;
      59             :         }
      60          79 :         if (ht->u.flags & HASH_FLAG_PACKED) {
      61          10 :                 ADD_SIZE(sizeof(Bucket) * ht->nNumUsed);
      62             :         } else {
      63          69 :                 ADD_SIZE(sizeof(Bucket) * ht->nNumUsed + sizeof(uint32_t) * ht->nTableSize);
      64             :         }
      65             : 
      66         237 :         for (idx = 0; idx < ht->nNumUsed; idx++) {
      67         158 :                 p = ht->arData + idx;
      68         316 :                 if (Z_TYPE(p->val) == IS_UNDEF) continue;
      69             : 
      70             :                 /* persist bucket and key */
      71         157 :                 if (p->key) {
      72         124 :                         zend_uchar flags = GC_FLAGS(p->key) & ~ (IS_STR_PERSISTENT | IS_STR_INTERNED | IS_STR_PERMANENT);
      73         124 :                         ADD_INTERNED_STRING(p->key, 1);
      74         124 :                         GC_FLAGS(p->key) |= flags;
      75             :                 }
      76             : 
      77         157 :                 pPersistElement(&p->val TSRMLS_CC);
      78             :         }
      79             : }
      80             : 
      81           0 : static void zend_persist_ast_calc(zend_ast *ast TSRMLS_DC)
      82             : {
      83             :         uint32_t i;
      84             : 
      85           0 :         if (ast->kind == ZEND_AST_ZVAL) {
      86           0 :                 ADD_SIZE(sizeof(zend_ast_zval));
      87           0 :                 zend_persist_zval_calc(zend_ast_get_zval(ast) TSRMLS_CC);
      88           0 :         } else if (zend_ast_is_list(ast)) {
      89           0 :                 zend_ast_list *list = zend_ast_get_list(ast);
      90           0 :                 ADD_SIZE(sizeof(zend_ast_list) - sizeof(zend_ast *) + sizeof(zend_ast *) * list->children);
      91           0 :                 for (i = 0; i < list->children; i++) {
      92           0 :                         if (list->child[i]) {
      93           0 :                                 zend_persist_ast_calc(list->child[i] TSRMLS_CC);
      94             :                         }
      95             :                 }
      96             :         } else {
      97           0 :                 uint32_t children = zend_ast_get_num_children(ast);
      98           0 :                 ADD_SIZE(sizeof(zend_ast) - sizeof(zend_ast *) + sizeof(zend_ast *) * children);
      99           0 :                 for (i = 0; i < children; i++) {
     100           0 :                         if (ast->child[i]) {
     101           0 :                                 zend_persist_ast_calc(ast->child[i] TSRMLS_CC);
     102             :                         }
     103             :                 }
     104             :         }
     105           0 : }
     106             : 
     107        3063 : static void zend_persist_zval_calc(zval *z TSRMLS_DC)
     108             : {
     109             :         zend_uchar flags;
     110             :         uint size;
     111             : 
     112        3063 :         switch (Z_TYPE_P(z)) {
     113             :                 case IS_STRING:
     114             :                 case IS_CONSTANT:
     115        2364 :                         flags = Z_GC_FLAGS_P(z) & ~ (IS_STR_PERSISTENT | IS_STR_INTERNED | IS_STR_PERMANENT);
     116        2364 :                         ADD_INTERNED_STRING(Z_STR_P(z), 0);
     117        2364 :                         if (!Z_REFCOUNTED_P(z)) {
     118           0 :                                 Z_TYPE_FLAGS_P(z) &= ~ (IS_TYPE_REFCOUNTED | IS_TYPE_COPYABLE);
     119             :                         }
     120        2364 :                         Z_GC_FLAGS_P(z) |= flags;
     121        2364 :                         break;
     122             :                 case IS_ARRAY:
     123          67 :                         size = zend_shared_memdup_size(Z_ARR_P(z), sizeof(zend_array));
     124          67 :                         if (size) {
     125          67 :                                 ADD_SIZE(size);
     126          67 :                                 zend_hash_persist_calc(Z_ARRVAL_P(z), zend_persist_zval_calc TSRMLS_CC);
     127             :                         }
     128          67 :                         break;
     129             :                 case IS_REFERENCE:
     130           0 :                         size = zend_shared_memdup_size(Z_REF_P(z), sizeof(zend_reference));
     131           0 :                         if (size) {
     132           0 :                                 ADD_SIZE(size);
     133           0 :                                 zend_persist_zval_calc(Z_REFVAL_P(z) TSRMLS_CC);
     134             :                         }
     135           0 :                         break;
     136             :                 case IS_CONSTANT_AST:
     137           0 :                         size = zend_shared_memdup_size(Z_AST_P(z), sizeof(zend_ast_ref));
     138           0 :                         if (size) {
     139           0 :                                 ADD_SIZE(size);
     140           0 :                                 zend_persist_ast_calc(Z_ASTVAL_P(z) TSRMLS_CC);
     141             :                         }
     142             :                         break;
     143             :         }
     144        3063 : }
     145             : 
     146         451 : static void zend_persist_op_array_calc_ex(zend_op_array *op_array TSRMLS_DC)
     147             : {
     148         451 :         if (op_array->type != ZEND_USER_FUNCTION) {
     149           0 :                 return;
     150             :         }
     151             : 
     152         451 :         if (op_array->static_variables) {
     153          13 :                 ADD_DUP_SIZE(op_array->static_variables, sizeof(HashTable));
     154          13 :                 zend_hash_persist_calc(op_array->static_variables, zend_persist_zval_calc TSRMLS_CC);
     155             :         }
     156             : 
     157         451 :         if (zend_shared_alloc_get_xlat_entry(op_array->opcodes)) {
     158             :                 /* already stored */
     159           0 :                 if (op_array->function_name) {
     160           0 :                         zend_string *new_name = zend_shared_alloc_get_xlat_entry(op_array->function_name);
     161           0 :                         if (IS_ACCEL_INTERNED(new_name)) {
     162           0 :                                 op_array->function_name = new_name;
     163             :                         }
     164             :                 }
     165           0 :                 return;
     166             :         }
     167             : 
     168         451 :         if (op_array->literals) {
     169         451 :                 zval *p = op_array->literals;
     170         451 :                 zval *end = p + op_array->last_literal;
     171         451 :                 ADD_DUP_SIZE(op_array->literals, sizeof(zval) * op_array->last_literal);
     172        3838 :                 while (p < end) {
     173        2936 :                         zend_persist_zval_calc(p TSRMLS_CC);
     174        2936 :                         p++;
     175             :                 }
     176             :         }
     177             : 
     178         451 :         ADD_DUP_SIZE(op_array->opcodes, sizeof(zend_op) * op_array->last);
     179             : 
     180         451 :         if (op_array->function_name) {
     181          29 :                 zend_string *old_name = op_array->function_name;
     182          29 :                 zend_string *new_name = zend_shared_alloc_get_xlat_entry(old_name);
     183             : 
     184          29 :                 if (new_name) {
     185           0 :                         op_array->function_name = new_name;
     186             :                 } else {
     187          29 :                         ADD_INTERNED_STRING(op_array->function_name, 0);
     188          29 :                         zend_shared_alloc_register_xlat_entry(old_name, op_array->function_name);
     189             :                 }
     190             :     }
     191             : 
     192         451 :         if (op_array->filename) {
     193         451 :                 ADD_STRING(op_array->filename);
     194             :         }
     195             : 
     196         451 :         if (op_array->arg_info) {
     197             :                 uint32_t i;
     198             : 
     199          25 :                 ADD_DUP_SIZE(op_array->arg_info, sizeof(zend_arg_info) * op_array->num_args);
     200          55 :                 for (i = 0; i < op_array->num_args; i++) {
     201          30 :                         if (op_array->arg_info[i].name) {
     202             : //???                           ADD_INTERNED_STRING(op_array->arg_info[i].name, op_array->arg_info[i].name_len + 1);
     203          30 :                                 ADD_SIZE(op_array->arg_info[i].name_len + 1);
     204             :                         }
     205          30 :                         if (op_array->arg_info[i].class_name) {
     206             : //???                           ADD_INTERNED_STRING(op_array->arg_info[i].class_name, op_array->arg_info[i].class_name_len + 1);
     207           0 :                                 ADD_SIZE(op_array->arg_info[i].class_name_len + 1);
     208             :                         }
     209             : 
     210             :                 }
     211             :         }
     212             : 
     213         451 :         if (op_array->brk_cont_array) {
     214           2 :                 ADD_DUP_SIZE(op_array->brk_cont_array, sizeof(zend_brk_cont_element) * op_array->last_brk_cont);
     215             :         }
     216             : 
     217         451 :         if (ZCG(accel_directives).save_comments && op_array->doc_comment) {
     218           0 :                 ADD_STRING(op_array->doc_comment);
     219             :         }
     220             : 
     221         451 :         if (op_array->try_catch_array) {
     222           8 :                 ADD_DUP_SIZE(op_array->try_catch_array, sizeof(zend_try_catch_element) * op_array->last_try_catch);
     223             :         }
     224             : 
     225         451 :         if (op_array->vars) {
     226             :                 int i;
     227             : 
     228          71 :                 ADD_DUP_SIZE(op_array->vars, sizeof(zend_string*) * op_array->last_var);
     229         239 :                 for (i = 0; i < op_array->last_var; i++) {
     230         168 :                         ADD_INTERNED_STRING(op_array->vars[i], 0);
     231             :                 }
     232             :         }
     233             : }
     234             : 
     235          29 : static void zend_persist_op_array_calc(zval *zv TSRMLS_DC)
     236             : {
     237          29 :         ADD_ARENA_SIZE(sizeof(zend_op_array));
     238          29 :         zend_persist_op_array_calc_ex(Z_PTR_P(zv) TSRMLS_CC);
     239          29 : }
     240             : 
     241           2 : static void zend_persist_property_info_calc(zval *zv TSRMLS_DC)
     242             : {
     243           2 :         zend_property_info *prop = Z_PTR_P(zv);
     244             : 
     245           2 :         ADD_ARENA_SIZE(sizeof(zend_property_info));
     246           2 :         ADD_INTERNED_STRING(prop->name, 0);
     247           2 :         if (ZCG(accel_directives).save_comments && prop->doc_comment) {
     248           0 :                 ADD_STRING(prop->doc_comment);
     249             :         }
     250           2 : }
     251             : 
     252           1 : static void zend_persist_class_entry_calc(zval *zv TSRMLS_DC)
     253             : {
     254           1 :         zend_class_entry *ce = Z_PTR_P(zv);
     255             : 
     256           1 :         if (ce->type == ZEND_USER_CLASS) {
     257           1 :                 ADD_ARENA_SIZE(sizeof(zend_class_entry));
     258           1 :                 ADD_INTERNED_STRING(ce->name, 0);
     259           1 :                 zend_hash_persist_calc(&ce->function_table, zend_persist_op_array_calc TSRMLS_CC);
     260           1 :                 if (ce->default_properties_table) {
     261             :                     int i;
     262             : 
     263           0 :                         ADD_SIZE(sizeof(zval) * ce->default_properties_count);
     264           0 :                         for (i = 0; i < ce->default_properties_count; i++) {
     265           0 :                                 zend_persist_zval_calc(&ce->default_properties_table[i] TSRMLS_CC);
     266             :                         }
     267             :                 }
     268           1 :                 if (ce->default_static_members_table) {
     269             :                     int i;
     270             : 
     271           1 :                         ADD_SIZE(sizeof(zval) * ce->default_static_members_count);
     272           3 :                         for (i = 0; i < ce->default_static_members_count; i++) {
     273           2 :                                 zend_persist_zval_calc(&ce->default_static_members_table[i] TSRMLS_CC);
     274             :                         }
     275             :                 }
     276           1 :                 zend_hash_persist_calc(&ce->constants_table, zend_persist_zval_calc TSRMLS_CC);
     277             : 
     278           1 :                 if (ZEND_CE_FILENAME(ce)) {
     279           1 :                         ADD_STRING(ZEND_CE_FILENAME(ce));
     280             :                 }
     281           1 :                 if (ZCG(accel_directives).save_comments && ZEND_CE_DOC_COMMENT(ce)) {
     282           0 :                         ADD_STRING(ZEND_CE_DOC_COMMENT(ce));
     283             :                 }
     284             : 
     285           1 :                 zend_hash_persist_calc(&ce->properties_info, zend_persist_property_info_calc TSRMLS_CC);
     286             : 
     287           1 :                 if (ce->trait_aliases) {
     288           0 :                         int i = 0;
     289           0 :                         while (ce->trait_aliases[i]) {
     290           0 :                                 if (ce->trait_aliases[i]->trait_method) {
     291           0 :                                         if (ce->trait_aliases[i]->trait_method->method_name) {
     292           0 :                                                 ADD_INTERNED_STRING(ce->trait_aliases[i]->trait_method->method_name, 0);
     293             :                                         }
     294           0 :                                         if (ce->trait_aliases[i]->trait_method->class_name) {
     295           0 :                                                 ADD_INTERNED_STRING(ce->trait_aliases[i]->trait_method->class_name, 0);
     296             :                                         }
     297           0 :                                         ADD_SIZE(sizeof(zend_trait_method_reference));
     298             :                                 }
     299             : 
     300           0 :                                 if (ce->trait_aliases[i]->alias) {
     301           0 :                                         ADD_INTERNED_STRING(ce->trait_aliases[i]->alias, 0);
     302             :                                 }
     303           0 :                                 ADD_SIZE(sizeof(zend_trait_alias));
     304           0 :                                 i++;
     305             :                         }
     306           0 :                         ADD_SIZE(sizeof(zend_trait_alias*) * (i + 1));
     307             :                 }
     308             : 
     309           1 :                 if (ce->trait_precedences) {
     310           0 :                         int i = 0;
     311             : 
     312           0 :                         while (ce->trait_precedences[i]) {
     313           0 :                                 ADD_INTERNED_STRING(ce->trait_precedences[i]->trait_method->method_name, 0);
     314           0 :                                 ADD_INTERNED_STRING(ce->trait_precedences[i]->trait_method->class_name, 0);
     315           0 :                                 ADD_SIZE(sizeof(zend_trait_method_reference));
     316             : 
     317           0 :                                 if (ce->trait_precedences[i]->exclude_from_classes) {
     318           0 :                                         int j = 0;
     319             : 
     320           0 :                                         while (ce->trait_precedences[i]->exclude_from_classes[j].class_name) {
     321           0 :                                                 ADD_INTERNED_STRING(ce->trait_precedences[i]->exclude_from_classes[j].class_name, 0);
     322           0 :                                                 j++;
     323             :                                         }
     324           0 :                                         ADD_SIZE(sizeof(zend_class_entry*) * (j + 1));
     325             :                                 }
     326           0 :                                 ADD_SIZE(sizeof(zend_trait_precedence));
     327           0 :                                 i++;
     328             :                         }
     329           0 :                         ADD_SIZE(sizeof(zend_trait_precedence*) * (i + 1));
     330             :                 }
     331             :         }
     332           1 : }
     333             : 
     334         422 : static void zend_accel_persist_class_table_calc(HashTable *class_table TSRMLS_DC)
     335             : {
     336         422 :         zend_hash_persist_calc(class_table, zend_persist_class_entry_calc TSRMLS_CC);
     337         422 : }
     338             : 
     339         422 : uint zend_accel_script_persist_calc(zend_persistent_script *new_persistent_script, char *key, unsigned int key_length TSRMLS_DC)
     340             : {
     341         422 :         new_persistent_script->mem = NULL;
     342         422 :         new_persistent_script->size = 0;
     343         422 :         new_persistent_script->arena_mem = NULL;
     344         422 :         new_persistent_script->arena_size = 0;
     345         422 :         ZCG(current_persistent_script) = new_persistent_script;
     346             : 
     347         422 :         ADD_DUP_SIZE(new_persistent_script, sizeof(zend_persistent_script));
     348         422 :         ADD_DUP_SIZE(key, key_length + 1);
     349         422 :         ADD_STRING(new_persistent_script->full_path);
     350             : 
     351         422 :         zend_accel_persist_class_table_calc(&new_persistent_script->class_table TSRMLS_CC);
     352         422 :         zend_hash_persist_calc(&new_persistent_script->function_table, zend_persist_op_array_calc TSRMLS_CC);
     353         422 :         zend_persist_op_array_calc_ex(&new_persistent_script->main_op_array TSRMLS_CC);
     354             : 
     355         422 :         new_persistent_script->size += new_persistent_script->arena_size;
     356             : 
     357         422 :         ZCG(current_persistent_script) = NULL;
     358             : 
     359         422 :         return new_persistent_script->size;
     360             : }

Generated by: LCOV version 1.10

Generated at Sat, 22 Nov 2014 23:01:19 +0000 (37 hours ago)

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