Berkeley DB Memory Pool LRU algorithm

Source: Internet
Author: User

Priority based LRU

src/mp/mp_fget.c, __memp_fget()in, when initializing a page buffer, set its priority:

    bhp->priority = MPOOL_LRU_REDZONE;    

In src/mp/mp_fget.c, __memp_fput() , put a page buffer and make it reference minus one. bhp->ref-- if reference is reduced to 0, its priority is adjusted. The following bhp->priority = c_mp->lru_priority; and ++c_mp->lru_priority guaranteed LRU; And according to the incoming Priorit The y parameter is adjusted accordingly.
Prioriry Max is Uint32_max, so it may be necessary to wraparound. __MEMP_RESET_LRU () lowers the priority of all page buffer.

/* Update priority values. */if (priority = = Db_priority_very_low | | Mfp->priority = = mpool_pri_very_low) bhp->priority =0;else {/* * We don ' t lock the LRU priority or the pages field, if * we get garbage (which won ' t happen on a 32-bit machine), it * Only means a buffer had the wrong priority. */bhp->priority = c_mp->lru_priority;Switch (priority) {DefaultCase Db_priority_unchanged:pfactor = mfp->priority;BreakCase db_priority_very_low:pfactor = Mpool_pri_very_low;BreakCase db_priority_low:pfactor = Mpool_pri_low;BreakCase db_priority_default:pfactor = Mpool_pri_default;Breakcase db_priority_high:pfactor = Mpool_pri_high; break; case db_priority_very_high:pfactor = Mpool_pri_very_high; break;} adjust = 0; if (pfactor! = 0) adjust = (int) c_mp->pages/pfactor; if (F_isset (BHP, Bh_dirty)) Adjust + = (int) c_mp->pages/mpool_pri_dirty; if (Adjust > 0) {if (MPOOL_LRU_ Redzone-bhp->priority >= (u_int32_t) adjust) bhp->priority + = adjust; } else if (Adjust < 0) if (Bhp->priority > (u_int32_t)-adjust) bhp->priority + = adjust;}     

...

/* * On every buffer put we update the cache lru priority and check * for wraparound. The increment doesn‘t need to be atomic: occasional * lost increments are okay; __memp_reset_lru handles race conditions. */if (++c_mp->lru_priority >= MPOOL_LRU_REDZONE &&    (t_ret = __memp_reset_lru(env, infop)) != 0 && ret == 0)    ret = t_ret;return (ret);

src/mp/mp_alloc.c, __memp_alloc(), the existing buffer memory is reused when the memory of the new buffer cannot be allocated. The algorithm is very complex, write one alone.

Berkeley DB Memory Pool LRU algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.