fast_task_queue.c 13.7 KB
Newer Older
Y
yuqing 已提交
1 2 3 4 5
//fast_task_queue.c

#include <errno.h>
#include <sys/resource.h>
#include <pthread.h>
Y
yuqing 已提交
6
#include <inttypes.h>
Y
yuqing 已提交
7 8 9 10 11 12 13
#include "fast_task_queue.h"
#include "logger.h"
#include "shared_func.h"
#include "pthread_func.h"

static struct fast_task_queue g_free_queue;

Y
yuqing 已提交
14
struct mpool_node {
Y
yuqing 已提交
15 16
	struct fast_task_info *blocks;
	struct fast_task_info *last_block;   //last block
Y
yuqing 已提交
17 18 19 20 21 22 23
	struct mpool_node *next;
};

struct mpool_chain {
    struct mpool_node *head;
    struct mpool_node *tail;
} g_mpool = {NULL, NULL};
Y
yuqing 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

#define ALIGNED_TASK_INFO_SIZE  MEM_ALIGN(sizeof(struct fast_task_info))

int task_queue_init(struct fast_task_queue *pQueue)
{
	int result;

	if ((result=init_pthread_lock(&(pQueue->lock))) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"init_pthread_lock fail, errno: %d, error info: %s", \
			__LINE__, result, STRERROR(result));
		return result;
	}

	pQueue->head = NULL;
	pQueue->tail = NULL;

	return 0;
}

Y
yuqing 已提交
45
static struct mpool_node *malloc_mpool(const int total_alloc_size)
Y
yuqing 已提交
46 47 48 49
{
	struct fast_task_info *pTask;
	char *p;
	char *pCharEnd;
Y
yuqing 已提交
50
	struct mpool_node *mpool;
Y
yuqing 已提交
51

Y
yuqing 已提交
52
	mpool = (struct mpool_node *)malloc(sizeof(struct mpool_node));
Y
yuqing 已提交
53 54 55 56 57
	if (mpool == NULL)
	{
		logError("file: "__FILE__", line: %d, " \
			"malloc %d bytes fail, " \
			"errno: %d, error info: %s", \
Y
yuqing 已提交
58
			__LINE__, (int)sizeof(struct mpool_node), \
Y
yuqing 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
			errno, STRERROR(errno));
		return NULL;
	}

	mpool->next = NULL;
	mpool->blocks = (struct fast_task_info *)malloc(total_alloc_size);
	if (mpool->blocks == NULL)
	{
		logError("file: "__FILE__", line: %d, " \
			"malloc %d bytes fail, " \
			"errno: %d, error info: %s", \
			__LINE__, total_alloc_size, \
			errno, STRERROR(errno));
		free(mpool);
		return NULL;
	}
	memset(mpool->blocks, 0, total_alloc_size);

	pCharEnd = ((char *)mpool->blocks) + total_alloc_size;
Y
yuqing 已提交
78
	for (p=(char *)mpool->blocks; p<pCharEnd; p += g_free_queue.block_size)
Y
yuqing 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
	{
		pTask = (struct fast_task_info *)p;
		pTask->size = g_free_queue.min_buff_size;

		pTask->arg = p + ALIGNED_TASK_INFO_SIZE;
		if (g_free_queue.malloc_whole_block)
		{
			pTask->data = (char *)pTask->arg + \
					g_free_queue.arg_size;
		}
		else
		{
			pTask->data = (char *)malloc(pTask->size);
			if (pTask->data == NULL)
			{
				char *pt;

				logError("file: "__FILE__", line: %d, " \
					"malloc %d bytes fail, " \
					"errno: %d, error info: %s", \
					__LINE__, pTask->size, \
					errno, STRERROR(errno));

				for (pt=(char *)mpool->blocks; pt < p; \
Y
yuqing 已提交
103
					pt += g_free_queue.block_size)
Y
yuqing 已提交
104 105 106 107 108 109 110 111 112 113 114
				{
					free(((struct fast_task_info *)pt)->data);
				}

				free(mpool->blocks);
				free(mpool);
				return NULL;
			}
		}
	}

Y
yuqing 已提交
115 116
	mpool->last_block = (struct fast_task_info *)(pCharEnd - g_free_queue.block_size);
	for (p=(char *)mpool->blocks; p<(char *)mpool->last_block; p += g_free_queue.block_size)
Y
yuqing 已提交
117 118
	{
		pTask = (struct fast_task_info *)p;
Y
yuqing 已提交
119
		pTask->next = (struct fast_task_info *)(p + g_free_queue.block_size);
Y
yuqing 已提交
120 121 122 123 124 125
	}
	mpool->last_block->next = NULL;

	return mpool;
}

