event.c 8.33 KB
Newer Older
1 2
/*
 * EMULAB-COPYRIGHT
Mike Hibler's avatar
Mike Hibler committed
3
 * Copyright (c) 2002, 2003 University of Utah and the Flux Group.
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
 * All rights reserved.
 */

/*
 * Testbed event system interface
 * Allow starting multiple clients in a synchronized way.
 */

#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/time.h> 

#include "lib/libtb/tbdefs.h"
#include "event/lib/event.h"

#include "decls.h"
#include "log.h"
#include "event.h"

//#define EVENTDEBUG

static Event_t		lastevent;
static event_handle_t	ehandle;

static int		gotevent;
static int		clientnum;

Mike Hibler's avatar
Mike Hibler committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
static int
useclient(int clinum, char *buf)
{
	char *cp;
	int low, high;

	cp = buf;
	if (cp != NULL) {
		while ((cp = strsep(&buf, ",")) != NULL) {
			if (sscanf(cp, "%d-%d", &low, &high) == 2) {
				if (clinum >= low && clinum <= high)
					return 1;
				continue;
			}
			if (sscanf(cp, "%d", &low) == 1) {
				if (clinum == low)
					return 1;
				continue;
			}
		}
	}
	return 0;
}

61 62 63 64 65 66 67 68 69 70 71
/*
 * type==START
 *	STAGGER=N PKTTIMEOUT=N IDLETIMER=N READAHEAD=N INPROGRESS=N REDODELAY=N
 * type==STOP
 *	STAGGER=N
 */
static int
parse_event(Event_t *event, char *etype, char *buf)
{
	char *cp;
	int val;
Mike Hibler's avatar
Mike Hibler committed
72 73
	char str[STRSIZE+1];
	int skipping = 0;
74 75 76 77 78 79 80 81 82 83 84 85 86

	memset(event, -1, sizeof *event);

	if (strcmp(etype, TBDB_EVENTTYPE_START) == 0)
		event->type = EV_START;
	else if (strcmp(etype, TBDB_EVENTTYPE_STOP) == 0)
		event->type = EV_STOP;
	else
		return 1;

	cp = buf;
	if (cp != NULL) {
		while ((cp = strsep(&buf, " ")) != NULL) {
Mike Hibler's avatar
Mike Hibler committed
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
			/*
			 * Hideous Hack Alert!
			 *
			 * Assume hostname is of the form 'c-<num>.<domain>'
			 * and use <num> to determine our client number.
			 * We use that number and compare to the useclients
			 * string to determine if we should process this event.
			 * If not, we ignore this event.
			 */
			if (sscanf(cp, "USECLIENTS=%s", str) == 1) {
				if (!useclient(clientnum, str))
					skipping = 1;
				else
					skipping = 0;
				continue;
			}
			if (sscanf(cp, "SKIPCLIENTS=%s", str) == 1) {
				if (useclient(clientnum, str)) {
					gotevent = 0;
					return 0;
				}
				continue;
			}
			if (skipping)
				continue;

113 114
			if (sscanf(cp, "STARTDELAY=%d", &val) == 1) {
				event->data.start.startdelay = val;
Mike Hibler's avatar
Mike Hibler committed
115 116 117 118 119 120
				gotevent = 1;
				continue; 
			}
			if (sscanf(cp, "STARTAT=%d", &val) == 1) {
				event->data.start.startat = val;
				gotevent = 1;
121 122 123 124
				continue; 
			}
			if (sscanf(cp, "PKTTIMEOUT=%d", &val) == 1) {
				event->data.start.pkttimeout = val;
Mike Hibler's avatar
Mike Hibler committed
125
				gotevent = 1;
126 127 128 129
				continue; 
			}
			if (sscanf(cp, "IDLETIMER=%d", &val) == 1) {
				event->data.start.idletimer = val;
Mike Hibler's avatar
Mike Hibler committed
130
				gotevent = 1;
131 132 133 134
				continue; 
			}
			if (sscanf(cp, "CHUNKBUFS=%d", &val) == 1) {
				event->data.start.chunkbufs = val;
Mike Hibler's avatar
Mike Hibler committed
135 136 137 138 139 140 141 142 143 144 145
				gotevent = 1;
				continue; 
			}
			if (sscanf(cp, "WRITEBUFMEM=%d", &val) == 1) {
				event->data.start.writebufmem = val;
				gotevent = 1;
				continue; 
			}
			if (sscanf(cp, "MAXMEM=%d", &val) == 1) {
				event->data.start.maxmem = val;
				gotevent = 1;
146 147 148 149
				continue; 
			}
			if (sscanf(cp, "READAHEAD=%d", &val) == 1) {
				event->data.start.readahead = val;
Mike Hibler's avatar
Mike Hibler committed
150
				gotevent = 1;
151 152 153 154
				continue; 
			}
			if (sscanf(cp, "INPROGRESS=%d", &val) == 1) {
				event->data.start.inprogress = val;
Mike Hibler's avatar
Mike Hibler committed
155
				gotevent = 1;
156 157 158 159
				continue; 
			}
			if (sscanf(cp, "REDODELAY=%d", &val) == 1) {
				event->data.start.redodelay = val;
Mike Hibler's avatar
Mike Hibler committed
160
				gotevent = 1;
161 162 163 164
				continue; 
			}
			if (sscanf(cp, "IDLEDELAY=%d", &val) == 1) {
				event->data.start.idledelay = val;
Mike Hibler's avatar
Mike Hibler committed
165
				gotevent = 1;
166 167 168 169
				continue; 
			}
			if (sscanf(cp, "SLICE=%d", &val) == 1) {
				event->data.start.slice = val;
Mike Hibler's avatar
Mike Hibler committed
170
				gotevent = 1;
171 172 173 174
				continue; 
			}
			if (sscanf(cp, "ZEROFILL=%d", &val) == 1) {
				event->data.start.zerofill = val;
Mike Hibler's avatar
Mike Hibler committed
175
				gotevent = 1;
176 177 178 179
				continue; 
			}
			if (sscanf(cp, "RANDOMIZE=%d", &val) == 1) {
				event->data.start.randomize = val;
Mike Hibler's avatar
Mike Hibler committed
180
				gotevent = 1;
181 182 183 184
				continue; 
			}
			if (sscanf(cp, "NOTHREADS=%d", &val) == 1) {
				event->data.start.nothreads = val;
Mike Hibler's avatar
Mike Hibler committed
185
				gotevent = 1;
186 187
				continue; 
			}
188 189
			if (sscanf(cp, "DOSTYPE=%d", &val) == 1) {
				event->data.start.dostype = val;
Mike Hibler's avatar
Mike Hibler committed
190
				gotevent = 1;
191 192
				continue; 
			}
193 194
			if (sscanf(cp, "DEBUG=%d", &val) == 1) {
				event->data.start.debug = val;
Mike Hibler's avatar
Mike Hibler committed
195
				gotevent = 1;
196 197 198 199
				continue; 
			}
			if (sscanf(cp, "TRACE=%d", &val) == 1) {
				event->data.start.trace = val;
Mike Hibler's avatar
Mike Hibler committed
200 201 202 203 204 205 206
				gotevent = 1;
				continue; 
			}
			if (sscanf(cp, "TRACEPREFIX=%s", str) == 1) {
				strncpy(event->data.start.traceprefix,
					str, STRSIZE-1);
				gotevent = 1;
207 208 209 210
				continue; 
			}
			if (sscanf(cp, "EXITSTATUS=%d", &val) == 1) {
				event->data.stop.exitstatus = val;
Mike Hibler's avatar
Mike Hibler committed
211
				gotevent = 1;
212 213
				continue; 
			}
Mike Hibler's avatar
Mike Hibler committed
214 215 216 217 218 219 220
#ifdef DOLOSSRATE
			{
				double plr;
				if (sscanf(cp, "PLR=%lf", &plr) == 1) {
					event->data.start.plr = plr;
					gotevent = 1;
					continue; 
221 222
				}
			}
Mike Hibler's avatar
Mike Hibler committed
223
#endif
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
		}
	}
	return 0;
}

static void
callback(event_handle_t handle, event_notification_t notification, void *data)
{
	char		buf[7][64];
	char		args[256];
	int		len = 64;

	buf[0][0] = buf[1][0] = buf[2][0] = buf[3][0] = 0;
	buf[4][0] = buf[5][0] = buf[6][0] = 0;
	event_notification_get_site(handle, notification, buf[0], len);
	event_notification_get_expt(handle, notification, buf[1], len);
	event_notification_get_group(handle, notification, buf[2], len);
	event_notification_get_host(handle, notification, buf[3], len);
	event_notification_get_objtype(handle, notification, buf[4], len);
	event_notification_get_objname(handle, notification, buf[5], len);
	event_notification_get_eventtype(handle, notification, buf[6], len);
	event_notification_get_arguments(handle, notification,
					 args, sizeof(args));

#ifdef EVENTDEBUG
	{
		struct timeval now;
		static int ecount;

		gettimeofday(&now, NULL);
		fprintf(stderr, "Event %d: %lu.%03lu %s %s %s %s %s %s %s %s\n",
			++ecount, now.tv_sec, now.tv_usec / 1000,
			buf[0], buf[1], buf[2],
			buf[3], buf[4], buf[5], buf[6], args);
	}
#endif
	if (parse_event(&lastevent, buf[6], args))
		log("bogus event '%s %s' ignored", buf[6], args);
}

int
EventInit(char *server)
{
	char buf[BUFSIZ], ipbuf[BUFSIZ];
	struct hostent *he;
	struct in_addr myip;
	char *ipaddr;
	address_tuple_t	tuple;
	    
	if (server == NULL) {
		warning("no event server specified");
		return 1;
	}

	if (gethostname(buf, sizeof(buf)) < 0) {
		pwarning("could not get hostname");
		return 1;
	}

	if ((he = gethostbyname(buf)) == NULL) {
		warning("could not get IP address from hostname");
		return 1;
	}

	memcpy((char *)&myip, he->h_addr, he->h_length);
	strcpy(ipbuf, inet_ntoa(myip));
	ipaddr = ipbuf;

#if 1
	/*
	 * Hideous Hack Alert!
	 *
	 * Assume our hostname is of the form 'c-<num>.<domain>'
	 * and use <num> to determine our client number.
	 * We use that number later to compare to the MAXCLIENTS
	 * field to determine if we should process an event.
	 */
	if (sscanf(buf, "c-%d.", &clientnum) != 1) {
		warning("could not determine client number from hostname %s",
			buf);
		return 1;
	} else if (debug)
		log("client number %d for event handling", clientnum);
#endif

	/*
	 * Convert server/port to elvin thing.
	 */
	snprintf(buf, sizeof(buf), "elvin://%s", server);
	server = buf;

	/*
	 * Construct an address tuple for subscribing to events for
	 * this node.
	 */
	tuple = address_tuple_alloc();
	if (tuple == NULL) {
		warning("could not allocate an address tuple");
		return 1;
	}
	tuple->host	 = ADDRESSTUPLE_ANY; /* ipaddr; */
	tuple->site      = ADDRESSTUPLE_ANY;
	tuple->group     = ADDRESSTUPLE_ANY;
	tuple->expt      = ADDRESSTUPLE_ANY;
	tuple->objtype   = TBDB_OBJECTTYPE_FRISBEE;
	tuple->objname   = ADDRESSTUPLE_ANY;
	tuple->eventtype = ADDRESSTUPLE_ANY;

	/*
	 * Register with the event system. 
	 */
	ehandle = event_register(server, 0);
	if (ehandle == NULL) {
		warning("could not register with event system");
		address_tuple_free(tuple);
		return 1;
	}
	
	/*
	 * Subscribe to the event we specified above.
	 */
	if (!event_subscribe(ehandle, callback, tuple, NULL)) {
		warning("could not subscribe to FRISBEE events");
		address_tuple_free(tuple);
		return 1;
	}
	address_tuple_free(tuple);
	return 0;
}

int
EventCheck(Event_t *event)
{
	int rv;

	gotevent = 0;
	rv = event_poll(ehandle);
	if (rv)
		fatal("event_poll failed, err=%d\n", rv);
	if (gotevent)
		memcpy(event, &lastevent, sizeof lastevent);
	return gotevent;
}

void
EventWait(int eventtype, Event_t *event)
{
	while (EventCheck(event) == 0 ||
	       (eventtype != EV_ANY && event->type != eventtype))
		/* sleep? */;
}