Exemplo n.º 1
0
/* Attempt to see how much time is used by timer interrupt */
static void callibrate(int verbose)
{
  double oldt;
  struct tms t;
  clock_t oldc;
  int e = 0;
  times(&t);
  oldc = t.tms_utime;
  start_counter();
  oldt = get_counter();
  while (e <NEVENT) {
    double newt = get_counter();
    if (newt-oldt >= THRESHOLD) {
      clock_t newc;
      times(&t);
      newc = t.tms_utime;
      if (newc > oldc) {
	double cpt = (newt-oldt)/(newc-oldc);
	if ((cyc_per_tick == 0.0 || cyc_per_tick > cpt) && cpt > RECORDTHRESH)
	  cyc_per_tick = cpt;
	/*
	if (verbose)
	  printf("Saw event lasting %.0f cycles and %d ticks.  Ratio = %f\n",
		 newt-oldt, (int) (newc-oldc), cpt);
	*/
	e++;
	oldc = newc;
      }
      oldt = newt;
    }
  }
  if (verbose)
    printf("Setting cyc_per_tick to %f\n", cyc_per_tick);
}
Exemplo n.º 2
0
void context_switch_experiment() {
	printf("# context sw experiment\n");
	fflush(stdout);
    int    ShmID;
    ui    *ShmPTR;
    pid_t  pid;
    int status = 0;
    int cw_sample_count = 500;

    // Request OS to allocate some shared memory.
    ShmID = shmget(IPC_PRIVATE, 3*sizeof(ui), IPC_CREAT | 0666);
    if (ShmID < 0) { printf("ERROR: shmget failed to allocate memory\n"); exit(1);}

    // Get pinter to shared memry.
    ShmPTR = (ui *) shmat(ShmID, NULL, 0);
    if ((ui) ShmPTR == (ui)-1) { printf("ERROR: shmat error\n"); exit(1);}


    for (int i = 0; i < cw_sample_count; i++) {
    	// Notion here is as following:
    	// Child writes absolute cycle count into the first two ShmPTR locations.
    	// Parent reads ShmPtr[0 and 1], if they are equal, this means that the child
    	// was not interrupted while writing to shared memory.
    	// Parent then takes it's own CPU cycle count and compares it to child's value.

		ShmPTR[0] = 1; //for parent to read child.
		ShmPTR[1] = 2; //2nd confirmation copy, used as sort of mutex.

		ShmPTR[2] = 0; //for child to read parent
		pid = fork();
		if (pid < 0) {printf("ERROR on fork\n"); exit(1);}
		if (pid == 0) { //child
			while (1) {
				ShmPTR[0] = ShmPTR[1] = get_counter();
				if (ShmPTR[2] != 0) { //parent wrote something into shared memory.
					break;
				}
			}
			exit(0);
		} else { //parent
			while (1) {
				if (ShmPTR[0] == ShmPTR[1]) {
					ui parent_cycles = get_counter();
					ui child_cycles = ShmPTR[0];

					ShmPTR[2] = 1; // Instruct child to exit.
					printf("%lu\n", parent_cycles - child_cycles);
					fflush(stdout);
					wait(&status); // wait till child exits
					break;
				}
			}
		}
    }


    //
    shmctl(ShmID, IPC_RMID, NULL); //Shared memory should be released before exiting.
    exit(1);
}
Exemplo n.º 3
0
word gamedata::get_spendable_rupies()
{
    if(get_bit(quest_rules, qr_SHOPCHEAT) || get_dcounter(1)>=0)
        return get_counter(1);
    else
        return get_counter(1)+get_dcounter(1);
}
Exemplo n.º 4
0
/* while sleeping for sleeptime seconds */
double mhz(int verbose, int sleeptime)
{
    double rate;

    start_counter();
    sleep(sleeptime);
    rate = get_counter() / (1e6*sleeptime);
    if (verbose)
        printf("Processor clock rate ?= %.1f MHz\n", rate);
    start_counter();
   printf("delay is %.1f. ", get_counter());
    return rate;
}
Exemplo n.º 5
0
void run(int len, int trials, unsigned seed) {
    double t1, t2;
    int t;
    for (t = 0; t < trials; t++) {
	init(seed);
	start_counter();
	minmax1(a1, b1, len);
	t1 = get_counter();
	start_counter();
	minmax2(a2, b2, len);
	t2 = get_counter();
	printf("%d\t%.2f\t%.2f\n", len, t1/len, t2/len);
    }
}
Exemplo n.º 6
0
/* $begin time_p_warm */
double time_P_warm() 
{
    P(); /* Warm up the cache */
    start_counter(); 
    P(); 
    return get_counter(); 
}
Exemplo n.º 7
0
/* but not too often */
void request_and_resend (int sock, char * contact, keyset kset)
{
  if (get_counter (contact) <= 0) {
    printf ("unable to request and resend for %s, peer not found\n", contact);
    return;
  }
/*  printf ("request_and_resend (socket %d, peer %s)\n", sock, peer); */
  static char * old_contact = NULL;

  /* if it is the same peer as on the last call, we do nothing */
  if ((old_contact != NULL) && (strcmp (contact, old_contact) == 0)) {
/*    printf ("request_and_resend (%s), same as old peer\n", peer); */
    return;
  }

  if (old_contact != NULL)
    free (old_contact);
  old_contact = strcpy_malloc (contact, "request_and_resend contact");

  /* request retransmission of any missing messages */
  int hops = 10;
  send_retransmit_request (contact, kset, sock, hops,
                           ALLNET_PRIORITY_LOCAL_LOW);

  /* resend any unacked messages, but no more than once every hour */
  static time_t last_resend = 0;
  time_t now = time (NULL);
  if (now - last_resend > 3600) {
    last_resend = now;
    resend_unacked (contact, kset, sock, hops, ALLNET_PRIORITY_LOCAL_LOW, 10);
  }
}
Exemplo n.º 8
0
/*
 *  stress_rdrand()
 *      stress Intel rdrand instruction
 */
