simulator-agent.cc 13.9 KB
Newer Older
1 2
/*
 * EMULAB-COPYRIGHT
3
 * Copyright (c) 2004-2011 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 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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 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 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
 * All rights reserved.
 */

/**
 * @file simulator-agent.cc
 *
 * Implementation file for the SIMULATOR agent.
 */

#include "config.h"

#include <stdlib.h>
#include <errno.h>
#include <string.h>

#include "popenf.h"
#include "systemf.h"
#include "rpc.h"
#include "log.h"
#include "simulator-agent.h"

using namespace emulab;

/**
 * A "looper" function for the simulator agent that dequeues and processes
 * events destined for the Simulator object.  This function will be passed to
 * pthread_create when a new thread needs to be created to handle events.
 *
 * @param arg The simulator agent object to handle events for.
 * @return NULL
 *
 * @see send_report
 * @see add_report_data
 * @see local_agent_queue
 * @see local_agent_dequeue
 */
static void *simulator_agent_looper(void *arg);

simulator_agent_t create_simulator_agent(void)
{
	simulator_agent_t sa, retval;

	if ((sa = (simulator_agent_t)
	     malloc(sizeof(struct _simulator_agent))) == NULL) {
		retval = NULL;
		errno = ENOMEM;
	}
	else if (local_agent_init(&sa->sa_local_agent) != 0) {
		retval = NULL;
	}
	else {
		int lpc;
		
		sa->sa_local_agent.la_looper = simulator_agent_looper;
		lnNewList(&sa->sa_error_records);
		for (lpc = 0; lpc < SA_RDK_MAX; lpc++)
			sa->sa_report_data[lpc] = NULL;
		
		retval = sa;
		sa = NULL;
	}

	free(sa);
	sa = NULL;

	return retval;
}

int simulator_agent_invariant(simulator_agent_t sa)
{
	assert(sa != NULL);
	assert(local_agent_invariant(&sa->sa_local_agent));
	lnCheck(&sa->sa_error_records);
	
	return 1;
}

int add_report_data(simulator_agent_t sa,
		    sa_report_data_kind_t rdk,
		    char *data,
		    unsigned long flags)
{
	char *new_data;
	int retval;

	assert(sa != NULL);
	assert(rdk >= 0);
	assert(rdk < SA_RDK_MAX);
	assert(data != NULL);

	if ((new_data = (char *)realloc(sa->sa_report_data[rdk],
					((sa->sa_report_data[rdk] != NULL) ?
					 strlen(sa->sa_report_data[rdk]) : 0) +
					strlen(data) +
					1 +
					1)) == NULL) {
		retval = -1;
		errno = ENOMEM;
	}
	else {
		if (sa->sa_report_data[rdk] == NULL)
			new_data[0] = '\0'; // Need to clear the fresh malloc.
		sa->sa_report_data[rdk] = new_data;
		
		strcat(sa->sa_report_data[rdk], data);
		if ((flags & SA_RDF_NEWLINE) &&
		    (data[strlen(data) - 1] != '\n')) {
			strcat(sa->sa_report_data[rdk], "\n");
		}

		retval = 0;
	}
	
	return retval;
}

static int remap_experiment(simulator_agent_t sa, int token)
{
	char nsfile[BUFSIZ];
	EmulabResponse er;
	int retval;

	snprintf(nsfile, sizeof(nsfile),
		 "/proj/%s/exp/%s/tbdata/%s-modify.ns",
		 pid, eid, eid);
	if (access(nsfile, R_OK) == -1) {
		snprintf(nsfile, sizeof(nsfile),
			 "/proj/%s/exp/%s/tbdata/%s.ns",
			 pid, eid, eid);
	}
	RPC_grab();
	retval = RPC_invoke("experiment.modify",
			    &er,
			    SPA_String, "proj", pid,
			    SPA_String, "exp", eid,
			    SPA_Boolean, "wait", true,
			    SPA_Boolean, "reboot", true,
			    SPA_Boolean, "restart_eventsys", true,
			    SPA_String, "nsfilepath", nsfile,
			    SPA_TAG_DONE);
	RPC_drop();

	if (retval != 0) {
		rename("tbdata/feedback_data.tcl",
		       "tbdata/feedback_data_failed.tcl");
		rename("tbdata/feedback_data_old.tcl",
		       "tbdata/feedback_data.tcl");
	}

	return retval;
}

