Commit db931ce6 authored by 's avatar
Browse files

Added abw_next_timestamp() to compute time of next getting of results


git-svn-id: file:///home/svn/mapi/trunk@329 8d5bb341-7cf1-0310-8cf6-ba355fef3186
parent 60bcf59a
......@@ -9,8 +9,8 @@ LIB_DIRS=
RLIB_DIRS=
INCLUDE_DIRS=
TARGETS=abw abw_dummy
OBJS=abw.o abw_mysql.o abw_dummy.o
TARGETS=abw abw_dummy test_abw_time
OBJS=abw.o abw_mysql.o abw_time.o abw_dummy.o test_abw_time.o
all: $(TARGETS)
......@@ -21,12 +21,15 @@ all: $(TARGETS)
# $(AR) rv $@ $(LIBSCAMPI_OBJS)
# $(RANLIB) $@
abw: abw.o abw_mysql.o
$(LD) -o $@ abw.o abw_mysql.o $(LIB_DIRS) $(LIBS) -Wl,-rpath,$(RLIB_DIRS)
abw: abw.o abw_mysql.o abw_time.o
$(LD) -o $@ abw.o abw_mysql.o abw_time.o $(LIB_DIRS) $(LIBS) -Wl,-rpath,$(RLIB_DIRS)
abw_dummy: abw_dummy.o
$(LD) -o $@ abw_dummy.o $(LIB_DIRS) $(LIBS) -Wl,-rpath,$(RLIB_DIRS)
test_abw_time: test_abw_time.o abw_time.o
$(LD) -o $@ test_abw_time.o abw_time.o $(LIB_DIRS) $(LIBS) -Wl,-rpath,$(RLIB_DIRS)
clean:
rm -f *.o *.a $(TARGETS)
rm -f *.out core
......@@ -265,6 +265,7 @@ int main(int argc, char *argv[])
direct_flow=0;
while ((opt=getopt(argc, argv, ARGS)) != -1) {
printf("%s: option: %c\n", __func__, opt);
switch(opt) {
case 'f':
if (direct_flow) {
......@@ -334,13 +335,20 @@ int main(int argc, char *argv[])
fprintf(stderr, "%s: link installed bandwidth must be positive\n", __func__);
exit(-1);
}
break;
case 'q':
global_spec.no_stdout=1;
break;
case 'b':
global_spec.no_mysql=1;
break;
case ':':
if (optopt=='q') {
global_spec.no_stdout=1;
break;
}
else if (optopt=='b') {
global_spec.no_mysql=1;
break;
}
else {
usage();
exit(-1);
}
case 'h':
usage();
exit(0);
......@@ -366,6 +374,9 @@ int main(int argc, char *argv[])
exit(-1);
}
printf("%s: no_stdout: %d, no_mysql: %d\n", __func__,
global_spec.no_stdout, global_spec.no_mysql);
/* Remaining command-line arguments form a header filter string */
for (i=optind; i<argc; i++) {
......
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "abw.h"
#include "abw_time.h"
int abw_next_timestamp(struct timeval *interval,
struct timeval *next,
struct timeval *wait) {
struct timeval current;
static struct timeval last;
if (interval==NULL ||
interval->tv_sec<0 || interval->tv_usec<0 ||
(interval->tv_sec==0 && interval->tv_usec==0) ||
interval->tv_usec>=1000000) {
fprintf(stderr, "%s: incorrect arguments\n", __func__);
return -1;
}
if (gettimeofday(&current, NULL)<0) {
fprintf(stderr, "%s: gettimeofday() failed\n", __func__);
return -1;
}
if (last.tv_sec) { /* this is not a first timestamp to compute */
last.tv_sec+=interval->tv_sec;
last.tv_usec+=interval->tv_usec;
}
/* If this is the first timestamp to compute or if the computed
timestamp is in the past then compute a new timestamp as the
earliest future time rounded to given interval */
if (!last.tv_sec ||
last.tv_sec < current.tv_sec ||
(last.tv_sec == current.tv_sec && last.tv_usec < current.tv_usec)) {
if (interval->tv_sec)
last.tv_sec=(current.tv_sec / interval->tv_sec * interval->tv_sec)
+ interval->tv_sec;
else
last.tv_sec=current.tv_sec;
if (interval->tv_usec)
last.tv_usec=(current.tv_usec / interval->tv_usec * interval->tv_usec)
+ interval->tv_usec;
else
last.tv_usec=0;
}
if (last.tv_usec>=1000000) {
last.tv_sec++;
last.tv_usec-=1000000;
}
if (next!=NULL) {
next->tv_sec=last.tv_sec;
next->tv_usec=last.tv_usec;
}
if (wait!=NULL) {
if ((last.tv_sec - current.tv_sec) > 1) {
wait->tv_sec=last.tv_sec - current.tv_sec - 1;
wait->tv_usec=(1000000-current.tv_usec) + last.tv_usec;
}
else if (last.tv_sec > current.tv_sec) {
wait->tv_sec=0;
wait->tv_usec=(1000000-current.tv_usec) + last.tv_usec;
}
else {
wait->tv_sec=0;
wait->tv_usec=last.tv_usec - current.tv_usec;
}
if (wait->tv_usec>=1000000) {
wait->tv_sec++;
wait->tv_usec-=1000000;
}
}
return -1;
} /* abw_next_timestamp() */
int abw_next_timestamp(struct timeval *interval,
struct timeval *next,
struct timeval *wait);
#include <stdio.h>
#include <sys/time.h>
#include <stdlib.h>
#include <unistd.h>
#include "abw_time.h"
int main(int argc, char *argv[]) {
struct timeval interval, next, wait;
if (argc!=3)
return -1;
interval.tv_sec=atoi(argv[1]);
interval.tv_usec=atoi(argv[2]);
printf("interval.tv_sec: %d, interval.tv_usec: %d\n",
(int)(interval.tv_sec), (int)(interval.tv_usec));
while (1) {
abw_next_timestamp(&interval, &next, &wait);
printf("next.tv_sec: %d, next.tv_usec: %d, wait.tv_sec: %d, wait.tv_usec: %d\n", (int)(next.tv_sec), (int)(next.tv_usec), (int)(wait.tv_sec), (int)(wait.tv_usec));
usleep(wait.tv_sec * 1000000 + wait.tv_usec);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment