Exemple #1
0
static void
read_lines(mux_ctx_t ctx)
{
	while (moar_ticks_p(ctx)) {
		struct ff_msg_s msg[1];

		if (read_line(ctx, msg) && parse_line(ctx, msg) >= 0) {
			write_tick(ctx, msg);
		}
	}
	return;
}
Exemple #2
0
static void
proc_l1bi5(mux_ctx_t ctx)
{
	union {
		struct dc_s bin[1];
		struct dqbi5_s bi5[2];
	} buf[1];
	ssize_t nrd;
	int fd = ctx->infd;

	/* read a probe */
	if (UNLIKELY((nrd = read(fd, buf->bi5, sizeof(buf->bi5))) <= 0)) {
		return;
	} else if (UNLIKELY(nrd == sizeof(*buf->bi5))) {
		/* only one record then, just go for bi5 format,
		 * make use of the tick compressor and dupe the record
		 * then just proceed normally */
		memcpy(buf->bi5 + 1, buf->bi5 + 0, sizeof(*buf->bi5));
	} else if (UNLIKELY((size_t)nrd < sizeof(buf->bi5))) {
		/* oooh incomplete innit? */
		return;
	}
	/* the only thing we can make assumptions about is the timestamp
	 * we check the two stamps in bi5 and compare their distance */
	{
		uint32_t ts0 = be32toh(buf->bi5[0].ts);
		uint32_t ts1 = be32toh(buf->bi5[1].ts);

		if (ts1 - ts0 > 60/*min*/ * 60/*sec*/ * 1000/*msec*/) {
			/* definitely old_fmt */
			goto old_fmt;
		}

		/* quickly polish the probe */
		buf->bi5[0].ts = ts0;
		buf->bi5[0].ap = be32toh(buf->bi5[0].ap);
		buf->bi5[0].bp = be32toh(buf->bi5[0].bp);
		buf->bi5[0].aq.i = be32toh(buf->bi5[0].aq.i);
		buf->bi5[0].bq.i = be32toh(buf->bi5[0].bq.i);

		buf->bi5[1].ts = ts1;
		buf->bi5[1].ap = be32toh(buf->bi5[1].ap);
		buf->bi5[1].bp = be32toh(buf->bi5[1].bp);
		buf->bi5[1].aq.i = be32toh(buf->bi5[1].aq.i);
		buf->bi5[1].bq.i = be32toh(buf->bi5[1].bq.i);
	}
	/* re-use the probe data */
	write_tick_bi5(ctx, buf->bi5 + 0);
	/* main loop */
	do {
		write_tick_bi5(ctx, buf->bi5 + 1);
	} while (rd1bi5(fd, buf->bi5 + 1));
	return;
old_fmt:
	/* polish the probe */
	buf->bin->ts = be64toh(buf->bin->ts);
	buf->bin->ap.i = be64toh(buf->bin->ap.i);
	buf->bin->bp.i = be64toh(buf->bin->bp.i);
	buf->bin->aq.i = be64toh(buf->bin->aq.i);
	buf->bin->bq.i = be64toh(buf->bin->bq.i);
	/* main loop */
	do {
		write_tick(ctx, buf->bin);
	} while (rd1(fd, buf->bin));
	return;
}
void DatabaseWork::run()
{
	DBWORK_LOG( "init(), real thread id = %d, logicid: %d", thread_id_, cur_thread_idx_) ;

	DBTask* db_task = NULL;
    /*! 统计每分钟的处理情况 */
    unsigned long task_count = 0;
    unsigned long last_count_tick = msec();
    unsigned long max_task_count = 0;

	/*! 当任务队列中任务指针为NULL时,表示结束该线程*/
    list_head cur_task_link;
    int b_exit = 0;
    while( true ) 
    {

        int ret = -1;
        INIT_LIST_HEAD( &cur_task_link );
          
        do 
        {
             ret = get_dbtask_list( cur_task_link );
             // 如果没有取到任务,就尝试执行,然后等待下次取任务
             if( ret < 0 )
             {
                 usleep(0); 
             }
        }while( ret < 0 );

        while( !list_empty( &cur_task_link ) ) 
        {
            list_head *pos = cur_task_link.next;
            list_del( pos );
            db_task = list_entry( pos, DBTask, link_ );
            // 用智能指正保证这个循环过后,DBTask一定会被delete
            // TODO:把delete的位置移到database_mng中,可以使用线程不安全的内存池,可能会快很多
            //      这样的话需要将db_task以链表的形式传会database_mng
            std::unique_ptr<DBTask> ptr( db_task ); 
            task_count++;

            int op_type = db_task->get_optype();
            if( MAX_DB_OP <= op_type )
            {
                DBWORK_ERR("Unknown msg from GameServer : %08x", op_type );
                continue;
            }

            /*! 如果是一个退出task将会退出这个线程 */
            if ( db_task->get_optype() == DBOP_EXIT ) 
            {
                b_exit = 1;
                DBWORK_LOG("is terminal task , real thread id = %d, logicid: %d", thread_id_, cur_thread_idx_) ;
                break;
            }

            // 找到函数,然后执行之
            DBOP_FUN pfn = func_map[packet_type];
            ( this->*( pfn ) )( db_task );
        }

        /*!写线程统计日志 */
        unsigned long diff = msec() - last_count_tick ;
        if( diff >= THREAD_LOG_PER_MS ) {

            if( task_count > max_task_count )
                max_task_count = task_count;

            unsigned long task_size = get_dbtask_left_num();
            write_tick( "[DBWORK_PROCESS] db thread id = %d process db_task_num = %ld use_time = %ld (ms), max_task_count = %ld , left_num  = %ld ", 
                            cur_thread_idx_, task_count, diff, max_task_count, task_size);

            task_count = 0; 
            last_count_tick = msec();
        }

        usleep(0); 
        if( b_exit )
            break;
    }

    DataBase::thread_end();
}