source: trunk/xcache.h @ 522

Last change on this file since 522 was 522, checked in by moo, 7 years ago

slide hits per second and hour

  • Property svn:eol-style set to native
File size: 9.4 KB
RevLine 
[1]1#ifndef __XCACHE_H
2#define __XCACHE_H
3#define XCACHE_NAME       "XCache"
[309]4#define XCACHE_VERSION    "2.0.0-dev"
[1]5#define XCACHE_AUTHOR     "mOo"
[442]6#define XCACHE_COPYRIGHT  "Copyright (c) 2005-2007"
[1]7#define XCACHE_URL        "http://xcache.lighttpd.net"
8
9#include <php.h>
10#include <zend_compile.h>
11#include <zend_API.h>
[485]12#include <zend.h>
[1]13#include "php_ini.h"
14#include "zend_hash.h"
15
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
[148]19#include "xc_shm.h"
[1]20#include "lock.h"
21
[165]22#define HAVE_INODE
[1]23#if !defined(ZEND_ENGINE_2_1) && (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 1 || PHP_MAJOR_VERSION > 5)
24#   define ZEND_ENGINE_2_1
25#endif
[78]26#if !defined(ZEND_ENGINE_2_2) && (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 2 || PHP_MAJOR_VERSION > 5)
27#   define ZEND_ENGINE_2_2
28#endif
[480]29#if !defined(ZEND_ENGINE_2_3) && (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 3 || PHP_MAJOR_VERSION > 5)
30#   define ZEND_ENGINE_2_3
31#endif
[1]32
[19]33#define NOTHING
[1]34/* ZendEngine code Switcher */
35#ifndef ZEND_ENGINE_2
36#   define ZESW(v1, v2) v1
37#else
38#   define ZESW(v1, v2) v2
39#endif
40
[485]41#ifdef ALLOCA_FLAG
42#   define my_do_alloca(size, use_heap) do_alloca(size, use_heap)
43#   define my_free_alloca(size, use_heap) free_alloca(size, use_heap)
44#else
45#   define my_do_alloca(size, use_heap) do_alloca(size)
46#   define my_free_alloca(size, use_heap) free_alloca(size)
47#   define ALLOCA_FLAG(x)
48#endif
49#ifndef Z_SET_ISREF
50#   define Z_SET_ISREF(z) (z).is_ref = 1;
51#endif
52#ifndef Z_UNSET_ISREF
53#   define Z_UNSET_ISREF(z) (z).is_ref = 0;
54#endif
55#ifndef Z_SET_REFCOUNT
56#   define Z_SET_REFCOUNT(z, rc) (z).refcount = rc;
57#endif
[491]58#ifndef IS_CONSTANT_TYPE_MASK
59#   define IS_CONSTANT_TYPE_MASK 0xf
60#endif
[485]61
[263]62/* {{{ dirty fix for PHP 6 */
63#ifdef add_assoc_long_ex
64static inline void my_add_assoc_long_ex(zval *arg, char *key, uint key_len, long value)
65{
66    add_assoc_long_ex(arg, key, key_len, value);
67}
68#   undef add_assoc_long_ex
69#   define add_assoc_long_ex my_add_assoc_long_ex
70#endif
71#ifdef add_assoc_bool_ex
72static inline void my_add_assoc_bool_ex(zval *arg, char *key, uint key_len, zend_bool value)
73{
74    add_assoc_bool_ex(arg, key, key_len, value);
75}
76#   undef add_assoc_bool_ex
77#   define add_assoc_bool_ex my_add_assoc_bool_ex
78#endif
79#ifdef add_assoc_null_ex
80static inline void my_add_assoc_null_ex(zval *arg, char *key, uint key_len)
81{
82    add_assoc_null_ex(arg, key, key_len);
83}
84#   undef add_assoc_null_ex
85#   define add_assoc_null_ex my_add_assoc_null_ex
86#endif
87/* }}} */
88
[1]89/* unicode */
90#ifdef IS_UNICODE
91#   define UNISW(text, unicode) unicode
92#else
93#   define UNISW(text, unicode) text
94#endif
95#define BUCKET_KEY_SIZE(b) \
96        (UNISW( \
97            (b)->nKeyLength, \
98                ((b)->key.type == IS_UNICODE) \
99                ? UBYTES(b->nKeyLength) \
100                : b->nKeyLength \
101                ))
[200]102#define BUCKET_KEY_S(b)    (UNISW((b)->arKey, (b)->key.arKey.s))
103#define BUCKET_KEY_U(b)    (UNISW((b)->arKey, (b)->key.arKey.u))
104#define BUCKET_KEY_TYPE(b) (UNISW(IS_STRING,  (b)->key.type))
[2]105#ifdef IS_UNICODE
[104]106#   define BUCKET_HEAD_SIZE(b) XtOffsetOf(Bucket, key.arKey)
[2]107#else
[11]108#   define BUCKET_HEAD_SIZE(b) XtOffsetOf(Bucket, arKey)
[2]109#endif
[1]110#define BUCKET_SIZE(b) (BUCKET_HEAD_SIZE(b) + BUCKET_KEY_SIZE(b))
111
112#ifndef IS_UNICODE
113typedef char *zstr;
[103]114#   define ZSTR_S(s)     (s)
115#   define ZSTR_U(s)     (s)
116#   define ZSTR_V(s)     (s)
117#   define ZSTR_PS(s)    (s)
118#   define ZSTR_PU(s)    (s)
119#   define ZSTR_PV(s)    (s)
[1]120#else
[103]121#   define ZSTR_S(zs)    ((zs).s)
122#   define ZSTR_U(zs)    ((zs).u)
123#   define ZSTR_V(zs)    ((zs).v)
124#   define ZSTR_PS(pzs)  ((pzs)->s)
125#   define ZSTR_PU(pzs)  ((pzs)->u)
126#   define ZSTR_PV(pzs)  ((pzs)->v)
[1]127#endif
128
[103]129#ifndef ZSTR
130#   define ZSTR(s)      (s)
131#endif
132
133#ifndef Z_UNIVAL
134#   define Z_UNIVAL(zval) (zval).value.str.val
135#   define Z_UNILEN(zval) (zval).value.str.len
136#endif
137
[1]138/* {{{ u hash wrapper */
139#ifndef IS_UNICODE
140#   define zend_u_hash_add(ht, type, arKey, nKeyLength, pData, nDataSize, pDest) \
141       zend_hash_add(ht, arKey, nKeyLength, pData, nDataSize, pDest)
142
[506]143#   define zend_u_hash_quick_add(ht, type, arKey, nKeyLength, h, pData, nDataSize, pDest) \
144       zend_hash_quick_add(ht, arKey, nKeyLength, h, pData, nDataSize, pDest)
145
[88]146#   define zend_u_hash_update(ht, type, arKey, nKeyLength, pData, nDataSize, pDest) \
147       zend_hash_update(ht, arKey, nKeyLength, pData, nDataSize, pDest)
148
[506]149#   define zend_u_hash_quick_update(ht, type, arKey, nKeyLength, h, pData, nDataSize, pDest) \
150       zend_hash_quick_update(ht, arKey, nKeyLength, h, pData, nDataSize, pDest)
151
[1]152#   define zend_u_hash_find(ht, type, arKey, nKeyLength, pData) \
153       zend_hash_find(ht, arKey, nKeyLength, pData)
154
[200]155#   define zend_u_hash_quick_find(ht, type, arKey, nKeyLength, h, pData) \
156       zend_hash_quick_find(ht, arKey, nKeyLength, h, pData)
157
[1]158#   define add_u_assoc_zval_ex(arg, type, key, key_len, value) \
159        add_assoc_zval_ex(arg, key, key_len, value)
160
[268]161#   define zend_u_is_auto_global(type, name, name_len) \
162        zend_is_auto_global(name, name_len)
[1]163#endif
164/* }}} */
165
166
167#define ECALLOC_N(x, n) ((x) = ecalloc(n, sizeof((x)[0])))
168#define ECALLOC_ONE(x) ECALLOC_N(x, 1)
169
170
171
172typedef ulong xc_hash_value_t;
173typedef struct {
174    int bits;
175    int size;
176    int mask;
177} xc_hash_t;
178
179/* the class entry type to be stored in class_table */
180typedef ZESW(zend_class_entry, zend_class_entry*) xc_cest_t;
181
182/* xc_cest_t to (zend_class_entry*) */
183#define CestToCePtr(st) (ZESW(\
184            &(st), \
185            st \
186            ) )
187
188/* ZCEP=zend class entry ptr */
189#define ZCEP_REFCOUNT_PTR(pce) (ZESW( \
190            (pce)->refcount, \
191            &((pce)->refcount) \
192            ))
193
194#define ZCE_REFCOUNT_PTR(ce) ZCE_REFCOUNT_PTR(&ce)
195
196typedef zend_op_array *(zend_compile_file_t)(zend_file_handle *h, int type TSRMLS_DC);
197
[311]198typedef struct _xc_entry_t xc_entry_t;
199typedef struct _xc_entry_data_php_t xc_entry_data_php_t;
[1]200/* {{{ xc_cache_t */
[11]201typedef struct {
[1]202    int cacheid;
203    xc_hash_t  *hcache; /* hash to cacheid */
204
[11]205    time_t     compiling;
[1]206    zend_ulong misses;
207    zend_ulong hits;
208    zend_ulong clogs;
209    zend_ulong ooms;
[456]210    zend_ulong errors;
[1]211    xc_lock_t  *lck;
212    xc_shm_t   *shm; /* to which shm contains us */
213    xc_mem_t   *mem; /* to which mem contains us */
214
215    xc_entry_t **entries;
[32]216    int entries_count;
[311]217    xc_entry_data_php_t **phps;
218    int phps_count;
[1]219    xc_entry_t *deletes;
[32]220    int deletes_count;
[311]221    xc_hash_t  *hentry; /* hash settings to entry */
222    xc_hash_t  *hphp;   /* hash settings to php */
[114]223
224    time_t     last_gc_deletes;
225    time_t     last_gc_expires;
[522]226
227    time_t     hits_by_hour_cur_time;
228    zend_uint  hits_by_hour_cur_slot;
229    zend_ulong hits_by_hour[24];
230    time_t     hits_by_second_cur_time;
231    zend_uint  hits_by_second_cur_slot;
232    zend_ulong hits_by_second[5];
[1]233} xc_cache_t;
234/* }}} */
235/* {{{ xc_classinfo_t */
236typedef struct {
[14]237#ifdef IS_UNICODE
238    zend_uchar type;
239#endif
[103]240    zstr      key;
[1]241    zend_uint key_size;
[506]242    ulong     h;
[1]243    xc_cest_t cest;
[212]244    int       oplineno;
[1]245} xc_classinfo_t;
246/* }}} */
[95]247#ifdef HAVE_XCACHE_CONSTANT
248/* {{{ xc_constinfo_t */
249typedef struct {
250#ifdef IS_UNICODE
251    zend_uchar type;
252#endif
[103]253    zstr      key;
[95]254    zend_uint key_size;
[506]255    ulong     h;
[95]256    zend_constant constant;
257} xc_constinfo_t;
258/* }}} */
259#endif
[1]260/* {{{ xc_funcinfo_t */
261typedef struct {
[14]262#ifdef IS_UNICODE
263    zend_uchar type;
264#endif
[103]265    zstr      key;
[1]266    zend_uint key_size;
[506]267    ulong     h;
[1]268    zend_function func;
269} xc_funcinfo_t;
270/* }}} */
[268]271#ifdef ZEND_ENGINE_2_1
272/* {{{ xc_autoglobal_t */
273typedef struct {
274#ifdef IS_UNICODE
275    zend_uchar type;
276#endif
277    zstr       key;
278    zend_uint  key_len;
[506]279    ulong      h;
[268]280} xc_autoglobal_t;
281/* }}} */
282#endif
[1]283typedef enum { XC_TYPE_PHP, XC_TYPE_VAR } xc_entry_type_t;
[311]284typedef char xc_md5sum_t[16];
[496]285/* {{{ xc_compilererror_t */
286typedef struct {
287    uint lineno;
288    int error_len;
289    char *error;
290} xc_compilererror_t;
291/* }}} */
[1]292/* {{{ xc_entry_data_php_t */
[311]293struct _xc_entry_data_php_t {
294    xc_hash_value_t hvalue; /* hash of md5 */
295    xc_entry_data_php_t *next;
296    xc_cache_t *cache;      /* which cache it's on */
297
298    xc_md5sum_t md5;        /* md5sum of the source */
299    zend_ulong  refcount;   /* count of entries referencing to this data */
300
[1]301    size_t sourcesize;
[311]302    zend_ulong hits;        /* hits of this php */
303    size_t     size;
[1]304
305    zend_op_array *op_array;
306
[95]307#ifdef HAVE_XCACHE_CONSTANT
308    zend_uint constinfo_cnt;
309    xc_constinfo_t *constinfos;
310#endif
311
[1]312    zend_uint funcinfo_cnt;
313    xc_funcinfo_t *funcinfos;
314
315    zend_uint classinfo_cnt;
316    xc_classinfo_t *classinfos;
[212]317    zend_bool have_early_binding;
[268]318
319#ifdef ZEND_ENGINE_2_1
320    zend_uint autoglobal_cnt;
321    xc_autoglobal_t *autoglobals;
322#endif
[311]323
[496]324#ifdef E_STRICT
325    zend_uint compilererror_cnt;
326    xc_compilererror_t *compilererrors;
327#endif
328
[311]329    zend_bool  have_references;
330};
[1]331/* }}} */
332/* {{{ xc_entry_data_var_t */
333typedef struct {
334    zval   *value;
[311]335
336    zend_bool  have_references;
[1]337} xc_entry_data_var_t;
338/* }}} */
339typedef zvalue_value xc_entry_name_t;
340/* {{{ xc_entry_t */
341struct _xc_entry_t {
342    xc_entry_type_t type;
343    xc_hash_value_t hvalue;
344    xc_entry_t *next;
345    xc_cache_t *cache;      /* which cache it's on */
346
347    size_t     size;
[311]348    zend_ulong refcount;    /* count of instances holding this entry */
[1]349    zend_ulong hits;
350    time_t     ctime;           /* the ctime of this entry */
351    time_t     atime;           /* the atime of this entry */
352    time_t     dtime;           /* the deletion time of this entry */
[114]353    long       ttl;             /* ttl of time entry, var only */
[1]354
355#ifdef IS_UNICODE
356    zend_uchar name_type;
357#endif
358    xc_entry_name_t name;
359
360    union {
361        xc_entry_data_php_t *php;
362        xc_entry_data_var_t *var;
363    } data;
[233]364
[311]365    time_t mtime;           /* the mtime of origin source file */
366#ifdef HAVE_INODE
367    int device;             /* the filesystem device */
368    int inode;              /* the filesystem inode */
369#endif
[1]370};
371/* }}} */
372
373extern zend_module_entry xcache_module_entry;
374#define phpext_xcache_ptr &xcache_module_entry
375
376int xc_is_rw(const void *p);
377int xc_is_ro(const void *p);
378int xc_is_shm(const void *p);
[498]379void xc_gc_add_op_array(zend_op_array *op_array TSRMLS_DC);
[1]380
381#endif /* __XCACHE_H */
Note: See TracBrowser for help on using the repository browser.