void main()
{
TRISB=0x00;
unsigned int i;
 while (1)
 {
 PORTB=0x00;
 for(i=0;i<=1000;i++)
 timer_func();
 PORTB=0xff;
 for(i=0;i<=1000;i++)
 timer_func();

  }
 
 }
/* Timer thread.
 * This dedicated thread waits for posts on the timer semaphore.
 * For each post, timer_func() is called once.
 * 
 * This ensures that the timer_func() is not called in a signal context.
 */
static void *
timerthread(void *_) 
{
	while (!timer_stopped) {
		int rc = sem_wait(&timer_sem);		// retry on EINTR
		if (rc == -1 && errno == EINTR)
		    continue;
		if (rc == -1) {
		    perror("sem_wait");
		    exit(-1);
		}

		timer_func();	// user-specific timerfunc, can do anything it wants
	}
	return 0;
}
/* Timer thread.
 * This dedicated thread waits for posts on the timer semaphore.
 * For each post, timer_func() is called once.
 *
 * This ensures that the timer_func() is not called in a signal context.
 */
static void *
timerthread(void *_)
{
	while (!timer_stopped) {
		int rc = sem_wait(&timer_sem);		/* same as P(sem) */
		if (rc == -1 && errno == EINTR)     /* call failed due to interrupt, retry */
		    continue;
		if (rc == -1) {                     /* called due to something else */
		    printf("timerthread failed on sem_wait\n");
		    exit(-1);
		}

		timer_func();	/* user-specific timerfunc, can do anything it wants */
	}

	return 0;
}
/* Timer thread.
 * This dedicated thread waits for posts on the timer semaphore.
 * For each post, timer_func() is called once.
 *
 * This ensures that the timer_func() is not called in a signal context.
 */