Y
yuqing 已提交
126 127 128
int free_queue_init_ex(const int max_connections, const int init_connections,
        const int alloc_task_once, const int min_buff_size,
        const int max_buff_size, const int arg_size)
Y
yuqing 已提交
129
{
Y
yuqing 已提交
130
#define MAX_DATA_SIZE  (256 * 1024 * 1024)
Y
yuqing 已提交
131
	int64_t total_size;
Y
yuqing 已提交
132
	struct mpool_node *mpool;
Y
yuqing 已提交
133
	int alloc_size;
Y
yuqing 已提交
134
    int alloc_once;
Y
yuqing 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
	int result;
	int loop_count;
	int aligned_min_size;
	int aligned_max_size;
	int aligned_arg_size;
	rlim_t max_data_size;

	if ((result=init_pthread_lock(&(g_free_queue.lock))) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"init_pthread_lock fail, errno: %d, error info: %s", \
			__LINE__, result, STRERROR(result));
		return result;
	}

	aligned_min_size = MEM_ALIGN(min_buff_size);
	aligned_max_size = MEM_ALIGN(max_buff_size);
	aligned_arg_size = MEM_ALIGN(arg_size);
Y
yuqing 已提交
153 154
	g_free_queue.block_size = ALIGNED_TASK_INFO_SIZE + aligned_arg_size;
	alloc_size = g_free_queue.block_size * init_connections;
Y
yuqing 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
	if (aligned_max_size > aligned_min_size)
	{
		total_size = alloc_size;
		g_free_queue.malloc_whole_block = false;
		max_data_size = 0;
	}
	else
	{
		struct rlimit rlimit_data;

		if (getrlimit(RLIMIT_DATA, &rlimit_data) < 0)
		{
			logError("file: "__FILE__", line: %d, " \
				"call getrlimit fail, " \
				"errno: %d, error info: %s", \
				__LINE__, errno, STRERROR(errno));
			return errno != 0 ? errno : EPERM;
		}
		if (rlimit_data.rlim_cur == RLIM_INFINITY)
		{
Y
yuqing 已提交
175
			max_data_size = MAX_DATA_SIZE;
Y
yuqing 已提交
176 177 178 179
		}
		else
		{
			max_data_size = rlimit_data.rlim_cur;
Y
yuqing 已提交
180
			if (max_data_size > MAX_DATA_SIZE)
Y
yuqing 已提交
181
			{
Y
yuqing 已提交
182
				max_data_size = MAX_DATA_SIZE;
Y
yuqing 已提交
183 184 185
			}
		}

Y
yuqing 已提交
186 187
		if (max_data_size >= (int64_t)(g_free_queue.block_size + aligned_min_size) *
			(int64_t)init_connections)
Y
yuqing 已提交
188 189
		{
			total_size = alloc_size + (int64_t)aligned_min_size *
Y
yuqing 已提交
190
					init_connections;
Y
yuqing 已提交
191
			g_free_queue.malloc_whole_block = true;
Y
yuqing 已提交
192
			g_free_queue.block_size += aligned_min_size;
Y
yuqing 已提交
193 194 195 196 197 198 199 200 201 202
		}
		else
		{
			total_size = alloc_size;
			g_free_queue.malloc_whole_block = false;
			max_data_size = 0;
		}
	}

	g_free_queue.max_connections = max_connections;
203
	g_free_queue.alloc_connections = init_connections;
Y
yuqing 已提交
204 205 206
    if (alloc_task_once <= 0)
    {
        g_free_queue.alloc_task_once = 256;
Y
yuqing 已提交
207 208 209 210 211
		alloc_once = MAX_DATA_SIZE / g_free_queue.block_size;
        if (g_free_queue.alloc_task_once > alloc_once)
        {
            g_free_queue.alloc_task_once = alloc_once;
        }
Y
yuqing 已提交
212 213 214 215 216
    }
    else
    {
        g_free_queue.alloc_task_once = alloc_task_once;
    }
