Ejemplo n.º 1
0
int do_item_link(item *it, const uint32_t hv) {
    /* MEMCACHED_ITEM_LINK(ITEM_key(it), it->nkey, it->nbytes); */
    /* assert((it->it_flags & (ITEM_LINKED|ITEM_SLABBED)) == 0); */
    /* printf("item link 1\n"); */
    /* LOCK_CLOCK(); */
    /* it->it_flags |= ITEM_LINKED; */
    /* it->time = current_time; */
    /* printf("item link 2\n"); */

    /* STATS_LOCK(); */
    /* printf("item link 3\n"); */
    /* stats.curr_bytes += ITEM_ntotal(it); */
    /* stats.curr_items += 1; */
    /* stats.total_items += 1; */
    /* STATS_UNLOCK(); */

    /* /\* Allocate a new CAS ID on link. *\/ */
    /* ITEM_set_cas(it, (settings.use_cas) ? get_cas_id() : 0); */
    /* assoc_insert(it, hv); */
    /* item_link_q(it); */
    /* refcount_incr(&it->refcount); */

    /* UNLOCK_CLOCK(); */

    assert((it->it_flags & (ITEM_LINKED|ITEM_SLABBED)) == 0);

    it->it_flags |= ITEM_LINKED;
    it->time = current_time;
    assoc_insert(it, hv);

    return 1;
}
Ejemplo n.º 2
0
int do_item_link(item *it) {
    MEMCACHED_ITEM_LINK(ITEM_key(it), it->nbytes);
    assert((it->it_flags & (ITEM_LINKED|ITEM_SLABBED)) == 0);
    assert(it->nbytes < (1024 * 1024));  /* 1MB max size */
    it->it_flags |= ITEM_LINKED;
    it->time = current_time;
    assoc_insert(it);

    STATS_LOCK();
    stats.curr_bytes += ITEM_ntotal(it);
    stats.curr_items += 1;
    stats.total_items += 1;
    STATS_UNLOCK();

#ifdef USE_REPLICATION
    /* Allocate a new CAS ID on link. */
    if(!(it->it_flags & ITEM_REPDATA))
        it->cas_id = get_cas_id();
#else
    /* Allocate a new CAS ID on link. */
    it->cas_id = get_cas_id();
#endif /* USE_REPLICATION */

    item_link_q(it);

    return 1;
}
Ejemplo n.º 3
0
//将item插入到哈希表和LRU队列中,hv为哈希值
int do_item_link(item *it, const uint32_t hv) {
    MEMCACHED_ITEM_LINK(ITEM_key(it), it->nkey, it->nbytes);
    //确保这个item已经从slab分配出去并且还没插入到LRU队列中
    assert((it->it_flags & (ITEM_LINKED|ITEM_SLABBED)) == 0);
    mutex_lock(&cache_lock);
    //加入link标记
    it->it_flags |= ITEM_LINKED;
    it->time = current_time;

    STATS_LOCK();
    stats.curr_bytes += ITEM_ntotal(it);
    stats.curr_items += 1;
    stats.total_items += 1;
    STATS_UNLOCK();

    /* Allocate a new CAS ID on link. */
    ITEM_set_cas(it, (settings.use_cas) ? get_cas_id() : 0);
    //插入到hash表中
    assoc_insert(it, hv);
    //item插入到链表中
    item_link_q(it);
    //引用计数加1
    refcount_incr(&it->refcount);
    mutex_unlock(&cache_lock);

    return 1;
}
Ejemplo n.º 4
0
void slabs_alloc_test(void) {
    unsigned int total_chunk = 0;
    
    const char *key = "charliezhao";
    size_t nkey = strlen(key) + 1;
    item *ptr = (item *)slabs_alloc(1024, slabs_clsid(1024), &total_chunk);
    strcpy(ITEM_key(ptr), key);   
    ptr->nkey = nkey;
    strcpy(ITEM_data(ptr), "xuechaozhao");
    uint32_t hv = jenkins_hash(key, strlen(key));
    assoc_insert(ptr, hv);

    for(int i = 0; i <= 10922; ++i) {
        void *ptr = slabs_alloc(96, slabs_clsid(96), &total_chunk);
        if(ptr == NULL) {
            fprintf(stderr, "i: %7d slabs_alloc fail\n", 
                    i);
            break;
        }
        else {
            slabs_free(ptr, 96, slabs_clsid(96)); 
        }
    }

    item *ptr2 = assoc_find(key, nkey, hv);
    fprintf(stdout, "key:%20s value:%20s\n", ITEM_key(ptr2), ITEM_data(ptr2)); 
}
Ejemplo n.º 5
0
int item_link(item *it) {
    int needed = it->ntotal;
    unsigned long long maxbytes;
    int maxitems;

    maxbytes = settings.maxbytes ? settings.maxbytes : UINT_MAX;
    maxitems = settings.maxitems ? settings.maxitems : INT_MAX;

    while(items_tail && (stats.curr_bytes + needed > maxbytes ||
                         stats.curr_items + 1 > maxitems)) {
        drop_tail();
    }
    
    it->it_flags |= ITEM_LINKED;
    it->time = time(0);
    assoc_insert(it->key, (void *)it);

    stats.curr_bytes += needed;
    stats.curr_items += 1;
    stats.total_items += 1;

    item_link_q(it);

    return 1;
}
Ejemplo n.º 6
0
/* 形成了一个完成的 item 后, 就要把它放入两个数据结构中, 一是 memcached 的哈希表,
memcached 运行过程中只有一个哈希表, 二是 item 所在的 slabclass 的 LRU 队列. */
int do_item_link(item *it, const uint32_t hv) {
    MEMCACHED_ITEM_LINK(ITEM_key(it), it->nkey, it->nbytes);
    assert((it->it_flags & (ITEM_LINKED|ITEM_SLABBED)) == 0);
    mutex_lock(&cache_lock);
    it->it_flags |= ITEM_LINKED;
    it->time = current_time;

    STATS_LOCK();
    stats.curr_bytes += ITEM_ntotal(it);
    stats.curr_items += 1;
    stats.total_items += 1;
    STATS_UNLOCK();

    /* Allocate a new CAS ID on link. */
    ITEM_set_cas(it, (settings.use_cas) ? get_cas_id() : 0);

    /* 把 item 放入哈希表 */
    assoc_insert(it, hv);
    /* 把 item 放入 LRU 队列*/
    item_link_q(it);

    refcount_incr(&it->refcount);
    mutex_unlock(&cache_lock);

    return 1;
}
Ejemplo n.º 7
0
int do_item_link(item *it) {
    MEMCACHED_ITEM_LINK(ITEM_key(it), it->nkey, it->nbytes);
    assert((it->it_flags & (ITEM_LINKED|ITEM_SLABBED)) == 0);
    assert(it->nbytes < (1024 * 1024));  /* 1MB max size */
    it->it_flags |= ITEM_LINKED;
    it->time = current_time;
    assoc_insert(it);

#ifdef MOXI_ITEM_MALLOC
    it->refcount++;
#endif

    STATS_LOCK();
    stats.curr_bytes += ITEM_ntotal(it);
    stats.curr_items += 1;
    stats.total_items += 1;
    STATS_UNLOCK();

    /* Allocate a new CAS ID on link. */
    ITEM_set_cas(it, (settings.use_cas) ? get_cas_id() : 0);

    item_link_q(it);

    return 1;
}
Ejemplo n.º 8
0
static void _item_link(struct item *it) {
	assert(it->magic == ITEM_MAGIC);
	assert(!item_is_linked(it));
	assert(!item_is_slabbed(it));
    it->flags |= ITEM_LINKED;
    assoc_insert(it);
    item_link_q(it, true);
}
Ejemplo n.º 9
0
int item_link(item *it) {
    assert((it->it_flags & (ITEM_LINKED|ITEM_SLABBED)) == 0);
    assert(it->nbytes < 1048576);
    it->it_flags |= ITEM_LINKED;
    it->time = current_time;
    assoc_insert(ITEM_key(it), it);

    stats.curr_bytes += ITEM_ntotal(it);
    stats.curr_items += 1;
    stats.total_items += 1;

    item_link_q(it);

    return 1;
}
Ejemplo n.º 10
0
int do_item_link(item *it) {
    assert((it->it_flags & (ITEM_LINKED|ITEM_SLABBED)) == 0);
    assert(it->nbytes < (1024 * 1024));  /* 1MB max size */
    it->it_flags |= ITEM_LINKED;
    it->time = current_time;
    assoc_insert(it);

    STATS_LOCK();
    stats.curr_bytes += ITEM_ntotal(it);
    stats.curr_items += 1;
    stats.total_items += 1;
    STATS_UNLOCK();

    /* Allocate a new CAS ID on link. */
    it->cas_id = get_cas_id();

    item_link_q(it);

    return 1;
}
Ejemplo n.º 11
0
int do_item_link(item *it) {
    //    MEMCACHED_ITEM_LINK(ITEM_key(it), it->nbytes);
    //    assert((it->it_flags & (ITEM_LINKED|ITEM_SLABBED)) == 0);
    assert(it->nbytes < (1024 * 1024)); /* 1MB max size */
    it->it_flags |= ITEM_LINKED;
    //<>
    it->time = now_ms;
    assoc_insert(it);

    stats.curr_bytes += ITEM_ntotal(it);
    stats.curr_items++;
    stats.total_items++;

    /* Allocate a new CAS ID on link. */
    it->cas_id = get_cas_id();

    item_link_q(it);

    return 1;
}
Ejemplo n.º 12
0
int do_item_link(struct default_engine *engine, hash_item *it) {
    MEMCACHED_ITEM_LINK(item_get_key(it), it->nkey, it->nbytes);
    assert((it->iflag & (ITEM_LINKED|ITEM_SLABBED)) == 0);
    assert(it->nbytes < (1024 * 1024));  /* 1MB max size */
    it->iflag |= ITEM_LINKED;
    it->time = engine->server.core->get_current_time();
    assoc_insert(engine, engine->server.core->hash(item_get_key(it),
                                                        it->nkey, 0),
                 it);

    pthread_mutex_lock(&engine->stats.lock);
    engine->stats.curr_bytes += ITEM_ntotal(engine, it);
    engine->stats.curr_items += 1;
    engine->stats.total_items += 1;
    pthread_mutex_unlock(&engine->stats.lock);

    /* Allocate a new CAS ID on link. */
    item_set_cas(NULL, NULL, it, get_cas_id());

    item_link_q(engine, it);

    return 1;
}
Ejemplo n.º 13
0
//将item加入到hashtable和LRU链中
int do_item_link(item *it, const uint32_t hv) {
		syslog(LOG_INFO, "[%s:%s:%d]", __FILE__, __func__, __LINE__);
    MEMCACHED_ITEM_LINK(ITEM_key(it), it->nkey, it->nbytes);
    assert((it->it_flags & (ITEM_LINKED|ITEM_SLABBED)) == 0); //判断状态,即没有在hash表LRU链中或被释放
    mutex_lock(&cache_lock);
    it->it_flags |= ITEM_LINKED; //设置linked状态
    it->time = current_time;//设置最近访问的时间

    STATS_LOCK();
    stats.curr_bytes += ITEM_ntotal(it); //增加每个item所需要的字节大小,包括item结构体和item内容大小
    stats.curr_items += 1;
    stats.total_items += 1;
    STATS_UNLOCK();

    /* Allocate a new CAS ID on link. */
    ITEM_set_cas(it, (settings.use_cas) ? get_cas_id() : 0); //设置新CAS,CAS是memcache用来处理并发请求的一种机制
    assoc_insert(it, hv);//插入hashtable
    item_link_q(it); //加入LRU链
    refcount_incr(&it->refcount);
    mutex_unlock(&cache_lock);

    return 1;
}
Ejemplo n.º 14
0
int do_item_link(struct default_engine *engine, hash_item *it) {
    const hash_key* key = item_get_key(it);
    MEMCACHED_ITEM_LINK(hash_key_get_client_key(key), hash_key_get_client_key_len(key), it->nbytes);
    cb_assert((it->iflag & (ITEM_LINKED|ITEM_SLABBED)) == 0);
    it->iflag |= ITEM_LINKED;
    it->time = engine->server.core->get_current_time();

    assoc_insert(engine, crc32c(hash_key_get_key(key),
                                hash_key_get_key_len(key), 0),
                 it);

    cb_mutex_enter(&engine->stats.lock);
    engine->stats.curr_bytes += ITEM_ntotal(engine, it);
    engine->stats.curr_items += 1;
    engine->stats.total_items += 1;
    cb_mutex_exit(&engine->stats.lock);

    /* Allocate a new CAS ID on link. */
    item_set_cas(NULL, NULL, it, get_cas_id());

    item_link_q(engine, it);

    return 1;
}