Hash :
c0bf63ce
Author :
Date :
2010-12-02T14:13:33
tests: Use relative includes ("") instead of system includes (<>)
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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
/*
* based on test-eof.c
*/
#include "event2/event-config.h"
#ifdef WIN32
#include <winsock2.h>
#include <windows.h>
#else
#include <unistd.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#ifdef _EVENT_HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef _EVENT_HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "event2/event.h"
#include "event2/util.h"
#include <time.h>
struct cpu_usage_timer {
#ifdef WIN32
HANDLE thread;
FILETIME usertimeBegin;
FILETIME kerneltimeBegin;
#else
clock_t ticksBegin;
#endif
struct timeval timeBegin;
};
static void
start_cpu_usage_timer(struct cpu_usage_timer *timer)
{
#ifdef WIN32
int r;
FILETIME createtime, exittime;
timer->thread = GetCurrentThread();
r = GetThreadTimes(timer->thread, &createtime, &exittime,
&timer->usertimeBegin, &timer->kerneltimeBegin);
if (r==0) printf("GetThreadTimes failed.");
#else
timer->ticksBegin = clock();
#endif
evutil_gettimeofday(&timer->timeBegin, NULL);
}
#ifdef WIN32
static ev_int64_t
filetime_to_100nsec(const FILETIME *ft)
{
/* Number of 100-nanosecond units */
ev_int64_t n = ft->dwHighDateTime;
n <<= 32;
n += ft->dwLowDateTime;
return n;
}
static double
filetime_diff(const FILETIME *ftStart, const FILETIME *ftEnd)
{
ev_int64_t s, e, diff;
double r;
s = filetime_to_100nsec(ftStart);
e = filetime_to_100nsec(ftEnd);
diff = e - s;
r = (double) diff;
return r / 1.0e7;
}
#endif
static void
get_cpu_usage(struct cpu_usage_timer *timer, double *secElapsedOut,
double *secUsedOut, double *usageOut)
{
#ifdef WIN32
double usertime_seconds, kerneltime_seconds;
FILETIME createtime, exittime, usertimeEnd, kerneltimeEnd;
int r;
#else
clock_t ticksEnd;
#endif
struct timeval timeEnd, timeDiff;
double secondsPassed, secondsUsed;
#ifdef WIN32
r = GetThreadTimes(timer->thread, &createtime, &exittime,
&usertimeEnd, &kerneltimeEnd);
if (r==0) printf("GetThreadTimes failed.");
usertime_seconds = filetime_diff(&timer->usertimeBegin, &usertimeEnd);
kerneltime_seconds = filetime_diff(&timer->kerneltimeBegin, &kerneltimeEnd);
secondsUsed = kerneltime_seconds + usertime_seconds;
#else
ticksEnd = clock();
secondsUsed = (ticksEnd - timer->ticksBegin) / (double)CLOCKS_PER_SEC;
#endif
evutil_gettimeofday(&timeEnd, NULL);
evutil_timersub(&timeEnd, &timer->timeBegin, &timeDiff);
secondsPassed = timeDiff.tv_sec + (timeDiff.tv_usec / 1.0e6);
*secElapsedOut = secondsPassed;
*secUsedOut = secondsUsed;
*usageOut = secondsUsed / secondsPassed;
}
static void
write_cb(evutil_socket_t fd, short event, void *arg)
{
printf("write callback. should only see this once\n");
/* got what we want remove the event */
event_del(*(struct event**)arg);
/* opps changed my mind add it back again */
event_add(*(struct event**)arg,NULL);
/* not a good day for decisiveness, I really didn't want it after all */
event_del(*(struct event**)arg);
}
static void
timeout_cb(evutil_socket_t fd, short event, void *arg)
{
printf("timeout fired, time to end test\n");
event_del(*(struct event**)arg);
return;
}
int
main(int argc, char **argv)
{
struct event* ev;
struct event* timeout;
struct event_base* base;
evutil_socket_t pair[2];
struct timeval tv;
struct cpu_usage_timer timer;
double usage, secPassed, secUsed;
#ifdef WIN32
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(2, 2);
err = WSAStartup(wVersionRequested, &wsaData);
#endif
if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
return (1);
/* Initalize the event library */
base = event_base_new();
/* Initalize a timeout to terminate the test */
timeout = evtimer_new(base,timeout_cb,&timeout);
/* and watch for writability on one end of the pipe */
ev = event_new(base,pair[1],EV_WRITE | EV_PERSIST, write_cb, &ev);
tv.tv_sec = 5;
tv.tv_usec = 0;
evtimer_add(timeout, &tv);
event_add(ev, NULL);
start_cpu_usage_timer(&timer);
event_base_dispatch(base);
get_cpu_usage(&timer, &secPassed, &secUsed, &usage);
/* attempt to calculate our cpu usage over the test should be
virtually nil */
printf("usec used=%d, usec passed=%d, cpu usage=%.2f%%\n",
(int)(secUsed*1e6),
(int)(secPassed*1e6),
usage*100);
if (usage > 50.0) /* way too high */
return 1;
return 0;
}