static int do_modify(simulator_agent_t sa, int token, char *args)
{
	int rc, retval = 0;
	char *mode;

	assert(sa != NULL);
	assert(args != NULL);
	
	if ((rc = event_arg_get(args, "MODE", &mode)) <= 0) {
		error("no mode specified\n");
	}
	else if (strncasecmp("stabilize", mode, rc) == 0) {
		rename("tbdata/feedback_data.tcl",
		       "tbdata/feedback_data_old.tcl");
		if (systemf("loghole --port=%d --quiet sync",
			    DEFAULT_RPC_PORT) != 0) {
			error("failed to sync log holes\n");
		}
		else if (systemf("feedbacklogs %s %s", pid, eid) != 0) {
			if (sa->sa_flags & SAF_STABLE) {
				/* XXX log error */
				warning("unstabilized!\n");
			}
			else {
				retval = remap_experiment(sa, token);
			}
		}
		else {
			info("stabilized\n");
			sa->sa_flags |= SAF_STABLE;
		}
	}
	else {
		warning("unknown mode %s\n", mode);
	}

	return retval;
}

static void dump_report_data(FILE *file,
			     simulator_agent_t sa,
			     sa_report_data_kind_t srdk,
			     int clear)
{
	assert(file != NULL);
	assert(sa != NULL);
	assert(srdk >= 0);
	assert(srdk < SA_RDK_MAX);

	if ((sa->sa_report_data[srdk] != NULL) &&
	    (strlen(sa->sa_report_data[srdk]) > 0)) {
		fprintf(file, "\n%s\n", sa->sa_report_data[srdk]);
		if (clear) {
			free(sa->sa_report_data[srdk]);
			sa->sa_report_data[srdk] = NULL;
		}
	}
}

static int real_send_report(simulator_agent_t sa, char *args, bool error_report)
{
	struct lnList error_records;
	char loghole_name[BUFSIZ];
	int rc, retval;
	FILE *file;
	bool archive;
	char * tmp;
	const char * report_file_name;

	assert(sa != NULL);
	assert(args != NULL);
	
	/*
	 * Atomically move the error records from the agent object onto our
	 * local list and make the agent's list empty.
	 */
	if (pthread_mutex_lock(&sa->sa_local_agent.la_mutex) != 0)
		assert(0);
	lnMoveList(&error_records, &sa->sa_error_records);

	assert(lnEmptyList(&sa->sa_error_records));
	if (pthread_mutex_unlock(&sa->sa_local_agent.la_mutex) != 0)
		assert(0);
	
	if (debug) {
		char time_buf[24];
		struct timeval now;
		gettimeofday(&now, NULL);
		make_timestamp(time_buf, &now);
		info("Sending %sReport at %s with args \"%s\"\n",
		     error_report ? "Error " : "", time_buf, args);
	}

	/*
	 * Get the logs off the nodes so we can generate summaries from the
	 * error records.
	 */
	if ((rc = systemf("loghole --port=%d -vvv sync", DEFAULT_RPC_PORT))
	    != 0) {
		errorc("failed to sync log holes %d\n", rc);
	}

	report_file_name = error_report 
		? "logs/error-report.mail" 
		: "logs/report.mail";

	if ((file = fopen(report_file_name, "w")) == NULL) {
		errorc("could not create %s\n", report_file_name);
		retval = -1;
	}
	else {
		int rc, lpc, error_count;
		char *digester;
		FILE *vfile;
		
		retval = 0;

		error_count = lnCountNodes(&error_records);
		if (error_count > 0) {
			fprintf(file,
				"\n"
				"  *** %d error(s) were detected!\n"
				"      Details should be below in the logs.\n"
				"\n",
				error_count);
		}

		if ((vfile = popenf("loghole --port=%d validate",
				    "r",
				    DEFAULT_RPC_PORT)) == NULL) {
			fprintf(file, "[unable to validate logs]\n");
		}
		else {
			char buf[BUFSIZ];
			int total = 0;
			
			while ((rc = fread(buf,
					   1,
					   sizeof(buf),
					   vfile)) > 0) {
				fwrite(buf, 1, rc, file);
				total += rc;
			}
			pclose(vfile);
			vfile = NULL;

			if (total > 0)
				fprintf(file, "\n");
		}
		
		/* Dump user supplied stuff first, */
		dump_report_data(file, sa, SA_RDK_MESSAGE, 1);

		/* ... run the user-specified log digester, then */
		if ((rc = event_arg_get(args, "DIGESTER", &digester)) > 0) {
			FILE *dfile;
			
			digester[rc] = '\0';

			if ((dfile = popenf("%s | tee logs/digest.out",
					    "r",
					    digester)) == NULL) {
				fprintf(file,
					"[failed to run digester %s]\n",
					digester);
			}
			else {
				char buf[BUFSIZ];
				
				while ((rc = fread(buf,
						   1,
						   sizeof(buf),
						   dfile)) > 0) {
					fwrite(buf, 1, rc, file);
				}
				pclose(dfile);
				dfile = NULL;
			}

			fprintf(file, "\n");
		}
		
		fprintf(file, "Configuration:\n");
		dump_report_data(file, sa, SA_RDK_CONFIG, 0);

		fprintf(file, "\nLog:\n");
		dump_report_data(file, sa, SA_RDK_LOG, 1);
		
		/* ... dump the error records. */
		if (dump_error_records(&error_records, file) != 0) {
			errorc("dump_error_records failed");
			retval = -1;
		}
		
		fclose(file);
		file = NULL;
	}
	
	if (error_report) {
		archive = false;
	} else if ((rc = event_arg_get(args, "ARCHIVE", &tmp)) > 0) {
		if (rc == 4 && strncmp(tmp, "true", 4) == 0) {
			archive = true;
		} else if (rc == 5 && strncmp(tmp, "false", 5) == 0) {
			archive = false;
		} else {
			error("ARCHIVE must be \"true\" or \"false\", assuming \"false\"\n");
			archive = false;
		}
	} else {
		archive = true;
	}
	
	if (archive) {
		if ((file = popenf("loghole --port=%d --quiet archive --delete", 
				   "r", DEFAULT_RPC_PORT)) == NULL) {
			strcpy(loghole_name, eid);
			error("failed to archive log holes\n");
		}
		else {
			int len;
			
			fgets(loghole_name, sizeof(loghole_name), file);
			pclose(file);
			file = NULL;
			
			len = strlen(loghole_name);
			if (loghole_name[len - 1] == '\n')
				loghole_name[len - 1] = '\0';
		}
	}
	else {
		loghole_name[0] = '\0';
	}

	if (systemf("mail -s \"%s: %s/%s experiment %sreport\" %s "
		    "< %s",
		    OURDOMAIN,
		    pid,
		    loghole_name[0] ? loghole_name : eid,
		    error_report ? "error " : "",
		    getenv("USER"),
		    report_file_name) != 0) {
		errorc("could not execute send report\n");
		retval = -1;
	}
	
	delete_error_records(&error_records);

	assert(lnEmptyList(&error_records));
	
	return retval;
}

