static TOGO_M_CACHE_CHUNK * togo_m_cache_create_chunk(TOGO_M_CACHE_AREA * area) { TOGO_M_CACHE_CHUNK * chunk; u_char * p; chunk = (TOGO_M_CACHE_CHUNK *) togo_pool_calloc(togo_m_cache_pool, sizeof(TOGO_M_CACHE_CHUNK)); if (chunk == NULL) { togo_log(INFO, "create modules_cache's chunk fail."); return NULL; } p = (u_char *) togo_pool_alloc(togo_m_cache_pool, sizeof(u_char) * TOGO_M_CACHE_CHUNK_SIZE); if (p == NULL) { togo_log(INFO, "create modules_cache's chunk fail."); return NULL; } chunk->area = area; chunk->next = NULL; chunk->prev = NULL; chunk->p = p; togo_m_cache->total_size += TOGO_M_CACHE_CHUNK_SIZE; return chunk; }
BOOL togo_m_queue_count(u_char * name, TOGO_THREAD_ITEM * socket_item) { uint32_t count; TOGO_M_QUEUE * queue; size_t str_size; queue = togo_m_queue_get(name); count = (queue == NULL) ? 0 : queue->total_elt; u_char * str = (u_char *) togo_pool_alloc(queue->pool, 12); togo_itoa(count, str, 10); togo_send_data(socket_item, str, togo_strlen(str)); togo_pool_free_data(queue->pool, (void *) str); return TRUE; }
static TOGO_M_QUEUE * togo_m_queue_create(u_char * name) { TOGO_M_QUEUE * queue; TOGO_M_QUEUE_BLOCK * block; queue = (TOGO_M_QUEUE *) togo_pool_calloc(togo_m_queue_pool, sizeof(TOGO_M_QUEUE)); if (queue == NULL) { return NULL; } u_char * buf = (u_char *) togo_pool_alloc(togo_m_queue_pool, togo_pool_strlen(name)); if (buf == NULL) { return NULL; } togo_strcpy(buf, name); block = togo_m_queue_block_get(); if (block == NULL) { return NULL; } queue->name = buf; queue->block = block; queue->total_block = 1; queue->total_elt = 0; queue->total_size = togo_m_queue_block_size(); queue->total_hit = 0; queue->total_read = 0; queue->total_write = 0; queue->tail = NULL; queue->head = NULL; queue->pool = togo_m_queue_pool; pthread_mutex_init(&queue->qlock, NULL); return queue; }
void togo_m_cache_init(void) { uint32_t total_area, curr, i; uint32_t * area_table; TOGO_M_CACHE_AREA * area; togo_m_cache_pool = togo_pool_create(TOGO_M_CACHE_POOL_SIZE); if (togo_m_cache_pool == NULL) { togo_log(ERROR, "Initialize modules_cache's POOL fail."); togo_exit(); } togo_m_cache_hashtable = togo_hashtable_init(togo_m_cache_pool); if (togo_m_cache_hashtable == NULL) { togo_log(ERROR, "Initialize modules_cache's HASHTABLE fail."); togo_exit(); } togo_m_cache = (TOGO_M_CACHE *) togo_pool_calloc(togo_m_cache_pool, sizeof(TOGO_M_CACHE)); if (togo_m_cache == NULL) { togo_log(ERROR, "Initialize modules_cache's TOGO_M_CACHE fail."); togo_exit(); } togo_m_cache->pool = togo_m_cache_pool; togo_m_cache->total_area = 0; togo_m_cache->total_hit = 0; togo_m_cache->total_read = 0; togo_m_cache->total_size = 0; togo_m_cache->total_write = 0; togo_m_cache->is_flush = FALSE; pthread_mutex_init(&togo_m_cache->glock, NULL); /* Initialize the area table!*/ area_table = (uint32_t *) togo_pool_alloc(togo_m_cache_pool, sizeof(uint32_t) * TOGO_M_CACHE_AREA_TABLE_DEFAULT_SIZE); if (area_table == NULL) { togo_log(ERROR, "Initialize modules_cache's area_table fail."); togo_exit(); } togo_m_cache->area_table = area_table; total_area = i = 1; curr = TOGO_M_CACHE_ITEM_START; *area_table = curr; while (curr != TOGO_M_CACHE_CHUNK_SIZE) { total_area++; curr = (uint32_t)(curr * TOGO_M_CACHE_ITEM_POWER); if (curr >= TOGO_M_CACHE_CHUNK_SIZE) { curr = TOGO_M_CACHE_CHUNK_SIZE; } *(area_table + i) = curr; i++; } togo_m_cache->total_area = total_area; /* Initialize the memory of area!*/ area = (TOGO_M_CACHE_AREA *) togo_pool_calloc(togo_m_cache_pool, sizeof(TOGO_M_CACHE_AREA) * total_area); if (area == NULL) { togo_log(ERROR, "Initialize modules_cache's TOGO_M_CACHE_AREA fail."); togo_exit(); } togo_m_cache->area = area; for (i = 0; i < total_area; i++) { curr = *(area_table + i); togo_m_cache_create_area(curr, i, area); } }
static void togo_mt_doaccept(evutil_socket_t fd, short event, void *arg) { struct sockaddr_in client_addr; int in_size = sizeof(struct sockaddr_in); u_char * rbuf; u_char * sbuf; int client_socketfd = accept(fd, (struct sockaddr *) &client_addr, &in_size); if (client_socketfd < 0) { togo_log(ERROR, "Accept error."); return; } /** * We through the way of polling to select a thread, * Who will taking over a connection. */ int worker_thread_num = togo_global_c.worker_thread_num; int tid = (last_thread + 1) % worker_thread_num; TOGO_WORKER_THREAD * worker_thread = togo_worker_threads + tid; last_thread = tid; /** * Initialize a socket_item. Each connection will have one socket_item. * Through this socket_item , We store each connection details. * When this connection is disconnect, The socket_item will be freed. */ TOGO_THREAD_ITEM * socket_item = NULL; /* Search the free list */ if (togo_thread_flist->next != NULL) { pthread_mutex_lock(&togo_thread_flist->lock); if (togo_thread_flist->next != NULL) { socket_item = togo_thread_flist->next; togo_thread_flist->next = socket_item->next; togo_thread_flist->total--; } pthread_mutex_unlock(&togo_thread_flist->lock); } if (socket_item == NULL) { TOGO_POOL * worker_pool = togo_pool_create(TOGO_WORKER_POOL_SIZE); socket_item = (TOGO_THREAD_ITEM *) togo_pool_calloc(worker_pool, sizeof(TOGO_THREAD_ITEM)); rbuf = (u_char *) togo_pool_alloc(worker_pool, sizeof(u_char) * TOGO_S_RBUF_INIT_SIZE); sbuf = (u_char *) togo_pool_alloc(worker_pool, TOGO_S_SBUF_INIT_SIZE); if (rbuf == NULL || sbuf == NULL) { togo_log(ERROR, "togo_pool_alloc a rbuf or sbuf error."); return; } socket_item->worker_pool = worker_pool; socket_item->rbuf = rbuf; socket_item->sbuf = sbuf; socket_item->rsize = sizeof(u_char) * TOGO_S_RBUF_INIT_SIZE; socket_item->sbuf_size = sizeof(u_char) * TOGO_S_SBUF_INIT_SIZE; } socket_item->sfd = client_socketfd; socket_item->rcurr = socket_item->rbuf; socket_item->rbytes = 0; socket_item->ssize = 0; togo_q_push(worker_thread, socket_item); u_char buf[1]; buf[0] = 'c'; if (write(worker_thread->notify_send_fd, buf, 1) != 1) { togo_log(ERROR, "Write pipe error."); } }