Y
yuqing 已提交
217 218 219 220
	g_free_queue.min_buff_size = aligned_min_size;
	g_free_queue.max_buff_size = aligned_max_size;
	g_free_queue.arg_size = aligned_arg_size;

Y
yuqing 已提交
221 222 223 224 225 226 227 228
	logDebug("file: "__FILE__", line: %d, "
		"max_connections: %d, init_connections: %d, alloc_task_once: %d, "
        "min_buff_size: %d, max_buff_size: %d, block_size: %d, "
        "arg_size: %d, max_data_size: %d, total_size: %"PRId64,
        __LINE__, max_connections, init_connections,
        g_free_queue.alloc_task_once, aligned_min_size, aligned_max_size,
        g_free_queue.block_size, aligned_arg_size, (int)max_data_size, total_size);

Y
yuqing 已提交
229
	if ((!g_free_queue.malloc_whole_block) || (total_size <= max_data_size))
Y
yuqing 已提交
230 231
	{
		loop_count = 1;
Y
yuqing 已提交
232
		mpool = malloc_mpool(total_size);
Y
yuqing 已提交
233 234 235 236
		if (mpool == NULL)
		{
			return errno != 0 ? errno : ENOMEM;
		}
Y
yuqing 已提交
237 238
		g_mpool.head = mpool;
		g_mpool.tail = mpool;
Y
yuqing 已提交
239 240 241 242
	}
	else
	{
		int remain_count;
243
		int alloc_count;
Y
yuqing 已提交
244 245 246
		int current_alloc_size;

		loop_count = 0;
Y
yuqing 已提交
247 248
		remain_count = init_connections;
		alloc_once = max_data_size / g_free_queue.block_size;
Y
yuqing 已提交
249 250
		while (remain_count > 0)
		{
251
			alloc_count = (remain_count > alloc_once) ?
Y
yuqing 已提交
252
					alloc_once : remain_count;
253
			current_alloc_size = g_free_queue.block_size * alloc_count;
Y
yuqing 已提交
254
			mpool = malloc_mpool(current_alloc_size);
Y
yuqing 已提交
255 256 257 258 259 260
			if (mpool == NULL)
			{
				free_queue_destroy();
				return errno != 0 ? errno : ENOMEM;
			}

Y
yuqing 已提交
261
			if (g_mpool.tail == NULL)
Y
yuqing 已提交
262
			{
Y
yuqing 已提交
263
				g_mpool.head = mpool;
Y
yuqing 已提交
264 265 266
			}
			else
			{
Y
yuqing 已提交
267
				g_mpool.tail->next = mpool;
268
				g_mpool.tail->last_block->next = mpool->blocks;  //link previous mpool to current
Y
yuqing 已提交
269
			}
Y
yuqing 已提交
270
            g_mpool.tail = mpool;
Y
yuqing 已提交
271

272
			remain_count -= alloc_count;
Y
yuqing 已提交
273 274 275 276 277 278 279 280 281 282 283
			loop_count++;
		}

		logDebug("file: "__FILE__", line: %d, " \
			"alloc_once: %d", __LINE__, alloc_once);
	}

	logDebug("file: "__FILE__", line: %d, " \
		"malloc task info as whole: %d, malloc loop count: %d", \
		__LINE__, g_free_queue.malloc_whole_block, loop_count);

Y
yuqing 已提交
284
	if (g_mpool.head != NULL)
Y
yuqing 已提交
285
	{
Y
yuqing 已提交
286 287
		g_free_queue.head = g_mpool.head->blocks;
		g_free_queue.tail = g_mpool.tail->last_block;
Y
yuqing 已提交
288 289 290 291 292
	}

	return 0;
}

Y
yuqing 已提交
293 294 295 296 297 298 299
int free_queue_init(const int max_connections, const int min_buff_size,
		const int max_buff_size, const int arg_size)
{
    return free_queue_init_ex(max_connections, max_connections,
        0, min_buff_size, max_buff_size, arg_size);
}

Y
yuqing 已提交
300 301
void free_queue_destroy()
{
Y
yuqing 已提交
302 303
	struct mpool_node *mpool;
	struct mpool_node *mp;
Y
yuqing 已提交
304

Y
yuqing 已提交
305
	if (g_mpool.head == NULL)
Y
yuqing 已提交
306 307 308 309 310 311 312 313 314 315
	{
		return;
	}

	if (!g_free_queue.malloc_whole_block)
	{
		char *p;
		char *pCharEnd;
		struct fast_task_info *pTask;

Y
yuqing 已提交
316
		pCharEnd = ((char *)g_mpool.head->blocks) + g_free_queue.block_size *
317
				g_free_queue.alloc_connections;
Y
yuqing 已提交
318
		for (p=(char *)g_mpool.head->blocks; p<pCharEnd; p += g_free_queue.block_size)
Y
yuqing 已提交
319 320 321 322 323 324 325 326 327 328
		{
			pTask = (struct fast_task_info *)p;
			if (pTask->data != NULL)
			{
				free(pTask->data);
				pTask->data = NULL;
			}
		}
	}

Y
yuqing 已提交
329
	mpool = g_mpool.head;
Y
yuqing 已提交
330 331 332 333 334 335 336 337
	while (mpool != NULL)
	{
		mp = mpool;
		mpool = mpool->next;

		free(mp->blocks);
		free(mp);
	}
Y
yuqing 已提交
338
	g_mpool.head = g_mpool.tail = NULL;
Y
yuqing 已提交
339 340 341 342

	pthread_mutex_destroy(&(g_free_queue.lock));
}

Y
yuqing 已提交
343 344 345 346 347 348
static int free_queue_realloc()
{
	struct mpool_node *mpool;
	struct fast_task_info *head;
	struct fast_task_info *tail;
	int remain_count;
349
	int alloc_count;
Y
yuqing 已提交
350 351 352 353
	int current_alloc_size;

    head = tail = NULL;
	remain_count = g_free_queue.max_connections -
354 355
        g_free_queue.alloc_connections;
    alloc_count = (remain_count > g_free_queue.alloc_task_once) ?
Y
yuqing 已提交
356
        g_free_queue.alloc_task_once : remain_count;
357
    if (alloc_count > 0)
Y
yuqing 已提交
358
	{
359
		current_alloc_size = g_free_queue.block_size * alloc_count;
Y
yuqing 已提交
360 361 362
		mpool = malloc_mpool(current_alloc_size);
		if (mpool == NULL)
		{
Y
yuqing 已提交
363
            return ENOMEM;
Y
yuqing 已提交
364 365 366 367 368 369 370 371 372 373 374 375
		}

		if (g_mpool.tail == NULL)
		{
			g_mpool.head = mpool;
		}
		else
		{
			g_mpool.tail->next = mpool;
		}
        g_mpool.tail = mpool;

376
        head = mpool->blocks;
Y
yuqing 已提交
377 378
        tail = mpool->last_block;

379
		remain_count -= alloc_count;
Y
yuqing 已提交
380
	}
Y
yuqing 已提交
381 382 383
    else {
        return ENOSPC;
    }
Y
yuqing 已提交
384 385 386 387 388 389 390 391 392 393 394

    if (g_free_queue.head == NULL)
    {
        g_free_queue.head = head;
    }
    if (g_free_queue.tail != NULL)
    {
        g_free_queue.tail->next = head;
    }
    g_free_queue.tail = tail;

395
    g_free_queue.alloc_connections += alloc_count;
Y
yuqing 已提交
396

Y
yuqing 已提交
397
	logDebug("file: "__FILE__", line: %d, "
398 399
		"alloc_connections: %d, realloc %d elements", __LINE__,
        g_free_queue.alloc_connections, alloc_count);
Y
yuqing 已提交
400

Y
yuqing 已提交
401
    return 0;
Y
yuqing 已提交
402 403
}

Y
yuqing 已提交
404 405
struct fast_task_info *free_queue_pop()
{
Y
yuqing 已提交
406 407 408 409 410 411
    struct fast_task_info *pTask;
	if ((pTask=task_queue_pop(&g_free_queue)) != NULL)
    {
        return pTask;
    }

412
    if (g_free_queue.alloc_connections >= g_free_queue.max_connections)
Y
yuqing 已提交
413 414 415 416 417
    {
        return NULL;
    }

	pthread_mutex_lock(&g_free_queue.lock);
418
    if (g_free_queue.alloc_connections >= g_free_queue.max_connections)
Y
yuqing 已提交
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
    {
        if (g_free_queue.head == NULL)
        {
            pthread_mutex_unlock(&g_free_queue.lock);
            return NULL;
        }
    }
    else
    {
        if (free_queue_realloc() != 0)
        {
            pthread_mutex_unlock(&g_free_queue.lock);
            return NULL;
        }
    }
	pthread_mutex_unlock(&g_free_queue.lock);

