9 static rpmtime_t rpmsw_overhead = 0;
11 static rpmtime_t rpmsw_cycles = 1;
13 static int rpmsw_initialized = 0;
15 rpmsw rpmswNow(rpmsw sw)
17 if (!rpmsw_initialized)
21 if (gettimeofday(&sw->u.tv, NULL))
27 * Return difference of 2 timeval stamps in micro-seconds.
28 * @param *etv end timeval
29 * @param *btv begin timeval
30 * @return difference in milli-seconds
33 rpmtime_t tvsub(const struct timeval * etv,
34 const struct timeval * btv)
37 if (etv == NULL || btv == NULL) return 0;
38 secs = etv->tv_sec - btv->tv_sec;
39 for (usecs = etv->tv_usec - btv->tv_usec; usecs < 0; usecs += 1000000)
41 return ((secs * 1000000) + usecs);
44 rpmtime_t rpmswDiff(rpmsw end, rpmsw begin)
46 unsigned long long ticks = 0;
48 if (end == NULL || begin == NULL)
50 ticks = tvsub(&end->u.tv, &begin->u.tv);
51 if (ticks >= rpmsw_overhead)
52 ticks -= rpmsw_overhead;
54 ticks /= rpmsw_cycles;
58 rpmtime_t rpmswInit(void)
60 struct rpmsw_s begin, end;
61 rpmtime_t sum_overhead = 0;
64 rpmsw_initialized = 1;
69 /* Convergence for simultaneous cycles and overhead is overkill ... */
70 for (i = 0; i < 3; i++) {
71 /* Calculate timing overhead in usecs. */
72 (void) rpmswNow(&begin);
73 sum_overhead += rpmswDiff(rpmswNow(&end), &begin);
75 rpmsw_overhead = sum_overhead/(i+1);
78 return rpmsw_overhead;
81 int rpmswEnter(rpmop op, ssize_t rc)
91 (void) rpmswNow(&op->begin);
95 rpmtime_t rpmswExit(rpmop op, ssize_t rc)
102 op->usecs += rpmswDiff(rpmswNow(&end), &op->begin);
105 op->begin = end; /* structure assignment */
109 rpmtime_t rpmswAdd(rpmop to, rpmop from)
112 if (to != NULL && from != NULL) {
113 to->count += from->count;
114 to->bytes += from->bytes;
115 to->usecs += from->usecs;
121 rpmtime_t rpmswSub(rpmop to, rpmop from)
124 if (to != NULL && from != NULL) {
125 to->count -= from->count;
126 to->bytes -= from->bytes;
127 to->usecs -= from->usecs;