rpmio/rpmsw.c

Go to the documentation of this file.
00001 
00005 #include "system.h"
00006 #include <rpmsw.h>
00007 #include "debug.h"
00008 
00009 #if defined(__LCLINT__)
00010 /*@-exportheader@*/
00011 extern int nanosleep(const struct timespec *__requested_time,
00012                 /*@out@*/ /*@null@*/ struct timespec *__remaining)
00013         /*@globals errno @*/
00014         /*@modifies *__remaining, errno @*/;
00015 /*@=exportheader@*/
00016 #endif
00017 
00018 /*@unchecked@*/
00019 static rpmtime_t rpmsw_overhead = 0;
00020 
00021 /*@unchecked@*/
00022 static rpmtime_t rpmsw_cycles = 1;
00023 
00024 /*@unchecked@*/
00025 static int rpmsw_initialized = 0;
00026 
00027 rpmsw rpmswNow(rpmsw sw)
00028 {
00029     if (!rpmsw_initialized)
00030         (void) rpmswInit();
00031     if (sw == NULL)
00032         return NULL;
00033     if (gettimeofday(&sw->u.tv, NULL))
00034         return NULL;
00035     return sw;
00036 }
00037 
00044 static inline
00045 rpmtime_t tvsub(/*@null@*/ const struct timeval * etv,
00046                 /*@null@*/ const struct timeval * btv)
00047         /*@*/
00048 {
00049     time_t secs, usecs;
00050     if (etv == NULL  || btv == NULL) return 0;
00051     secs = etv->tv_sec - btv->tv_sec;
00052     for (usecs = etv->tv_usec - btv->tv_usec; usecs < 0; usecs += 1000000)
00053         secs--;
00054     return ((secs * 1000000) + usecs);
00055 }
00056 
00057 rpmtime_t rpmswDiff(rpmsw end, rpmsw begin)
00058 {
00059     unsigned long long ticks = 0;
00060 
00061     if (end == NULL || begin == NULL)
00062         return 0;
00063     ticks = tvsub(&end->u.tv, &begin->u.tv);
00064     if (ticks >= rpmsw_overhead)
00065         ticks -= rpmsw_overhead;
00066     if (rpmsw_cycles > 1)
00067         ticks /= rpmsw_cycles;
00068     return ticks;
00069 }
00070 
00071 rpmtime_t rpmswInit(void)
00072         /*@globals rpmsw_cycles, rpmsw_initialized, rpmsw_overhead,
00073                 rpmsw_type @*/
00074         /*@modifies rpmsw_cycles, rpmsw_initialized, rpmsw_overhead,
00075                 rpmsw_type @*/
00076 {
00077     struct rpmsw_s begin, end;
00078     rpmtime_t sum_overhead = 0;
00079     rpmtime_t cycles;
00080     int i;
00081 
00082     rpmsw_initialized = 1;
00083 
00084     rpmsw_overhead = 0;
00085     rpmsw_cycles = 0;
00086 
00087     /* Convergence for simultaneous cycles and overhead is overkill ... */
00088     for (i = 0; i < 3; i++) {
00089         /* Calculate timing overhead in usecs. */
00090 /*@-uniondef@*/
00091         (void) rpmswNow(&begin);
00092         sum_overhead += rpmswDiff(rpmswNow(&end), &begin);
00093 /*@=uniondef@*/
00094 
00095         rpmsw_overhead = sum_overhead/(i+1);
00096     }
00097 
00098     return rpmsw_overhead;
00099 }
00100 
00101 int rpmswEnter(rpmop op, ssize_t rc)
00102 {
00103     if (op == NULL)
00104         return 0;
00105 
00106     op->count++;
00107     if (rc < 0) {
00108         op->bytes = 0;
00109         op->usecs = 0;
00110     }
00111 /*@-uniondef@*/
00112     (void) rpmswNow(&op->begin);
00113 /*@=uniondef@*/
00114     return 0;
00115 }
00116 
00117 rpmtime_t rpmswExit(rpmop op, ssize_t rc)
00118 {
00119     struct rpmsw_s end;
00120 
00121     if (op == NULL)
00122         return 0;
00123 
00124 /*@-uniondef@*/
00125     op->usecs += rpmswDiff(rpmswNow(&end), &op->begin);
00126 /*@=uniondef@*/
00127     if (rc > 0)
00128         op->bytes += rc;
00129     op->begin = end;    /* structure assignment */
00130     return op->usecs;
00131 }
00132 
00133 rpmtime_t rpmswAdd(rpmop to, rpmop from)
00134 {
00135     rpmtime_t usecs = 0;
00136     if (to != NULL && from != NULL) {
00137         to->count += from->count;
00138         to->bytes += from->bytes;
00139         to->usecs += from->usecs;
00140         usecs = to->usecs;
00141     }
00142     return usecs;
00143 }
00144 
00145 rpmtime_t rpmswSub(rpmop to, rpmop from)
00146 {
00147     rpmtime_t usecs = 0;
00148     if (to != NULL && from != NULL) {
00149         to->count -= from->count;
00150         to->bytes -= from->bytes;
00151         to->usecs -= from->usecs;
00152         usecs = to->usecs;
00153     }
00154     return usecs;
00155 }

Generated on Tue Jan 15 14:32:10 2013 for rpm by  doxygen 1.4.7