A benchmark showing that kfifo is really super fast
Because kfifo is porting from linux kernel, this repo also uses GPLv2.
A benchmark showing that kfifo is really super fast
License: GNU General Public License v2.0
#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屏障这么用不行?
大神,请帮忙看下原因吧。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.