예제 #1
0
파일: show-tex-list.c 프로젝트: t-k-/OPMES
int main()
{
	char     *env_input;
	char     path[4096];

	/* CGI initial print */
	printf("Content-type: text/html\n\n");

	/* create trace/log */
	trace_init("list.log");

	/* get GET content from environment variable */ 
	env_input = getenv("QUERY_STRING");
	if (env_input == NULL) {
		trace(WEB, "No env variable: QUERY_STRING.\n", NULL);
		goto exit;
	}

	/* extract page number from GET content */
	sscanf(env_input, "p=%s", path);

	/* echo HTML */
	cat("head.cat");
	printf("file: %s <br/>", basename(path));
	printf("[dmath]%s[/dmath]", first_line(path));
	cat("neck.cat");

	echo_tex_li(path);
	
	cat("ass.cat");
	cat("tail.cat");

exit:
	/* delete trace/log */
	trace_unfree();
	trace_uninit();
	return 0;
}
예제 #2
0
int main(int argc, char **argv)
{
    char *trace_file_name;
    int trace_view_on = 0;
    int branch_prediction_method = 0;
    unsigned int cycle_number = 0;

    // Parse Inputs
    if (argc == 2) {
        trace_file_name = argv[1];
        trace_view_on = 0;
        branch_prediction_method = 0;       
    } else if (argc == 4) {
        trace_file_name = argv[1];
        trace_view_on = atoi(argv[2]);
        branch_prediction_method = (atoi(argv[3]) == 1) ? 1 : 0;
    } else {
        fprintf(stdout, "\nUSAGE: tv <trace_file> <switch - any character> <branch_prediction - 0|1>\n");
        fprintf(stdout, "\n(switch) to turn on or off individual item view.\n");
        fprintf(stdout, "(branch_prediction) sets the branch prediction method as \'assume not taken\' (0), or a 1-bit branch predictor (1)\n\n");
        exit(0);
    }

    // debug - print parsed options
    char dbg_msg[200];
    sprintf(dbg_msg,
            "\n-debug- parsed inputs. file=%s, view_trace=%d, branch_pred=%d\n",
            trace_file_name,
            (trace_view_on == 0) ? 0 : 1,
            (branch_prediction_method == 0) ? 0 : 1
           );
    debug_print(dbg_msg);

    // Open the trace file.
    fprintf(stdout, "\n ** opening file %s\n", trace_file_name);
    trace_fd = fopen(trace_file_name, "rb");
    if (!trace_fd) {
        fprintf(stdout, "\ntrace file %s not opened.\n\n", trace_file_name);
        exit(0);
    }

    trace_init();

    // store what instruction is in each stage of the pipeline (can be no-ops)
    struct trace_item new_instruction;
    struct trace_item if_stage;
    struct trace_item id_stage;
    struct trace_item ex_stage;
    struct trace_item mem_stage;
    struct trace_item wb_stage;
    zero_buf(&new_instruction);
    zero_buf(&if_stage);
    zero_buf(&id_stage);
    zero_buf(&ex_stage);
    zero_buf(&mem_stage);
    zero_buf(&wb_stage);
    memset(&btb_table, 0, sizeof(short) * BTB_ENTRIES);

    int instructions_left = 5;  
    while(instructions_left) {
        cycle_number++;

        if(trace_view_on) { 
            print_finished_instruction(&wb_stage, cycle_number);
        }

        int hazard = 0;

        //detection of non-happy
        if(branch_prediction_method == 0 && ex_stage.type == ti_BRANCH){
            if(ex_stage.PC + 4 != id_stage.PC){
                hazard = 2; //incorrect no prediction
                debug_print("[HAZARD] Incorrect default (not taken) prediciton\n");
            }
        }
        else if(ex_stage.type == ti_JTYPE|| ex_stage.type == ti_JRTYPE){
            hazard = 2;  //jump
            debug_print("[HAZARD] jump\n");
        }
        else if(branch_prediction_method == 1 && ex_stage.type == ti_BRANCH){
            if(ex_stage.PC +4 == id_stage.PC){ // not taken
                if(get_btb_value(ex_stage.PC) == 1){ //predict taken
                    hazard = 2; //branch
                    debug_print("[HAZARD] predicted taken when not taken\n");
                    set_btb_value(ex_stage.PC, 0);
                }
            }
            else{ //taken
                if(get_btb_value(ex_stage.PC) == 0){ //predict not taken
                    hazard = 2; //branch
                    debug_print("[HAZARD] predicted not taken when taken\n");
                    set_btb_value(ex_stage.PC, 1);
                }
            }
        }
        else if(ex_stage.type == ti_LOAD){
            if(ex_stage.dReg == id_stage.sReg_a || ex_stage.dReg == id_stage.sReg_b){
                hazard = 1;  //forward
                debug_print("[HAZARD] forward\n");
            }
        }


        wb_stage  = mem_stage;
        mem_stage = ex_stage; 

        switch(hazard) {
            case 0: //happy path
                ex_stage = id_stage;
                id_stage = if_stage;
                if(!read_instruction(&if_stage)) {
                    instructions_left--;
                    zero_buf(&if_stage);
                }
                break;

            case 1: //forward
                zero_buf(&ex_stage); 
                break;

            case 2: //branch resolve/jump resolve
                add_queued_instruction(&id_stage);
                add_queued_instruction(&if_stage);
                zero_buf(&id_stage);
                zero_buf(&if_stage);  
                ex_stage = id_stage;
                id_stage = if_stage;
                if(!read_instruction(&if_stage)) {
                    instructions_left--;
                    zero_buf(&if_stage);
                }
                break;

            default:
                printf("f**k.\n");
                exit(1);
        }
    }

    printf("+ Simulation terminates at cycle : %u\n", cycle_number); 

    trace_uninit();

    exit(0);
}
예제 #3
0
파일: test-score.c 프로젝트: t-k-/OPMES
int main()
{
	struct list_it li_subpath = LIST_NULL;
	struct subpath *p;
	struct searcher *se;
	struct score_cache *cache;
	struct score_tr *st; 

	struct brw brw;
	brw.pin[1] = 0;
	brw.fan[0] = 1;
	brw.fan[1] = 0;
	
	printf("size=%ld\n", sizeof(struct score_tr) + 
	       sizeof(struct searcher) + sizeof(struct score_cache));
	trace_init("test-score.log");

	p = cp_malloc(sizeof(struct subpath));
	strcpy(p->dir, "a dir");
	p->brw.symbol_id = S_Alpha;
	p->brw.pin[0] = 2;
	p->brw.fan[0] = 1;
	p->brw.pin[1] = 0;
	p->brw.fan[1] = 0;
	LIST_NODE_CONS(p->ln);
	list_insert_one_at_tail(&p->ln, &li_subpath, NULL, NULL);

	p = cp_malloc(sizeof(struct subpath));
	strcpy(p->dir, "b dir");
	p->brw.symbol_id = S_Alpha;
	p->brw.pin[0] = 1;
	p->brw.fan[0] = 1;
	p->brw.pin[1] = 0;
	p->brw.fan[1] = 0;
	LIST_NODE_CONS(p->ln);
	list_insert_one_at_tail(&p->ln, &li_subpath, NULL, NULL);

	p = cp_malloc(sizeof(struct subpath));
	strcpy(p->dir, "a dir");
	p->brw.symbol_id = S_Beta;
	p->brw.pin[0] = 3;
	p->brw.fan[0] = 1;
	p->brw.pin[1] = 0;
	p->brw.fan[1] = 0;
	LIST_NODE_CONS(p->ln);
	list_insert_one_at_tail(&p->ln, &li_subpath, NULL, NULL);

	//subpaths_print(&li_subpath, stdout);

	/* transfer subpath into searcher */
	se = searcher_new();
	
	searcher_cons(se, &li_subpath);
	subpaths_free(&li_subpath);
	
	/* score tree construction */
	st = score_tr_new();
	score_tr_setup(st, se->n_qpath);

	cache = cp_malloc(sizeof(struct score_cache));

	{ /* search loop */
		brw.symbol_id = S_alpha;
		brw.pin[0] = 12;
		score_push(st, se, cache, 0, &brw, 0);

		brw.symbol_id = S_beta;
		brw.pin[0] = 21;
		score_push(st, se, cache, 0, &brw, 0);

		brw.symbol_id = S_alpha;
		brw.pin[0] = 22;
		score_push(st, se, cache, 1, &brw, 0);

		printf(C_MAGENTA "before calc:\n" C_RST);
		score_tr_print(st, stdout);
		searcher_print(se, stdout);

		score_calc(st, se, cache, 0);
		printf(C_CYAN "score = %d\n" C_RST, st->score);

		score_tr_clear(st);
		searcher_clear(se);

		printf(C_MAGENTA "after clear:\n" C_RST);
		score_tr_print(st, stdout);
		searcher_print(se, stdout);

/* redundance */
		brw.symbol_id = S_alpha;
		brw.pin[0] = 1;
		score_push(st, se, cache, 0, &brw, 0);

		brw.symbol_id = S_beta;
		brw.pin[0] = 2;
		score_push(st, se, cache, 1, &brw, 0);

		brw.symbol_id = S_alpha;
		brw.pin[0] = 3;
		score_push(st, se, cache, 0, &brw, 0);

		brw.symbol_id = S_beta;
		brw.pin[0] = 4;
		score_push(st, se, cache, 0, &brw, 0);

		brw.symbol_id = S_alpha;
		brw.pin[0] = 5;
		score_push(st, se, cache, 1, &brw, 0);

		printf(C_MAGENTA "before calc:\n" C_RST);
		score_tr_print(st, stdout);
		searcher_print(se, stdout);

		score_calc(st, se, cache, 0);
		printf(C_CYAN "score = %d\n" C_RST, st->score);

		score_tr_clear(st);
		searcher_clear(se);

		printf(C_MAGENTA "after clear:\n" C_RST);
		score_tr_print(st, stdout);
		searcher_print(se, stdout);
	}

	cp_free(cache);
	cp_free(st);

	searcher_empty(se);
	cp_free(se);

	trace_unfree();
	trace_uninit();
	return 0;
}
예제 #4
0
파일: cache.c 프로젝트: Mattstra/CS1541
int main(int argc, char **argv)
{
  struct trace_item *tr_entry;
  size_t size;
  char *trace_file_name;
  int trace_view_on ;

  if (argc == 1) {
    fprintf(stdout, "\nUSAGE: tv <trace_file> <switch - any character>\n");
    fprintf(stdout, "\n(switch) to turn on or off individual item view.\n\n");
    exit(0);
  }


// here you should extract the cache parameters from the command line
  trace_file_name = argv[1];
  trace_view_on = atoi(argv[2]);
  cache_size = atoi(argv[3]);
  block_size = atoi(argv[4]);
  associativity = atoi(argv[5]);
  policy = atoi(argv[6]);

  double log = log2(cache_size);
  cache_size = pow(2, 10) * pow(2,log );
  size = cache_size;

  printf("size of cache is %d\n block size is %d\n associativity is %d\n", size, block_size, associativity);
  fprintf(stdout, "\n ** opening file %s\n", trace_file_name);

  trace_fd = fopen(trace_file_name, "rb");

  if (!trace_fd) {
    fprintf(stdout, "\ntrace file %s not opened.\n\n", trace_file_name);
    exit(0);
  }

  trace_init();
  // here should call cache_create(cache_size, block_size, associativity, replacement_policy)
  struct cache_t *cp = cache_create(size, block_size, associativity, policy);


  while(1) {
    size = trace_get_item(&tr_entry);

    if (!size) {       /* no more instructions to simulate */
	  printf("+ number of accesses : %d \n", accesses);
      printf("+ number of reads : %d \n", read_accesses);
      printf("+ number of writes : %d \n", write_accesses);
      printf("+ number of hits : %d \n", hits);
      printf("+ number of misses : %d \n", misses);
      printf("+ number of misses with write back : %d \n", misses_with_writeback);

	  break;
    }
    else{              /* process only loads and stores */;
	  if (tr_entry->type == ti_LOAD) {
			if (trace_view_on) printf("LOAD %x \n",tr_entry->Addr) ;
			accesses ++;
			read_accesses++ ;
            load = cache_access(cp, tr_entry->Addr, 0, cycles);



	  }
	  if (tr_entry->type == ti_STORE) {
    		  if (trace_view_on) printf("STORE %x \n",tr_entry->Addr) ;
			accesses ++;
			write_accesses++ ;
			store = cache_access(cp, tr_entry->Addr, 1, cycles);
	  }
	  // based on the value returned, update the statisctics for hits, misses and misses_with_writeback
	  if(load == 0)
      {
          hits++;
          load = -1;
      }

      if(store == 0)
      {
          hits++;
          store = -1;
      }

      if(load == 1)
      {
          misses++;
          load = -1;
      }

      if(store == 1)
      {
          misses++;
          store = -1;
      }

      if(load == 2)
      {
          misses_with_writeback++;
          load = -1;
      }

      if(store == 2)
      {
          misses_with_writeback++;
          store = -1;
      }

    }
    cycles++;
  }

  trace_uninit();

  exit(0);
}
예제 #5
0
int main(int argc, char **argv)
{
    struct trace_item *tr_entry;
    size_t size;
    char *trace_file_name;
    int trace_view_on, cache_size, block_size;
    int associativity, replacement_policy;
	enum cache_policy policy;
	struct cache_t *cp;
	struct timeval gettimeofdayreturnstruct;
	unsigned long long timestamp_in_microsec;
	int cache_access_status;
	FILE *file_results; //we will be writing our results out to a file
	
	//define default
	trace_view_on = 1;
	cache_size = 1; //1 KB
	block_size = 4; //4 bytes = 1 word
	associativity = 1; //1-way associativity
	replacement_policy = 0; //0 for LRU, 1 for FIFO
	
    if (argc == 1) {
        fprintf(stdout, "nUSAGE: tv <trace_file> <switch - any character>n");
        fprintf(stdout, "n(switch) to turn on or off individual item view.nn");
        exit(0);
    }
 		
	trace_file_name = argv[1]; 	
    
	// here you should extract the cache parameters from the command line
	if (argc == 7)
	{
		trace_view_on = atoi(argv[2]) ;
		// here you should extract the cache parameters from the command line
		cache_size = atoi(argv[3]); //in kilobytes
		block_size = atoi(argv[4]);
		associativity = atoi(argv[5]);
		if ((cache_size != (cache_size & -cache_size)) || (block_size != (block_size & -block_size)) || (associativity != (associativity & -associativity))) { //should be restricted to the power of 2
			fprintf(stdout, "Cache size, block size, and block associativity have to be a power of 2. (For example: 1, 2, 4, 8, 16, ...");
			exit(0);		
		}
		replacement_policy = atoi(argv[6]);
		if ( !(replacement_policy == 0 || replacement_policy == 1) ){
			fprintf(stdout, "\nMake sure that you pick either 0 for LRU replacement or 1 for FIFO replacement.");
			fprintf(stdout, " %d is not a valid number.", replacement_policy);
			exit(0);
		}
	}
    	
    if(replacement_policy){
        policy = FIFO;
    }
    else{
        policy = LRU;
    }
	
    fprintf(stdout, "n ** opening file %sn", trace_file_name);
    
    trace_fd = fopen(trace_file_name, "rb");
    
    if (!trace_fd) {
        fprintf(stdout, "ntrace file %s not opened.nn", trace_file_name);
        exit(0);
    }
    
    trace_init();
	
	file_results = fopen("./results.txt", "w"); //open text file for writing out results
    
	//print back all the parameters
	printf("Parameters:");
	fprintf(file_results,"\n\nParameters:");
	printf("\nTrace Name: %s", trace_file_name);
	fprintf(file_results, "\nTrace Name: %s", trace_file_name);
	printf("\nCache Size: %d KBYTES", cache_size);
	fprintf(file_results, "\nCache Size: %d KBYTES", cache_size);
	printf("\nBlock Size: %d BYTES", block_size);
	fprintf(file_results, "\nBlock Size: %d BYTES", block_size);
	printf("\nAssociativity: %d", associativity);
	fprintf(file_results, "\nAssociativity: %d", associativity);
	if (replacement_policy == 0) {
		printf("\nReplacement Policy: LRU");
		fprintf(file_results, "\nReplacement Policy: LRU");
	}
	else {
		printf("\nReplacement Policy: FIFO");
		fprintf(file_results, "\nReplacement Policy: FIFO");
	}
	
    // here should call cache_create(cache_size, block_size, associativity, replacement_policy)
    cp = cache_create(cache_size, block_size, associativity, policy);
	   
	while(1) {
        size = trace_get_item(&tr_entry);        
        if (!size) {       /* no more instructions to simulate */
			printf("\n\nResults:");
			fprintf(file_results, "\n\nResults:");
			printf("\nCache Accesses: %d", accesses);
			fprintf(file_results, "\nCache Accesses: %d", accesses);
			printf("\nCache Read Accesses: %d", read_accesses);
			fprintf(file_results, "\nCache Read Accesses: %d", read_accesses);
			printf("\nCache Write Accesses: %d", write_accesses);
			fprintf(file_results, "\nCache Write Accesses: %d", write_accesses);
			printf("\nCache Hits: %d", hits);
			fprintf(file_results, "\nCache Hits: %d", hits);
			printf("\nCache Misses: %d", misses);
			fprintf(file_results, "\nCache Misses: %d", misses);
			printf("\nCache Writebacks: %d", misses_with_writeback);
			fprintf(file_results, "\nCache Writebacks: %d", misses_with_writeback);
            break;
        }
        else{              /* process only loads and stores */;
			gettimeofday(&gettimeofdayreturnstruct, NULL);
			timestamp_in_microsec = (unsigned long long)(1000000ULL * gettimeofdayreturnstruct.tv_sec + gettimeofdayreturnstruct.tv_usec);
            if (tr_entry->type == ti_LOAD) {
                if (trace_view_on) {
					printf("\n\nLOAD %x n",tr_entry->Addr);
					fprintf(file_results, "\n\nLOAD %x n",tr_entry->Addr); 
				}
                // call cache_access(struct cache_t *cp, tr_entry->Addr, access_type)
				cache_access_status = cache_access(cp, tr_entry->Addr, tr_entry->type, file_results, trace_view_on, timestamp_in_microsec);
				read_accesses = read_accesses + 1;
				accesses = accesses + 1;
            }
            else if (tr_entry->type == ti_STORE) {
                if (trace_view_on) {
					printf("\n\nSTORE %x n",tr_entry->Addr) ;
					fprintf(file_results, "\n\nSTORE %x n",tr_entry->Addr) ;
				}
                // call cache_access(struct cache_t *cp, tr_entry->Addr, access_type)
				cache_access_status = cache_access(cp, tr_entry->Addr, tr_entry->type, file_results, trace_view_on, timestamp_in_microsec);
				write_accesses =  write_accesses + 1;
				accesses = accesses + 1;
            }
			else {
				cache_access_status = 100; //not a load or store
			}
            // based on the value returned, update the statisctics for hits, misses and misses_with_writeback
			if(cache_access_status == 0){ //0 if a hit, 1 if a miss or 2 if a miss_with_write_back
				hits = hits + 1;
                if (trace_view_on) { 
					printf("\nStatus: hit");
					fprintf(file_results, "\nStatus: hit");
				}
			}
			else if (cache_access_status == 1) {
				misses = misses + 1;
                if (trace_view_on) {
					printf("\nStatus: miss");
					fprintf(file_results, "\nStatus: miss");
				}
			}
			else if (cache_access_status == 2) {
				misses_with_writeback = misses_with_writeback + 1;
				if (trace_view_on) {
					printf("\nStatus: miss with writeback");
					fprintf(file_results, "\nStatus: miss with writeback");
				}					
			}
        }
    }
	
	fclose(file_results); //close output file
	
    trace_uninit();
    
    exit(0);
}
예제 #6
0
파일: index.c 프로젝트: t-k-/OPMES
int main(int argc, char* argv[])
{
	char   *frml = NULL, *url = NULL;
	struct list_it li_subpath;
	void          *db_formula;
	void          *db_webpage;
	void          *db_textree;
	BOOL           bad_parse = 0;
	int            subpath_err;
	uint64_t       records;
	CP_ID          id_new;
	CP_NBRW        n_brw;
	int            c;

	/* log file initialize */
	trace_init("index.log");

	/* open key-value database files */
	db_formula = db_init(CONFIG_FORMULA_DB_NAME, DB_OMOD_WR);
	db_webpage = db_init(CONFIG_WEBPAGE_DB_NAME, DB_OMOD_WR);
	db_textree = db_init(CONFIG_TEXTREE_DB_NAME, DB_OMOD_WR);

	/* set parser_root to NULL initially */
	/* or "go to exit" might free a random address. */
	parser_root = NULL;

	/* handle program arguments */
	while ((c = getopt(argc, argv, "hf:u:")) != -1) {
		switch (c) {
		case 'h':
			printf("DESCRIPTION:\n");
			printf("index raw collection.\n");
			printf("\n");
			printf("SYNOPSIS:\n");
			printf("%s -h | "
			       "-f <formula string> "
			       "-u <url> "
			       "\n",
			       argv[0]);
			goto exit;

		case 'f':
			frml = cp_strdup(optarg);
			break;
		
		case 'u':
			url = cp_strdup(optarg);
			break;

		default:
			goto exit;
		}
	}

	if (frml == NULL || url == NULL) {
		printf("bad argument.\n");
		goto exit;
	}

	printf("index... (`" C_RED "%s" C_RST "' @ %s)\n", frml, url);

	/* before parsing, it is necessary to substitute possible
	 * carriage return(s) with space. Because a Mathjax content
	 * $ a\<^M>b $ will be interpreted as $ a\ b $, we infer a 
	 * line-feed or carriage return should be replaced by a space.
	 */
	sub_cr_space(frml);

	/* parse formula string */
	if (!cp_parse(frml))
		goto exit;

	/* handle parser error */
	if (parser_err_flg) {
		parser_err_flg = 0;
		printf("parser: %s\n", parser_err_msg);

		bad_parse = 1;
		goto exit;
	}

	/* check parser root */
	if (NULL == parser_root) {
		trace(INDEX, "NULL parser root.\n", NULL);

		bad_parse = 1;
		goto exit;
	}

	/* assign new formula ID in order */
	records = (CP_ID)db_num_records(db_formula);

	if (0 != records % 3) 
		trace(INDEX, "bad records number: %u.\n", records);

	if (records / 3 + 1 >= MAX_CP_ID) {
		trace(INDEX, "NO available ID to assign.\n", NULL);
		goto exit;
	} else {
		id_new = records / 3 + 1;
		printf("assign ID = %u\n", id_new);
	}

	/* prepare posting file */
	n_brw = tex_tr_update(parser_root);
	if (n_brw == 0) {
		trace(INDEX, "tex_tr_update() fails.\n", NULL);
		goto exit;
	}

	li_subpath = tex_tr_subpaths(parser_root, &subpath_err);
	if (subpath_err) {
		trace(INDEX, "tex_tr_subpaths() fails.\n", NULL);

		subpaths_free(&li_subpath);
		goto exit;
	}

	/* write posting file */
	posting_write(&li_subpath, id_new);
	subpaths_free(&li_subpath);

	/* index formula */
	if (db_formula) {
		if (!index_formula(db_formula, id_new, frml, url, n_brw))
			goto exit;
	} else {
		trace(INDEX, "fail to open %s for writing.\n", 
		      CONFIG_FORMULA_DB_NAME);
		goto exit;
	}

	/* index URL */
	if (db_webpage) {
		index_webpage(db_webpage, url, id_new);
	}

	/* index tree shape */
	if (db_textree) {
		index_textree(db_textree, id_new);
	}

exit:
	/* releases textree */
	if (parser_root) {
		tex_tr_release(parser_root);
		parser_root = NULL;
	}

	/* free program argument string */
	if (frml)
		cp_free(frml);

	if (url)
		cp_free(url);

	/* database release */
	if (db_formula)
		db_release(db_formula);

	if (db_webpage)
		db_release(db_webpage);

	if (db_textree)
		db_release(db_textree);

	/* unfree trace */
	if (0 != trace_unfree()) {
		for (c = 0; c < argc; c ++) {
			trace(UNFREE, "%s \n", argv[c]);
		}
		
		trace(UNFREE, "\n", NULL);
	}

	/* log release */
	trace_uninit();

	return bad_parse;
}