Пример #1
0
//togo_pool.c
void togo_pool_test()
{
	togo_log(DEBUG, "Testing togo_pool.c start ============================");
	void * p;
	void * p1;

	//togo_pool_create
	TOGO_POOL * pool = togo_pool_create(10 * 1024);
	if (pool->total_size == 10288 && pool->max == 10 * 1024) {
		togo_log(DEBUG,
				"Testing function:togo_pool_create .............................OK");
	} else {
		togo_log(DEBUG,
				"Testing function:togo_pool_create .............................FAIL");
		togo_exit();
	}

	//togo_pool_calloc
	p1 = togo_pool_calloc(pool, 10 * 1024);
	p = togo_pool_calloc(pool, 10);
	togo_strcpy(p, "woshishen");
	if (strcmp(p, "woshishen") == 0 && togo_strlen(p) == 9
			&& pool->large->size == 10 * 1024) {
		togo_log(DEBUG,
				"Testing function:togo_pool_calloc .............................OK");
	} else {
		togo_log(DEBUG,
				"Testing function:togo_pool_calloc .............................FAIL");
		togo_exit();
	}

	//togo_pool_realloc(pool, 20);
	p = togo_pool_realloc(pool, (void *) p, 10, 15);
	togo_strcpy(p, "woshishen11234");
	if (strcmp(p, "woshishen11234") == 0 && togo_strlen(p) == 14) {
		togo_log(DEBUG,
				"Testing function:togo_pool_realloc .............................OK");
	} else {
		togo_log(DEBUG,
				"Testing function:togo_pool_realloc .............................FAIL");
		togo_exit();
	}

	//togo_pool_free_data
	togo_pool_free_data(pool, (void *) p);
	togo_pool_free_large(pool, (void *) p1);

	if (pool->large == NULL) {
		togo_log(DEBUG,
				"Testing function:togo_pool_free_large .............................OK");
	} else {
		togo_log(DEBUG,
				"Testing function:togo_pool_free_large .............................FAIL");
		togo_exit();
	}
	togo_log(DEBUG, "Testing togo_pool.c end ============================\r\n");
}
Пример #2
0
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;

}
Пример #3
0
static void togo_q_init(TOGO_WORKER_THREAD *worker_thread)
{
	worker_thread->queue = (TOGO_THREAD_QUEUE *) togo_pool_calloc(
			togo_global_pool, sizeof(TOGO_THREAD_QUEUE));
	worker_thread->queue->head = NULL;
	worker_thread->queue->tail = NULL;
}
Пример #4
0
static void togo_m_queue_block_create()
{
	TOGO_M_QUEUE_BLOCK * block_s;
	u_char * block;
	uint32_t i;

	for (i = 0; i < TOGO_M_QUEUE_BLOCK_NUM; i++) {

		/* Alloc a large memory to store blocks.
		 * Each large memory can store TOGO_M_QUEUE_BLOCK_NUM block.*/
		block_s = (TOGO_M_QUEUE_BLOCK *) togo_pool_calloc(togo_m_queue_pool,
				sizeof(TOGO_M_QUEUE_BLOCK));
		block = (u_char *) togo_pool_calloc(togo_m_queue_pool,
				TOGO_M_QUEUE_BLOCK_SIZE);
		if (block_s == NULL || block == NULL) {

			if (block != NULL) {
				togo_pool_free_large(togo_m_queue_pool, (void *) block);
			}
			if (block_s != NULL) {
				togo_pool_free_data(togo_m_queue_pool, (void *) block_s);
			}
			togo_log(ERROR, "Initialize modules_queue's block fail.");
			return;
		}

		block_s->buf = block;
		block_s->nelt = 0;
		block_s->next = (TOGO_M_QUEUE_BLOCK *) togo_m_queue_fblock->block;
		block_s->prev = NULL;
		block_s->size = TOGO_M_QUEUE_BLOCK_SIZE;
		block_s->curr = block;

		togo_m_queue_fblock->block = block_s;
		togo_m_queue_fblock->total++;
	}
}
Пример #5
0
static void togo_mt_init()
{
	togo_thread_flist = (TOGO_THREAD_FLIST *) togo_pool_calloc(togo_global_pool,
			sizeof(TOGO_THREAD_FLIST));
	if (togo_thread_flist == NULL) {
		togo_log(ERROR, "Initialize togo_thread_flist error.");
	}

	togo_thread_flist->next = NULL;
	togo_thread_flist->total = 0;
	pthread_mutex_init(&togo_thread_flist->lock, NULL);

	pthread_create(&server_main_thread, NULL, togo_mt_process, NULL);
	togo_log(INFO, "Initialize main thread, ip:%s port:%d.", togo_global_c.ip,
			togo_global_c.port);
}
Пример #6
0
static void togo_wt_init()
{
	int i, j;
	int worker_thread_num = togo_global_c.worker_thread_num;

	togo_worker_threads = (TOGO_WORKER_THREAD *) togo_pool_calloc(
			togo_global_pool, sizeof(TOGO_WORKER_THREAD) * worker_thread_num);

	for (i = 0; i < worker_thread_num; i++) {
		togo_wt_setup(&togo_worker_threads[i]);
	}

	for (j = 0; j < worker_thread_num; j++) {
		togo_wt_create(&togo_worker_threads[j]);
	}

}
Пример #7
0
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;
}
Пример #8
0
void togo_m_queue_init(void)
{
	togo_m_queue_pool = togo_pool_create(TOGO_M_QUEUE_POOL_SIZE);
	if (togo_m_queue_pool == NULL) {
		togo_log(ERROR, "Initialize modules_queue's pool fail.");
		togo_exit();
	}

	togo_m_queue_hashtable = togo_hashtable_init(togo_m_queue_pool);
	if (togo_m_queue_hashtable == NULL) {
		togo_log(ERROR, "Initialize modules_queue's hashtable fail.");
		togo_exit();
	}

	togo_m_queue_fblock = (TOGO_M_QUEUE_FBLOCK *) togo_pool_calloc(
			togo_m_queue_pool, sizeof(TOGO_M_QUEUE_FBLOCK));
	if (togo_m_queue_fblock == NULL) {
		togo_log(ERROR, "Initialize modules_queue's free block fail.");
		togo_exit();
	}
	pthread_mutex_init(&togo_m_queue_fblock->flock, NULL);
	pthread_mutex_init(&togo_m_queue_glock, NULL);

}
Пример #9
0
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);
	}

}
Пример #10
0
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.");
	}

}