static int stress_rdrand(const args_t *args)
{
	if (rdrand_supported) {
		double time_start, duration, billion_bits;
		bool lock = false;

		time_start = time_now();
		do {
#if defined(__x86_64__) || defined(__x86_64)
			RDRAND64x32();
#else
			RDRAND32x64();
#endif
			inc_counter(args);
		} while (keep_stressing());

		duration = time_now() - time_start;
		billion_bits = ((double)get_counter(args) * 64.0 * 32.0) / 1000000000.0;

		pr_lock(&lock);
		pr_dbg_lock(&lock, "%s: %.3f billion random bits read "
			"(instance %" PRIu32")\n",
			args->name, billion_bits, args->instance);
		if (duration > 0.0) {
			pr_dbg_lock(&lock, "%s: %.3f billion random bits per "
				"second (instance %" PRIu32")\n",
				args->name,
				(double)billion_bits / duration,
				args->instance);
		}
		pr_unlock(&lock);
	}
	return EXIT_SUCCESS;
}
Exemplo n.º 9
0
u_int64_t inactive_periods(int num, u_int64_t threshold, Sample *samples) {
  ui initial_value = get_counter();
  ui last_value = initial_value;

  for (int i = 0; i < num;) {
    ui new_value = get_counter();
    ui diff = new_value - last_value; // difference
    if (diff > threshold) {
      samples[i].start = last_value;
      samples[i].end = new_value;
      i++;
    }
    last_value = new_value;
  }
  return initial_value;
}
Exemplo n.º 10
0
int main()
{
    int i,j;
    unsigned time1, time2, time3;
    start_counter();
    for (i = 0; i < SQSIZE; i++)
    {
		for (j = 0; j < SQSIZE; j++)
		{
			square[i][j].c = 0;
			square[i][j].m = 0;
			square[i][j].y = 1;
			square[i][j].k = 0;
		}
    }
    time1 = get_counter();

    start_counter();
    for (i = 0; i < SQSIZE; i++)
    {
		for (j = 0; j < SQSIZE; j++)
		{
			square[i][j].c = 0;
			square[i][j].m = 0;
			square[i][j].y = 1;
			square[i][j].k = 0;
		}
    }
    time2 = get_counter();
    
    start_counter();
    for (i = 0; i < SQSIZE; i++)
    {
		for (j = 0; j < SQSIZE; j++)
		{
			square[j][i].c = 0;
			square[j][i].m = 0;
			square[j][i].y = 1;
			square[j][i].k = 0;
		}
    }
    time3 = get_counter();
    printf("T1 (Cold cache time):    %12u  (%.1fx)\n", time1,(double)time1/time2);
    printf("T2 (Warm cache time):    %12u  (1.0x)\n", time2);
    printf("T3 (Poor locality time): %12u  (%.1fx)\n", time3,(double)time3/time2);
}
Exemplo n.º 11
0
/* $begin time_p_cold */
double time_P_cold() 
{
    P(); /* Warm up instruction cache */ 
    clear_cache(); /* Clear data cache */ 
    start_counter(); 
    P(); 
    return get_counter(); 
}
Exemplo n.º 12
0
// by Ged, 4-1-2004
int inactive_duration(int thresh) 
{
    double value;
    start_counter();
    while(value = get_counter())
      if(value > thresh) return (int) value;
      else start_counter(); //resets counter, so we check between reads 
}
Exemplo n.º 13
0
int hashtable_dec_counter(char* key) {
	LM_DBG("decrementing counter for <%s>\n", key);
	int *d = get_counter(key);
	if (*d > 0)
		*d = *d - 1;
	if (*d == 0)
		remove_counter(key);
	return *d;
}
Exemplo n.º 14
0
int main (int argc, char **argv)
{
  /*
   * Setup the Scheme library by calling "___setup" with appropriate
   * parameters.  The call to "___setup_params_reset" sets all parameters
   * to their default setting.
   */

  ___setup_params_struct setup_params;

  ___setup_params_reset (&setup_params);

  setup_params.version = ___VERSION;
  setup_params.linker  = SCHEME_LIBRARY_LINKER;

  ___setup (&setup_params);

  /* Main part of program: start N threads that increment a counter */

  counter = new_counter (); /* create a new counter */

  pthread_mutex_init (&mut, NULL); /* initialize mutex */

  {
    int i;
    pthread_t tid[N];
    void* results[N];

    for (i = 0; i<N; i++)
      pthread_create (&tid[i], NULL, thread_main, NULL);

    for (i = 0; i<N; i++)
      pthread_join (tid[i], &results[i]);
  }

  {
    int got_throw = 0;
    int count = 0;

    ___ON_THROW(count = get_counter (counter), got_throw=1);

    if (got_throw)
      printf ("Scheme threw an exception\n");
    else
      printf ("final count = %d\n", count);
  }

  /* we don't need the counter anymore */

  ___ON_THROW(release_counter (counter),);

  /* Cleanup the Scheme library */

  ___cleanup ();

  return 0;
}
/*
 * Timer function called every second to gather statistics
 * from the 77105. This is done because the h/w registers
 * will overflow if not read at least once per second. The
 * kernel's stats are much higher precision. Also, having
 * a separate copy of the stats allows implementation of
 * an ioctl which gathers the stats *without* zero'ing them.
 */
