Advertisement
Guest User

Hannes Landeholm

a guest
Jan 18th, 2011
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 25.88 KB | None | 0 0
  1. diff -crB apc_vanilla/apc_cache.c apc/apc_cache.c
  2. *** apc_vanilla/apc_cache.c 2011-01-11 19:06:38.000000000 +0000
  3. --- apc/apc_cache.c 2011-01-16 06:50:35.000000000 +0000
  4. ***************
  5. *** 229,235 ****
  6.   /* }}} */
  7.  
  8.   /* {{{ apc_cache_create */
  9. ! apc_cache_t* apc_cache_create(int size_hint, int gc_ttl, int ttl TSRMLS_DC)
  10.   {
  11.       apc_cache_t* cache;
  12.       int cache_size;
  13. --- 229,237 ----
  14.   /* }}} */
  15.  
  16.   /* {{{ apc_cache_create */
  17. ! /* SHARED MMAP HACK CODE START */
  18. ! apc_cache_t* apc_cache_create(int size_hint, int gc_ttl, int ttl TSRMLS_DC, int static_cache_id)
  19. ! /* SHARED MMAP HACK CODE END */
  20.   {
  21.       apc_cache_t* cache;
  22.       int cache_size;
  23. ***************
  24. *** 240,272 ****
  25.       cache = (apc_cache_t*) apc_emalloc(sizeof(apc_cache_t) TSRMLS_CC);
  26.       cache_size = sizeof(cache_header_t) + num_slots*sizeof(slot_t*);
  27.  
  28. !     cache->shmaddr = apc_sma_malloc(cache_size TSRMLS_CC);
  29. !     if(!cache->shmaddr) {
  30. !         apc_error("Unable to allocate shared memory for cache structures.  (Perhaps your shared memory size isn't large enough?). " TSRMLS_CC);
  31. !         return NULL;
  32. !     }
  33. !     memset(cache->shmaddr, 0, cache_size);
  34. !
  35. !     cache->header = (cache_header_t*) cache->shmaddr;
  36. !     cache->header->num_hits = 0;
  37. !     cache->header->num_misses = 0;
  38. !     cache->header->deleted_list = NULL;
  39. !     cache->header->start_time = time(NULL);
  40. !     cache->header->expunges = 0;
  41. !     cache->header->busy = 0;
  42. !
  43. !     cache->slots = (slot_t**) (((char*) cache->shmaddr) + sizeof(cache_header_t));
  44.       cache->num_slots = num_slots;
  45.       cache->gc_ttl = gc_ttl;
  46.       cache->ttl = ttl;
  47. -     CREATE_LOCK(cache->header->lock);
  48. - #if NONBLOCKING_LOCK_AVAILABLE
  49. -     CREATE_LOCK(cache->header->wrlock);
  50. - #endif
  51. -     memset(cache->slots, 0, sizeof(slot_t*)*num_slots);
  52.       cache->expunge_cb = apc_cache_expunge;
  53.       cache->has_lock = 0;
  54. !
  55.       return cache;
  56.   }
  57.   /* }}} */
  58. --- 242,298 ----
  59.       cache = (apc_cache_t*) apc_emalloc(sizeof(apc_cache_t) TSRMLS_CC);
  60.       cache_size = sizeof(cache_header_t) + num_slots*sizeof(slot_t*);
  61.  
  62. !   /* SHARED MMAP HACK CODE START */
  63. !   static pthread_mutex_t cache_init_mutex = PTHREAD_MUTEX_INITIALIZER;
  64. !   pthread_mutex_lock(&cache_init_mutex);
  65. !   // Get static cache pointers.
  66. !   if (static_cache_id == APC_HACK_STATIC_CACHE_CORE)
  67. !       cache->shmaddr = apc_sma_hack_core_cache_addr_get();
  68. !   else if (static_cache_id == APC_HACK_STATIC_CACHE_USER)
  69. !       cache->shmaddr = apc_sma_hack_user_cache_addr_get();
  70. !   else
  71. !       cache->shmaddr = (void*)0;
  72. !   char init_header = 0;
  73. !   if (cache->shmaddr == (void*)0) {
  74. !       // Allocate a new shared memory address.
  75. !       cache->shmaddr = apc_sma_malloc(cache_size TSRMLS_CC);
  76. !       if(!cache->shmaddr) {
  77. !           apc_error("Unable to allocate shared memory for cache structures.  (Perhaps your shared memory size isn't large enough?). " TSRMLS_CC);
  78. !           pthread_mutex_unlock(&cache_init_mutex);
  79. !           return NULL;
  80. !       }
  81. !       memset(cache->shmaddr, 0, cache_size);
  82. !       // Set static cache pointers.
  83. !       if (static_cache_id == APC_HACK_STATIC_CACHE_CORE)
  84. !           apc_sma_hack_core_cache_addr_set(cache->shmaddr);
  85. !       else if (static_cache_id == APC_HACK_STATIC_CACHE_USER)
  86. !           apc_sma_hack_user_cache_addr_set(cache->shmaddr);
  87. !       init_header = 1;
  88. !   }
  89. !   cache->slots = (slot_t**) (((char*) cache->shmaddr) + sizeof(cache_header_t));
  90. !   cache->header = (cache_header_t*) cache->shmaddr;
  91. !   if (init_header == 1) {
  92. !       cache->header->num_hits = 0;
  93. !       cache->header->num_misses = 0;
  94. !       cache->header->deleted_list = NULL;
  95. !       cache->header->start_time = time(NULL);
  96. !       cache->header->expunges = 0;
  97. !       cache->header->busy = 0;
  98. !       CREATE_LOCK(cache->header->lock);
  99. !   #if NONBLOCKING_LOCK_AVAILABLE
  100. !       CREATE_LOCK(cache->header->wrlock);
  101. !   #endif
  102. !       memset(cache->slots, 0, sizeof(slot_t*)*num_slots);
  103. !   }
  104. !   pthread_mutex_unlock(&cache_init_mutex);
  105. !   // Thread local cache data...
  106.       cache->num_slots = num_slots;
  107.       cache->gc_ttl = gc_ttl;
  108.       cache->ttl = ttl;
  109.       cache->expunge_cb = apc_cache_expunge;
  110.       cache->has_lock = 0;
  111. !   /* SHARED MMAP HACK CODE END */
  112. !  
  113.       return cache;
  114.   }
  115.   /* }}} */
  116. diff -crB apc_vanilla/apc_cache.h apc/apc_cache.h
  117. *** apc_vanilla/apc_cache.h 2011-01-11 19:06:38.000000000 +0000
  118. --- apc/apc_cache.h 2011-01-16 02:05:48.000000000 +0000
  119. ***************
  120. *** 149,154 ****
  121. --- 149,156 ----
  122.   };
  123.   /* }}} */
  124.  
  125. + /* SHARED MMAP HACK CODE START */
  126. +
  127.   /*
  128.    * apc_cache_create creates the shared memory compiler cache. This function
  129.    * should be called just once (ideally in the web server parent process, e.g.
  130. ***************
  131. *** 167,173 ****
  132.    * is needed.  This helps in cleaning up the cache and ensuring that entries
  133.    * hit frequently stay cached and ones not hit very often eventually disappear.
  134.    */
  135. ! extern T apc_cache_create(int size_hint, int gc_ttl, int ttl TSRMLS_DC);
  136.  
  137.   /*
  138.    * apc_cache_destroy releases any OS resources associated with a cache object.
  139. --- 169,177 ----
  140.    * is needed.  This helps in cleaning up the cache and ensuring that entries
  141.    * hit frequently stay cached and ones not hit very often eventually disappear.
  142.    */
  143. ! extern T apc_cache_create(int size_hint, int gc_ttl, int ttl TSRMLS_DC, int static_cache_id);
  144. !
  145. ! /* SHARED MMAP HACK CODE END */
  146.  
  147.   /*
  148.    * apc_cache_destroy releases any OS resources associated with a cache object.
  149. diff -crB apc_vanilla/apc_globals.h apc/apc_globals.h
  150. *** apc_vanilla/apc_globals.h   2011-01-11 19:06:38.000000000 +0000
  151. --- apc/apc_globals.h   2011-01-16 01:54:50.000000000 +0000
  152. ***************
  153. *** 76,82 ****
  154.   #endif
  155.       char** filters;         /* array of regex filters that prevent caching */
  156.       void* compiled_filters; /* compiled regex filters */
  157. !
  158.       /* module variables */
  159.       zend_bool initialized;       /* true if module was initialized */
  160.       apc_stack_t* cache_stack;    /* the stack of cached executable code */
  161. --- 76,87 ----
  162.   #endif
  163.       char** filters;         /* array of regex filters that prevent caching */
  164.       void* compiled_filters; /* compiled regex filters */
  165. !  
  166. !   /* SHARED MMAP HACK CODE START */
  167. !   time_t init_time;            /* time when module was initialized */
  168. !   void* reserved_mmap_addr;    /* reserved address to use when mmap'ing memory */
  169. !   /* SHARED MMAP HACK CODE END */
  170. !  
  171.       /* module variables */
  172.       zend_bool initialized;       /* true if module was initialized */
  173.       apc_stack_t* cache_stack;    /* the stack of cached executable code */
  174. diff -crB apc_vanilla/apc.h apc/apc.h
  175. *** apc_vanilla/apc.h   2011-01-11 19:06:38.000000000 +0000
  176. --- apc/apc.h   2011-01-16 00:57:56.000000000 +0000
  177. ***************
  178. *** 109,114 ****
  179. --- 109,120 ----
  180.   #define APC_NEGATIVE_MATCH 1
  181.   #define APC_POSITIVE_MATCH 2
  182.  
  183. + /* SHARED MMAP HACK CODE START */
  184. + #define APC_HACK_STATIC_CACHE_NONE 0
  185. + #define APC_HACK_STATIC_CACHE_CORE 1
  186. + #define APC_HACK_STATIC_CACHE_USER 2
  187. + /* SHARED MMAP HACK CODE END */
  188. +  
  189.   #define apc_time() \
  190.       (APCG(use_request_time) ? (time_t) sapi_get_request_time(TSRMLS_C) : time(0));
  191.  
  192. diff -crB apc_vanilla/apc_main.c apc/apc_main.c
  193. *** apc_vanilla/apc_main.c  2011-01-11 19:06:38.000000000 +0000
  194. --- apc/apc_main.c  2011-01-16 02:17:15.000000000 +0000
  195. ***************
  196. *** 799,813 ****
  197.   /* {{{ module init and shutdown */
  198.  
  199.   int apc_module_init(int module_number TSRMLS_DC)
  200. ! {
  201.       /* apc initialization */
  202.   #if APC_MMAP
  203.       apc_sma_init(APCG(shm_segments), APCG(shm_size), APCG(mmap_file_mask) TSRMLS_CC);
  204.   #else
  205.       apc_sma_init(APCG(shm_segments), APCG(shm_size), NULL TSRMLS_CC);
  206.   #endif
  207. !     apc_cache = apc_cache_create(APCG(num_files_hint), APCG(gc_ttl), APCG(ttl) TSRMLS_CC);
  208. !     apc_user_cache = apc_cache_create(APCG(user_entries_hint), APCG(gc_ttl), APCG(user_ttl) TSRMLS_CC);
  209.  
  210.       /* override compilation */
  211.       old_compile_file = zend_compile_file;
  212. --- 799,813 ----
  213.   /* {{{ module init and shutdown */
  214.  
  215.   int apc_module_init(int module_number TSRMLS_DC)
  216. ! {
  217.       /* apc initialization */
  218.   #if APC_MMAP
  219.       apc_sma_init(APCG(shm_segments), APCG(shm_size), APCG(mmap_file_mask) TSRMLS_CC);
  220.   #else
  221.       apc_sma_init(APCG(shm_segments), APCG(shm_size), NULL TSRMLS_CC);
  222.   #endif
  223. !     apc_cache = apc_cache_create(APCG(num_files_hint), APCG(gc_ttl), APCG(ttl) TSRMLS_CC, APC_HACK_STATIC_CACHE_CORE);
  224. !     apc_user_cache = apc_cache_create(APCG(user_entries_hint), APCG(gc_ttl), APCG(user_ttl) TSRMLS_CC, APC_HACK_STATIC_CACHE_USER);
  225.  
  226.       /* override compilation */
  227.       old_compile_file = zend_compile_file;
  228. diff -crB apc_vanilla/apc_mmap.c apc/apc_mmap.c
  229. *** apc_vanilla/apc_mmap.c  2011-01-11 19:06:38.000000000 +0000
  230. --- apc/apc_mmap.c  2011-01-17 00:00:27.000000000 +0000
  231. ***************
  232. *** 30,35 ****
  233. --- 30,36 ----
  234.   #include "apc.h"
  235.   #include "apc_mmap.h"
  236.   #include "apc_lock.h"
  237. + #include "apc_globals.h"
  238.  
  239.   #if APC_MMAP
  240.  
  241. ***************
  242. *** 53,148 ****
  243.   # define MAP_ANON MAP_ANONYMOUS
  244.   #endif
  245.  
  246.   apc_segment_t apc_mmap(char *file_mask, size_t size TSRMLS_DC)
  247.   {
  248. !     apc_segment_t segment;
  249.  
  250. !     int fd = -1;
  251. !     int flags = MAP_SHARED | MAP_NOSYNC;
  252.       int remap = 1;
  253. !
  254.       /* If no filename was provided, do an anonymous mmap */
  255. !     if(!file_mask || (file_mask && !strlen(file_mask))) {
  256. ! #if !defined(MAP_ANON)
  257. !         apc_error("Anonymous mmap does not apear to be available on this system (MAP_ANON/MAP_ANONYMOUS).  Please see the apc.mmap_file_mask INI option." TSRMLS_CC);
  258. ! #else
  259. !         fd = -1;
  260. !         flags = MAP_SHARED | MAP_ANON;
  261. !         remap = 0;
  262. ! #endif
  263. !     } else if(!strcmp(file_mask,"/dev/zero")) {
  264. !         fd = open("/dev/zero", O_RDWR, S_IRUSR | S_IWUSR);
  265. !         if(fd == -1) {
  266. !             apc_error("apc_mmap: open on /dev/zero failed:" TSRMLS_CC);
  267. !             goto error;
  268. !         }
  269. !         remap = 0; /* cannot remap */
  270. !     } else if(strstr(file_mask,".shm")) {
  271. !         /*
  272. !          * If the filemask contains .shm we try to do a POSIX-compliant shared memory
  273. !          * backed mmap which should avoid synchs on some platforms.  At least on
  274. !          * FreeBSD this implies MAP_NOSYNC and on Linux it is equivalent of mmap'ing
  275. !          * a file in a mounted shmfs.  For this to work on Linux you need to make sure
  276. !          * you actually have shmfs mounted.  Also on Linux, make sure the file_mask you
  277. !          * pass in has a leading / and no other /'s.  eg.  /apc.shm.XXXXXX
  278. !          * On FreeBSD these are mapped onto the regular filesystem so you can put whatever
  279. !          * path you want here.
  280. !          */
  281. !         if(!mktemp(file_mask)) {
  282. !             apc_error("apc_mmap: mktemp on %s failed:" TSRMLS_CC, file_mask);
  283. !             goto error;
  284. !         }
  285. !         fd = shm_open(file_mask, O_CREAT|O_RDWR, S_IRUSR|S_IWUSR);
  286. !         if(fd == -1) {
  287. !             apc_error("apc_mmap: shm_open on %s failed:" TSRMLS_CC, file_mask);
  288. !             goto error;
  289. !         }
  290. !         if (ftruncate(fd, size) < 0) {
  291. !             close(fd);
  292. !             shm_unlink(file_mask);
  293. !             apc_error("apc_mmap: ftruncate failed:" TSRMLS_CC);
  294. !             goto error;
  295. !         }
  296. !         shm_unlink(file_mask);
  297.       } else {
  298. !         /*
  299. !          * Otherwise we do a normal filesystem mmap
  300. !          */
  301. !         fd = mkstemp(file_mask);
  302. !         if(fd == -1) {
  303. !             apc_error("apc_mmap: mkstemp on %s failed:" TSRMLS_CC, file_mask);
  304. !             goto error;
  305. !         }
  306. !         if (ftruncate(fd, size) < 0) {
  307. !             close(fd);
  308. !             unlink(file_mask);
  309. !             apc_error("apc_mmap: ftruncate failed:" TSRMLS_CC);
  310. !             goto error;
  311. !         }
  312. !         unlink(file_mask);
  313. !     }
  314. !
  315. !     segment.shmaddr = (void *)mmap(NULL, size, PROT_READ | PROT_WRITE, flags, fd, 0);
  316.       segment.size = size;
  317.  
  318.   #ifdef APC_MEMPROTECT
  319. !     if(remap) {
  320. !         segment.roaddr = (void *)mmap(NULL, size, PROT_READ, flags, fd, 0);
  321. !     } else {
  322. !         segment.roaddr = NULL;
  323. !     }
  324.   #endif
  325.  
  326. !     if((long)segment.shmaddr == -1) {
  327.           apc_error("apc_mmap: mmap failed:" TSRMLS_CC);
  328. !     }
  329. !
  330. !     if(fd != -1) close(fd);
  331. !    
  332.       return segment;
  333.  
  334.   error:
  335. !
  336.       segment.shmaddr = (void*)-1;
  337.       segment.size = 0;
  338.   #ifdef APC_MEMPROTECT
  339. --- 54,105 ----
  340.   # define MAP_ANON MAP_ANONYMOUS
  341.   #endif
  342.  
  343. + /* SHARED MMAP HACK CODE START */
  344. +
  345.   apc_segment_t apc_mmap(char *file_mask, size_t size TSRMLS_DC)
  346.   {
  347. !   if (size != APCG(shm_size)) {
  348. !       apc_error("apc_mmap: allocation request with incorrect shm_size!" TSRMLS_CC);
  349. !       goto error;
  350. !   }
  351.  
  352. !     apc_segment_t segment;
  353. !  
  354. !     int flags = MAP_SHARED | MAP_NOSYNC | MAP_FIXED;
  355.       int remap = 1;
  356. !   int fd = -1;
  357. !  
  358.       /* If no filename was provided, do an anonymous mmap */
  359. !     if(!file_mask || (file_mask && !strlen(file_mask)) || !strcmp(file_mask,"/dev/zero")) {
  360. !       apc_error("apc_mmap: no file mask (or /dev/zero) was used. this a mmap hacked APC version which require this." TSRMLS_CC);
  361. !       goto error;
  362.       } else {
  363. !         fd = open(file_mask, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
  364. !       if (fd == -1) {
  365. !           apc_error("open: open on %s failed: %s (%d)" TSRMLS_CC, file_mask, strerror(errno), errno);
  366. !           goto error;
  367. !       }
  368. !       if (ftruncate(fd, size) < 0) {
  369. !           apc_error("apc_mmap: ftruncate failed:" TSRMLS_CC);
  370. !           goto error;
  371. !       }
  372. !   }
  373. !  
  374. !     segment.shmaddr = (void *)mmap(APCG(reserved_mmap_addr), size, PROT_READ | PROT_WRITE, flags, fd, 0);
  375.       segment.size = size;
  376. +   close(fd);
  377.  
  378.   #ifdef APC_MEMPROTECT
  379. !     #error APC_MEMPROTECT IS NOT SUPPORTED IN THIS MMAP HACKED APC VERSION
  380.   #endif
  381.  
  382. !     if((long)segment.shmaddr == -1)
  383.           apc_error("apc_mmap: mmap failed:" TSRMLS_CC);
  384. !  
  385.       return segment;
  386.  
  387.   error:
  388. !   close(fd);
  389.       segment.shmaddr = (void*)-1;
  390.       segment.size = 0;
  391.   #ifdef APC_MEMPROTECT
  392. ***************
  393. *** 165,172 ****
  394. --- 123,133 ----
  395.  
  396.   }
  397.  
  398. + /* SHARED MMAP HACK CODE END */
  399. +
  400.   #endif
  401.  
  402. +
  403.   /*
  404.    * Local variables:
  405.    * tab-width: 4
  406. diff -crB apc_vanilla/apc_sma.c apc/apc_sma.c
  407. *** apc_vanilla/apc_sma.c   2011-01-11 19:06:38.000000000 +0000
  408. --- apc/apc_sma.c   2011-01-16 06:32:02.000000000 +0000
  409. ***************
  410. *** 55,60 ****
  411. --- 55,65 ----
  412.       apc_lck_t sma_lock;     /* segment lock, MUST BE ALIGNED for futex locks */
  413.       size_t segsize;         /* size of entire segment */
  414.       size_t avail;           /* bytes available (not necessarily contiguous) */
  415. +   /* SHARED MMAP HACK CODE START */
  416. +   time_t session_id;      /* comparing this to know if this is an old or new session */
  417. +   void* core_cache_addr;  /* static shared core cache address */
  418. +     void* user_cache_addr;  /* static shared user cache address */
  419. +   /* SHARED MMAP HACK CODE END */
  420.   #if ALLOC_DISTRIBUTION
  421.       size_t adist[30];
  422.   #endif
  423. ***************
  424. *** 77,82 ****
  425. --- 82,105 ----
  426.   static volatile size_t block_id = 0;
  427.   #endif
  428.  
  429. + /* SHARED MMAP HACK CODE START */
  430. + void* apc_sma_hack_core_cache_addr_get() {
  431. +   return SMA_HDR(0)->core_cache_addr;
  432. + }
  433. +
  434. + void apc_sma_hack_core_cache_addr_set(void* addr) {
  435. +   SMA_HDR(0)->core_cache_addr = addr;
  436. + }
  437. +
  438. + void* apc_sma_hack_user_cache_addr_get() {
  439. +   return SMA_HDR(0)->user_cache_addr;
  440. + }
  441. +
  442. + void apc_sma_hack_user_cache_addr_set(void* addr) {
  443. +   SMA_HDR(0)->user_cache_addr = addr;
  444. + }
  445. + /* SHARED MMAP HACK CODE END */
  446. +
  447.   #define APC_SMA_CANARIES 1
  448.  
  449.   typedef struct block_t block_t;
  450. ***************
  451. *** 335,421 ****
  452.  
  453.   void apc_sma_init(int numseg, size_t segsize, char *mmap_file_mask TSRMLS_DC)
  454.   {
  455. !     uint i;
  456. !
  457.       if (sma_initialized) {
  458.           return;
  459.       }
  460.       sma_initialized = 1;
  461. !
  462. ! #if APC_MMAP
  463. !     /*
  464. !      * I don't think multiple anonymous mmaps makes any sense
  465. !      * so force sma_numseg to 1 in this case
  466. !      */
  467. !     if(!mmap_file_mask ||
  468. !        (mmap_file_mask && !strlen(mmap_file_mask)) ||
  469. !        (mmap_file_mask && !strcmp(mmap_file_mask, "/dev/zero"))) {
  470. !         sma_numseg = 1;
  471. !     } else {
  472. !         sma_numseg = numseg > 0 ? numseg : DEFAULT_NUMSEG;
  473. !     }
  474. ! #else
  475. !     sma_numseg = numseg > 0 ? numseg : DEFAULT_NUMSEG;
  476. ! #endif
  477. !
  478. !     sma_segsize = segsize > 0 ? segsize : DEFAULT_SEGSIZE;
  479. !
  480.       sma_segments = (apc_segment_t*) apc_emalloc((sma_numseg * sizeof(apc_segment_t)) TSRMLS_CC);
  481. !
  482. !     for (i = 0; i < sma_numseg; i++) {
  483. !         sma_header_t*   header;
  484. !         block_t     *first, *empty, *last;
  485. !         void*       shmaddr;
  486. !
  487. ! #if APC_MMAP
  488. !         sma_segments[i] = apc_mmap(mmap_file_mask, sma_segsize TSRMLS_CC);
  489. !         if(sma_numseg != 1) memcpy(&mmap_file_mask[strlen(mmap_file_mask)-6], "XXXXXX", 6);
  490. ! #else
  491. !         sma_segments[i] = apc_shm_attach(apc_shm_create(i, sma_segsize TSRMLS_CC), sma_segsize TSRMLS_CC);
  492. ! #endif
  493. !        
  494. !         sma_segments[i].size = sma_segsize;
  495. !
  496. !         shmaddr = sma_segments[i].shmaddr;
  497. !
  498. !         header = (sma_header_t*) shmaddr;
  499. !         apc_lck_create(NULL, 0, 1, header->sma_lock);
  500. !         header->segsize = sma_segsize;
  501. !         header->avail = sma_segsize - ALIGNWORD(sizeof(sma_header_t)) - ALIGNWORD(sizeof(block_t)) - ALIGNWORD(sizeof(block_t));
  502.   #if ALLOC_DISTRIBUTION
  503. !         {
  504. !            int j;
  505. !            for(j=0; j<30; j++) header->adist[j] = 0;
  506. !         }
  507. ! #endif
  508. !         first = BLOCKAT(ALIGNWORD(sizeof(sma_header_t)));
  509. !         first->size = 0;
  510. !         first->fnext = ALIGNWORD(sizeof(sma_header_t)) + ALIGNWORD(sizeof(block_t));
  511. !         first->fprev = 0;
  512. !         first->prev_size = 0;
  513. !         SET_CANARY(first);
  514.   #ifdef __APC_SMA_DEBUG__
  515. !         block->id = -1;
  516.   #endif
  517. !         empty = BLOCKAT(first->fnext);
  518. !         empty->size = header->avail - ALIGNWORD(sizeof(block_t));
  519. !         empty->fnext = OFFSET(empty) + empty->size;
  520. !         empty->fprev = ALIGNWORD(sizeof(sma_header_t));
  521. !         empty->prev_size = 0;
  522. !         SET_CANARY(empty);
  523.   #ifdef __APC_SMA_DEBUG__
  524. !         empty->id = -1;
  525.   #endif
  526. !         last = BLOCKAT(empty->fnext);
  527. !         last->size = 0;
  528. !         last->fnext = 0;
  529. !         last->fprev =  OFFSET(empty);
  530. !         last->prev_size = empty->size;
  531. !         SET_CANARY(last);
  532.   #ifdef __APC_SMA_DEBUG__
  533. !         last->id = -1;
  534.   #endif
  535. !     }
  536.   }
  537.   /* }}} */
  538.  
  539. --- 358,438 ----
  540.  
  541.   void apc_sma_init(int numseg, size_t segsize, char *mmap_file_mask TSRMLS_DC)
  542.   {
  543. !   /* SHARED MMAP HACK CODE START */
  544. !  
  545. !   if (sma_numseg > 0) {
  546. !       apc_error("apc_mmap: this hacked apc version requires number of segments to be exactly one!" TSRMLS_CC);
  547. !       return;
  548. !   }
  549. !   sma_numseg = 1;
  550. !   sma_segsize = segsize;
  551. !  
  552. !   // Making this function a critical section since it is called
  553. !   // multiple times for each new request...
  554. !   static pthread_mutex_t sma_init_mutex = PTHREAD_MUTEX_INITIALIZER;
  555. !   pthread_mutex_lock(&sma_init_mutex);
  556. !  
  557.       if (sma_initialized) {
  558. +       pthread_mutex_unlock(&sma_init_mutex);
  559.           return;
  560.       }
  561.       sma_initialized = 1;
  562. !  
  563.       sma_segments = (apc_segment_t*) apc_emalloc((sma_numseg * sizeof(apc_segment_t)) TSRMLS_CC);
  564. !   sma_segments[0] = apc_mmap(mmap_file_mask, sma_segsize TSRMLS_CC);
  565. !   char* shmaddr = SMA_ADDR(0);
  566. !   sma_header_t* header = (sma_header_t*) shmaddr;
  567. !   // Compare session id with current session id to know
  568. !   // if the segment needs to be initialized or not.
  569. !   if (header->session_id == APCG(init_time)) {
  570. !       pthread_mutex_unlock(&sma_init_mutex);
  571. !       return;
  572. !   }
  573. !   apc_lck_create(NULL, 0, 1, header->sma_lock);
  574. !   header->session_id = APCG(init_time);
  575. !   header->core_cache_addr = NULL;
  576. !   header->user_cache_addr = NULL;
  577. !   header->segsize = sma_segsize;
  578. !   header->avail = sma_segsize - ALIGNWORD(sizeof(sma_header_t)) - ALIGNWORD(sizeof(block_t)) - ALIGNWORD(sizeof(block_t));
  579.   #if ALLOC_DISTRIBUTION
  580. !   {
  581. !      int j;
  582. !      for(j=0; j<30; j++) header->adist[j] = 0;
  583. !   }
  584. ! #endif
  585. !     block_t     *first, *empty, *last;
  586. !      
  587. !   first = BLOCKAT(ALIGNWORD(sizeof(sma_header_t)));
  588. !   first->size = 0;
  589. !   first->fnext = ALIGNWORD(sizeof(sma_header_t)) + ALIGNWORD(sizeof(block_t));
  590. !   first->fprev = 0;
  591. !   first->prev_size = 0;
  592. !   SET_CANARY(first);
  593.   #ifdef __APC_SMA_DEBUG__
  594. !   block->id = -1;
  595.   #endif
  596. !   empty = BLOCKAT(first->fnext);
  597. !   empty->size = header->avail - ALIGNWORD(sizeof(block_t));
  598. !   empty->fnext = OFFSET(empty) + empty->size;
  599. !   empty->fprev = ALIGNWORD(sizeof(sma_header_t));
  600. !   empty->prev_size = 0;
  601. !   SET_CANARY(empty);
  602.   #ifdef __APC_SMA_DEBUG__
  603. !   empty->id = -1;
  604.   #endif
  605. !   last = BLOCKAT(empty->fnext);
  606. !   last->size = 0;
  607. !   last->fnext = 0;
  608. !   last->fprev =  OFFSET(empty);
  609. !   last->prev_size = empty->size;
  610. !   SET_CANARY(last);
  611.   #ifdef __APC_SMA_DEBUG__
  612. !   last->id = -1;
  613.   #endif
  614. !
  615. !   pthread_mutex_unlock(&sma_init_mutex);
  616. !      
  617. !   /* SHARED MMAP HACK CODE END */
  618.   }
  619.   /* }}} */
  620.  
  621. diff -crB apc_vanilla/apc_sma.h apc/apc_sma.h
  622. *** apc_vanilla/apc_sma.h   2011-01-11 19:06:38.000000000 +0000
  623. --- apc/apc_sma.h   2011-01-16 02:10:07.000000000 +0000
  624. ***************
  625. *** 46,51 ****
  626. --- 46,58 ----
  627.   #endif
  628.   };
  629.  
  630. + /* SHARED MMAP HACK CODE START */
  631. + extern void* apc_sma_hack_core_cache_addr_get();
  632. + extern void apc_sma_hack_core_cache_addr_set(void* addr);
  633. + extern void* apc_sma_hack_user_cache_addr_get();
  634. + extern void apc_sma_hack_user_cache_addr_set(void* addr);
  635. + /* SHARED MMAP HACK CODE END */
  636. +
  637.   extern void apc_sma_init(int numseg, size_t segsize, char *mmap_file_mask TSRMLS_DC);
  638.   extern void apc_sma_cleanup(TSRMLS_D);
  639.   extern void* apc_sma_malloc(size_t size TSRMLS_DC);
  640. diff -crB apc_vanilla/php_apc.c apc/php_apc.c
  641. *** apc_vanilla/php_apc.c   2011-01-11 19:06:38.000000000 +0000
  642. --- apc/php_apc.c   2011-01-16 22:17:26.000000000 +0000
  643. ***************
  644. *** 289,294 ****
  645. --- 289,295 ----
  646.       php_info_print_table_start();
  647.       php_info_print_table_header(2, "APC Support", APCG(enabled) ? "enabled" : "disabled");
  648.       php_info_print_table_row(2, "Version", PHP_APC_VERSION);
  649. +     php_info_print_table_row(2, "Hacks", "Hacked for shared MMAP support");
  650.   #ifdef __DEBUG_APC__
  651.       php_info_print_table_row(2, "APC Debugging", "Enabled");
  652.   #else
  653. ***************
  654. *** 334,365 ****
  655.       ZEND_INIT_MODULE_GLOBALS(apc, php_apc_init_globals, php_apc_shutdown_globals);
  656.  
  657.       REGISTER_INI_ENTRIES();
  658.  
  659.       /* Disable APC in cli mode unless overridden by apc.enable_cli */
  660.       if(!APCG(enable_cli) && !strcmp(sapi_module.name, "cli")) {
  661.           APCG(enabled) = 0;
  662.       }
  663.  
  664. -     if (APCG(enabled)) {
  665. -         if(APCG(initialized)) {
  666. -             apc_process_init(module_number TSRMLS_CC);
  667. -         } else {
  668. -             apc_module_init(module_number TSRMLS_CC);
  669. -             apc_zend_init(TSRMLS_C);
  670. -             apc_process_init(module_number TSRMLS_CC);
  671. - #ifdef MULTIPART_EVENT_FORMDATA
  672. -             /* File upload progress tracking */
  673. -             if(APCG(rfc1867)) {
  674. -                 php_rfc1867_callback = apc_rfc1867_progress;
  675. -             }
  676. - #endif
  677. -             apc_iterator_init(module_number TSRMLS_CC);
  678. -         }
  679. -
  680. -         zend_register_long_constant("APC_BIN_VERIFY_MD5", sizeof("APC_BIN_VERIFY_MD5"), APC_BIN_VERIFY_MD5, (CONST_CS | CONST_PERSISTENT), module_number TSRMLS_CC);
  681. -         zend_register_long_constant("APC_BIN_VERIFY_CRC32", sizeof("APC_BIN_VERIFY_CRC32"), APC_BIN_VERIFY_CRC32, (CONST_CS | CONST_PERSISTENT), module_number TSRMLS_CC);
  682. -     }
  683. -
  684.       return SUCCESS;
  685.   }
  686.   /* }}} */
  687. --- 335,363 ----
  688.       ZEND_INIT_MODULE_GLOBALS(apc, php_apc_init_globals, php_apc_shutdown_globals);
  689.  
  690.       REGISTER_INI_ENTRIES();
  691. +  
  692. +   /* SHARED MMAP HACK CODE START */
  693. +   APCG(init_time) = time(0);
  694. +   // Reserved page aligned memory for mmap so that addresses will be the same for all cloned childs.
  695. +   // We must page align since this address will be fed into mmap with MAP_FIXED.
  696. +   long mmap_shm_size = APCG(shm_size);
  697. +   long page_size = getpagesize();
  698. +   if ((mmap_shm_size % page_size) > 0)
  699. +       mmap_shm_size += (page_size - (mmap_shm_size % page_size));
  700. +   void* reserved_mmap_addr;
  701. +   int ret = posix_memalign(&reserved_mmap_addr, page_size, mmap_shm_size);
  702. +   if (ret != 0) {
  703. +       apc_error("posix_memalign failed: %d" TSRMLS_CC, ret);
  704. +       return FAILURE;
  705. +   }
  706. +   APCG(reserved_mmap_addr) = reserved_mmap_addr;
  707. +   /* SHARED MMAP HACK CODE END */
  708.  
  709.       /* Disable APC in cli mode unless overridden by apc.enable_cli */
  710.       if(!APCG(enable_cli) && !strcmp(sapi_module.name, "cli")) {
  711.           APCG(enabled) = 0;
  712.       }
  713.  
  714.       return SUCCESS;
  715.   }
  716.   /* }}} */
  717. ***************
  718. *** 389,401 ****
  719.   /* {{{ PHP_RINIT_FUNCTION(apc) */
  720.   static PHP_RINIT_FUNCTION(apc)
  721.   {
  722. !     if(APCG(enabled)) {
  723.           apc_request_init(TSRMLS_C);
  724.  
  725.   #if HAVE_SIGACTION
  726.           apc_set_signals(TSRMLS_C);
  727.   #endif
  728.       }
  729.       return SUCCESS;
  730.   }
  731.   /* }}} */
  732. --- 387,418 ----
  733.   /* {{{ PHP_RINIT_FUNCTION(apc) */
  734.   static PHP_RINIT_FUNCTION(apc)
  735.   {
  736. !   /* SHARED MMAP HACK CODE START */
  737. !     if (APCG(enabled)) {
  738. !         if(!APCG(initialized)) {
  739. !             apc_module_init(module_number TSRMLS_CC);
  740. !             apc_zend_init(TSRMLS_C);
  741. !             apc_process_init(module_number TSRMLS_CC);
  742. ! #ifdef MULTIPART_EVENT_FORMDATA
  743. !             /* File upload progress tracking */
  744. !             if(APCG(rfc1867)) {
  745. !                 php_rfc1867_callback = apc_rfc1867_progress;
  746. !             }
  747. ! #endif
  748. !             apc_iterator_init(module_number TSRMLS_CC);
  749. !           zend_register_long_constant("APC_BIN_VERIFY_MD5", sizeof("APC_BIN_VERIFY_MD5"), APC_BIN_VERIFY_MD5, (CONST_CS | CONST_PERSISTENT), module_number TSRMLS_CC);
  750. !           zend_register_long_constant("APC_BIN_VERIFY_CRC32", sizeof("APC_BIN_VERIFY_CRC32"), APC_BIN_VERIFY_CRC32, (CONST_CS | CONST_PERSISTENT), module_number TSRMLS_CC);
  751. !         }
  752. !      
  753.           apc_request_init(TSRMLS_C);
  754.  
  755.   #if HAVE_SIGACTION
  756.           apc_set_signals(TSRMLS_C);
  757.   #endif
  758.       }
  759. +   /* SHARED MMAP HACK CODE END */
  760. +  
  761. +  
  762.       return SUCCESS;
  763.   }
  764.   /* }}} */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement