Exemple #1
0
Fichier : gc.c Projet : hsk/docs
/*
void test_pipes1() {
  enum {frame_START, frame_SIZE, A, B, C, frame_END};
  ENTER_FRAME_ENUM(frame);

  A = gc_alloc_int(1); // 1
  assert(vm->heap_num==1);

  PUSH_VM(vm1);

    assert(vm->heap_num==0);
    frame[B] = test_new_vm2(A);
    assert(vm->heap_num==4);
    frame[B] = test_new_vm2(frame[B]);
    assert(vm->heap_num==8);
    frame[B] = test_new_vm2(frame[B]);
    assert(vm->heap_num==12);
  printf("id change check.........\n");
  POP_VM(vm1,frame[B]);
  assert(vm->heap_num==8);
  printf("id change check.........\n");
  gc_collect();
  assert(vm->heap_num==7);
  LEAVE_FRAME(frame);
}

void test_pipes2() {
  enum {frame_START, frame_SIZE, A, B, C, frame_END};
  ENTER_FRAME_ENUM(frame);

  A = gc_alloc_int(1); // 1

  assert(vm->heap_num==1);
  PUSH_VM(vm1);
    assert(vm->heap_num==0);
    frame[B] = test_new_vm2(A); // 生きているのが2つ死んでいるのが2つ
    assert(vm->heap_num==4);
    gc_collect_pipe(frame[B]);// bだけコピーして後は消す
    assert(vm->heap_num==2);
    frame[B] = test_new_vm2(frame[B]);// bを渡すと 2つのデータが入ってるのを渡す
    assert(vm->heap_num==6); // 生きてる4、死んでる2
    gc_collect_pipe(frame[B]);// bだけコピーして後は消す
    assert(vm->heap_num==4);// 生きてる4
    frame[B] = test_new_vm2(frame[B]);// 4+4=
    assert(vm->heap_num==8);// 4 + 4 = 8 生きてる6死んでる2と
    gc_collect_pipe(frame[B]);// bだけコピーして後は消す
    assert(vm->heap_num==6);// 生きてる6

  printf("id change check.........\n");
  POP_VM(vm1, frame[B]); // 世界が終わる
  // ヒープ上には、元のデータ1と世界のデータ1と6つの生きているで8個
  assert(vm->heap_num==8);
  printf("id change check.........\n");
  gc_collect();// gcで世界のデータが消える。
  assert(vm->heap_num==7);
  LEAVE_FRAME(frame);
}
*/
void test_multi_vm() {
    ENTER_FRAME(frame,8);
    Object* A = pool(gc_alloc_int(1));

    assert(vm->heap_num==1);
    VM* tmp_vm = pool(vm);
    VM* VM1 = pool(vm_new());// 世界を作る
    VM* VM2 = pool(vm_new());// 世界を作る
    assert(vm->heap_num==3);
    vm = VM1;// 世界を移動
    assert(vm->heap_num==0);
    vm->record = test_int(A->intv);// 計算する
    assert(vm->heap_num==1);

    Object* C;
    vm = VM2;// 世界を移動
    assert(vm->heap_num==0);
    C = pool(test_int(A->intv));// 計算する
    assert(vm->heap_num==1);
    vm_end(C, tmp_vm);

    vm = tmp_vm;// 元に戻る
    assert(vm->heap_num==4);

    Object* B = pool(vm_get_record(VM1));// コピーとる
    assert(vm->heap_num==5);
    printf("id change check.........\n");
    gc_collect();
    assert(vm->heap_num==5);
    LEAVE_FRAME(frame);
}
Exemple #2
0
Fichier : test.c Projet : hsk/docs
void get_action() {
  printf("HTTP/1.0 200 OK\n");
  printf("text/html\n");
  printf("Cache-Control: max-age=0\n\n");

  printf("<html>\n");
  printf("<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />\n");
  printf("<body>\n");
  printf("<h1>test gc</h1>");

  printf("start test\n");
  enum {frame_START, frame_SIZE, VM1, VM2, A, B, C, frame_END};
  ENTER_FRAME_ENUM(frame);
  frame[A] = gc_alloc_int(1);

  printf("test\n");

  assert(vm->heap_num==1);
  VM* tmp_vm = vm;
  frame[VM1] = (Object*)vm_new();// 世界を作る
  frame[VM2] = (Object*)vm_new();// 世界を作る
  assert(vm->heap_num==3);

  vm = (VM*)frame[VM1];// 世界を移動
    assert(vm->heap_num==0);
    vm->record = gc_alloc_int(frame[A]->intv);// 計算する
    printf("vm = %d\n", vm->record->intv);
    assert(vm->heap_num==1);

  vm = (VM*)frame[VM2];// 世界を移動
    assert(vm->heap_num==0);
    vm->record = gc_alloc_int(frame[A]->intv);// 計算する
    assert(vm->heap_num==1);

  vm = tmp_vm;// 元に戻る
  assert(vm->heap_num==3);

    printf("vm = %d\n", ((VM*)frame[VM1])->record->intv);

  frame[B] = vm_get_record((VM*)frame[VM1]);// コピーとる
  frame[C] = vm_get_record((VM*)frame[VM2]);// コピーとる
  assert(vm->heap_num==5);
  frame[VM1] = NULL; // 世界を消す
  frame[VM2] = NULL; // 世界を消す
  //gc_collect();
  //assert(vm->heap_num==3);
  printf("test888 %d %d\n", frame[A]->intv, frame[B]->intv+frame[C]->intv);
  LEAVE_FRAME(frame);
  printf("<hr/>\n");
  printf("<a href=\"javascript:history.back()\">back</a>\n");
  printf("</body>\n");
  printf("</html>\n");
}
Exemple #3
0
int main(int argc, char** argv)
{
    int fp = open(argv[1], O_RDONLY);
    if(fp < 0) {
        perror("Error opening file");
        return 1;
    }

    VmFile vmfile = vmfile_open(fp);
    if(vmfile == NULL) {
        printf("Error: %d\n", vmerrno);
        return vmerrno;
    }

    printf("Header: %s\n", vmfile_header_get(vmfile));
    printf("Version : %d.%d\n", vmfile_version_major_get(vmfile),\
            vmfile_version_minor_get(vmfile));
    printf("Start Of Code Segment: %d\n", vmfile_entry_get(vmfile));
    printf("Instructions: %d\n", vmfile_size_get(vmfile));
    printf("Instruction Dump:\n");
    vmfile_inst_print(vmfile);

    printf("Output of Program\n");
    VmState state = vmstate_new(vmfile);
    Vm vm = vm_new(vmfile, state);
    vm_start(vm);

    vmfile_close(vmfile);
    vmstate_del(state);
    vm_del(vm);
    
    return 0;
}
/**
 * Creates a new instance of Gunderscript object with a Virtual
 * Machine only.
 * instance: pointer to a Gunderscript object that will receive
 * the instance.
 * stackSize: the size for the VM stack in bytes. The VM does not
 * dynamically resize so this value is absolute for this instance.
 * callbacksSize: the number of callbacks slots. This number defines
 * how many native functions can be bound to this instance. Increase
 * this value if vm_reg_callback() fails, or if gunderscript_new()
 * always returns false.
 * returns: true if creation succeeds, and false if fails. Failure can
 * occur due to malloc failure or if callbacksSize is too small to
 * contain all of the standard libraries.
 */