static void *
timerthread(void *_)
{
	struct timespec start;
	struct timespec end;
	unsigned long long int differ[100];
	unsigned long long int sum=0;
	long long int sd=0;
	int i=0;
	while (!timer_stopped) {
		clock_gettime(CLOCK_REALTIME,&start);
		int rc = sem_wait(&timer_sem);		/* same as P(sem) */
		if (rc == -1 && errno == EINTR)     /* call failed due to interrupt, retry */
		    continue;
		if (rc == -1) {                     /* called due to something else */
		    printf("timerthread failed on sem_wait\n");
		    exit(-1);
		}
		//clock_gettime(CLOCK_REALTIME,&start);
		timer_func();	/* user-specific timerfunc, can do anything it wants */
		clock_gettime(CLOCK_REALTIME,&end);
    	differ[i]=diff(start,end);
    	printf("%lld\n",differ[i]);
    	i++;
	}
		//calculate the average timeout duration
	    for (i=0;i<100; i++)
	    {
	    	sum+=differ[i];
	    }
	    printf("average time out %lld\n",sum/100);

	    //calculate the standard deviation of the time out duration
	    for (i=0;i<100;i++)
	    {
	    	sd+=(differ[i]-sum/100)*(differ[i]-sum/100);
	    }
	    long long int result=0;
	    result= sqrt(sd);
	    printf("stand divireation %lld\n",result);
	return 0;
}
Example #5
0
File: clock.c Project: MarginC/kame
static void
clkintr(struct clockframe frame)
{

	if (timecounter->tc_get_timecount == i8254_get_timecount) {
		mtx_lock_spin(&clock_lock);
		if (i8254_ticked)
			i8254_ticked = 0;
		else {
			i8254_offset += timer0_max_count;
			i8254_lastcount = 0;
		}
		clkintr_pending = 0;
		mtx_unlock_spin(&clock_lock);
	}
	timer_func(&frame);
#ifdef SMP
	if (timer_func == hardclock)
		forward_hardclock();
#endif
	switch (timer0_state) {

	case RELEASED:
		break;

	case ACQUIRED:
		if ((timer0_prescaler_count += timer0_max_count)
		    >= hardclock_max_count) {
			timer0_prescaler_count -= hardclock_max_count;
			hardclock(&frame);
#ifdef SMP
			forward_hardclock();
#endif
		}
		break;

	case ACQUIRE_PENDING:
		mtx_lock_spin(&clock_lock);
		i8254_offset = i8254_get_timecount(NULL);
		i8254_lastcount = 0;
		timer0_max_count = TIMER_DIV(new_rate);
		outb(TIMER_MODE, TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT);
		outb(TIMER_CNTR0, timer0_max_count & 0xff);
		outb(TIMER_CNTR0, timer0_max_count >> 8);
		mtx_unlock_spin(&clock_lock);
		timer_func = new_function;
		timer0_state = ACQUIRED;
		break;

	case RELEASE_PENDING:
		if ((timer0_prescaler_count += timer0_max_count)
		    >= hardclock_max_count) {
			mtx_lock_spin(&clock_lock);
			i8254_offset = i8254_get_timecount(NULL);
			i8254_lastcount = 0;
			timer0_max_count = hardclock_max_count;
			outb(TIMER_MODE,
			     TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT);
			outb(TIMER_CNTR0, timer0_max_count & 0xff);
			outb(TIMER_CNTR0, timer0_max_count >> 8);
			mtx_unlock_spin(&clock_lock);
			timer0_prescaler_count = 0;
			timer_func = hardclock;
			timer0_state = RELEASED;
			hardclock(&frame);
#ifdef SMP
			forward_hardclock();
#endif
		}
		break;
	}
Example #6
0
int main(int argc, char **argv) {
  pthread_t         timer_thread_id,
                    sig_handler_id,
                    writer_id;
  int               status, c;
  sigset_t          set;
  struct sigaction  act;
  char             *mode = NULL;
  void             *(*writer_func)(void *arg);
  void             *(*sig_processor)(void *arg);


  /* Start by masking the "interesting" signals,
   * SIGINT:   To abort
   * SIGRTMIN: Time to issue another I/O (simulates incoming data to be logged)
   * */
  sigemptyset (&set);
  sigaddset(&set, SIGINT );
  sigaddset(&set, SIGRTMIN );

  /* Because all threads inherit the signal mask from their creator, all threads
   * in this process will have these 2 signals masked unless one explicitly
   * unmasks it. */
  status = pthread_sigmask( SIG_BLOCK, &set, NULL );

  /*  parse arguments */
  while ( (c = getopt(argc, argv, "f:(logfile)i:(iops)m:(mode)d(debug)")) != EOF ) {
    switch (c) {
      case 'f':   /* log'f'ile name */
        filename = strdup(optarg);
        printf("Logging to file: %s\n",optarg);
        break;
      case 'i':   /* 'i'ops to generate */
        iops = atoi(optarg);
        printf("Generating %s IOPS\n",optarg);
        break;
      case 'm':   /* 'm'ode to run in, tiny or sane */
        mode = strdup(optarg);
        printf("Attempting to run in %s mode\n",optarg);
        break;
      case 'd':   /* For debugging of signal generation/sigwait */
        sig_processor = &sig_counter; 
        printf("DEBUG on\n");             
        break;                            
      default:
        break;
    }
  }
  /* default IOPS to 50 */
  if (iops == 0)
    iops = 50;
  /* fail if filename not set */
  if (! filename) {
    perror("Must provide filename to log to");
    exit(1);
  }
  if (! mode) {
    /* default to tiny mode */
    mode = "tiny";
    writer_func = &tiny_writer;
  } else {
    if ( strcmp(mode, "tiny") == 0 ) {  
      writer_func = &tiny_writer;            
    } else if ( strcmp(mode, "sane") == 0 ) {
      writer_func = &sane_writer;            
    } else {                                
      printf("ERROR: unknown mode %s\n",mode);
      exit(1);                              
    }
  }

  report_resolution();

  /* Create the sigwait/sigwaitinfo thread */
  status = pthread_create( &sig_handler_id, NULL, sig_processor, NULL);
  if (status != 0) {
    perror("Startup of signal processor thread");
    exit(1);
  }
  /* Create the writer thread */
  status = pthread_create( &writer_id, NULL, writer_func, NULL);
  if (status != 0) {
    perror("Startup of writer thread");
    exit(1);
  }

  /* Start up the timer */
  timer_func(iops);

  printf("Hit Ctrl-C to stop...\n");

  /* Wait for the sigwait thread to receive SIGINT and signal the condition
   * variable */
  status = pthread_mutex_lock( &intr_mutex );
  if (status != 0) {
    perror("main thread intr_mutex lock");
    exit(1);
  }
  while (!interrupted) {
    status = pthread_cond_wait( &intr_cond, &intr_mutex );
    if (status != 0) {
      perror("main thread intr_cond wait");
      exit(1);
    }
  }
  status = pthread_mutex_unlock( &intr_mutex );
  if (status != 0) {
    perror("main thread unlock intr_mutex");
    exit(1);
  }
  printf("Terminating\n");
  exit(0);
}