2010-07-19 13:44:56 +12:00
|
|
|
/*
|
2012-02-10 17:29:53 -05:00
|
|
|
* Copyright (c) 2010-2012 Niels Provos and Nick Mathewson
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2010-07-19 13:44:56 +12:00
|
|
|
*/
|
|
|
|
|
2010-07-07 16:45:03 -04:00
|
|
|
#include "event2/event-config.h"
|
2010-07-19 13:44:56 +12:00
|
|
|
|
2011-05-25 19:50:56 -04:00
|
|
|
#ifdef _WIN32
|
2010-07-19 13:44:56 +12:00
|
|
|
#include <winsock2.h>
|
2010-08-06 13:34:51 -04:00
|
|
|
#include <windows.h>
|
2010-07-19 13:44:56 +12:00
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2012-02-29 15:07:31 -05:00
|
|
|
#ifdef EVENT__HAVE_SYS_TIME_H
|
2010-07-19 13:44:56 +12:00
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
|
|
|
|
2012-02-29 15:07:31 -05:00
|
|
|
#ifdef EVENT__HAVE_SYS_SOCKET_H
|
2010-07-19 13:44:56 +12:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#endif
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2010-12-02 14:13:33 -05:00
|
|
|
#include "event2/event.h"
|
|
|
|
#include "event2/util.h"
|
2010-07-19 13:44:56 +12:00
|
|
|
#include <time.h>
|
|
|
|
|
2010-08-06 13:34:51 -04:00
|
|
|
struct cpu_usage_timer {
|
2011-05-25 19:50:56 -04:00
|
|
|
#ifdef _WIN32
|
2010-08-06 13:34:51 -04:00
|
|
|
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)
|
|
|
|
{
|
2011-05-25 19:50:56 -04:00
|
|
|
#ifdef _WIN32
|
2010-08-06 13:34:51 -04:00
|
|
|
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);
|
|
|
|
}
|
2011-05-25 19:50:56 -04:00
|
|
|
#ifdef _WIN32
|
2010-08-06 13:34:51 -04:00
|
|
|
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)
|
|
|
|
{
|
2011-05-25 19:50:56 -04:00
|
|
|
#ifdef _WIN32
|
2010-08-06 13:34:51 -04:00
|
|
|
double usertime_seconds, kerneltime_seconds;
|
|
|
|
FILETIME createtime, exittime, usertimeEnd, kerneltimeEnd;
|
|
|
|
int r;
|
|
|
|
#else
|
|
|
|
clock_t ticksEnd;
|
|
|
|
#endif
|
|
|
|
struct timeval timeEnd, timeDiff;
|
|
|
|
double secondsPassed, secondsUsed;
|
|
|
|
|
2011-05-25 19:50:56 -04:00
|
|
|
#ifdef _WIN32
|
2010-08-06 13:34:51 -04:00
|
|
|
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;
|
|
|
|
}
|
2010-07-19 13:44:56 +12:00
|
|
|
|
|
|
|
static void
|
|
|
|
write_cb(evutil_socket_t fd, short event, void *arg)
|
2010-07-19 15:03:43 +02:00
|
|
|
{
|
2010-07-19 13:44:56 +12:00
|
|
|
printf("write callback. should only see this once\n");
|
2010-07-19 15:03:43 +02:00
|
|
|
|
2010-07-19 13:44:56 +12:00
|
|
|
/* 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)
|
2010-07-19 15:03:43 +02:00
|
|
|
{
|
2010-07-19 13:44:56 +12:00
|
|
|
printf("timeout fired, time to end test\n");
|
|
|
|
event_del(*(struct event**)arg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2024-04-22 15:31:10 +08:00
|
|
|
struct event* ev = NULL;
|
|
|
|
struct event* timeout = NULL;
|
|
|
|
struct event_base* base = NULL;
|
2010-07-19 13:44:56 +12:00
|
|
|
|
2024-06-24 14:19:12 +08:00
|
|
|
evutil_socket_t pair[2] = {EVUTIL_INVALID_SOCKET, EVUTIL_INVALID_SOCKET};
|
2010-07-19 13:44:56 +12:00
|
|
|
struct timeval tv;
|
2010-08-06 13:34:51 -04:00
|
|
|
struct cpu_usage_timer timer;
|
2010-07-19 13:44:56 +12:00
|
|
|
|
2010-08-06 13:34:51 -04:00
|
|
|
double usage, secPassed, secUsed;
|
2010-07-19 13:44:56 +12:00
|
|
|
|
2011-05-25 19:50:56 -04:00
|
|
|
#ifdef _WIN32
|
2010-07-19 13:44:56 +12:00
|
|
|
WORD wVersionRequested;
|
|
|
|
WSADATA wsaData;
|
|
|
|
|
|
|
|
wVersionRequested = MAKEWORD(2, 2);
|
|
|
|
|
2012-11-01 17:38:34 -04:00
|
|
|
(void) WSAStartup(wVersionRequested, &wsaData);
|
2010-07-19 13:44:56 +12:00
|
|
|
#endif
|
|
|
|
if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
|
|
|
|
return (1);
|
|
|
|
|
2019-08-06 18:19:15 +08:00
|
|
|
/* Initialize the event library */
|
2013-12-16 13:45:45 +01:00
|
|
|
if (!(base = event_base_new()))
|
|
|
|
return (1);
|
2010-07-19 13:44:56 +12:00
|
|
|
|
2019-08-06 18:19:15 +08:00
|
|
|
/* Initialize a timeout to terminate the test */
|
2010-07-19 13:44:56 +12:00
|
|
|
timeout = evtimer_new(base,timeout_cb,&timeout);
|
2024-06-24 14:19:12 +08:00
|
|
|
if (timeout == NULL) {
|
|
|
|
perror("evtimer_new");
|
2024-04-22 15:31:10 +08:00
|
|
|
goto err;
|
2024-06-24 14:19:12 +08:00
|
|
|
}
|
2010-07-19 13:44:56 +12:00
|
|
|
/* and watch for writability on one end of the pipe */
|
|
|
|
ev = event_new(base,pair[1],EV_WRITE | EV_PERSIST, write_cb, &ev);
|
2024-06-24 14:19:12 +08:00
|
|
|
if (ev == NULL) {
|
|
|
|
perror("event_new");
|
2024-04-22 15:31:10 +08:00
|
|
|
goto err;
|
2024-06-24 14:19:12 +08:00
|
|
|
}
|
2012-01-27 15:10:28 -05:00
|
|
|
tv.tv_sec = 1;
|
|
|
|
tv.tv_usec = 500*1000;
|
2010-07-19 13:44:56 +12:00
|
|
|
|
|
|
|
evtimer_add(timeout, &tv);
|
|
|
|
|
|
|
|
event_add(ev, NULL);
|
|
|
|
|
2010-08-06 13:34:51 -04:00
|
|
|
start_cpu_usage_timer(&timer);
|
2010-07-19 13:44:56 +12:00
|
|
|
|
2010-11-22 21:02:34 -05:00
|
|
|
event_base_dispatch(base);
|
2010-07-19 13:44:56 +12:00
|
|
|
|
2012-06-06 21:57:12 +02:00
|
|
|
event_free(ev);
|
|
|
|
event_free(timeout);
|
|
|
|
event_base_free(base);
|
|
|
|
|
2010-08-06 13:34:51 -04:00
|
|
|
get_cpu_usage(&timer, &secPassed, &secUsed, &usage);
|
2010-07-19 13:44:56 +12:00
|
|
|
|
|
|
|
/* attempt to calculate our cpu usage over the test should be
|
|
|
|
virtually nil */
|
2010-07-19 15:03:43 +02:00
|
|
|
|
2010-08-06 13:34:51 -04:00
|
|
|
printf("usec used=%d, usec passed=%d, cpu usage=%.2f%%\n",
|
|
|
|
(int)(secUsed*1e6),
|
|
|
|
(int)(secPassed*1e6),
|
|
|
|
usage*100);
|
2010-07-19 13:44:56 +12:00
|
|
|
|
|
|
|
if (usage > 50.0) /* way too high */
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
2024-04-22 15:31:10 +08:00
|
|
|
err:
|
|
|
|
if (ev)
|
|
|
|
event_free(ev);
|
|
|
|
if (timeout)
|
|
|
|
event_free(timeout);
|
2024-06-24 14:19:12 +08:00
|
|
|
if (pair[0] != EVUTIL_INVALID_SOCKET)
|
2024-04-22 15:31:10 +08:00
|
|
|
evutil_closesocket(pair[0]);
|
2024-06-24 14:19:12 +08:00
|
|
|
if (pair[1] != EVUTIL_INVALID_SOCKET)
|
2024-04-22 15:31:10 +08:00
|
|
|
evutil_closesocket(pair[1]);
|
|
|
|
if (base)
|
|
|
|
event_base_free(base);
|
|
|
|
return 1;
|
2010-07-19 13:44:56 +12:00
|
|
|
}
|
|
|
|
|