2 * Copyright (c) 2010-2012 Niels Provos and Nick Mathewson
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "event2/event-config.h"
35 #include <sys/types.h>
37 #ifdef EVENT__HAVE_SYS_TIME_H
41 #ifdef EVENT__HAVE_SYS_SOCKET_H
42 #include <sys/socket.h>
50 #include "event2/event.h"
51 #include "event2/util.h"
54 struct cpu_usage_timer {
57 FILETIME usertimeBegin;
58 FILETIME kerneltimeBegin;
62 struct timeval timeBegin;
65 start_cpu_usage_timer(struct cpu_usage_timer *timer)
69 FILETIME createtime, exittime;
70 timer->thread = GetCurrentThread();
71 r = GetThreadTimes(timer->thread, &createtime, &exittime,
72 &timer->usertimeBegin, &timer->kerneltimeBegin);
73 if (r==0) printf("GetThreadTimes failed.");
75 timer->ticksBegin = clock();
78 evutil_gettimeofday(&timer->timeBegin, NULL);
82 filetime_to_100nsec(const FILETIME *ft)
84 /* Number of 100-nanosecond units */
85 ev_int64_t n = ft->dwHighDateTime;
87 n += ft->dwLowDateTime;
91 filetime_diff(const FILETIME *ftStart, const FILETIME *ftEnd)
93 ev_int64_t s, e, diff;
95 s = filetime_to_100nsec(ftStart);
96 e = filetime_to_100nsec(ftEnd);
104 get_cpu_usage(struct cpu_usage_timer *timer, double *secElapsedOut,
105 double *secUsedOut, double *usageOut)
108 double usertime_seconds, kerneltime_seconds;
109 FILETIME createtime, exittime, usertimeEnd, kerneltimeEnd;
114 struct timeval timeEnd, timeDiff;
115 double secondsPassed, secondsUsed;
118 r = GetThreadTimes(timer->thread, &createtime, &exittime,
119 &usertimeEnd, &kerneltimeEnd);
120 if (r==0) printf("GetThreadTimes failed.");
121 usertime_seconds = filetime_diff(&timer->usertimeBegin, &usertimeEnd);
122 kerneltime_seconds = filetime_diff(&timer->kerneltimeBegin, &kerneltimeEnd);
123 secondsUsed = kerneltime_seconds + usertime_seconds;
126 secondsUsed = (ticksEnd - timer->ticksBegin) / (double)CLOCKS_PER_SEC;
128 evutil_gettimeofday(&timeEnd, NULL);
129 evutil_timersub(&timeEnd, &timer->timeBegin, &timeDiff);
130 secondsPassed = timeDiff.tv_sec + (timeDiff.tv_usec / 1.0e6);
132 *secElapsedOut = secondsPassed;
133 *secUsedOut = secondsUsed;
134 *usageOut = secondsUsed / secondsPassed;
138 write_cb(evutil_socket_t fd, short event, void *arg)
140 printf("write callback. should only see this once\n");
142 /* got what we want remove the event */
143 event_del(*(struct event**)arg);
145 /* opps changed my mind add it back again */
146 event_add(*(struct event**)arg,NULL);
148 /* not a good day for decisiveness, I really didn't want it after all */
149 event_del(*(struct event**)arg);
154 timeout_cb(evutil_socket_t fd, short event, void *arg)
156 printf("timeout fired, time to end test\n");
157 event_del(*(struct event**)arg);
162 main(int argc, char **argv)
165 struct event* timeout;
166 struct event_base* base;
168 evutil_socket_t pair[2];
170 struct cpu_usage_timer timer;
172 double usage, secPassed, secUsed;
175 WORD wVersionRequested;
178 wVersionRequested = MAKEWORD(2, 2);
180 (void) WSAStartup(wVersionRequested, &wsaData);
182 if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
185 /* Initalize the event library */
186 if (!(base = event_base_new()))
189 /* Initalize a timeout to terminate the test */
190 timeout = evtimer_new(base,timeout_cb,&timeout);
191 /* and watch for writability on one end of the pipe */
192 ev = event_new(base,pair[1],EV_WRITE | EV_PERSIST, write_cb, &ev);
195 tv.tv_usec = 500*1000;
197 evtimer_add(timeout, &tv);
201 start_cpu_usage_timer(&timer);
203 event_base_dispatch(base);
207 event_base_free(base);
209 get_cpu_usage(&timer, &secPassed, &secUsed, &usage);
211 /* attempt to calculate our cpu usage over the test should be
214 printf("usec used=%d, usec passed=%d, cpu usage=%.2f%%\n",
216 (int)(secPassed*1e6),
219 if (usage > 50.0) /* way too high */