Coder Social home page Coder Social logo

kfifo-benchmark's Introduction

kfifo-benchmark

A benchmark showing that kfifo is really super fast

Because kfifo is porting from linux kernel, this repo also uses GPLv2.

kfifo-benchmark's People

Contributors

airekans avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

kfifo-benchmark's Issues

kfifo自己实现的版本不是可靠,这是核心代码。

#define membarrier(arg...) 				__sync_synchronize(##arg)//useless for lockfree.
// 向缓冲区中存放数据
static uint32_t 
__RingQuePush(RingQueHdwl *hdwl, void *buff, uint32_t size)
{
    assert( NULL != hdwl && NULL != buff);
	uint32_t left = hdwl->m_qcap - (hdwl->m_wpos-hdwl->m_rpos);
	if (left < size) {
		//fprintf(stderr, "space is not enough! left:%d < size:%d\n", left, size);
		return 0;//防止存入不完整数据.
	}
    size = MIN(size, hdwl->m_qcap - hdwl->m_wpos + hdwl->m_rpos);
	membarrier();
    /* first put the data starting from fifo->m_wpos to buff end */
    uint32_t len  = MIN(size, hdwl->m_qcap - (hdwl->m_wpos & (hdwl->m_qcap - 1)));
    memcpy(hdwl->m_buff + (hdwl->m_wpos & (hdwl->m_qcap - 1)), buff, len);
    /* then put the rest (if any) at the beginning of the buff */
    memcpy(hdwl->m_buff, buff + len, size - len);
	membarrier();
    hdwl->m_wpos += size;
    return size;
}

// 从缓冲区中取数据
static uint32_t 
__RingQuePopd(RingQueHdwl *hdwl, void * buff, uint32_t size)
{
    assert( NULL != hdwl && NULL != buff);	
	
	memset(buff, 0, size);
    size = MIN(size, hdwl->m_wpos - hdwl->m_rpos);
	membarrier();
    /* first get the data from fifo->m_rpos until the end of the buff */
    uint32_t len = MIN(size, hdwl->m_qcap - (hdwl->m_rpos & (hdwl->m_qcap - 1)));
    memcpy(buff, hdwl->m_buff + (hdwl->m_rpos & (hdwl->m_qcap - 1)), len);
    /* then get the rest (if any) from the beginning of the buff */
    memcpy(buff + len, hdwl->m_buff, size - len);
	membarrier();
    hdwl->m_rpos += size;
    return size;
}

RingQueHdwl*  ringHdw = NULL;
void *test_func1(void *arg)  {
	int count =0, push_count=0;
	while(1) {
		if (RingQueNull(ringHdw)) {
			if (push_count >= 1000000)
				break;
			char buff[4] = "1234";
			uint32_t len = RingQuePush(ringHdw, buff, sizeof(buff));
			if (len>0){
				push_count++;
				//printf("-->>[%d]push %d(exp=%d) bytes\n", push_count, len, sizeof(buff));
			}
		}		
	}
	//usleep(100*1000);
	printf("@@@>%d=push> null=%d,full=%d,size=%u\n",push_count,RingQueNull(ringHdw),RingQueFull(ringHdw),RingQueSize(ringHdw));
	return NULL;
}
void *test_func2(void *arg)  {
	int count =0, pop_count=0;
	while(1) {
		if (!RingQueNull(ringHdw)) {			
			char buff[5] = "8888";
			uint32_t len = RingQuePopd(ringHdw, buff, sizeof(buff)-1);
			if (len>0) {
				pop_count++;
				//printf("-->>[%d]popd %d(exp=%d) bytes: %s\n", pop_count, len, sizeof(buff),buff);
			}
			if (pop_count >= 1000000)
				break;
			else {
				if (pop_count >1000000-10)
				printf("pop_count=%d\n",pop_count);
			}
		}
	}
	printf("@@@>%d=popd> null=%d,full=%d,size=%u\n",pop_count, RingQueNull(ringHdw),RingQueFull(ringHdw),RingQueSize(ringHdw));
	return NULL;
}
void test_ring(void )  
{  
    ringHdw = RingQueInit(NULL, 15, 1);
	membarrier();
	pthread_t id[2];  
	pthread_create(&id[0],NULL,test_func1,NULL);
	pthread_create(&id[1],NULL,test_func2,NULL);  
	int i=0;
	for(i=0;i<2;++i){  
		pthread_join(id[i],NULL);  
	}  
}  

大致逻辑如上,发现push的次数和pop不一致,加上锁后一切正常,是不是gcc屏障这么用不行?
大神,请帮忙看下原因吧。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.