1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
/*----------------------------------------------------------------------------*/
/* Xymon monitor library. */
/* */
/* This is a library module, part of libxymon. */
/* It contains routines for timing program execution. */
/* */
/* Copyright (C) 2002-2011 Henrik Storner <henrik@storner.dk> */
/* */
/* This program is released under the GNU General Public License (GPL), */
/* version 2. See the file "COPYING" for details. */
/* */
/*----------------------------------------------------------------------------*/
static char rcsid[] = "$Id: timing.c 8069 2019-07-23 15:29:06Z jccleaver $";
#include <unistd.h> // For POSIX timer definitions
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <stdio.h>
#include "libxymon.h"
int timing = 0;
typedef struct timestamp_t {
char *eventtext;
struct timespec eventtime;
struct timestamp_t *prev;
struct timestamp_t *next;
} timestamp_t;
static timestamp_t *stamphead = NULL;
static timestamp_t *stamptail = NULL;
time_t gettimer(void)
{
int res;
struct timespec t;
#if (_POSIX_TIMERS > 0) && defined(_POSIX_MONOTONIC_CLOCK)
res = clock_gettime(CLOCK_MONOTONIC, &t);
if(-1 == res)
{
res = clock_gettime(CLOCK_REALTIME, &t);
if(-1 == res)
{
return time(NULL);
}
}
return (time_t) t.tv_sec;
#else
return time(NULL);
#endif
}
void getntimer(struct timespec *tp)
{
struct timeval t;
struct timezone tz;
int res;
#if (_POSIX_TIMERS > 0) && defined(_POSIX_MONOTONIC_CLOCK)
res = clock_gettime(CLOCK_MONOTONIC, tp);
if(-1 == res)
{
res = clock_gettime(CLOCK_REALTIME, tp);
if(-1 != res) return;
/* Fall through to use gettimeofday() */
}
#endif
gettimeofday(&t, &tz);
tp->tv_sec = t.tv_sec;
tp->tv_nsec = 1000*t.tv_usec;
}
void add_timestamp(const char *msg)
{
if (timing) {
timestamp_t *newstamp = (timestamp_t *) malloc(sizeof(timestamp_t));
getntimer(&newstamp->eventtime);
newstamp->eventtext = strdup(msg);
if (stamphead == NULL) {
newstamp->next = newstamp->prev = NULL;
stamphead = newstamp;
}
else {
newstamp->prev = stamptail;
newstamp->next = NULL;
stamptail->next = newstamp;
}
stamptail = newstamp;
}
}
int ntimerus(struct timespec *start, struct timespec *now)
{
struct timespec tdiff;
/* See how long the query took */
if (now) {
memcpy(&tdiff, now, sizeof(struct timespec));
}
else {
getntimer(&tdiff);
}
if (tdiff.tv_nsec < start->tv_nsec) {
tdiff.tv_sec--;
tdiff.tv_nsec += 1000000000;
}
tdiff.tv_sec -= start->tv_sec;
tdiff.tv_nsec -= start->tv_nsec;
return (tdiff.tv_sec*1000000 + tdiff.tv_nsec/1000);
}
void show_timestamps(char **buffer)
{
timestamp_t *s;
struct timespec dif;
SBUF_DEFINE(outbuf);
char buf1[80];
if (!timing || (stamphead == NULL)) return;
MEMDEFINE(buf1);
SBUF_MALLOC(outbuf, 4096);
strncpy(outbuf, "\n\nTIME SPENT\n", outbuf_buflen);
strncat(outbuf, "Event ", (outbuf_buflen - strlen(outbuf)));
strncat(outbuf, " Start time", (outbuf_buflen - strlen(outbuf)));
strncat(outbuf, " Duration\n", (outbuf_buflen - strlen(outbuf)));
for (s=stamphead; (s); s=s->next) {
snprintf(buf1, sizeof(buf1), "%-40s ", s->eventtext);
strncat(outbuf, buf1, (outbuf_buflen - strlen(outbuf)));
snprintf(buf1, sizeof(buf1), "%10u.%06u ", (unsigned int)s->eventtime.tv_sec, (unsigned int)s->eventtime.tv_nsec / 1000);
strncat(outbuf, buf1, (outbuf_buflen - strlen(outbuf)));
if (s->prev) {
tvdiff(&((timestamp_t *)s->prev)->eventtime, &s->eventtime, &dif);
snprintf(buf1, sizeof(buf1), "%10u.%06u ", (unsigned int)dif.tv_sec, (unsigned int)dif.tv_nsec / 1000);
strncat(outbuf, buf1, (outbuf_buflen - strlen(outbuf)));
}
else strncat(outbuf, " -", (outbuf_buflen - strlen(outbuf)));
strncat(outbuf, "\n", (outbuf_buflen - strlen(outbuf)));
if ((outbuf_buflen - strlen(outbuf)) < 200) {
SBUF_REALLOC(outbuf, outbuf_buflen + 4096);
}
}
tvdiff(&stamphead->eventtime, &stamptail->eventtime, &dif);
snprintf(buf1, sizeof(buf1), "%-40s ", "TIME TOTAL"); strncat(outbuf, buf1, (outbuf_buflen - strlen(outbuf)));
snprintf(buf1, sizeof(buf1), "%-18s", ""); strncat(outbuf, buf1, (outbuf_buflen - strlen(outbuf)));
snprintf(buf1, sizeof(buf1), "%10u.%06u ", (unsigned int)dif.tv_sec, (unsigned int)dif.tv_nsec / 1000); strncat(outbuf, buf1, (outbuf_buflen - strlen(outbuf)));
strncat(outbuf, "\n", (outbuf_buflen - strlen(outbuf)));
if (buffer == NULL) {
printf("%s", outbuf);
xfree(outbuf);
}
else *buffer = outbuf;
MEMUNDEFINE(buf1);
}
long total_runtime(void)
{
if (timing)
return (stamptail->eventtime.tv_sec - stamphead->eventtime.tv_sec);
else
return 0;
}
|