33 #include <sys/ioctl.h>
35 #include <sys/types.h>
80 #define WRITE_LEN_ALL -1
97 static double htime(
void){
98 static struct timeval t0;
100 struct timeval t1, td;
101 struct timeval carry = { 0, 0 };
104 gettimeofday(&t0, 0);
108 gettimeofday(&t1, 0);
110 if (t1.tv_usec < t0.tv_usec){
111 carry.tv_usec = 1000000;
114 td.tv_usec = t1.tv_usec + carry.tv_usec - t0.tv_usec;
115 td.tv_sec = t1.tv_sec - carry.tv_sec - t0.tv_sec;
117 double tds = td.tv_sec + (double)(td.tv_usec)/1000000;
121 static int write_meta(
int fd,
int ibuf,
int nbuf)
127 "IBUF=%d\n" "NBUF=%d\n" "NSAMPLES=%llu\n" "HTIME=%.3f\n",
129 return write(fd, buf, strlen(buf));
134 struct stat stat_buf;
135 int rc = stat(buf, &stat_buf);
136 DIAG(
"stat:rc %d errno %d\n", rc, errno);
138 err(
"OVERRUN: NO_OVERWRITE SET and \"%s\" exists", buf);
140 }
else if (errno != ENOENT){
141 err(
"OVERRUN: NO_OVERWRITE SET and \"%s\" exists", buf);
149 #define O_MODE (O_WRONLY|O_CREAT|O_TRUNC)
150 #define PERM (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
155 #define OUTROOTFMT "%s/%06d/"
156 #define OUTFMT2 "%s/%06d/%d.%02d"
157 #define OUTFMT3 "%s/%06d/%d.%03d"
172 static char hn[
MAXHN];
173 gethostname(hn,
MAXHN-1);
182 hg(new unsigned[_maxbins]),
185 for (
int ii = 0; ii < maxbins; ++ii){
190 for (
int ii = 0; ii < maxbins; ++ii){
191 fprintf(stderr,
"%5d%c", hg[ii], ii+1 >= maxbins?
'\n':
',');
202 static void writeSBD(
struct StreamBufferDef* sbd,
const char* data_fname)
209 sprintf(buf,
"%s/err.log",
OUTROOT);
210 fp = fopen(buf,
"w");
212 fprintf(stderr,
"ERROR failed to open file \"%s\"\n", buf);
218 fprintf(fp,
"%s,%08x,%08x,%d,%d,%d,%s\n",
232 fprintf(stderr,
"%c",
ibuf==0?
'\r':
'.');
238 fprintf(stderr,
"\r%06d", cycle++);
242 fprintf(stderr,
"%02d\n",
ibuf);
245 static char data_fname[80];
246 static char old_fname[80];
248 static int _ibuf = -1;
255 fprintf(stderr,
"WARNING potential buffer cycle detected %lu/%lu skip %d > %d\n",
258 fprintf(stderr,
"ERROR: buffer %lu/%lu skip %d -> %d\n",
279 writeSBD(sbd, data_fname);
282 strcpy(buf, data_fname);
285 write_meta(out_meta,
ibuf, nbuf);
297 perror(
"write fail");
305 if (strlen(old_fname)){
307 strcpy(
buf4k, old_fname);
315 strcpy(old_fname, data_fname);
328 fprintf(stderr,
"ERROR NOT IBUF_MAGIC %08x %08x\n",
348 perror(
"ioctl RTM_T_START_STREAM failed");
353 DIAG(
"CALLING read\n");
357 DIAG(
" read returned %d\n", nread);
360 int nb = nread/
SBDSZ;
363 int nwrite =
sizeof(int);
366 if (bufno <= ifirst ){
379 DIAG(
"CALLING process\n");
380 process(bufno, ++nbuf, sbd+
ibuf);
382 dbg(2,
"write [%d] %d\n",
ibuf, bufno);
384 DIAG(
"CALLING write\n");
385 if (write(fp, &bufno, nwrite) != nwrite ){
386 perror(
"write failed");
390 printf(
"NBUFS %d reached, quitting now\n",
NBUFS);
395 perror(
"read error");
410 static void run_stop_monitor(
char *monitor)
421 sprintf(arg1,
"%d", ppid);
422 info(
"monitor: %s %s\n", monitor, arg1);
423 int rc = execlp(monitor, basename(monitor), arg1, (
char*)NULL);
426 perror(
"execlp failed");
433 struct sched_param prams = {
436 prams.sched_priority = prio;
438 int rc = sched_setscheduler(0, SCHED_FIFO, &prams);
440 perror(
"sched_setscheduler()");
445 static void init_defaults(
int argc,
char* argv[])
449 if (
getenv(
"RTM_DEVNUM")){
450 devnum = atol(
getenv(
"RTM_DEVNUM"));
457 if (
getenv(
"RTM_MAXITER")){
461 if (
getenv(
"RTM_NELEMS")){
469 if (
getenv(
"RTM_USLEEP")){
473 if (
getenv(
"RTM_VERBOSE")){
477 if (
getenv(
"RTM_PUT_DATA")){
499 if (
getenv(
"NO_OVERWRITE")){
505 if (
getenv(
"PUT4KPERFILE")){
509 if (
getenv(
"OUTPUT_META")){
512 setvbuf(stdout, 0, _IOLBF, 0);
515 if ((monitor =
getenv(
"KILL_ON_STOP")) != 0){
516 run_stop_monitor(monitor);
521 unsigned tb = strtoul(argv[1], 0, 0);
525 err(
"stream-disk [NBUFFERS]");
531 int main(
int argc,
char* argv[])
533 init_defaults(argc, argv);