    return task_queue_pop(&g_free_queue);
Y
yuqing 已提交
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
}

int free_queue_push(struct fast_task_info *pTask)
{
	char *new_buff;
	int result;

	*(pTask->client_ip) = '\0';
	pTask->length = 0;
	pTask->offset = 0;
	pTask->req_count = 0;

	if (pTask->size > g_free_queue.min_buff_size) //need thrink
	{
		new_buff = (char *)malloc(g_free_queue.min_buff_size);
		if (new_buff == NULL)
		{
			logWarning("file: "__FILE__", line: %d, " \
				"malloc %d bytes fail, " \
				"errno: %d, error info: %s", \
				__LINE__, g_free_queue.min_buff_size, \
				errno, STRERROR(errno));
		}
		else
		{
			free(pTask->data);
			pTask->size = g_free_queue.min_buff_size;
			pTask->data = new_buff;
		}
	}

	if ((result=pthread_mutex_lock(&g_free_queue.lock)) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"call pthread_mutex_lock fail, " \
			"errno: %d, error info: %s", \
			__LINE__, result, STRERROR(result));
	}

	pTask->next = g_free_queue.head;
	g_free_queue.head = pTask;
	if (g_free_queue.tail == NULL)
	{
		g_free_queue.tail = pTask;
	}

	if ((result=pthread_mutex_unlock(&g_free_queue.lock)) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"call pthread_mutex_unlock fail, " \
			"errno: %d, error info: %s", \
			__LINE__, result, STRERROR(result));
	}

	return result;
}

int free_queue_count()
{
	return task_queue_count(&g_free_queue);
}

499
int free_queue_alloc_connections()
500
{
501
    return g_free_queue.alloc_connections;
502 503
}

Y
yuqing 已提交
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
int task_queue_push(struct fast_task_queue *pQueue, \
		struct fast_task_info *pTask)
{
	int result;

	if ((result=pthread_mutex_lock(&(pQueue->lock))) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"call pthread_mutex_lock fail, " \
			"errno: %d, error info: %s", \
			__LINE__, result, STRERROR(result));
		return result;
	}

	pTask->next = NULL;
	if (pQueue->tail == NULL)
	{
		pQueue->head = pTask;
	}
	else
	{
		pQueue->tail->next = pTask;
	}
	pQueue->tail = pTask;

	if ((result=pthread_mutex_unlock(&(pQueue->lock))) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"call pthread_mutex_unlock fail, " \
			"errno: %d, error info: %s", \
			__LINE__, result, STRERROR(result));
	}

	return 0;
}

struct fast_task_info *task_queue_pop(struct fast_task_queue *pQueue)
{
	struct fast_task_info *pTask;
	int result;

	if ((result=pthread_mutex_lock(&(pQueue->lock))) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"call pthread_mutex_lock fail, " \
			"errno: %d, error info: %s", \
			__LINE__, result, STRERROR(result));
		return NULL;
	}

	pTask = pQueue->head;
	if (pTask != NULL)
	{
		pQueue->head = pTask->next;
		if (pQueue->head == NULL)
		{
			pQueue->tail = NULL;
		}
	}

	if ((result=pthread_mutex_unlock(&(pQueue->lock))) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"call pthread_mutex_unlock fail, " \
			"errno: %d, error info: %s", \
			__LINE__, result, STRERROR(result));
	}

	return pTask;
}

int task_queue_count(struct fast_task_queue *pQueue)
{
	struct fast_task_info *pTask;
	int count;
	int result;

	if ((result=pthread_mutex_lock(&(pQueue->lock))) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"call pthread_mutex_lock fail, " \
			"errno: %d, error info: %s", \
			__LINE__, result, STRERROR(result));
		return 0;
	}

	count = 0;
	pTask = pQueue->head;
	while (pTask != NULL)
	{
		pTask = pTask->next;
		count++;
	}

	if ((result=pthread_mutex_unlock(&(pQueue->lock))) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"call pthread_mutex_unlock fail, " \
			"errno: %d, error info: %s", \
			__LINE__, result, STRERROR(result));
	}

	return count;
}