Esempio n. 1
0
int _tmain(int argc, _TCHAR* argv[])
{
	if(MAIN_DELAY_SECONDS > 0)
		delay_start();

	PandemicSim *sim = new PandemicSim();
	
	sim->timeSimulation();
	getchar();
}
Esempio n. 2
0
static int
delimit_session(char **argv)
{
	struct timeval time_start, time_end;
	time_t the_time;
	struct rusage ru;
	unsigned left_over;
	pid_t pid;
	int status;
	int ret = 0;

	/*
	 * take care of the easy case first: no command to start
	 */
	if (argv == NULL || *argv == NULL) {

		if (options.trigger_delay && delay_start() == -1)
			return -1;

		/*
		 * this will start the session in each "worker" thread
		 */
		pthread_barrier_wait(&barrier.barrier);

		time(&the_time);
		vbprintf("measurements started on %s\n", asctime(localtime(&the_time)));

		the_time = 0;

		if (options.session_timeout != PFMON_NO_TIMEOUT) {
			printf("<session to end in %u seconds>\n", options.session_timeout);

			left_over = sleep(options.session_timeout);
			if (left_over)
				pfmon_print_quit_reason(quit_reason);
			else
				time(&the_time);
		} else {
			printf("<press ENTER to stop session>\n");

			ret = getchar();
			if (ret == EOF) 
				pfmon_print_quit_reason(quit_reason);
			else
				time(&the_time);
		}
		if (the_time) vbprintf("measurements completed at %s\n", asctime(localtime(&the_time)));

		return 0;
	}
	gettimeofday(&time_start, NULL);

	/*
	 * we fork+exec the command to run during our system wide monitoring
	 * session. When the command ends, we stop the session and print
	 * the results.
	 */
	if ((pid=fork()) == -1) {
		warning("Cannot fork new process\n");
		return -1;
	}

	if (pid == 0) {		 
		pid = getpid();

		if (options.opt_verbose) {
			char **p = argv;
			vbprintf("starting process [%d]: ", pid);
			while (*p) vbprintf("%s ", *p++);
			vbprintf("\n");
		}
		if (options.opt_pin_cmd) {
			vbprintf("applied cpu-list for %s\n", *argv);
			if (pfmon_set_affinity(pid,  options.virt_cpu_mask)) {
				warning("could not pin %s to cpu-list\n");
			}
		}
		/*
		 * The use of ptrace() allows us to actually start monitoring after the exec()
		 * is done, i.e., when the new program is ready to go back to user mode for the
		 * "first time". With this technique, we can actually activate the workers
		 * only when the process is ready to execute. Hence, we can capture even
		 * the short lived workloads without measuring the overhead caused by fork/exec.
		 * We will capture the overhead of the PTRACE_DETACH, though.
		 */
		if (options.trigger_delay == 0) {
			if (ptrace(PTRACE_TRACEME, 0, NULL, NULL) == -1) {
				warning("cannot ptrace me: %s\n", strerror(errno));
				exit(1);
			}
		}
		if (options.opt_cmd_no_verbose) {
			dup2 (open("/dev/null", O_WRONLY), 1);
			dup2 (open("/dev/null", O_WRONLY), 2);
		}	

		execvp(argv[0], argv);

		warning("child: cannot exec %s: %s\n", argv[0], strerror(errno));
		exit(-1);
	} 

	if (options.trigger_delay) {
		if (delay_start() == -1) {
			warning("process %d terminated before session was activated, nothing measured\n", pid);
			return -1;
		}
	} else {
		vbprintf("waiting for [%d] to exec\n", pid);
		/* 
	 	 * wait for the child to exec 
	 	 */
		waitpid(pid, &status, WUNTRACED);
	}

	/*
	 * this will start the session in each "worker" thread
	 *
	 */
	pthread_barrier_wait(&barrier.barrier);

	/*
	 * let the task run free now
	 */
	if (options.trigger_delay == 0)	ptrace(PTRACE_DETACH, pid, NULL, NULL);

	ret = wait4(pid, &status, 0, &ru);

	gettimeofday(&time_end, NULL);

	if (ret == -1) {
		if (errno == EINTR) { 
			pfmon_print_quit_reason(quit_reason);
			ret = 0;  /* will cause the session to print results so far */
		} else {
			vbprintf("unexpected wait() error for [%d]: %s\n", pid, strerror(errno));
		}
	} else {
		if (WEXITSTATUS(status) != 0) {
			warning("process %d exited with non zero value (%d): results may be incorrect\n", 
				pid, WEXITSTATUS(status));
		}
		if (options.opt_show_rusage) show_task_rusage(&time_start, &time_end, &ru);
	}
	return ret;
}