GSAPI bool gunderscript_new_vm(Gunderscript * instance, size_t stackSize,
		      int callbacksSize) {
  assert(instance != NULL);
  assert(stackSize > 0);
  assert(callbacksSize > 0);

  /* allocate virtual machine */
  instance->vm = vm_new(stackSize, callbacksSize);
  instance->err = GUNDERSCRIPTERR_SUCCESS;
  if(instance->vm == NULL) {
    return false;
  }

  /* initialize system libraries */
  if(!libsys_install(instance)
     || !libmath_install(instance)
     || !libstr_install(instance)
     || !libarray_install(instance)) {
    vm_free(instance->vm);
    return false;
  }

  instance->compiler = NULL;

  return true;
}
Exemple #5
0
int
main(int argc, char* argv[]) {
  if (argc < 2) return -1;
  char *buf = read_file(argv[1]);
  struct VM* vm = vm_new(4000);
  value res = vm_run(vm, buf);
  print_value(res);
  vm_close(vm);
  free(buf);
  return 0;
}
Exemple #6
0
// Execute a command
// Yes, this is only a bunch of hardcoded crap
static void executeCommand(char *command, int argc, char **argv)
{
	if(strcmp(command, "reboot") == 0) reboot();
	else if(strcmp(command, "clear") == 0) printf("\e[H\e[2J");
	else if(strcmp(command, "pid") == 0)
	{
		task_t* proc = scheduler_getCurrentTask();
		if(proc != NULL)
			printf("procnum: %d\n", proc->pid);
	}
	else if(strcmp(command, "halt") == 0) halt();
	else if(strcmp(command, "freeze") == 0) freeze();
	else if(strcmp(command, "panic") == 0) panic("Test panic for debugging");
	else if(strcmp(command, "kill") == 0) asm("mov %eax, 1; int 0x80;");
	else if(strcmp(command, "triplefault") == 0)
	{
		struct vm_context *ctx = vm_new();
		paging_apply(ctx);
	}
	else if(strcmp(command, "pagefault") == 0)
		*((char *)vm_faultAddress) = 0;
	else if(strcmp(command, "reload") == 0)
		paging_apply(vm_kernelContext);
	else if(strcmp(command, "rebuild") == 0)
	{
		vm_set_cache(vm_kernelContext, NULL);
		paging_apply(vm_kernelContext);
	}
	else if(strcmp(command, "dump") == 0)
		vm_dump(vm_currentContext);
    else if (strcmp(command, "kb") == 0)
    {
        if (argc != 1)
        {
            printf("usage: kb <layoutname>\n");
            return;
        }

        if (keyboard_setlayout(argv[0]) == -1)
        {
            printf("unknown layout\n");
            return;
        }
    }
	else
	{
		if(strlen(command) > 0 && command[0] != '#')
			printf("error: command '%s' not found.\n", command);
	}
}
Exemple #7
0
int main() {
	try {
		int num_cpus = 1;
		vm_new(num_cpus);
		int cpu = 0;
		Registers* regs = cpu_map_registers(cpu);
		cpu_run(cpu);
	}
	catch (std::exception& e) {
		printf("error: %s\n", e.what());
	}
	vm_cleanup();
	return 0;
}
Exemple #8
0
struct process *
process_spawn(struct closure *k)
{
	struct vm *vm;
	struct process *p;

	vm = vm_new(current_process->vm->program, current_process->vm->prog_size);
	vm_set_pc(vm, k->label);

	vm->current_ar = activation_new_on_heap(
	    k->arity + k->locals, NULL, k->ar);

	p = process_new(vm);
#ifdef DEBUG
	if (trace_scheduling)
		printf("process #%d created\n", p->number);
#endif

	return(p);
}
Exemple #9
0
int
main(int argc, char **argv)
{
	char **real_argv = argv;
	struct scan_st *sc;
	struct symbol_table *stab;
	struct ast *a;
	char *source = NULL;
	int opt;
	int err_count = 0;
#ifdef DEBUG
	int run_program = 1;
	int dump_symbols = 0;
	int dump_program = 0;
	int dump_icode = 0;
#endif

#ifdef DEBUG
	setvbuf(stdout, NULL, _IOLBF, 0);
#endif

	/*
	 * Get command-line arguments.
	 */
	while ((opt = getopt(argc, argv, OPTS)) != -1) {
		switch(opt) {
#ifdef DEBUG
		case 'c':
			trace_scheduling++;
			break;
#ifdef POOL_VALUES
		case 'd':
			trace_pool++;
			break;
#endif
		case 'g':
			trace_gc++;
			break;
#endif
		case 'G':
			gc_trigger = atoi(optarg);
			break;
#ifdef DEBUG
		case 'i':
			dump_icode++;
			break;
		case 'l':
			trace_gen++;
			break;
		case 'm':
			trace_vm++;
			break;
		case 'n':
			run_program = 0;
			break;
		case 'o':
			trace_valloc++;
			break;
		case 'p':
			dump_program = 1;
			break;
		case 's':
			dump_symbols = 1;
			break;
		case 'v':
			trace_activations++;
			break;
		case 'y':
			trace_type_inference++;
			break;
#endif
		case '?':
		default:
			usage(argv);
		}
	}
	argc -= optind;
	argv += optind;

	if (*argv != NULL)
		source = *argv;
	else
		usage(real_argv);

#ifdef POOL_VALUES
	value_pool_new();
#endif

	gc_target = gc_trigger;
	if ((sc = scan_open(source)) != NULL) {
		stab = symbol_table_new(NULL, 0);
		global_ar = activation_new_on_heap(100, NULL, NULL);
		register_std_builtins(stab);
		report_start();
		a = parse_program(sc, stab);
		scan_close(sc);
#ifdef DEBUG
		if (dump_symbols)
			symbol_table_dump(stab, 1);
		if (dump_program) {
			ast_dump(a, 0);
		}
#endif
#ifndef DEBUG
		symbol_table_free(stab);
		types_free();
#endif
		err_count = report_finish();
		if (err_count == 0) {
			struct iprogram *ip;
			struct vm *vm;
			struct process *p;
			unsigned char *program;

			program = bhuna_malloc(16384);

			ip = ast_gen_iprogram(a);
			iprogram_eliminate_nops(ip);
			iprogram_eliminate_useless_jumps(ip);
			iprogram_optimize_tail_calls(ip);
			iprogram_optimize_push_small_ints(ip);
			iprogram_eliminate_dead_code(ip);
			iprogram_gen(&program, ip);
#ifdef DEBUG
			if (dump_icode > 0)
				iprogram_dump(ip, program);
#endif

			vm = vm_new(program, 16384);
			vm_set_pc(vm, program);
			vm->current_ar = global_ar;
			p = process_new(vm);
			/* ast_dump(a, 0); */
			if (RUN_PROGRAM) {
				process_scheduler();
			}
			vm_free(vm);
			bhuna_free(program);
			/*value_dump_global_table();*/
		}

		ast_free(a);	/* XXX move on up */
		/* gc(); */		/* actually do a full blow out at the end */
		/* activation_free_from_stack(global_ar); */
#ifdef DEBUG
		symbol_table_free(stab);
		types_free();
		if (trace_valloc > 0) {
			/*
			value_dump_global_table();
			*/
			printf("Created:  %8d\n", num_vars_created);
			printf("Cached:   %8d\n", num_vars_cached);
			printf("Freed:    %8d\n", num_vars_freed);
		}
		if (trace_activations > 0) {
			printf("AR's alloc'ed:  %8d\n", activations_allocated);
			printf("AR's freed:     %8d\n", activations_freed);
		}
#ifdef POOL_VALUES
		if (trace_pool > 0) {
			pool_report();
		}
#endif
#endif
		return(0);
	} else {
		fprintf(stderr, "Can't open `%s'\n", source);
		return(1);
	}
}
Exemple #10
0
int main(int argc, char **argv) {
	/* SIMULATION VARIABLES */
	int n = 0;      // processes
	int cs_t = 13;  // context switch time (in ms)
	FILE *inf;      // input file (processes.txt)
	FILE *outf;   // output file (simout.txt)

	// recording variables
	float a_cpu_t = 0.0;  // average cpu burst time
	float a_turn_t = 0.0; // average turnaround time
	float a_wait_t = 0.0; // average wait time
	int total_cs = 0;     // total context switches
	int total_burst = 0;  // total cpu bursts
	int total_mem = 256;  // total virtual memory
	int total_t = 0;      // total time
	int total_d = 0;      // total defrag time
	int t_slice = 80;     // round robin time slice
	int t_memmove = 10;   // defragmentation memmove time

	if(argc != 1) {
		printf("There are no arguments associated with this simulation. Input is read from processes.txt.\n");
		return 1;
	}

	// priority queues and process
	priority_queue srtf = pq_new(n), srtn = pq_new(n), srtb = pq_new(n), rrf = pq_new(n), rrn = pq_new(n), rrb = pq_new(n);
	process *p1, *p2, *p3, *p4, *p5, *p6;

	/* read file */
	if((inf = fopen("processes.txt", "r")) == NULL) {
		perror("open() failed");
		return EXIT_FAILURE;
	}

	if((outf = fopen("simout.txt", "w")) == NULL) {
		perror("fopen() failed");
		return EXIT_FAILURE;
	}

	// get current line
	char *line = NULL;
	size_t nbytes = 0;

	while(getline(&line, &nbytes, inf) != -1) {
		// trim the string first
		char *newline;
		newline = trim(line);

		if(*newline != 0) {
			// get values
			char pn;
			int at, bt, bn, it, mem;
			sscanf(newline, "%c|%i|%i|%i|%i|%i", &pn, &at, &bt, &bn, &it, &mem);

			// allocate and init
			p1 = malloc(sizeof(process));
			p2 = malloc(sizeof(process));
			p3 = malloc(sizeof(process));
			p4 = malloc(sizeof(process));
			p5 = malloc(sizeof(process));
			p6 = malloc(sizeof(process));
			*p1 = proc_new(pn, at, bt, bn, it, 0, mem);
			*p2 = *p1;
			*p3 = *p1;
			*p4 = *p1;
			*p5 = *p1;
			*p6 = *p1;

			// calculate burst statistics
			a_cpu_t += bt * bn;
			total_burst += bn;

			// push to queues
			++n;
			pq_push(srtf, p1, at);
			pq_push(srtn, p2, at);
			pq_push(srtb, p3, at);
			pq_push(rrf, p2, at);
			pq_push(rrn, p5, at);
			pq_push(rrb, p6, at);
		}
	}

	// free memory
	free(line);

	// create virtual memory
	v_memory vm_ff = vm_new(total_mem, 'f');
	v_memory vm_nf = vm_new(total_mem, 'n');
	v_memory vm_bf = vm_new(total_mem, 'b');

	/* ===begin simulations=== */
	a_cpu_t /= total_burst;
	s_srt(srtf, vm_ff, n, cs_t, &a_wait_t, &total_cs, &total_t, &total_d, t_memmove);
	simout("SRT", "First-Fit", outf, &a_cpu_t, &a_turn_t, &a_wait_t, &total_cs, total_burst, total_t, total_d);
	printf("\n\n");
	s_srt(srtn, vm_nf, n, cs_t, &a_wait_t, &total_cs, &total_t, &total_d, t_memmove);
	simout("SRT", "First-Fit", outf, &a_cpu_t, &a_turn_t, &a_wait_t, &total_cs, total_burst, total_t, total_d);
	printf("\n\n");
	s_srt(srtb, vm_bf, n, cs_t, &a_wait_t, &total_cs, &total_t, &total_d, t_memmove);
	simout("SRT", "First-Fit", outf, &a_cpu_t, &a_turn_t, &a_wait_t, &total_cs, total_burst, total_t, total_d);
	printf("\n\n");
	s_rr(rrf, vm_ff, n, cs_t, &a_wait_t, &total_cs, &total_t, &total_d, t_memmove, t_slice);
	simout("RR", "First-Fit", outf, &a_cpu_t, &a_turn_t, &a_wait_t, &total_cs, total_burst, total_t, total_d);
	printf("\n\n");
	s_rr(rrn, vm_nf, n, cs_t, &a_wait_t, &total_cs, &total_t, &total_d, t_memmove, t_slice);
	simout("RR", "First-Fit", outf, &a_cpu_t, &a_turn_t, &a_wait_t, &total_cs, total_burst, total_t, total_d);
	printf("\n\n");
	s_rr(rrb, vm_bf, n, cs_t, &a_wait_t, &total_cs, &total_t, &total_d, t_memmove, t_slice);
	simout("RR", "First-Fit", outf, &a_cpu_t, &a_turn_t, &a_wait_t, &total_cs, total_burst, total_t, total_d);

	// close streams
	fclose(inf);
	fclose(outf);

	// free memory and exit gracefully
	vm_free(vm_ff);
	vm_free(vm_nf);
	vm_free(vm_bf);
	pq_free(srtf);
	pq_free(srtn);
	pq_free(srtb);
	pq_free(rrf);
	pq_free(rrn);
	pq_free(rrb);
	free(srtf);
	free(srtn);
	free(srtb);
	free(rrf);
	free(rrn);
	free(rrb);
	return EXIT_SUCCESS;
}