int send_report(simulator_agent_t sa, char *args) {
	return real_send_report(sa, args, false);
}

int send_error_report(simulator_agent_t sa, char *args) {
	return real_send_report(sa, args, true);
}

static int do_reset(simulator_agent_t sa, char *args)
{
	int retval = 0;

	assert(sa != NULL);
	assert(args != NULL);

	if (systemf("loghole --port=%d --quiet clean --force",
		    DEFAULT_RPC_PORT) != 0) {
		error("failed to clean log holes\n");
	}

	return retval;
}

static int do_snapshot(simulator_agent_t sa, char *args)
{
	char *loghole_args;
	int retval = 0;

	assert(sa != NULL);
	assert(args != NULL);
	
	if (event_arg_get(args, "LOGHOLE_ARGS", &loghole_args) <= 0) {
		loghole_args = "";
	}
	
	if (systemf("loghole --port=%d sync %s",
		    DEFAULT_RPC_PORT, loghole_args) != 0) {
		error("failed to sync log holes\n");
	}

	return retval;
}

static int do_stoprun(simulator_agent_t sa, int token, char *args)
{
	int retval = 0;

	assert(sa != NULL);
	assert(args != NULL);

	/*
	 * Not allowed to use waitmode; will deadlock the event system!
	 */
	if (systemf("template_stoprun -t %d -p %s -e %s",
		    token, pid, eid) != 0) {
		error("failed to stop current run\n");
		retval = -1;
	}

	return retval;
}

static int strreltime(char *buf, size_t buflen, time_t secs)
{
    int hours, mins, retval = 0;
    char *signage = "";
    
    assert(buf != NULL);
    
    if (secs < 0) {
	secs = abs(secs);
	signage = "-";
    }
    
    hours = secs / (60 * 60);
    mins = ((secs - (hours * 60 * 60)) / 60) % 60;
    secs = secs % 60;

    if (hours)
	snprintf(buf, buflen, "%s%dh%dm%ds", signage, hours, mins, secs);
    else if (mins)
	snprintf(buf, buflen, "%s%dm%ds", signage, mins, secs);
    else
	snprintf(buf, buflen, "%s%ds", signage, secs);
    return retval;
}

