Memcached婧愮爜鍒嗘瀽涔媔tems.c

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Memcached婧愮爜鍒嗘瀽涔媔tems.c相关的知识,希望对你有一定的参考价值。

鏍囩锛?/p>

  1. #include "memcached.h"
  2. #include <sys/stat.h>
  3. #include <sys/socket.h>
  4. #include <sys/signal.h>
  5. #include <sys/resource.h>
  6. #include <fcntl.h>
  7. #include <netinet/in.h>
  8. #include <errno.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <time.h>
  13. #include <assert.h>
  14. #include <unistd.h>
  15. static void item_link_q(item *it);
  16. static void item_unlink_q(item *it);
  17. #define LARGEST_ID POWER_LARGEST
  18. typedef struct {
  19.     uint64_t evicted;
  20.     uint64_t evicted_nonzero;
  21.     rel_time_t evicted_time;
  22.     uint64_t reclaimed;
  23.     uint64_t outofmemory;
  24.     uint64_t tailrepairs;
  25.     uint64_t expired_unfetched;
  26.     uint64_t evicted_unfetched;
  27.     uint64_t crawler_reclaimed;
  28. } itemstats_t;
  29. static item *heads[LARGEST_ID]; //鍚勪釜slabclass鐨凩RU闃熷垪澶存寚閽堟暟缁?/span>
  30. static item *tails[LARGEST_ID]; //鍚勪釜slabclass鐨凩RU闃熷垪灏炬寚閽堟暟缁?/span>
  31. static crawler crawlers[LARGEST_ID]; //鍚勪釜slabclass鐨刬tem鐖櫕鏁扮粍
  32. static itemstats_t itemstats[LARGEST_ID]; //鍚勪釜slabclass鐨刬tem缁熻鏁扮粍
  33. static unsigned int sizes[LARGEST_ID]; //鍚勪釜slabclass鐨刢hunk澶у皬鏁扮粍
  34. static int crawler_count = 0;
  35. static volatile int do_run_lru_crawler_thread = 0;
  36. static int lru_crawler_initialized = 0;
  37. static pthread_mutex_t lru_crawler_lock = PTHREAD_MUTEX_INITIALIZER;
  38. static pthread_cond_t lru_crawler_cond = PTHREAD_COND_INITIALIZER;
  39. //閲嶇疆缁熻
  40. void item_stats_reset(void) {
  41.     mutex_lock(&cache_lock);
  42.     memset(itemstats, 0, sizeof(itemstats));
  43.     mutex_unlock(&cache_lock);
  44. }
  45. /* Get the next CAS id for a new item. */
  46. uint64_t get_cas_id(void) {
  47.     static uint64_t cas_id = 0;
  48.     return ++cas_id;
  49. }
  50. /* Enable this for reference-count debugging. */
  51. #if 0
  52. # define DEBUG_REFCNT(it,op) \
  53.                 fprintf(stderr, "item %x refcnt(%c) %d %c%c%c\n", \
  54.                         it, op, it->refcount, \
  55.                         (it->it_flags & ITEM_LINKED) ? 鈥楲鈥?span class="pln"> : 鈥?鈥?span class="pun">, \
  56.                         (it->it_flags & ITEM_SLABBED) ? 鈥楽鈥?span class="pln"> : 鈥?鈥?span class="pun">)
  57. #else
  58. # define DEBUG_REFCNT(it,op) while(0)
  59. #endif
  60. /**
  61.     绠楀嚭item鎬诲ぇ灏?/span>
  62.  */
  63. static size_t item_make_header(const uint8_t nkey, const int flags, const int nbytes,
  64.                      char *suffix, uint8_t *nsuffix) {
  65.     /* suffix is defined at 40 chars elsewhere.. */
  66.     *nsuffix = (uint8_t) snprintf(suffix, 40, " %d %d\r\n", flags, nbytes - 2);
  67.     return sizeof(item) + nkey + *nsuffix + nbytes;
  68. }
  69. /**
  70. item鍒嗛厤
  71. 鎶婅繖涓嚱鏁板紕娓呮锛屽熀鏈氨鎶妋emcached鍐呭瓨绠$悊鏈哄埗澶т綋寮勬竻妤氫簡銆?/span>
  72. */
  73. item *do_item_alloc(char *key, const size_t nkey, const int flags,
  74.                     const rel_time_t exptime, const int nbytes,
  75.                     const uint32_t cur_hv) {
  76.     uint8_t nsuffix;
  77.     item *it = NULL;
  78.     char suffix[40];
  79.     size_t ntotal = item_make_header(nkey + 1, flags, nbytes, suffix, &nsuffix); //item鎬诲ぇ灏?/span>
  80.     if (settings.use_cas) {
  81.         ntotal += sizeof(uint64_t); //濡傛灉鏈夌敤鍒癱as 閭d箞item澶у皬杩樿鍔犱笂unit64_t鐨剆ize
  82.     }
  83.     unsigned int id = slabs_clsid(ntotal); //鏍规嵁item澶у皬锛屾壘鍒伴€傚悎鐨剆labclass
  84.     if (id == 0)
  85.         return 0;
  86.     mutex_lock(&cache_lock); //cache閿?/span>
  87.     /* do a quick check if we have any expired items in the tail.. */
  88.     /* 鍑嗗鍒嗛厤鏂扮殑item浜嗭紝闅忎究蹇€熺瀯涓€涓媗ru閾捐〃鏈熬鏈夋病鏈夎繃鏈焛tem锛屾湁鐨勮瘽灏辩敤杩囨湡鐨勭┖闂?*/
  89.     int tries = 5;
  90.     int tried_alloc = 0;
  91.     item *search;
  92.     void *hold_lock = NULL;
  93.     rel_time_t oldest_live = settings.oldest_live;
  94.     search = tails[id]; //杩欎釜tails鏄竴涓叏灞€鍙橀噺锛宼ails[xx]鏄痠d涓簒x鐨剆labclass lru閾捐〃鐨勫熬閮?/span>
  95.     /* We walk up *only* for locked items. Never searching for expired.
  96.      * Waste of CPU for almost all deployments */
  97.     //浠嶭RU閾捐〃灏鹃儴锛堝氨鏄渶涔呮病浣跨敤杩囩殑item锛夊紑濮嬪線鍓嶆壘
  98.     for (; tries > 0 && search != NULL; tries--, search=search->prev) {
  99.         if (search->nbytes == 0 && search->nkey == 0 && search->it_flags == 1) {
  100.             /* We are a crawler, ignore it. */
  101.             /*
  102.                 杩欓噷娉ㄩ噴鎰忔€濇槸璇存垜浠幇鍦ㄦ槸浠ョ埇铏殑韬唤鏉ョ埇鍑鸿繃鏈熺殑绌洪棿锛?/span>
  103.                 鍍忕埇鍒拌繖绉嶅緢鎬殑item锛屽氨鍒浜嗭紝涓嶆槸鐖櫕瑕佸仛鐨勪簨锛屼笉瑕佸氨琛屼簡銆?/span>
  104.              */
  105.             tries++;
  106.             continue;
  107.         }
  108.         /**
  109.         浣犱細鐪嬪埌寰堝鍦版柟鏈夎繖涓猦v锛岀畝鍗曡涓嬶紝鍏跺疄瀹冩槸瀵筰tem鐨勪竴涓猦ash锛屽緱鍒癶v鍊硷紝杩欎釜hv涓昏鏈変袱涓?/span>
  110.         浣滅敤锛?/span>
  111.         1锛夌敤浜巋ash琛ㄤ繚瀛榠tem锛岄€氳繃hv璁$畻鍑哄搱甯岃〃涓殑妗跺彿
  112.         2锛夌敤浜巌tem lock琛ㄤ腑閿佷綇item锛岄€氳繃hv璁$畻鍑哄簲璇ョ敤item lock琛ㄤ腑鍝釜閿佸褰撳墠item杩涜鍔犻攣
  113.         杩欎袱鑰呴兘娑夊強鍒颁竴涓矑搴﹂棶棰橈紝涓嶅彲鑳戒繚璇佹瘡涓笉涓€鏍风殑key鐨刪v涓嶄細鐩稿悓锛屾墍鏈塰ash鏂规硶閮藉彲鑳?/span>
  114.         鍑虹幇鍐茬獊銆?/span>
  115.         鎵€浠ash琛ㄤ腑鐢ㄩ摼琛ㄧ殑鏂瑰紡澶勭悊鍐茬獊鐨刬tem锛岃€宨tem lock琛ㄤ腑浼氬涓猧tem鍏变韩涓€涓攣锛屾垨鑰呰
  116.         澶氫釜妗跺叡浜竴涓攣銆?/span>
  117.         */
  118.         uint32_t hv = hash(ITEM_key(search), search->nkey);
  119.          /**
  120.          灏濊瘯鍘婚攣浣忓綋鍓峣tem銆?/span>
  121.          */
  122.         if (hv == cur_hv || (hold_lock = item_trylock(hv)) == NULL)
  123.             continue;
  124.         /* Now see if the item is refcount locked */
  125.         if (refcount_incr(&search->refcount) != 2) {
  126.             refcount_decr(&search->refcount);
  127.             /* Old rare bug could cause a refcount leak. We haven鈥榯 seen
  128.              * it in years, but we leave this code in to prevent failures
  129.              * just in case
  130.             娌$湅鎳傝繖閲岀殑鎰忔€?....
  131.              */
  132.             if (settings.tail_repair_time &&
  133.                     search->time + settings.tail_repair_time < current_time) {
  134.                 itemstats[id].tailrepairs++;
  135.                 search->refcount = 1;
  136.                 do_item_unlink_nolock(search, hv);
  137.             }
  138.             if (hold_lock)
  139.                 item_trylock_unlock(hold_lock);
  140.             continue;
  141.         }
  142.         /* Expired or flushed */
  143.         //瓒呮椂浜?..
  144.         if ((search->exptime != 0 && search->exptime < current_time)
  145.             || (search->time <= oldest_live && oldest_live <= current_time)) {
  146.             itemstats[id].reclaimed++;
  147.             if ((search->it_flags & ITEM_FETCHED) == 0) {
  148.                 itemstats[id].expired_unfetched++;
  149.             }
  150.             it = search; //鎷夸笅绌洪棿
  151.             slabs_adjust_mem_requested(it->slabs_clsid, ITEM_ntotal(it), ntotal); //鏇存柊缁熻鏁版嵁
  152.             /**
  153.             浠€涔堟槸link锛屽湪杩欑畝鍗曡涓嬶紝灏辨槸鎶奿tem鍔犲埌鍝堝笇琛ㄥ拰LRU閾捐〃鐨勮繃绋嬨€傝瑙乮tems::do_item_link鍑芥暟杩欓噷鎶奿tem鏃х殑link鍙栨秷鎺夛紝褰撳墠鍑芥暟do_item_alloc鐨勫伐浣滃彧鏄嬁绌洪棿锛岃€屽線鍚庡彲鐭ラ亾鎷垮埌item绌洪棿鍚庝細瀵硅繖鍧梚tem杩涜“link”宸ヤ綔锛岃€岃繖閲岃繖鍧梚tem绌洪棿鏄棫鐨刬tem瓒呮椂鐒跺悗鎷挎潵鐢ㄧ殑锛屾墍浠ュ厛鎶婂畠unlink鎺?/span>
  154.             */
  155.             do_item_unlink_nolock(it, hv);
  156.             /* Initialize the item block: */
  157.             it->slabs_clsid = 0;
  158.         } else if ((it = slabs_alloc(ntotal, id)) == NULL) {/*濡傛灉娌℃湁鎵惧埌瓒呮椂鐨刬tem锛屽垯
  159.                 璋冪敤slabs_alloc鍒嗛厤绌洪棿锛岃瑙乻labs_alloc
  160.                 濡傛灉slabs_alloc鍒嗛厤绌洪棿澶辫触锛屽嵆杩斿洖NULL锛屽垯寰€涓嬭蛋锛屼笅闈㈢殑浠g爜鏄?/span>
  161.                 鎶奓RU鍒楄〃鏈€鍚庝竴涓粰娣樻卑锛屽嵆浣縤tem娌℃湁杩囨湡銆?/span>
  162.                 杩欓噷涓€鑸槸鍙敤鍐呭瓨宸茬粡婊′簡锛岄渶瑕佹寜LRU杩涜娣樻卑鐨勬椂鍊欍€?/span>
  163.                 //************mark: $1**************
  164.             */
  165.             tried_alloc = 1; //鏍囪涓€涓嬶紝琛ㄧず鏈夎繘鍏ユ鍒嗘敮锛岃〃绀烘湁灏濊瘯杩囪皟鐢╯labs_alloc鍘诲垎閰嶆柊鐨勭┖闂淬€?/span>
  166.             //璁颁笅琚窐姹癷tem鐨勪俊鎭紝鍍忔垜浠娇鐢╩emcached缁忓父浼氭煡鐪嬬殑evicted_time灏辨槸鍦ㄨ繖閲岃祴鍊煎暒锛?/span>
  167.             if (settings.evict_to_free == 0) {
  168.                 itemstats[id].outofmemory++;
  169.             } else {
  170.                 itemstats[id].evicted++;
  171.                 itemstats[id].evicted_time = current_time - search->time; //琚窐姹扮殑item璺濈涓婃浣跨敤澶氶暱鏃堕棿浜?/span>
  172.                 if (search->exptime != 0)
  173.                     itemstats[id].evicted_nonzero++;
  174.                 if ((search->it_flags & ITEM_FETCHED) == 0) {
  175.                     itemstats[id].evicted_unfetched++;
  176.                 }
  177.                 it = search;
  178.                 slabs_adjust_mem_requested(it->slabs_clsid, ITEM_ntotal(it), ntotal);//鏇存柊缁熻鏁版嵁
  179.                 do_item_unlink_nolock(it, hv); //浠庡搱甯岃〃鍜孡RU閾捐〃涓垹鎺?/span>
  180.                 /* Initialize the item block: */
  181.                 it->slabs_clsid = 0;
  182.                 /*
  183.                  濡傛灉褰撳墠slabclass鏈塱tem琚窐姹版帀浜嗭紝璇存槑鍙敤鍐呭瓨閮芥弧浜嗭紝鍐嶄篃娌℃湁
  184.                  slab鍙垎閰嶄簡锛?/span>
  185.                  鑰屽鏋?slab_automove=2 (榛樿鏄?)锛岃繖鏍蜂細瀵艰嚧angry妯″紡锛?/span>
  186.                  灏辨槸鍙鍒嗛厤澶辫触浜嗭紝灏遍┈涓婅繘琛宻lab閲嶅垎閰嶏細鎶婂埆鐨剆labclass绌洪棿鐗虹壊
  187.                  鎺変竴浜涳紝椹笂缁欑幇鍦ㄧ殑slabclass鍒嗛厤绌洪棿锛岃€屼笉浼氬悎鐞嗗湴鏍规嵁娣樻卑缁熻
  188.                  鏁版嵁鏉ュ垎鏋愯鎬庝箞閲嶅垎閰嶏紙slab_automove = 1鍒欎細锛夈€?/span>
  189.                  */
  190.                 if (settings.slab_automove == 2)
  191.                     slabs_reassign(-1, id);
  192.             }
  193.         }
  194.         refcount_decr(&search->refcount);
  195.         /* If hash values were equal, we don鈥榯 grab a second lock */
  196.         if (hold_lock)
  197.             item_trylock_unlock(hold_lock);
  198.         break;
  199.     }
  200.     /**
  201.     濡傛灉涓婇潰鐨刦or寰幆閲岄潰娌℃湁鎵惧埌绌洪棿锛屽苟涓旀病鏈夎繘鍏ヨ繃else if ((it = slabs_alloc(ntotal, id)) == NULL)杩欎釜鍒嗘敮娌℃湁
  202.     灏濊瘯璋僺labs_alloc鍒嗛厤绌洪棿锛堟湁杩欑鍙兘鎬э級锛岄偅涔堬紝涓嬮潰杩欒浠g爜灏辨槸鍐嶅皾璇曞垎閰嶃€?/span>
  203.     浣犱細瑙夊緱涓婇潰閭d釜寰幆鍐欏緱鐗圭籂缁擄紝閫昏緫涓嶆竻锛屼及璁′綘涔熺湅閱変簡銆傚叾瀹炴暣涓垎閰嶅師鍒欐槸杩欐牱瀛愶細
  204.     1锛夊厛浠嶭RU閾捐〃鎵句笅鐪嬬湅鏈夋病鏈夋伆濂借繃鏈熺殑绌洪棿锛屾湁鐨勮瘽灏辩敤杩欎釜绌洪棿銆?/span>
  205.     2锛夊鏋滄病鏈夎繃鏈熺殑绌洪棿锛屽氨鍒嗛厤鏂扮殑绌洪棿銆?/span>
  206.     3锛夊鏋滃垎閰嶆柊鐨勭┖闂村け璐ワ紝閭d箞寰€寰€鏄唴瀛橀兘鐢ㄥ厜浜嗭紝鍒欎粠LRU閾捐〃涓妸鏈€鏃х殑鍗充娇娌¤繃鏈熺殑item娣樻卑鎺夛紝绌洪棿鍒嗙粰鏂扮殑item鐢ㄣ€?/span>
  207.     闂鏄細杩欎釜浠?ldquo;LRU閾捐〃鎵惧埌鐨刬tem”鏄竴涓笉纭畾鐨勪笢瑗匡紝鏈夊彲鑳借繖涓猧tem鏁版嵁寮傚父锛屾湁鍙兘杩欎釜item鐢变簬涓庡埆鐨刬tem鍏辩敤閿佺殑妗跺彿
  208.     杩欎釜妗惰閿佷綇浜嗭紝鎵€浠ユ€讳箣鍚勭鍘熷洜杩欎釜item姝ゅ埢涓嶄竴瀹氬彲鐢紝鍥犳鐢ㄤ簡涓€涓惊鐜皾璇曟壘鍑犳锛堜笂闈㈡槸5锛夈€?/span>
  209.     鎵€浠ラ€昏緫鏄細
  210.     1锛夋垜鍏堟壘5娆RU鐪嬬湅鏈夋病鏈夊彲鐢ㄧ殑杩囨湡鐨刬tem锛屾湁灏辩敤瀹冦€傦紙for寰幆5娆★級
  211.     2锛?娆℃病鏈夋壘鍒板彲鐢ㄧ殑杩囨湡鐨刬tem锛岄偅鎴戝垎閰嶆柊鐨勩€?/span>
  212.     3锛夊垎閰嶆柊鐨勪笉鎴愬姛锛岄偅鎴戝啀鎵?娆$湅鐪嬫湁娌℃湁鍙敤鐨勮櫧鐒舵病杩囨湡鐨刬tem锛屾窐姹板畠锛屾妸绌洪棿缁欐柊鐨刬tem鐢ㄣ€傦紙for寰幆5娆★級
  213.     閭d箞杩欓噷鏈変釜闂锛屽鏋滀唬鐮佽鍐欏緱閫昏緫娓呮櫚涓€鐐癸紝鎴戝緱鍐欎袱涓猣or寰幆锛屼竴涓槸涓轰簡绗?锛夋鍓?ldquo;鎵惧彲鐢ㄧ殑杩囨湡鐨?rdquo;item锛?/span>
  214.     涓€涓槸绗?锛夋涓嶆垚鍔熷悗“鎵惧彲鐢ㄧ殑鐢ㄦ潵娣樻卑鐨?rdquo;绌洪棿銆傝€屼笖鏈夐噸澶嶇殑閫昏緫“鎵惧埌鍙敤鐨?rdquo;锛屾墍浠emcached浣滆€呭氨鍚堝湪涓€璧蜂簡锛?/span>
  215.     鐒跺悗鍙兘鎶婄2锛夋涔熷鍒癴or寰幆閲岄潰锛岀‘瀹炴尯灏村艾鐨勩€傘€傘€備及璁emcached浣滆€呬篃鍐欏緱寰堢籂缁撱€傘€傘€?/span>
  216.     鎵€浠ュ氨寰堟湁鍙兘鍑虹幇5娆¢兘娌℃壘鍒板彲鐢ㄧ殑绌洪棿锛岄兘娌¤繘鍏ヨ繃elseif閭d釜鍒嗘敮灏辫continue鎺変簡锛屼负浜嗚涓嬫湁娌℃湁杩涜繃elseif
  217.     鍒嗘敮灏辨尗鎸湴鐢ㄤ竴涓猼ried_alloc鍙橀噺鏉ュ仛璁板彿銆傘€?/span>
  218.     */
  219.     if (!tried_alloc && (tries == 0 || search == NULL))
  220.         it = slabs_alloc(ntotal, id);
  221.     if (it == NULL) {
  222.         itemstats[id].outofmemory++;
  223.         mutex_unlock(&cache_lock);
  224.         return NULL; //娌¢敊锛佷細鏈夊垎閰嶆柊绌洪棿涓嶆垚鍔燂紝鑰屼笖灏濊瘯5娆℃窐姹版棫鐨刬tem涔熸病鎴愬姛鐨勬椂鍊欙紝鍙兘杩斿洖NULL銆傘€?/span>
  225.     }
  226.     assert(it->slabs_clsid == 0);
  227.     assert(it != heads[id]);
  228.     //鏉ュ埌杩欓噷锛岃鏄巌tem鍒嗛厤鎴愬姛锛屼笅闈富瑕佹槸涓€浜涘垵濮嬪寲宸ヤ綔銆?/span>
  229.     /* Item initialization can happen outside of the lock; the item鈥榮 already
  230.      * been removed from the slab LRU.
  231.      */
  232.     it->refcount = 1; /* the caller will have a reference */
  233.     mutex_unlock(&cache_lock);
  234.     it->next = it->prev = it->h_next = 0;
  235.     it->slabs_clsid = id;
  236.     DEBUG_REFCNT(it, 鈥?鈥?span class="pun">);
  237.     it->it_flags = settings.use_cas ? ITEM_CAS : 0;
  238.     it->nkey = nkey;
  239.     it->nbytes = nbytes;
  240.     memcpy(ITEM_key(it), key, nkey);
  241.     it->exptime = exptime;
  242.     memcpy(ITEM_suffix(it), suffix, (size_t)nsuffix);
  243.     it->nsuffix = nsuffix;
  244.     return it;
  245. }
  246. /**
  247. 鎶婅繖鍧梚tem free鎺夛紝浠ヤ緵鍐嶅埄鐢紝娉ㄦ剰杩欓噷鐨刦ree涓嶆槸鎸囨妸鍐呭瓨绌洪棿閲婃斁鍝︼紝
  248. 鑰屾槸鎶婅繖鍧梚tem 鍙樹负“绌洪棽”
  249. */
  250. void item_free(item *it) {
  251.     size_t ntotal = ITEM_ntotal(it);
  252.     unsigned int clsid;
  253.     assert((it->it_flags & ITEM_LINKED) == 0);
  254.     assert(it != heads[it->slabs_clsid]);
  255.     assert(it != tails[it->slabs_clsid]);
  256.     assert(it->refcount == 0);
  257.     /* so slab size changer can tell later if item is already free or not */
  258.     clsid = it->slabs_clsid;
  259.     it->slabs_clsid = 0; //鍦ㄨ繖鎶奻ree鎺夌殑item 鐨剆labs_clsid璁句负0
  260.     DEBUG_REFCNT(it, 鈥楩鈥?span class="pun">);
  261.     slabs_free(it, ntotal, clsid);
  262. }
  263. /**
  264.  * 妫€鏌tem澶у皬
  265.  */
  266. bool item_size_ok(const size_t nkey, const int flags, const int nbytes) {
  267.     char prefix[40];
  268.     uint8_t nsuffix;
  269.     size_t ntotal = item_make_header(nkey + 1, flags, nbytes,
  270.                                      prefix, &nsuffix);
  271.     if (settings.use_cas) {
  272.         ntotal += sizeof(uint64_t);
  273.     }
  274.     return slabs_clsid(ntotal) != 0;
  275. }
  276. /**
  277. 鎶奿tem鎻掑叆鐩稿簲鐨剆labclass lru閾捐〃涓€屽凡
  278. */
  279. static void item_link_q(item *it) { /* item is the new head */
  280.     item **head, **tail;
  281.     assert(it->slabs_clsid < LARGEST_ID);
  282.     assert((it->it_flags & ITEM_SLABBED) == 0);
  283.     head = &heads[it->slabs_clsid];
  284.     tail = &tails[it->slabs_clsid];
  285.     assert(it != *head);
  286.     assert((*head && *tail) || (*head == 0 && *tail == 0));
  287.     it->prev = 0;
  288.     it->next = *head;
  289.     if (it->next) it->next->prev = it;
  290.     *head = it;
  291.     if (*tail == 0) *tail = it;
  292.     sizes[it->slabs_clsid]++;
  293.     return;
  294. }
  295. /**
  296. 鎶奿tem浠庣浉搴旂殑slabclass lru閾捐〃涓垹鎺夎€屽凡锛屼笅闈㈠氨鏄粡鍏哥殑鍒犻櫎閾捐〃閫昏緫浠g爜浜?/span>
  297. */
  298. static void item_unlink_q(item *it) {
  299.     item **head, **tail;
  300.     assert(it->slabs_clsid < LARGEST_ID);
  301.     head = &heads[it->slabs_clsid];
  302.     tail = &tails[it->slabs_clsid];
  303.     if (*head == it) {
  304.         assert(it->prev == 0);
  305.         *head = it->next;
  306.     }
  307.     if (*tail == it) {
  308.         assert(it->next == 0);
  309.         *tail = it->prev;
  310.     }
  311.     assert(it->next != it);
  312.     assert(it->prev != it);
  313.     if (it->next) it->next->prev = it->prev;
  314.     if (it->prev) it->prev->next = it->next;
  315.     sizes[it->slabs_clsid]--;
  316.     return;
  317. }
  318. /**
  319. 鎶奿tem "link"璧锋潵锛屼富瑕佸寘鎷細
  320. 1锛夋敼鍙樹竴浜涚粺璁℃暟鎹?/span>
  321. 2锛夋妸item鍔犲埌鍝堝笇琛?/span>
  322. 3锛夋妸item鎻掑叆鍒扮浉搴旂殑slabclass lru閾捐〃涓?/span>
  323. */
  324. int do_item_link(item *it, const uint32_t hv) {
  325.     MEMCACHED_ITEM_LINK(ITEM_key(it), it->nkey, it->nbytes);
  326.     assert((it->it_flags & (ITEM_LINKED|ITEM_SLABBED)) == 0);
  327.     mutex_lock(&cache_lock);
  328.     it->it_flags |= ITEM_LINKED;
  329.     it->time = current_time;
  330.     STATS_LOCK();
  331.     stats.curr_bytes += ITEM_ntotal(it);
  332.     stats.curr_items += 1;
  333.     stats.total_items += 1;
  334.     STATS_UNLOCK();
  335.     /* Allocate a new CAS ID on link. */
  336.     ITEM_set_cas(it, (settings.use_cas) ? get_cas_id() : 0);
  337.     assoc_insert(it, hv); //鎻掑叆鍝堝笇琛?/span>
  338.     item_link_q(it); //鍔犲叆LRU閾捐〃
  339.     refcount_incr(&it->refcount);
  340.     mutex_unlock(&cache_lock);
  341.     return 1;
  342. }
  343. /**
  344. 灏辨槸鍜宒o_item_link鍙嶈繃鏉ョ殑涓€浜涙搷浣?/span>
  345. */
  346. void do_item_unlink(item *it, const uint32_t hv) {
  347.     MEMCACHED_ITEM_UNLINK(ITEM_key(it), it->nkey, it->nbytes);
  348.     mutex_lock(&cache_lock);
  349.     if ((it->it_flags & ITEM_LINKED) != 0) {
  350.         it->it_flags &= ~ITEM_LINKED;
  351.         STATS_LOCK();
  352.         stats.curr_bytes -= ITEM_ntotal(it);
  353.         stats.curr_items -= 1;
  354.         STATS_UNLOCK();
  355.         assoc_delete(ITEM_key(it), it->nkey, hv);
  356.         item_unlink_q(it);
  357.         do_item_remove(it);
  358.     }
  359.     mutex_unlock(&cache_lock);
  360. }
  361. /* FIXME: Is it necessary to keep this copy/pasted code? */
  362. void do_item_unlink_nolock(item *it, const uint32_t hv) {
  363.     MEMCACHED_ITEM_UNLINK(ITEM_key(it), it->nkey, it->nbytes);
  364.     if ((it->it_flags & ITEM_LINKED) != 0) {
  365.         it->it_flags &= ~ITEM_LINKED;
  366.         STATS_LOCK();
  367.         stats.curr_bytes -= ITEM_ntotal(it);
  368.         stats.curr_items -= 1;
  369.         STATS_UNLOCK();
  370.         assoc_delete(ITEM_key(it), it->nkey, hv);
  371.         item_unlink_q(it);
  372.         do_item_remove(it);
  373.     }
  374. }
  375. /**
  376. 鎸囧悜item鐨勬寚閽堜笉鐢ㄧ殑鏃跺€欓兘浼氳皟鐢ㄦ鍑芥暟
  377. */
  378. void do_item_remove(item *it) {
  379.     MEMCACHED_ITEM_REMOVE(ITEM_key(it), it->nkey, it->nbytes);
  380.     assert((it->it_flags & ITEM_SLABBED) == 0);
  381.     assert(it->refcount > 0);
  382.     if (refcount_decr(&it->refcount) == 0) { //寮曠敤璁℃暟鍑?锛屽綋寮曠敤璁℃暟涓?鏃讹紝鎵嶇湡姝f妸item free鎺夈€?/span>
  383.         item_free(it);
  384.     }
  385. }
  386. /**
  387. 涓昏浣滅敤鏄噸缃湪鏈€杩戜娇鐢ㄩ摼琛ㄤ腑鐨勪綅缃紝鏇存柊鏈€杩戜娇鐢ㄦ椂闂?/span>
  388. */
  389. void do_item_update(item *it) {
  390.     MEMCACHED_ITEM_UPDATE(ITEM_key(it), it->nkey, it->nbytes);
  391.     if (it->time < current_time - ITEM_UPDATE_INTERVAL) {
  392.         assert((it->it_flags & ITEM_SLABBED) == 0);
  393.         mutex_lock(&cache_lock);
  394.         if ((it->it_flags & ITEM_LINKED) != 0) {
  395.             item_unlink_q(it);
  396.             it->time = current_time;
  397.             item_link_q(it);
  398.         }
  399.         mutex_unlock(&cache_lock);
  400.     }
  401. }
  402. int do_item_replace(item *it, item *new_it, const uint32_t hv) {
  403.     MEMCACHED_ITEM_REPLACE(ITEM_key(it), it->nkey, it->nbytes,
  404.                            ITEM_key(new_it), new_it->nkey, new_it->nbytes);
  405.     assert((it->it_flags & ITEM_SLABBED) == 0);
  406.     do_item_unlink(it, hv);
  407.     return do_item_link(new_it, hv);
  408. }
  409. void item_stats_evictions(uint64_t *evicted) {
  410.     int i;
  411.     mutex_lock(&cache_lock);
  412.     for (i = 0; i < LARGEST_ID; i++) {
  413.         evicted[i] = itemstats[i].evicted;
  414.     }
  415.     mutex_unlock(&cache_lock);
  416. }
  417. void do_item_stats_totals(ADD_STAT add_stats, void *c) {
  418.     itemstats_t totals;
  419.     memset(&totals, 0, sizeof(itemstats_t));
  420.     int i;
  421.     for (i = 0; i < LARGEST_ID; i++) {
  422.         totals.expired_unfetched += itemstats[i].expired_unfetched;
  423.         totals.evicted_unfetched += itemstats[i].evicted_unfetched;
  424.         totals.evicted += itemstats[i].evicted;
  425.         totals.reclaimed += itemstats[i].reclaimed;
  426.         totals.crawler_reclaimed += itemstats[i].crawler_reclaimed;
  427.     }
  428.     APPEND_STAT("expired_unfetched", "%llu",
  429.                 (unsigned long long)totals.expired_unfetched);
  430.     APPEND_STAT("evicted_unfetched", "%llu",
  431.                 (unsigned long long)totals.evicted_unfetched);
  432.     APPEND_STAT("evictions", "%llu",
  433.                 (unsigned long long)totals.evicted);
  434.     APPEND_STAT("reclaimed", "%llu",
  435.                 (unsigned long long)totals.reclaimed);
  436.     APPEND_STAT("crawler_reclaimed", "%llu",
  437.                 (unsigned long long)totals.crawler_reclaimed);
  438. }
  439. void do_item_stats(ADD_STAT add_stats, void *c) {
  440.     int i;
  441.     for (i = 0; i < LARGEST_ID; i++) {
  442.         if (tails[i] != NULL) {
  443.             const char *fmt = "items:%d:%s";
  444.             char key_str[STAT_KEY_LEN];
  445.             char val_str[STAT_VAL_LEN];
  446.             int klen = 0, vlen = 0;
  447.             if (tails[i] == NULL) {
  448.                 /* We removed all of the items in this slab class */
  449.                 continue;
  450.             }
  451.             APPEND_NUM_FMT_STAT(fmt, i, "number", "%u", sizes[i]);
  452.             APPEND_NUM_FMT_STAT(fmt, i, "age", "%u", current_time - tails[i]->time);
  453.             APPEND_NUM_FMT_STAT(fmt, i, "evicted",
  454.                                 "%llu", (unsigned long long)itemstats[i].evicted);
  455.             APPEND_NUM_FMT_STAT(fmt, i, "evicted_nonzero",
  456.                                 "%llu", (unsigned long long)itemstats[i].evicted_nonzero);
  457.             APPEND_NUM_FMT_STAT(fmt, i, "evicted_time",
  458.                                 "%u", itemstats[i].evicted_time);
  459.             APPEND_NUM_FMT_STAT(fmt, i, "outofmemory",
  460.                                 "%llu", (unsigned long long)itemstats[i].outofmemory);
  461.             APPEND_NUM_FMT_STAT(fmt, i, "tailrepairs",
  462.                                 "%llu", (unsigned long long)itemstats[i].tailrepairs);
  463.             APPEND_NUM_FMT_STAT(fmt, i, "reclaimed",
  464.                                 "%llu", (unsigned long long)itemstats[i].reclaimed);
  465.             APPEND_NUM_FMT_STAT(fmt, i, "expired_unfetched",
  466.                                 "%llu", (unsigned long long)itemstats[i].expired_unfetched);
  467.             APPEND_NUM_FMT_STAT(fmt, i, "evicted_unfetched",
  468.                                 "%llu", (unsigned long long)itemstats[i].evicted_unfetched);
  469.             APPEND_NUM_FMT_STAT(fmt, i, "crawler_reclaimed",
  470.                                 "%llu", (unsigned long long)itemstats[i].crawler_reclaimed);
  471.         }
  472.     }
  473.     /* getting here means both ascii and binary terminators fit */
  474.     add_stats(NULL, 0, NULL, 0, c);
  475. }
  476. void do_item_stats_sizes(ADD_STAT add_stats, void *c) {
  477.     /* max 1MB object, divided into 32 bytes size buckets */
  478.     const int num_buckets = 32768;
  479.     unsigned int *histogram = calloc(num_buckets, sizeof(int));
  480.     if (histogram != NULL) {
  481.         int i;
  482.         /* build the histogram */
  483.         for (i = 0; i < LARGEST_ID; i++) {
  484.             item *iter = heads[i];
  485.             while (iter) {
  486.                 int ntotal = ITEM_ntotal(iter);
  487.                 int bucket = ntotal / 32;
  488.                 if ((ntotal % 32) != 0) bucket++;
  489.                 if (bucket < num_buckets) histogram[bucket]++;
  490.                 iter = iter->next;
  491.             }
  492.         }
  493.         /* write the buffer */
  494.         for (i = 0; i < num_buckets; i++) {
  495.             if (histogram[i] != 0) {
  496.                 char key[8];
  497.                 snprintf(key, sizeof(key), "%d", i * 32);
  498.                 APPEND_STAT(key, "%u", histogram[i]);
  499.             }
  500.         }
  501.         free(histogram);
  502.     }
  503.     add_stats(NULL, 0, NULL, 0, c);
  504. }
  505.  
  506. //璇诲彇item鏁版嵁
  507. item *do_item_get(const char *key, const size_t nkey, const uint32_t hv) {
  508.     //mutex_lock(&cache_lock);
  509.     item *it = assoc_find(key, nkey, hv);
  510.     if (it != NULL) {
  511.         refcount_incr(&it->refcount);
  512.         if (slab_rebalance_signal &&
  513.             ((void *)it >= slab_rebal.slab_start && (void *)it < slab_rebal.slab_end)) {
  514.             do_item_unlink_nolock(it, hv);
  515.             do_item_remove(it);
  516.             it = NULL;
  517.         }
  518.     }
  519.     //mutex_unlock(&cache_lock);
  520.     int was_found = 0;
  521.     if (settings.verbose > 2) {
  522.         int ii;
  523.         if (it == NULL) {
  524.             fprintf(stderr, "> NOT FOUND ");
  525.         } else {
  526.             fprintf(stderr, "> FOUND KEY ");
  527.             was_found++;
  528.         }
  529.         for (ii = 0; ii < nkey; ++ii) {
  530.             fprintf(stderr, "%c", key[ii]);
  531.         }
  532.     }
  533.     if (it != NULL) {
  534.         if (settings.oldest_live != 0 && settings.oldest_live <= current_time &&
  535.             it->time <= settings.oldest_live) {
  536.             do_item_unlink(it, hv);
  537.             do_item_remove(it);
  538.             it = NULL;
  539.             if (was_found) {
  540.                 fprintf(stderr, " -nuked by flush");
  541.             }
  542.         } else if (it->exptime != 0 && it->exptime <= current_time) {
  543.             do_item_unlink(it, hv);
  544.             do_item_remove(it);
  545.             it = NULL;
  546.             if (was_found) {
  547.                 fprintf(stderr, " -nuked by expire");
  548.             }
  549.         } else {
  550.             it->it_flags |= ITEM_FETCHED;
  551.             DEBUG_REFCNT(it, 鈥?鈥?span class="pun">);
  552.         }
  553.     }
  554.     if (settings.verbose > 2)
  555.         fprintf(stderr, "\n");
  556.     return it;
  557. }
  558. item *do_item_touch(const char *key, size_t nkey, uint32_t exptime,
  559.                     const uint32_t hv) {
  560.     item *it = do_item_get(key, nkey, hv);
  561.     if (it != NULL) {
  562.         it->exptime = exptime;
  563.     }
  564.     return it;
  565. }
  566. /* expires items that are more recent than the oldest_live setting. */
  567. void do_item_flush_expired(void) {
  568.     int i;
  569.     item *iter, *next;
  570.     if (settings.oldest_live == 0)
  571.         return;
  572.     for (i = 0; i < LARGEST_ID; i++) {
  573.         for (iter = heads[i]; iter != NULL; iter = next) {
  574.             /* iter->time of 0 are magic objects. */
  575.             if (iter->time != 0 && iter->time >= settings.oldest_live) {
  576.                 next = iter->next;
  577.                 if ((iter->it_flags & ITEM_SLABBED) == 0) {
  578.                     do_item_unlink_nolock(iter, hash(ITEM_key(iter), iter->nkey));
  579.                 }
  580.             } else {
  581.                 /* We鈥榲e hit the first old item. Continue to the next queue. */
  582.                 break;
  583.             }
  584.         }
  585.     }
  586. }
  587. static void crawler_link_q(item *it) { /* item is the new tail */
  588.     item **head, **tail;
  589.     assert(it->slabs_clsid < LARGEST_ID);
  590.     assert(it->it_flags == 1);
  591.     assert(it->nbytes == 0);
  592.     head = &heads[it->slabs_clsid];
  593.     tail = &tails[it->slabs_clsid];
  594.     assert(*tail != 0);
  595.     assert(it != *tail);
  596.     assert((*head && *tail) || (*head == 0 && *tail == 0));
  597.     it->prev = *tail;
  598.     it->next = 0;
  599.     if (it->prev) {
  600.         assert(it->prev->next == 0);
  601.         it->prev->next = it;
  602.     }
  603.     *tail = it;
  604.     if (*head == 0) *head = it;
  605.     return;
  606. }
  607. static void crawler_unlink_q(item *it) {
  608.     item **head, **tail;
  609.     assert(it->slabs_clsid < LARGEST_ID);
  610.     head = &heads[it->slabs_clsid];
  611.     tail = &tails[it->slabs_clsid];
  612.     if (*head == it) {
  613.         assert(it->prev == 0);
  614.         *head = it->next;
  615.     }
  616.     if (*tail == it) {
  617.         assert(it->next == 0);
  618.         *tail = it->prev;
  619.     }
  620.     assert(it->next != it);
  621.     assert(it->prev != it);
  622.     if (it->next) it->next->prev = it->prev;
  623.     if (it->prev) it->prev->next = it->next;
  624.     return;
  625. }
  626.  
  627. static item *crawler_crawl_q(item *it) {
  628.     item **head, **tail;
  629.     assert(it->it_flags == 1);
  630.     assert(it->nbytes == 0);
  631.     assert(it->slabs_clsid < LARGEST_ID);
  632.     head = &heads[it->slabs_clsid];
  633.     tail = &tails[it->slabs_clsid];
  634.     /* We鈥榲e hit the head, pop off */
  635.     if (it->prev == 0) {
  636.         assert(*head == it);
  637.         if (it->next) {
  638.             *head = it->next;
  639.             assert(it->next->prev == it);
  640.             it->next->prev = 0;
  641.         }
  642.         return NULL; /* Done */
  643.     }
  644.     assert(it->prev != it);
  645.     if (it->prev) {
  646.         if (*head == it->prev) {
  647.             *head = it;
  648.         }
  649.         if (*tail == it) {
  650.             *tail = it->prev;
  651.         }
  652.         assert(it->next != it);
  653.         if (it->next) {
  654.             assert(it->prev->next == it);
  655.             it->prev->next = it->next;
  656.             it->next->prev = it->prev;
  657.         } else {
  658.             it->prev->next = 0;
  659.         }
  660.         it->next = it->prev;
  661.         it->prev = it->next->prev;
  662.         it->next->prev = it;
  663.         if (it->prev) {
  664.             it->prev->next = it;
  665.         }
  666.     }
  667.     assert(it->next != it);
  668.     assert(it->prev != it);
  669.     return it->next; /* success */
  670. }
  671. /* I pulled this out to make the main thread clearer, but it reaches into the
  672.  * main thread鈥榮 values too much. Should rethink again.
  673.  涓婇潰杩欏彞娉ㄩ噴浣滆€呮槸璇达紝浠栨妸鐢ㄧ埇铏鐞嗚繃鏈熺殑item鐨勫伐浣滄斁鍒板彟涓€涓笓闂ㄧ殑绾跨▼閲屽幓鍋?/span>
  674.  鏄负浜嗚涓荤嚎绋嬪共鍑€涓€鐐癸紝浣嗘槸杩欑嚎绋嬬殑宸ヤ綔娑夊強鍒板お澶氫富绾跨▼鐨勪笢瑗夸簡锛屽緱閲嶆柊鎯虫兂..
  675.  杩欎釜鍑芥暟鐨勪綔鐢ㄦ槸“璇勪及”涓€涓嬭繖涓猧tem鏄惁搴旇free鎺夈€傚叾瀹炰富瑕佸氨鏄湅涓嬫湁娌℃湁杩囨湡鍟
  676.  褰撶劧鐢ㄦ埛璁剧疆鐨剆ettings.oldest_live鍙傛暟涔熷姞鍏ュ埌鑰冭檻涓?/span>
  677.  */
  678. static void item_crawler_evaluate(item *search, uint32_t hv, int i) {
  679.     rel_time_t oldest_live = settings.oldest_live;
  680.     if ((search->exptime != 0 && search->exptime < current_time)
  681.         || (search->time <= oldest_live && oldest_live <= current_time)) {
  682.         itemstats[i].crawler_reclaimed++;
  683.         if (settings.verbose > 1) {
  684.             int ii;
  685.             char *key = ITEM_key(search);
  686.             fprintf(stderr, "LRU crawler found an expired item (flags: %d, slab: %d): ",
  687.                 search->it_flags, search->slabs_clsid);
  688.             for (ii = 0; ii < search->nkey; ++ii) {
  689.                 fprintf(stderr, "%c", key[ii]);
  690.             }
  691.             fprintf(stderr, "\n");
  692.         }
  693.         if ((search->it_flags & ITEM_FETCHED) == 0) {
  694.             itemstats[i].expired_unfetched++;
  695.         }
  696.         do_item_unlink_nolock(search, hv);
  697.         do_item_remove(search);
  698.         assert(search->slabs_clsid == 0);
  699.     } else {
  700.         refcount_decr(&search->refcount);
  701.     }
  702. }
  703. /**
  704. item鐖櫕绾跨▼鍏ュ彛锛岃礋璐d粠lru閾捐〃涓妸杩囨湡鐨刬tem free鎺?/span>
  705. */
  706. static void *item_crawler_thread(void *arg) {
  707.     int i;
  708.     pthread_mutex_lock(&lru_crawler_lock);
  709.     if (settings.verbose > 2)
  710.         fprintf(stderr, "Starting LRU crawler background thread\n");
  711.     while (do_run_lru_crawler_thread) {
  712.     pthread_cond_wait(&lru_crawler_cond, &lru_crawler_lock);
  713.     while (crawler_count) {
  714.         item *search = NULL;
  715.         void *hold_lock = NULL;
  716.         for (i = 0; i < LARGEST_ID; i++) {
  717.             if (crawlers[i].it_flags != 1) {
  718.                 continue;
  719.             }
  720.             pthread_mutex_lock(&cache_lock);
  721.             search = crawler_crawl_q((item *)&crawlers[i]);
  722.             if (search == NULL ||
  723.                 (crawlers[i].remaining && --crawlers[i].remaining < 1)) {
  724.                 if (settings.verbose > 2)
  725.                     fprintf(stderr, "Nothing left to crawl for %d\n", i);
  726.                 crawlers[i].it_flags = 0;
  727.                 crawler_count--;
  728.                 crawler_unlink_q((item *)&crawlers[i]);
  729.                 pthread_mutex_unlock(&cache_lock);
  730.                 continue;
  731.             }
  732.             uint32_t hv = hash(ITEM_key(search), search->nkey);
  733.             /* Attempt to hash item lock the "search" item. If locked, no
  734.              * other callers can incr the refcount
  735.              */
  736.             if ((hold_lock = item_trylock(hv)) == NULL) {
  737.                 pthread_mutex_unlock(&cache_lock);
  738.                 continue;
  739.             }
  740.             /* Now see if the item is refcount locked */
  741.             if (refcount_incr(&search->refcount) != 2) {
  742.                 refcount_decr(&search->refcount);
  743.                 if (hold_lock)
  744.                     item_trylock_unlock(hold_lock);
  745.                 pthread_mutex_unlock(&cache_lock);
  746.                 continue;
  747.             }
  748.             item_crawler_evaluate(search, hv, i);
  749.             if (hold_lock)
  750.                 item_trylock_unlock(hold_lock);
  751.             pthread_mutex_unlock(&cache_lock);
  752.             if (settings.lru_crawler_sleep)
  753.                 usleep(settings.lru_crawler_sleep);
  754.         }
  755.     }
  756.     if (settings.verbose > 2)
  757.         fprintf(stderr, "LRU crawler thread sleeping\n");
  758.     STATS_LOCK();
  759.     stats.lru_crawler_running = false;
  760.     STATS_UNLOCK();
  761.     }
  762.     pthread_mutex_unlock(&lru_crawler_lock);
  763.     if (settings.verbose > 2)
  764.         fprintf(stderr, "LRU crawler thread stopping\n");
  765.     return NULL;
  766. }
  767. static pthread_t item_crawler_tid;
  768. //鍋滄item鐖櫕绾跨▼
  769. int stop_item_crawler_thread(void) {
  770.     int ret;
  771.     pthread_mutex_lock(&lru_crawler_lock);
  772.     do_run_lru_crawler_thread = 0;
  773.     pthread_cond_signal(&lru_crawler_cond);
  774.     pthread_mutex_unlock(&lru_crawler_lock);
  775.     if ((ret = pthread_join(item_crawler_tid, NULL)) != 0) {
  776.         fprintf(stderr, "Failed to stop LRU crawler thread: %s\n", strerror(ret));
  777.         return -1;
  778.     }
  779.     settings.lru_crawler = false;
  780.     return 0;
  781. }
  782. /**
  783. 鍚姩item 鐖櫕绾跨▼
  784. */
  785. int start_item_crawler_thread(void) {
  786.     int ret;
  787.     if (settings.lru_crawler)
  788.         return -1;
  789.     pthread_mutex_lock(&lru_crawler_lock);
  790.     do_run_lru_crawler_thread = 1;
  791.     settings.lru_crawler = true;
  792.     if ((ret = pthread_create(&item_crawler_tid, NULL,
  793.         item_crawler_thread, NULL)) != 0) {
  794.         fprintf(stderr, "Can鈥榯 create LRU crawler thread: %s\n",
  795.             strerror(ret));
  796.         pthread_mutex_unlock(&lru_crawler_lock);
  797.         return -1;
  798.     }
  799.     pthread_mutex_unlock(&lru_crawler_lock);
  800.     return 0;
  801. }
  802. enum crawler_result_type lru_crawler_crawl(char *slabs) {
  803.     char *b = NULL;
  804.     uint32_t sid = 0;
  805.     uint8_t tocrawl[POWER_LARGEST];
  806.     if (pthread_mutex_trylock(&lru_crawler_lock) != 0) {
  807.         return CRAWLER_RUNNING;
  808.     }
  809.     pthread_mutex_lock(&cache_lock);
  810.     if (strcmp(slabs, "all") == 0) {
  811.         for (sid = 0; sid < LARGEST_ID; sid++) {
  812.             tocrawl[sid] = 1;
  813.         }
  814.     } else {
  815.         for (char *p = strtok_r(slabs, ",", &b);
  816.              p != NULL;
  817.              p = strtok_r(NULL, ",", &b)) {
  818.             if (!safe_strtoul(p, &sid) || sid < POWER_SMALLEST
  819.                     || sid > POWER_LARGEST) {
  820.                 pthread_mutex_unlock(&cache_lock);
  821.                 pthread_mutex_unlock(&lru_crawler_lock);
  822.                 return CRAWLER_BADCLASS;
  823.             }
  824.             tocrawl[sid] = 1;
  825.         }
  826.     }
  827.     for (sid = 0; sid < LARGEST_ID; sid++) {
  828.         if (tocrawl[sid] != 0 && tails[sid] != NULL) {
  829.             if (settings.verbose > 2)
  830.                 fprintf(stderr, "Kicking LRU crawler off for slab %d\n", sid);
  831.             crawlers[sid].nbytes = 0;
  832.             crawlers[sid].nkey = 0;
  833.             crawlers[sid].it_flags = 1; /* For a crawler, this means enabled. */
  834.             crawlers[sid].next = 0;
  835.             crawlers[sid].prev = 0;
  836.             crawlers[sid].time = 0;
  837.             crawlers[sid].remaining = settings.lru_crawler_tocrawl;
  838.             crawlers[sid].slabs_clsid = sid;
  839.             crawler_link_q((item *)&crawlers[sid]);
  840.             crawler_count++;
  841.         }
  842.     }
  843.     pthread_mutex_unlock(&cache_lock);
  844.     pthread_cond_signal(&lru_crawler_cond);
  845.     STATS_LOCK();
  846.     stats.lru_crawler_running = true;
  847.     STATS_UNLOCK();
  848.     pthread_mutex_unlock(&lru_crawler_lock);
  849.     return CRAWLER_OK;
  850. }
  851. //鍒濆鍖杔ru item鐖櫕绾跨▼
  852. int init_lru_crawler(void) {
  853.     if (lru_crawler_initialized == 0) {
  854.         if (pthread_cond_init(&lru_crawler_cond, NULL) != 0) {
  855.             fprintf(stderr, "Can鈥榯 initialize lru crawler condition\n");
  856.             return -1;
  857.         }
  858.         pthread_mutex_init(&lru_crawler_lock, NULL);
  859.         lru_crawler_initialized = 1;
  860.     }
  861.     return 0;
  862. }

以上是关于Memcached婧愮爜鍒嗘瀽涔媔tems.c的主要内容,如果未能解决你的问题,请参考以下文章

AtomicInteger 婧愮爜鍒嗘瀽

ArrayList婧愮爜鍒嗘瀽

JDK婧愮爜鍒嗘瀽鍒濇鏁寸悊

log4j2婧愮爜鍒嗘瀽-1

绮惧敖 MyBatis 婧愮爜鍒嗘瀽 - SqlSession 浼氳瘽涓?SQL 鎵ц鍏ュ彛

鏁欏コ鏈嬪弸璇绘噦绯诲垪锛歞ubbo鐨勫彲鎵╁睍鏈哄埗SPI 婧愮爜鍒嗘瀽