static void idt77105_stats_timer_func(unsigned long dummy)
{
	struct idt77105_priv *walk;
	struct atm_dev *dev;
	struct idt77105_stats *stats;

        DPRINTK("IDT77105 gathering statistics\n");
	for (walk = idt77105_all; walk; walk = walk->next) {
		dev = walk->dev;
                
		stats = &walk->stats;
                stats->symbol_errors += get_counter(dev, IDT77105_CTRSEL_SEC);
                stats->tx_cells += get_counter(dev, IDT77105_CTRSEL_TCC);
                stats->rx_cells += get_counter(dev, IDT77105_CTRSEL_RCC);
                stats->rx_hec_errors += get_counter(dev, IDT77105_CTRSEL_RHEC);
	}
        if (!start_timer) mod_timer(&stats_timer,jiffies+IDT77105_STATS_TIMER_PERIOD);
}
Exemplo n.º 16
0
/**
 * Get time + cycle count at a moment in time.
 * Do some busy waiting.
 * Get time + cycles count at the end again.
 * Compute mhz based on difference in time and cycle count.
 *
 * Repeat experiment 20 times and pick the median value to avoid unexpected hops.
 */
ui get_cpu_mhz() {
	int cpu_samples_count = 20;
	   ui * cpu_samples = (ui *) malloc(sizeof(ui)*cpu_samples_count);

	   for (int ci = 0; ci < cpu_samples_count; ci++) {
		 timespec time_start, time_end;
		 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time_start);
		 ui start_cycles = get_counter();
		 // 375000000 ~= 1 second or so.
		 for (long i = 0; i < 500000; i++) {  // busy waiting.
		  int unused_number = 1 << 1;
		  (void) unused_number; //supress warning from compiler.
		 }
		 ui end_cycles = get_counter();
		 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time_end);

		 timespec diff_total;
		 diff_total.tv_sec = 0;  diff_total.tv_nsec = 0;
		 diff_total = diff(time_start, time_end);
		 ui elapsed_cycles = end_cycles - start_cycles;

		 double total_time = ((double) diff_total.tv_nsec / 1000000000.0L) + diff_total.tv_sec; // nanoseconds to seconds + seconds.
		 double multiplier = 1.0L / total_time;
		 ui herz = (ui) floor((double) elapsed_cycles * multiplier);
		 //printf("%lu\n", herz);
		 cpu_samples[ci] = herz;
	   }

	  // sort all aquired CPU values.
	  qsort(cpu_samples, cpu_samples_count, sizeof(ui), ui_cmpfunc);

//	  for (int i = 0; i < cpu_samples_count ; i++) {
//		  printf("val: %lu\n", cpu_samples[i]);
//	  }

	  // pick the median, which will likley be reasonably accurate.
	  ui cpu_mhz = cpu_samples[cpu_samples_count / 2];

	  //printf("Cpu value: %lu\n", cpu_mhz);
	  free(cpu_samples);
	  return cpu_mhz;

}
Exemplo n.º 17
0
/* Determine clock rate by measuring cycles
   elapsed while sleeping for sleeptime seconds */
double mhz_full(int verbose, int sleeptime)
{
  double rate;
  start_counter();
  sleep(sleeptime);
  rate = get_counter()/(1e6*sleeptime);
  if (verbose) 
    printf("Processor Clock Rate ~= %.1f MHz\n", rate);
  return rate;
}
Exemplo n.º 18
0
double fcyc(test_funct f, long int *params)
{
    double result;
    init_sampler();
    if (compensate) {
	do {
	    double cyc;
	    if (clear_cache)
		clear();
	    start_counter();
	    f(params);
	    cyc = get_counter();
	    if (cyc > 0.0)
		add_sample(cyc);
	} while (!has_converged() && samplecount < maxsamples);
    } else {
	do {
	    double cyc;
	    if (clear_cache)
		clear();
	    start_counter();
	    f(params);
	    cyc = get_counter();
	    if (cyc > 0.0)
		add_sample(cyc);
	} while (!has_converged() && samplecount < maxsamples);
    }
#ifdef DEBUG
    {
	long int i;
	printf(" %ld smallest values: [", kbest);
	for (i = 0; i < kbest; i++)
	    printf("%.0f%s", values[i], i==kbest-1 ? "]\n" : ", ");
    }
#endif
    result = values[0];
#if !KEEP_VALS
    free(values); 
    values = NULL;
#endif
    return result;  
}
Exemplo n.º 19
0
double ovhd()
{
  /* Do it twice to eliminate cache effects */
  int i;
  double result;
  for (i = 0; i < 2; i++) {
    start_counter();
    result = get_counter();
  }
  return result;
}
Exemplo n.º 20
0
Arquivo: clock.c Projeto: 0x0nil/csapp
double mhz(int verbose, int sleeptime)
{
	double rate;

	start_counter();
	sleep(sleeptime);
	rate = get_counter();
	if(verbose)
		printf("processor clock rate ~= %.1f MHz\n",rate);
	return rate;
}
Exemplo n.º 21
0
int main(int argc, char **argv) {
	srand(time(NULL));
	sef_startup();

	Whack* whack = startWhack();
	vg_init(0x114);
	int done = 0; //whack->done;
	//Bitmap* board = loadBitmap("/home/lcom/lcom1415-t3g15/projeto/res/board.bmp");
	//Bitmap* bt = loadBitmap("/home/lcom/lcom1415-t3g15/projeto/res/exit-button.bmp");
	//drawBitmapWithoutBg(board, 0, 0, ALIGN_LEFT);
	//drawBitmapWithoutBg(bt, 200, 120, ALIGN_LEFT);

	//MainMenu* teste = createMainMenu();
	//drawMainMenu(teste);

	//deleteMainMenu(teste);

	/*Game* teste = createGame();
	//OptionsMenu* teste = createOptionsMenu(0, 0);
		drawGame(teste);
	flipMouse();
					drawMouse();
					flipDisplay();

		sleep(10);

	deleteGame(teste);*/

	while (!whack->done) {
		updateWhack(whack);
		if (get_counter(whack->timer) % 4 == 0){
			if (!whack->done && whack->draw) {
				drawWhack(whack);
			}

			if (getMouse()->draw) {
				flipMouse();
				drawMouse();
				flipDisplay();
			}
		}
	}
	stopWhack(whack);


	vg_exit();



	printf("\nTerminated\n");

	return 0;
}
Exemplo n.º 22
0
double ovhd() {
    /* Find lowest among multiple samples */
    int i;
    double result = 1E10;
    for (i = 0; i < 100; i++) {
	double sample;
	start_counter();
	sample = get_counter();
	if (sample < result)
	    result = sample;
    }
    return result;
}
Exemplo n.º 23
0
double get_comp_counter() {
  double time = get_counter();
  double ctime;
  struct tms t;
  clock_t ticks;
  times(&t);
  ticks = t.tms_utime - start_tick;
  ctime = time - ticks*cyc_per_tick;
  /*
  printf("Measured %.0f cycles.  Ticks = %d.  Corrected %.0f cycles\n",
	 time, (int) ticks, ctime);
  */
  return ctime;
}
/* Gather a single sample that estimates the CPU freqency. Check the TSC register, sleep for the 
 * given amount of time, and then read back the TSC register. The difference in clock cycles over
 * the known time is used to estimate the CPU freqency.
 */
