⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 measure.c

📁 QNX ver4.25 education example code
💻 C
字号:
/****************************************************************************** * *  measure.c:  *  set ticksize to .5 on your P100+ *  Run as: tt -p 29 -u 2500000 *    max_variance of .1 ms is good (default) ******************************************************************************/#ifdef __USAGE%C [ -u uprate (nsec)  -p priority  -m max_variance (nsec)]priority   = priority to run at (0 to 29) Default=10e.g. %C -p 29 -u 250000 #endif #include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/kernel.h>#include <sys/vc.h>#include <sys/seginfo.h>#include <sys/osinfo.h>#include <sys/name.h>#include <i86.h>#include <conio.h>#include <sys/irqinfo.h>#include <sys/proxy.h>#include <sys/sched.h>#include <sys/timers.h>#include <sys/name.h>#include <fcntl.h>#include <errno.h>#include <string.h>#include <sys/trace.h>#include <sys/tracecod.h>#include <math.h>#include <time.h>#include <signal.h>#include "64bit.h"/* globals */double   seconds_per_tick;Long     start_time64, stop_time64;int      priority = 10;#define NANOS_PER_SEC    1000000000#define MICROS_PER_SEC   1000000#define MILLIS_PER_SEC   1000pid_t				t_proxy;struct itimerspec	timer;struct sigevent		t_event;unsigned long 		up_rate = 1 * NANOS_PER_SEC;   /* default 1 second */unsigned long max_variance_long = 100 * MILLIS_PER_SEC;   /* default 0.1 msec */double max_variance;double diff, variation;int log_rate = 10000;#define MYCODE _TRACE_TEMPORARY#define HIGHEST_PRIORITY     29set_my_priority( int priority ) {	int ret;    ret = setprio( getpid(), priority );    if ( ret == -1 ) {       fprintf(stderr,"Error setting priority. (%s)\n",strerror(errno));       exit(-1);       }	return(ret);	}sync_pentium() {	Long diff;	double ticks_per_second;	int previous_priority;    // get synchronization values for this Pentium	sleep(1);	do {        previous_priority = set_my_priority( HIGHEST_PRIORITY );    	rdtsc64( &start_time64 );  // snap time        sleep(1);    	rdtsc64( &stop_time64 );   // snap time        set_my_priority( previous_priority );		} while (start_time64.lo > stop_time64.lo);    _Lsub( &diff, stop_time64, start_time64 ); 	ticks_per_second = (float)diff.lo;    seconds_per_tick = 1.000000000 / ticks_per_second;	printf( "\nPentium Clock Speed Measurement:\n\n" );	printf( "1 second took %lf cycles on this machine.\n", ticks_per_second );	printf( "Each cycle is %18.15lf seconds, or %8.3lf nanoseconds.\n",              seconds_per_tick, seconds_per_tick * NANOS_PER_SEC );	printf( "This is a %3.1f MHz Pentium.\n\n",		(float) ticks_per_second / 1000000.0);    }prepare_to_go() {	int id;	t_proxy = qnx_proxy_attach( 0, 0, 0, -1 );	if ( t_proxy == -1 )	{		perror( "qnx_proxy_attach:" );		exit( -1 );	}	t_event.sigev_signo = -t_proxy;	id = timer_create( CLOCK_REALTIME, &t_event );	if ( id == -1 )	{		perror( "timer_create:" );		exit( -1 );	}    max_variance = (double)max_variance_long / (double)NANOS_PER_SEC;    printf("max_var = %12.9f, up_rate=%ld (nsec)\n",             max_variance, up_rate);    fflush(stdout);				timer.it_value.tv_sec     = 0L;	timer.it_value.tv_nsec    = up_rate;	timer.it_interval.tv_sec  = 0L;	timer.it_interval.tv_nsec = up_rate;	timer_settime(id, 0, &timer, NULL);	}main(int argc, char **argv) {    int     c;    while ( ( c = getopt( argc, argv, "u:p:m:l:" ) ) != -1 ) {         switch(c) {            case 'u' : up_rate = atol(optarg);                       break;            case 'l' : log_rate = atol(optarg);                       break;            case 'p' : priority = atoi(optarg);                       if ( priority < 0 || priority > 29 ) {                          fprintf(stderr,"Invalid priority. Defaulting to 10.\n");                          priority = 10;                          }                       break;            case 'm' : max_variance_long = atol(optarg);                       break;            }         }    if ( setprio( getpid(), priority ) == -1 ) {       fprintf(stderr,"Error setting priority. (%s)\n",strerror(errno));       exit(-1);       }	sync_pentium();    prepare_to_go();    go();	}go() {    Long diffl;    unsigned counter  = 0;    unsigned lcounter = 0;	char buff[100];	pid_t pid;	char msg;	double diffd, master;    int previous_priority;    int i;    unsigned short c[5];    double tmp;	// set our priority high so we get a good master reading    previous_priority = set_my_priority( HIGHEST_PRIORITY );   	rdtsc64( &stop_time64 );  // snap time    while (1) {                      start_time64.hi = stop_time64.hi;        start_time64.lo = stop_time64.lo;        pid = Receive( 0, &msg, sizeof(msg) );        // read 5 port values        for ( i=0; i < 5; i++ )            c[i] = inp(0x42);        // do some math on them        tmp = c[0] * c[1] * sin(c[2]+c[3]) + c[4];     		rdtsc64( &stop_time64 );  // snap time        _Lsub( &diffl, stop_time64, start_time64 );         diffd = (float)diffl.lo * seconds_per_tick;        switch ( counter ) {           case 0:  // first one .. skip it                    break;           case 1:  // this is our benchmark                    master = diffd;                    // set ourselves back in priority                    set_my_priority( previous_priority );                    break;           default: // all the rest                    variation = diffd - master;        			if ( fabs(variation) > max_variance ) {           				sprintf(buff, "diff=%12.9f, var=%12.9f, bench=%12.9f\n",                           diffd, variation, master);            		        Trace2b( (MYCODE | 1), 3, counter,                                   counter - lcounter, strlen(buff), buff );           		        lcounter = counter;                        }                    if ( !(counter % log_rate) ) {           				sprintf(buff, "diff=%12.9f, var=%12.9f, bench=%12.9f\n",                           diffd, variation, master);            		        Trace1b( (MYCODE | 2), 3, counter,                                   strlen(buff), buff );                        }                    break;           }        counter++;        }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -