ipc/chromium/src/third_party/libevent/test/test-changelist.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * Copyright (c) 2010-2012 Niels Provos and Nick Mathewson
michael@0 3 *
michael@0 4 * Redistribution and use in source and binary forms, with or without
michael@0 5 * modification, are permitted provided that the following conditions
michael@0 6 * are met:
michael@0 7 * 1. Redistributions of source code must retain the above copyright
michael@0 8 * notice, this list of conditions and the following disclaimer.
michael@0 9 * 2. Redistributions in binary form must reproduce the above copyright
michael@0 10 * notice, this list of conditions and the following disclaimer in the
michael@0 11 * documentation and/or other materials provided with the distribution.
michael@0 12 * 3. The name of the author may not be used to endorse or promote products
michael@0 13 * derived from this software without specific prior written permission.
michael@0 14 *
michael@0 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
michael@0 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
michael@0 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
michael@0 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
michael@0 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
michael@0 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
michael@0 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 25 */
michael@0 26
michael@0 27 #include "event2/event-config.h"
michael@0 28
michael@0 29 #ifdef WIN32
michael@0 30 #include <winsock2.h>
michael@0 31 #include <windows.h>
michael@0 32 #else
michael@0 33 #include <unistd.h>
michael@0 34 #endif
michael@0 35 #include <sys/types.h>
michael@0 36 #include <sys/stat.h>
michael@0 37 #ifdef _EVENT_HAVE_SYS_TIME_H
michael@0 38 #include <sys/time.h>
michael@0 39 #endif
michael@0 40
michael@0 41 #ifdef _EVENT_HAVE_SYS_SOCKET_H
michael@0 42 #include <sys/socket.h>
michael@0 43 #endif
michael@0 44 #include <fcntl.h>
michael@0 45 #include <stdlib.h>
michael@0 46 #include <stdio.h>
michael@0 47 #include <string.h>
michael@0 48 #include <errno.h>
michael@0 49
michael@0 50 #include "event2/event.h"
michael@0 51 #include "event2/util.h"
michael@0 52 #include <time.h>
michael@0 53
michael@0 54 struct cpu_usage_timer {
michael@0 55 #ifdef WIN32
michael@0 56 HANDLE thread;
michael@0 57 FILETIME usertimeBegin;
michael@0 58 FILETIME kerneltimeBegin;
michael@0 59 #else
michael@0 60 clock_t ticksBegin;
michael@0 61 #endif
michael@0 62 struct timeval timeBegin;
michael@0 63 };
michael@0 64 static void
michael@0 65 start_cpu_usage_timer(struct cpu_usage_timer *timer)
michael@0 66 {
michael@0 67 #ifdef WIN32
michael@0 68 int r;
michael@0 69 FILETIME createtime, exittime;
michael@0 70 timer->thread = GetCurrentThread();
michael@0 71 r = GetThreadTimes(timer->thread, &createtime, &exittime,
michael@0 72 &timer->usertimeBegin, &timer->kerneltimeBegin);
michael@0 73 if (r==0) printf("GetThreadTimes failed.");
michael@0 74 #else
michael@0 75 timer->ticksBegin = clock();
michael@0 76 #endif
michael@0 77
michael@0 78 evutil_gettimeofday(&timer->timeBegin, NULL);
michael@0 79 }
michael@0 80 #ifdef WIN32
michael@0 81 static ev_int64_t
michael@0 82 filetime_to_100nsec(const FILETIME *ft)
michael@0 83 {
michael@0 84 /* Number of 100-nanosecond units */
michael@0 85 ev_int64_t n = ft->dwHighDateTime;
michael@0 86 n <<= 32;
michael@0 87 n += ft->dwLowDateTime;
michael@0 88 return n;
michael@0 89 }
michael@0 90 static double
michael@0 91 filetime_diff(const FILETIME *ftStart, const FILETIME *ftEnd)
michael@0 92 {
michael@0 93 ev_int64_t s, e, diff;
michael@0 94 double r;
michael@0 95 s = filetime_to_100nsec(ftStart);
michael@0 96 e = filetime_to_100nsec(ftEnd);
michael@0 97 diff = e - s;
michael@0 98 r = (double) diff;
michael@0 99 return r / 1.0e7;
michael@0 100 }
michael@0 101 #endif
michael@0 102
michael@0 103 static void
michael@0 104 get_cpu_usage(struct cpu_usage_timer *timer, double *secElapsedOut,
michael@0 105 double *secUsedOut, double *usageOut)
michael@0 106 {
michael@0 107 #ifdef WIN32
michael@0 108 double usertime_seconds, kerneltime_seconds;
michael@0 109 FILETIME createtime, exittime, usertimeEnd, kerneltimeEnd;
michael@0 110 int r;
michael@0 111 #else
michael@0 112 clock_t ticksEnd;
michael@0 113 #endif
michael@0 114 struct timeval timeEnd, timeDiff;
michael@0 115 double secondsPassed, secondsUsed;
michael@0 116
michael@0 117 #ifdef WIN32
michael@0 118 r = GetThreadTimes(timer->thread, &createtime, &exittime,
michael@0 119 &usertimeEnd, &kerneltimeEnd);
michael@0 120 if (r==0) printf("GetThreadTimes failed.");
michael@0 121 usertime_seconds = filetime_diff(&timer->usertimeBegin, &usertimeEnd);
michael@0 122 kerneltime_seconds = filetime_diff(&timer->kerneltimeBegin, &kerneltimeEnd);
michael@0 123 secondsUsed = kerneltime_seconds + usertime_seconds;
michael@0 124 #else
michael@0 125 ticksEnd = clock();
michael@0 126 secondsUsed = (ticksEnd - timer->ticksBegin) / (double)CLOCKS_PER_SEC;
michael@0 127 #endif
michael@0 128 evutil_gettimeofday(&timeEnd, NULL);
michael@0 129 evutil_timersub(&timeEnd, &timer->timeBegin, &timeDiff);
michael@0 130 secondsPassed = timeDiff.tv_sec + (timeDiff.tv_usec / 1.0e6);
michael@0 131
michael@0 132 *secElapsedOut = secondsPassed;
michael@0 133 *secUsedOut = secondsUsed;
michael@0 134 *usageOut = secondsUsed / secondsPassed;
michael@0 135 }
michael@0 136
michael@0 137 static void
michael@0 138 write_cb(evutil_socket_t fd, short event, void *arg)
michael@0 139 {
michael@0 140 printf("write callback. should only see this once\n");
michael@0 141
michael@0 142 /* got what we want remove the event */
michael@0 143 event_del(*(struct event**)arg);
michael@0 144
michael@0 145 /* opps changed my mind add it back again */
michael@0 146 event_add(*(struct event**)arg,NULL);
michael@0 147
michael@0 148 /* not a good day for decisiveness, I really didn't want it after all */
michael@0 149 event_del(*(struct event**)arg);
michael@0 150
michael@0 151 }
michael@0 152
michael@0 153 static void
michael@0 154 timeout_cb(evutil_socket_t fd, short event, void *arg)
michael@0 155 {
michael@0 156 printf("timeout fired, time to end test\n");
michael@0 157 event_del(*(struct event**)arg);
michael@0 158 return;
michael@0 159 }
michael@0 160
michael@0 161 int
michael@0 162 main(int argc, char **argv)
michael@0 163 {
michael@0 164 struct event* ev;
michael@0 165 struct event* timeout;
michael@0 166 struct event_base* base;
michael@0 167
michael@0 168 evutil_socket_t pair[2];
michael@0 169 struct timeval tv;
michael@0 170 struct cpu_usage_timer timer;
michael@0 171
michael@0 172 double usage, secPassed, secUsed;
michael@0 173
michael@0 174 #ifdef WIN32
michael@0 175 WORD wVersionRequested;
michael@0 176 WSADATA wsaData;
michael@0 177
michael@0 178 wVersionRequested = MAKEWORD(2, 2);
michael@0 179
michael@0 180 (void) WSAStartup(wVersionRequested, &wsaData);
michael@0 181 #endif
michael@0 182 if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
michael@0 183 return (1);
michael@0 184
michael@0 185 /* Initalize the event library */
michael@0 186 base = event_base_new();
michael@0 187
michael@0 188 /* Initalize a timeout to terminate the test */
michael@0 189 timeout = evtimer_new(base,timeout_cb,&timeout);
michael@0 190 /* and watch for writability on one end of the pipe */
michael@0 191 ev = event_new(base,pair[1],EV_WRITE | EV_PERSIST, write_cb, &ev);
michael@0 192
michael@0 193 tv.tv_sec = 5;
michael@0 194 tv.tv_usec = 0;
michael@0 195
michael@0 196 evtimer_add(timeout, &tv);
michael@0 197
michael@0 198 event_add(ev, NULL);
michael@0 199
michael@0 200 start_cpu_usage_timer(&timer);
michael@0 201
michael@0 202 event_base_dispatch(base);
michael@0 203
michael@0 204 event_free(ev);
michael@0 205 event_free(timeout);
michael@0 206 event_base_free(base);
michael@0 207
michael@0 208 get_cpu_usage(&timer, &secPassed, &secUsed, &usage);
michael@0 209
michael@0 210 /* attempt to calculate our cpu usage over the test should be
michael@0 211 virtually nil */
michael@0 212
michael@0 213 printf("usec used=%d, usec passed=%d, cpu usage=%.2f%%\n",
michael@0 214 (int)(secUsed*1e6),
michael@0 215 (int)(secPassed*1e6),
michael@0 216 usage*100);
michael@0 217
michael@0 218 if (usage > 50.0) /* way too high */
michael@0 219 return 1;
michael@0 220
michael@0 221 return 0;
michael@0 222 }
michael@0 223

mercurial