pool_caching.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <pj/pool.h>
  20. #include <pj/assert.h>
  21. #include <pj/log.h>
  22. #include <pj/string.h>
  23. #include <pj/assert.h>
  24. #include <pj/lock.h>
  25. #include <pj/os.h>
  26. #include <pj/pool_buf.h>
  27. #if !PJ_HAS_POOL_ALT_API
  28. static pj_pool_t* cpool_create_pool(pj_pool_factory *pf,
  29. const char *name,
  30. pj_size_t initial_size,
  31. pj_size_t increment_sz,
  32. pj_pool_callback *callback);
  33. static void cpool_release_pool(pj_pool_factory *pf, pj_pool_t *pool);
  34. static void cpool_dump_status(pj_pool_factory *factory, pj_bool_t detail );
  35. static pj_bool_t cpool_on_block_alloc(pj_pool_factory *f, pj_size_t sz);
  36. static void cpool_on_block_free(pj_pool_factory *f, pj_size_t sz);
  37. static pj_size_t pool_sizes[PJ_CACHING_POOL_ARRAY_SIZE] =
  38. {
  39. 256, 512, 1024, 2048, 4096, 8192, 12288, 16384,
  40. 20480, 24576, 28672, 32768, 40960, 49152, 57344, 65536
  41. };
  42. /* Index where the search for size should begin.
  43. * Start with pool_sizes[5], which is 8192.
  44. */
  45. #define START_SIZE 5
  46. PJ_DEF(void) pj_caching_pool_init( pj_caching_pool *cp,
  47. const pj_pool_factory_policy *policy,
  48. pj_size_t max_capacity)
  49. {
  50. int i;
  51. pj_pool_t *pool;
  52. pj_status_t status;
  53. PJ_CHECK_STACK();
  54. pj_bzero(cp, sizeof(*cp));
  55. cp->max_capacity = max_capacity;
  56. pj_list_init(&cp->used_list);
  57. for (i=0; i<PJ_CACHING_POOL_ARRAY_SIZE; ++i)
  58. pj_list_init(&cp->free_list[i]);
  59. if (policy == NULL) {
  60. policy = &pj_pool_factory_default_policy;
  61. }
  62. pj_memcpy(&cp->factory.policy, policy, sizeof(pj_pool_factory_policy));
  63. cp->factory.create_pool = &cpool_create_pool;
  64. cp->factory.release_pool = &cpool_release_pool;
  65. cp->factory.dump_status = &cpool_dump_status;
  66. /* Deprecated, these callbacks are only used for updating cp.used_size &
  67. * cp.peak_used_size which are no longer used.
  68. */
  69. //cp->factory.on_block_alloc = &cpool_on_block_alloc;
  70. //cp->factory.on_block_free = &cpool_on_block_free;
  71. pool = pj_pool_create_on_buf("cachingpool", cp->pool_buf, sizeof(cp->pool_buf));
  72. status = pj_lock_create_simple_mutex(pool, "cachingpool", &cp->lock);
  73. /* This mostly serves to silent coverity warning about unchecked
  74. * return value. There's not much we can do if it fails. */
  75. PJ_ASSERT_ON_FAIL(status==PJ_SUCCESS, return);
  76. }
  77. PJ_DEF(void) pj_caching_pool_destroy( pj_caching_pool *cp )
  78. {
  79. int i;
  80. pj_pool_t *pool;
  81. PJ_CHECK_STACK();
  82. /* Delete all pool in free list */
  83. for (i=0; i < PJ_CACHING_POOL_ARRAY_SIZE; ++i) {
  84. pj_pool_t *next;
  85. pool = (pj_pool_t*) cp->free_list[i].next;
  86. for (; pool != (void*)&cp->free_list[i]; pool = next) {
  87. next = pool->next;
  88. pj_list_erase(pool);
  89. pj_pool_destroy_int(pool);
  90. }
  91. }
  92. /* Delete all pools in used list */
  93. pool = (pj_pool_t*) cp->used_list.next;
  94. while (pool != (pj_pool_t*) &cp->used_list) {
  95. pj_pool_t *next = pool->next;
  96. pj_list_erase(pool);
  97. PJ_LOG(4,(pool->obj_name,
  98. "Pool is not released by application, releasing now"));
  99. pj_pool_destroy_int(pool);
  100. pool = next;
  101. }
  102. if (cp->lock) {
  103. pj_lock_destroy(cp->lock);
  104. pj_lock_create_null_mutex(NULL, "cachingpool", &cp->lock);
  105. }
  106. }
  107. static pj_pool_t* cpool_create_pool(pj_pool_factory *pf,
  108. const char *name,
  109. pj_size_t initial_size,
  110. pj_size_t increment_sz,
  111. pj_pool_callback *callback)
  112. {
  113. pj_caching_pool *cp = (pj_caching_pool*)pf;
  114. pj_pool_t *pool;
  115. int idx;
  116. PJ_CHECK_STACK();
  117. pj_lock_acquire(cp->lock);
  118. /* Use pool factory's policy when callback is NULL */
  119. if (callback == NULL) {
  120. callback = pf->policy.callback;
  121. }
  122. /* Search the suitable size for the pool.
  123. * We'll just do linear search to the size array, as the array size itself
  124. * is only a few elements. Binary search I suspect will be less efficient
  125. * for this purpose.
  126. */
  127. if (initial_size <= pool_sizes[START_SIZE]) {
  128. for (idx=START_SIZE-1;
  129. idx >= 0 && pool_sizes[idx] >= initial_size;
  130. --idx)
  131. ;
  132. ++idx;
  133. } else {
  134. for (idx=START_SIZE+1;
  135. idx < PJ_CACHING_POOL_ARRAY_SIZE &&
  136. pool_sizes[idx] < initial_size;
  137. ++idx)
  138. ;
  139. }
  140. /* Check whether there's a pool in the list. */
  141. if (idx==PJ_CACHING_POOL_ARRAY_SIZE || pj_list_empty(&cp->free_list[idx])) {
  142. /* No pool is available. */
  143. /* Set minimum size. */
  144. if (idx < PJ_CACHING_POOL_ARRAY_SIZE)
  145. initial_size = pool_sizes[idx];
  146. /* Create new pool */
  147. pool = pj_pool_create_int(&cp->factory, name, initial_size,
  148. increment_sz, callback);
  149. if (!pool) {
  150. pj_lock_release(cp->lock);
  151. return NULL;
  152. }
  153. } else {
  154. /* Get one pool from the list. */
  155. pool = (pj_pool_t*) cp->free_list[idx].next;
  156. pj_list_erase(pool);
  157. /* Initialize the pool. */
  158. pj_pool_init_int(pool, name, increment_sz, callback);
  159. /* Update pool manager's free capacity. */
  160. if (cp->capacity > pj_pool_get_capacity(pool)) {
  161. cp->capacity -= pj_pool_get_capacity(pool);
  162. } else {
  163. cp->capacity = 0;
  164. }
  165. PJ_LOG(6, (pool->obj_name, "pool reused, size=%lu",
  166. (unsigned long)pool->capacity));
  167. }
  168. /* Put in used list. */
  169. pj_list_insert_before( &cp->used_list, pool );
  170. /* Mark factory data */
  171. pool->factory_data = (void*) (pj_ssize_t) idx;
  172. /* Increment used count. */
  173. ++cp->used_count;
  174. pj_lock_release(cp->lock);
  175. return pool;
  176. }
  177. static void cpool_release_pool( pj_pool_factory *pf, pj_pool_t *pool)
  178. {
  179. pj_caching_pool *cp = (pj_caching_pool*)pf;
  180. pj_size_t pool_capacity;
  181. unsigned i;
  182. PJ_CHECK_STACK();
  183. PJ_ASSERT_ON_FAIL(pf && pool, return);
  184. pj_lock_acquire(cp->lock);
  185. #if PJ_SAFE_POOL
  186. /* Make sure pool is still in our used list */
  187. if (pj_list_find_node(&cp->used_list, pool) != pool) {
  188. pj_lock_release(cp->lock);
  189. pj_assert(!"Attempt to destroy pool that has been destroyed before");
  190. return;
  191. }
  192. #endif
  193. /* Erase from the used list. */
  194. pj_list_erase(pool);
  195. /* Decrement used count. */
  196. --cp->used_count;
  197. pool_capacity = pj_pool_get_capacity(pool);
  198. /* Destroy the pool if the size is greater than our size or if the total
  199. * capacity in our recycle list (plus the size of the pool) exceeds
  200. * maximum capacity.
  201. . */
  202. if (pool_capacity > pool_sizes[PJ_CACHING_POOL_ARRAY_SIZE-1] ||
  203. cp->capacity + pool_capacity > cp->max_capacity)
  204. {
  205. pj_pool_destroy_int(pool);
  206. pj_lock_release(cp->lock);
  207. return;
  208. }
  209. /* Reset pool. */
  210. PJ_LOG(6, (pool->obj_name, "recycle(): cap=%lu, used=%lu(%lu%%)",
  211. (unsigned long)pool_capacity,
  212. (unsigned long)pj_pool_get_used_size(pool),
  213. (unsigned long)(pj_pool_get_used_size(pool)*100/
  214. pool_capacity)));
  215. pj_pool_reset(pool);
  216. pool_capacity = pj_pool_get_capacity(pool);
  217. /*
  218. * Otherwise put the pool in our recycle list.
  219. */
  220. i = (unsigned) (unsigned long) (pj_ssize_t) pool->factory_data;
  221. pj_assert(i<PJ_CACHING_POOL_ARRAY_SIZE);
  222. if (i >= PJ_CACHING_POOL_ARRAY_SIZE ) {
  223. /* Something has gone wrong with the pool. */
  224. pj_pool_destroy_int(pool);
  225. pj_lock_release(cp->lock);
  226. return;
  227. }
  228. pj_list_insert_after(&cp->free_list[i], pool);
  229. cp->capacity += pool_capacity;
  230. pj_lock_release(cp->lock);
  231. }
  232. static void cpool_dump_status(pj_pool_factory *factory, pj_bool_t detail )
  233. {
  234. #if PJ_LOG_MAX_LEVEL >= 3
  235. pj_caching_pool *cp = (pj_caching_pool*)factory;
  236. pj_lock_acquire(cp->lock);
  237. PJ_LOG(3,("cachpool", " Dumping caching pool:"));
  238. PJ_LOG(3,("cachpool", " Capacity=%lu, max_capacity=%lu, used_cnt=%lu",
  239. (unsigned long)cp->capacity, (unsigned long)cp->max_capacity,
  240. (unsigned long)cp->used_count));
  241. if (detail) {
  242. pj_pool_t *pool = (pj_pool_t*) cp->used_list.next;
  243. pj_size_t total_used = 0, total_capacity = 0;
  244. PJ_LOG(3,("cachpool", " Dumping all active pools:"));
  245. while (pool != (void*)&cp->used_list) {
  246. pj_size_t pool_capacity = pj_pool_get_capacity(pool);
  247. pj_pool_block *block = pool->block_list.next;
  248. unsigned nblocks = 0;
  249. while (block != &pool->block_list) {
  250. #if 0
  251. PJ_LOG(6, ("cachpool", " %16s block %u, size %ld",
  252. pj_pool_getobjname(pool), nblocks,
  253. (long)(block->end - block->buf + 1)));
  254. #endif
  255. nblocks++;
  256. block = block->next;
  257. }
  258. PJ_LOG(3,("cachpool", " %16s: %8lu of %8lu (%lu%%) used, "
  259. "nblocks: %d",
  260. pj_pool_getobjname(pool),
  261. (unsigned long)pj_pool_get_used_size(pool),
  262. (unsigned long)pool_capacity,
  263. (unsigned long)(pj_pool_get_used_size(pool)*
  264. 100/pool_capacity),
  265. nblocks));
  266. #if PJ_POOL_MAX_SEARCH_BLOCK_COUNT == 0
  267. if (nblocks >= 10) {
  268. PJ_LOG(3,("cachpool", " %16s has too many blocks (%d), "
  269. "consider increasing its initial and/or "
  270. "increment size for better performance",
  271. pj_pool_getobjname(pool), nblocks));
  272. }
  273. #endif
  274. total_used += pj_pool_get_used_size(pool);
  275. total_capacity += pool_capacity;
  276. pool = pool->next;
  277. }
  278. if (total_capacity) {
  279. PJ_LOG(3,("cachpool", " Total %9lu of %9lu (%lu %%) used!",
  280. (unsigned long)total_used,
  281. (unsigned long)total_capacity,
  282. (unsigned long)(total_used * 100 /
  283. total_capacity)));
  284. }
  285. }
  286. pj_lock_release(cp->lock);
  287. #else
  288. PJ_UNUSED_ARG(factory);
  289. PJ_UNUSED_ARG(detail);
  290. #endif
  291. }
  292. static pj_bool_t cpool_on_block_alloc(pj_pool_factory *f, pj_size_t sz)
  293. {
  294. pj_caching_pool *cp = (pj_caching_pool*)f;
  295. //Can't lock because mutex is not recursive
  296. //if (cp->mutex) pj_mutex_lock(cp->mutex);
  297. cp->used_size += sz;
  298. if (cp->used_size > cp->peak_used_size)
  299. cp->peak_used_size = cp->used_size;
  300. //if (cp->mutex) pj_mutex_unlock(cp->mutex);
  301. return PJ_TRUE;
  302. }
  303. static void cpool_on_block_free(pj_pool_factory *f, pj_size_t sz)
  304. {
  305. pj_caching_pool *cp = (pj_caching_pool*)f;
  306. //pj_mutex_lock(cp->mutex);
  307. cp->used_size -= sz;
  308. //pj_mutex_unlock(cp->mutex);
  309. }
  310. #endif /* PJ_HAS_POOL_ALT_API */