int get_sample(long milliseconds, u_int64_t *sample) 
{	
	struct timespec req;
	u_int64_t start, end;
	int err;

	// Set amount of time to sleep per sample
	req.tv_sec = 0;
	req.tv_nsec = milliseconds * 1000000L;

	// Measure clock-speed
	start = get_counter();
	err = nanosleep(&req, NULL);
	end = get_counter();

	if (err == -1) {
		return 0;
	}
	
	*sample = (end - start);

	return 1;
}
Exemplo n.º 25
0
/**
 * This function was initially used to determine the average interval between sampling cpu cycles, to find out context switch time.
 */
void print_intervals() {
  int count = 2000;
  //start_counter();

  ui * sample_values = malloc(sizeof(ui)*count);

  ui initial_value = get_counter();
  for (int i = 0, last_value = initial_value; i < count; i++) {
    ui new_value = get_counter();
    sample_values[i] = new_value - last_value; //difference
    last_value = new_value;
  }

  ui sum = 0;
  for (int i = 0; i < count; i++) {
    printf("%lu  ", sample_values[i]);
    sum += sample_values[i];
    if (i % 20 == 0) {
      printf("\n");
    }
  }
  printf("Sum: %lu\n", sum);
  free(sample_values);
}
Exemplo n.º 26
0
/* repeatedly calls the function to measure -- argument is iteration
 * count */ 
unsigned measure(int iter)
{
    int i;
    unsigned cmeas,cycles;
    volatile int res = 0;  /* don't let compiler optimize away fn
			    * calls */
    
    res += absdiff(0,1);  /* warm up cache */
    start_counter();
    for (i = 0; i < iter; i++)
	res += absdiff(valA[i],valB[i]);
    cmeas = get_counter();
    cycles = cmeas / iter;
    return cycles;
}
Exemplo n.º 27
0
/**
 * digging hole アルゴリズムにより問題を作成する
 * 点対称な問題を作成する。
 * 人間的解法で解ける盤面を生成する。
 * @param array ナンプレ盤面(入出力)
 * @param symmetric 対称形に生成するフラグ
 * @return 1: 作成できた
 * @return それ以外: 作成できなかった
 */
