1 #include <config.h>
2
3 #include <stdio.h>
4 #include "suck_config.h"
5 #include "timer.h"
6 #include "phrases.h"
7 #include "both.h"
8
9 #ifdef TIMEOUT
10 # if TIME_WITH_SYS_TIME
11 # include <sys/time.h>
12 # include <time.h>
13 # else
14 # if HAVE_SYS_TIME_H
15 # include <sys/time.h>
16 # else
17 # include <time.h>
18 # endif
19 # endif
20 #endif
21
22 #ifdef HAVE_UNISTD_H
23 #include <unistd.h>
24 #endif
25
26 #ifdef DMALLOC
27 #include <dmalloc.h>
28 #endif
29
30 /*internal functions */
31 #ifdef HAVE_GETTIMEOFDAY
32 double get_elapsed(struct timeval *);
33 #endif
34
35 /*-----------------------------------------------------------*/
36 #ifdef HAVE_GETTIMEOFDAY
TimerFunc(int which_function,long nradd,FILE * fpi)37 double TimerFunc(int which_function, long nradd, FILE *fpi) {
38 #else
39 void TimerFunc(int which_function, long nradd, FILE *fpi) {
40 #endif
41
42 #ifdef HAVE_GETTIMEOFDAY
43 static struct timeval start;
44 char strmins[32], strsecs[32], strbps[32];
45 #endif
46 static long nrbytes = 0L; /* just in case */
47 char strbytes[32];
48
49 #ifdef HAVE_GETTIMEOFDAY
50 double elapsed, bps, retval = 0.0;
51 long mins; /* long so can get mins to exact decimal */
52 #endif
53
54 switch(which_function) {
55 case TIMER_START:
56 nrbytes = 0L;
57 #ifdef HAVE_GETTIMEOFDAY
58 gettimeofday(&start, NULL);
59 #endif
60 break;
61 case TIMER_ADDBYTES:
62 nrbytes += nradd;
63 break;
64 case TIMER_GET_BPS:
65 elapsed = get_elapsed(&start);
66 retval = (elapsed > 0.0) ? nrbytes / elapsed : 0.0;
67 break;
68 case TIMER_DISPLAY:
69 #ifdef HAVE_GETTIMEOFDAY
70 if(nrbytes > 0) {
71 elapsed = get_elapsed(&start);
72 bps = (elapsed > 0.0) ? nrbytes / elapsed : 0.0;
73 sprintf(strbps,"%.1f", bps);
74 print_phrases(fpi, timer_phrases[2], strbps, NULL);
75 }
76 #endif
77 break;
78 case TIMER_TIMEONLY:
79 #ifdef HAVE_GETTIMEOFDAY
80 elapsed = get_elapsed(&start);
81 mins = ((long) elapsed) / 60 ; /* get minutes */
82 elapsed -= (mins * 60); /* subtract to get remainder */
83 sprintf(strmins, "%ld", mins);
84 sprintf(strsecs, "%.2f", elapsed);
85 print_phrases(fpi, timer_phrases[0], strmins, strsecs, NULL);
86 #endif
87 break;
88 case TIMER_TOTALS:
89 sprintf(strbytes, "%ld", nrbytes);
90 #ifdef HAVE_GETTIMEOFDAY
91 elapsed = get_elapsed(&start);
92 bps = (elapsed > 0.0 && nrbytes > 0) ? nrbytes / elapsed : 0.0;
93 mins = ((long) elapsed) / 60 ; /* get minutes */
94 elapsed -= (mins * 60); /* subtract to get remainder */
95 sprintf(strmins, "%ld", mins);
96 sprintf(strsecs, "%.2f", elapsed);
97 sprintf(strbps, "%.1f", bps);
98 print_phrases(fpi, timer_phrases[1], strbytes, strmins, strsecs, strbps, NULL);
99 #else
100 print_phrases(fpi, timer_phrases[3], strbytes, NULL);
101 #endif
102 break;
103 default:
104 /* ignore invalid commands */
105 break;
106 }
107 #ifdef HAVE_GETTIMEOFDAY
108 return retval;
109 #else
110 return;
111 #endif
112 }
113 /*-----------------------------------------------------------------------------------*/
114 #ifdef HAVE_GETTIMEOFDAY
115 double get_elapsed(struct timeval *start) {
116 struct timeval curr;
117 double elapsed;
118
119 /* compute elapsed time, in seconds */
120 gettimeofday(&curr, NULL);
121 elapsed = curr.tv_sec - start->tv_sec;
122 elapsed += (((double) (curr.tv_usec - start->tv_usec)) / 1000000.0);
123
124 return elapsed;
125 }
126 #endif
127