static int do_log(simulator_agent_t sa, char *message)
{
	time_t current_time;
	int retval = -1;
	char date[256];
	
	current_time = time(NULL);
	if (sa->sa_flags & SAF_TIME_STARTED) {
		strreltime(date,
			   sizeof(date),
			   current_time - sa->sa_time_start.tv_sec);
	}
	else {
		ctime_r(&current_time, date);
		date[strlen(date) - 1] = '\0';
	}
	strcat(date, " - ");
	fprintf(stderr, "log: %s%s\n", date, message);
	add_report_data(sa, SA_RDK_LOG, date, 0);
	add_report_data(sa, SA_RDK_LOG, message, SA_RDF_NEWLINE);

	return retval;
}

static void *simulator_agent_looper(void *arg)
{
	simulator_agent_t sa = (simulator_agent_t)arg;
	event_handle_t handle;
	void *retval = NULL;
	sched_event_t se;
	
	assert(arg != NULL);

	handle = sa->sa_local_agent.la_handle;

	while (local_agent_dequeue(&sa->sa_local_agent, 0, &se) == 0) {
		char evtype[TBDB_FLEN_EVEVENTTYPE];
		char argsbuf[BUFSIZ];

		assert(se.length == 1);
		
		if (!event_notification_get_eventtype(
			handle, se.notification, evtype, sizeof(evtype))) {
			error("couldn't get event type from notification %p\n",
			      se.notification);
		}
		else {
544 545
			int rc = 0;
			int32_t token = ~0;
546 547 548 549 550 551 552 553
			
			event_notification_get_arguments(handle,
							 se.notification,
							 argsbuf,
							 sizeof(argsbuf));
			event_notification_get_int32(handle,
						     se.notification,
						     "TOKEN",
554
						     &token);
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
			argsbuf[sizeof(argsbuf) - 1] = '\0';

			/* Strictly for the event viewer */
			event_notify(handle, se.notification);

			if (strcmp(evtype, TBDB_EVENTTYPE_SWAPOUT) == 0) {
				EmulabResponse er;
				
				RPC_grab();
				RPC_invoke("experiment.swapexp",
					   &er,
					   SPA_String, "proj", pid,
					   SPA_String, "exp", eid,
					   SPA_String, "direction", "out",
					   SPA_TAG_DONE);
				RPC_drop();
			}
			else if (strcmp(evtype, TBDB_EVENTTYPE_MODIFY) == 0) {
				do_modify(sa, token, argsbuf);
			}
			else if (strcmp(evtype, TBDB_EVENTTYPE_HALT) == 0) {
				rc = systemf("endexp %s %s", pid, eid);
			}
			else if (strcmp(evtype, TBDB_EVENTTYPE_DEBUG) == 0) {
				fprintf(stderr, "debug: %s\n", argsbuf);
			}
			else if (strcmp(evtype, TBDB_EVENTTYPE_MESSAGE) == 0) {
				fprintf(stderr, "msg: %s\n", argsbuf);
				add_report_data(sa,
						SA_RDK_MESSAGE,
						argsbuf,
						SA_RDF_NEWLINE);
			}
			else if (strcmp(evtype, TBDB_EVENTTYPE_LOG) == 0) {
				do_log(sa, argsbuf);
			}
			else if (strcmp(evtype, TBDB_EVENTTYPE_REPORT) == 0) {
				do_log(sa, "Sending report\n");
				send_report(sa, argsbuf);
			}
			else if (strcmp(evtype, TBDB_EVENTTYPE_RESET) == 0) {
				do_reset(sa, argsbuf);
			}
			else if (strcmp(evtype, TBDB_EVENTTYPE_SNAPSHOT) == 0){
				do_snapshot(sa, argsbuf);
			}
			else if (strcmp(evtype, TBDB_EVENTTYPE_STOPRUN) == 0){
				do_stoprun(sa, token, argsbuf);
			}
			else {
				error("cannot handle SIMULATOR event %s.",
				      evtype);
			}
			if (strcmp(evtype, TBDB_EVENTTYPE_RESET) == 0 ||
			    strcmp(evtype, TBDB_EVENTTYPE_REPORT) == 0 ||
			    strcmp(evtype, TBDB_EVENTTYPE_LOG) == 0 ||
			    strcmp(evtype, TBDB_EVENTTYPE_SNAPSHOT) == 0 ||
			    strcmp(evtype, TBDB_EVENTTYPE_MODIFY) == 0) {
				event_do(handle,
					 EA_Experiment, pideid,
					 EA_Type, TBDB_OBJECTTYPE_SIMULATOR,
					 EA_Name,
					 sa->sa_local_agent.la_link.ln_Name,
					 EA_Event, TBDB_EVENTTYPE_COMPLETE,
					 EA_ArgInteger, "ERROR", rc != 0,
					 EA_ArgInteger, "CTOKEN", token,
					 EA_TAG_DONE);
			}
		}

		sched_event_free(handle, &se);
	}

	return retval;
}