static int digging_hole(numpl_array * array, int symmetric)
{
    for (int i = 0; i < ARRAY_SIZE; i++) {
        array->ar[i].fixed = 1;
    }
    numpl_array save = *array;
    int loop_size = ARRAY_SIZE;
    if (symmetric) {
        loop_size = ARRAY_SIZE / 2;
    }
    for (int i = 0; i < loop_size; i++) {
        int idx1 = get_random(ARRAY_SIZE);
        int idx2 = get_counter(idx1);
        if (array->ar[idx1].fixed == 0) {
            continue;
        }
        array->ar[idx1].fixed = 0;
        array->ar[idx1].symbol = FULL_SYMBOL;
        if (symmetric) {
            array->ar[idx2].fixed = 0;
            array->ar[idx2].symbol = FULL_SYMBOL;
        }
        solve_info info;
        fixed_only(array, FULL_SYMBOL);
        int r = solve(array, &info);
        if (r == 0) {
            *array = save;
#if defined(DEBUG) && 0
            printf("load and return r = %d\n", r);
            //output(array);
#endif
            return 1;
        } else if (r == 1) {
            save = *array;
#if defined(DEBUG) && 0
            printf("save r = %d i = %d\n", r, i);
            //output_detail(array);
#endif
        } else {
            *array = save;
#if defined(DEBUG) && 0
            printf("load r = %d i = %d\n", r, i);
            //output_detail(array);
#endif
        }
    }
    return -1;
}
Exemplo n.º 28
0
int main (int argc, char *argv[])
{
  int iters = 1000000;
  double MHz = mhz(1);
  int i;
  int sum = 0;
  double cycs;
  start_counter();
  for (i = 0; i < iters; i++) {
    sum += i;
    cycs = get_counter();
  }
  printf("%d calls in %.0f clock cycles = %.3f usec/call\n",
	 iters, cycs, cycs / (MHz * iters));
  return 0;
}
Exemplo n.º 29
0
static BOOL io_format_interp(io_struct *ps, size_t *item_size, int *nitems,
		BOOL *is_string)
{
	char fmt;

	*is_string = False;
	*item_size = 0;
	*nitems = 1;

	if (ps->format_string == NULL)
		return False;

	fmt = ps->format_string[ps->format_offset];
	if (fmt == 0)
		return False;

	switch (fmt)
	{
		case 'W':
		{
			*item_size = 2;
			break;
		}
		case 'z':
		{
			*is_string = True;
			/* fall through... */
		}
		case 'B':
		{
			*item_size = 1;
			break;
		}
		case 'D':
		{
			*item_size = 4;
			break;
		}
		default:
		{
			return False;
		}
	}
	ps->format_offset++;
	*nitems = get_counter(ps->format_string, &ps->format_offset);
	return True;
}
Exemplo n.º 30
0
/**
 * digging hole アルゴリズムを再帰的に使い問題を作成する
 * symmetric が真なら点対称な問題を作成する。
 * 人間的解法で解ける盤面を生成する。
 * @param array ナンプレ盤面(入出力)
 * @param pos 盤面配列内の開始位置
 * @param info 解情報
 * @param symmetric 真なら点対称の問題を作成する
 * @return 1: 作成できた
 * @return それ以外: 作成できなかった
 */
static int digging_hole_recursion(numpl_array * array, int pos,
                                  solve_info * info, int symmetric)
{
    fixed_only(array, FULL_SYMBOL);
    int r = solve(array, info);
    if (r != 1) {
        return r;
    }
    numpl_array best = *array;
    numpl_array save = *array;
    int min_fixed = ARRAY_SIZE + 1;
    int loop_size = ARRAY_SIZE;
    if (symmetric) {
        loop_size = ARRAY_SIZE / 2;
    }
    for (int i = pos; i < loop_size; i++) {
        int idx1 = i;
        int idx2 = get_counter(idx1);
        if (array->ar[idx1].fixed == 0) {
            continue;
        }
        array->ar[idx1].fixed = 0;
        array->ar[idx1].symbol = FULL_SYMBOL;
        if (symmetric) {
            array->ar[idx2].fixed = 0;
            array->ar[idx2].symbol = FULL_SYMBOL;
        }
        r = digging_hole_recursion(array, i + 1, info, symmetric);
        if (r <= 0) {
            *array = save;
            continue;
        } else if (r == 1) {
            if (info->fx_count < min_fixed) {
                min_fixed = info->fx_count;
                best = *array;
            }
            *array = save;
        }
    }
    *array = best;
    if (min_fixed == ARRAY_SIZE + 1) {
        return -1;
    } else {
        return 1;
    }
}