poll test works @high speed

This commit is contained in:
2026-02-09 22:00:51 +03:00
parent c0c36388db
commit 9e84306a94

View File

@@ -24,7 +24,8 @@
#include <sys/ioctl.h>
#include <usefull_macros.h>
#define XYBUFSZ (2048)
// suppose that we ONLY poll data
#define XYBUFSZ (128)
struct{
int help;
@@ -79,66 +80,29 @@ static int op(const char *nm){
return fd;
}
static int eolcnt(buf_t *buf){
if(!buf) return -1;
int cnt = 0;
for(int i = 0; i < buf->len; ++i)
if(buf->buf[i] == '\n') ++cnt;
return cnt;
}
// move last record (if any) into head of buffer
static void movelast(buf_t *buf){
FNAME();
if(!buf) return;
DBG("buf was: %s", buf->buf);
int cnt = eolcnt(buf);
char *E = strrchr(buf->buf, '\n');
int idx = -1;
if(E){
idx = ++E - buf->buf; // position of symbol after '\n'
}else{
buf->len = strlen(buf->buf);
DBG("leave as is (%s)", buf->buf);
return;
}
DBG("cnt=%d, idx=%d", cnt, idx);
switch(cnt){
case 0: // EOL not found - clear buf
buf->len = 0;
break;
case 1: // only one record - move all after '\n'
if(idx > 0 && idx < XYBUFSZ){
buf->len = XYBUFSZ - idx;
memmove(buf->buf, E, buf->len);
}else buf->len = 0;
break;
default: // more than one record - move
{
int i = idx - 2;
for(; i > -1; --i)
if(buf->buf[i] == '\n') break;
++i;
buf->len = XYBUFSZ - i;
memmove(buf->buf, &buf->buf[i], buf->len);
}
}
buf->buf[buf->len] = 0;
DBG("MOVED; now buf[%d]=%s", buf->len, buf->buf);
}
// write to buffer next data portion; return FALSE in case of error
static int readstrings(buf_t *buf, int fd){
FNAME();
if(!buf){WARNX("Empty buffer"); return FALSE;}
int L = XYBUFSZ - buf->len;
if(L == 0){
DBG("len: %d", buf->len);
movelast(buf);
DBG("buffer overfull!", buf->len);
char *lastn = strrchr(buf->buf, '\n');
if(lastn){
fprintf(stderr, "BUFOVR: _%s_", buf->buf);
++lastn;
buf->len = XYBUFSZ - (lastn - buf->buf);
DBG("Memmove %d", buf->len);
memmove(lastn, buf->buf, buf->len);
buf->buf[buf->len] = 0;
}else buf->len = 0;
L = XYBUFSZ - buf->len;
}
int got = read(fd, &buf->buf[buf->len], L);
if(got < 0){
WARN("read()");
return FALSE;
}else if(got == 0) return TRUE;
}else if(got == 0){ DBG("NO data"); return TRUE; }
buf->len += got;
buf->buf[buf->len] = 0;
DBG("buf[%d]: %s", buf->len, buf->buf);
@@ -146,28 +110,22 @@ static int readstrings(buf_t *buf, int fd){
}
// return TRUE if got, FALSE if no data found
static int getdata(buf_t *buf, long *out){
if(!buf) return -1;
if(!buf || buf->len < 1) return FALSE;
// read record between last '\n' and previous (or start of string)
int cnt = eolcnt(buf);
if(cnt < 1) return FALSE;
char *last = strrchr(buf->buf, '\n');
if(!last) return FALSE; // WTF?
char *last = &buf->buf[buf->len - 1];
//DBG("buf: _%s_", buf->buf);
if(*last != '\n') return FALSE;
*last = 0;
char *prev = buf->buf;
if(cnt > 1) prev = strrchr(buf->buf, '\n') + 1;
if(!prev) prev = buf->buf; // ??
if(out) *out = atol(prev);
int l = strlen(++last);
if(l < XYBUFSZ){
buf->len = l;
if(l){
memmove(buf->buf, last, l);
DBG("moved: %s", buf->buf);
}else DBG("empty line");
}else{
buf->len = 0;
DBG("buffer clear");
//DBG("buf: _%s_", buf->buf);
char *prev = strrchr(buf->buf, '\n');
if(!prev) prev = buf->buf;
else{
fprintf(stderr, "MORETHANONE: _%s_", buf->buf);
++prev; // after last '\n'
}
if(out) *out = atol(prev);
// clear buffer
buf->len = 0;
return TRUE;
}
// try to write '\n' asking new data portion; return FALSE if failed
@@ -177,7 +135,7 @@ static int asknext(int fd){
int i = 0;
for(; i < 5; ++i){
int l = write(fd, "\n", 1);
DBG("l=%d", l);
//DBG("l=%d", l);
if(1 == l) return TRUE;
usleep(100);
}
@@ -204,14 +162,16 @@ int main(int argc, char **argv){
t0x = t0y = tstart = sl_dtime();
DBG("Start");
do{ // main cycle
if(poll(pfds, 2, 1) < 0){
if(poll(pfds, 2, 0) < 0){
WARN("poll()");
break;
}
if(pfds[0].revents && POLLIN){
DBG("got X");
if(!readstrings(&xbuf, Xfd)) break;
}
if(pfds[1].revents && POLLIN){
DBG("got Y");
if(!readstrings(&ybuf, Yfd)) break;
}
double curt = sl_dtime();