Ejemplo n.º 1
0
Archivo: aocmd.c Proyecto: cjecho/RTAI
int main(void)
{
	double omega = (2.0*M_PI*SIN_FREQ*SAMP_TIME)/1.0E9;
	RTIME until;
	RT_TASK *task;

	lsampl_t data[NCHAN*2];
	long k, sinewave, retval = 0;

	signal(SIGKILL, endme);
	signal(SIGTERM, endme);

	start_rt_timer(0);
	task = rt_task_init_schmod(nam2num("MYTASK"), 1, 0, 0, SCHED_FIFO, 0xF);
	printf("COMEDI CMD TEST BEGINS: SAMPLING FREQ: %d, RUN TIME: %d.\n", SAMP_FREQ, RUN_TIME);

	if (init_board()) {;
		printf("Board initialization failed.\n");
		return 1;
	}
	do_cmd();

	mlockall(MCL_CURRENT | MCL_FUTURE);
	rt_make_hard_real_time();

	until = rt_get_cpu_time_ns() + (long long)RUN_TIME*1000000000;
	for (k = 0; k < SAMP_FREQ*RUN_TIME && !end; k++) {
		sinewave =  (long)(maxdata/4*sin(k*omega));
		data[0] = (lsampl_t)(  sinewave + maxdata/2);
		data[1] = (lsampl_t)(- sinewave + maxdata/2);
		while (rt_comedi_command_data_write(dev, subdev, NCHAN, data) != NCHAN) {
			rt_sleep(nano2count(SAMP_TIME/2));
		}
		if (k == TRIGSAMP) {
			rt_comedi_trigger(dev, subdev);
		}
	}

	while (until > rt_get_cpu_time_ns()) {
		rt_sleep(nano2count(100000));
	}
	comedi_cancel(dev, subdev);
	comedi_close(dev);
	comedi_data_write(dev, subdev, 0, 0, AREF_GROUND, 2048);
	comedi_data_write(dev, subdev, 1, 0, AREF_GROUND, 2048);
	printf("COMEDI TEST ENDS.\n");

	if (retval < 0) {
		printf("rt_comedi_wait_timed overruns: %d\n", abs(retval));
	}

	stop_rt_timer();
	rt_make_soft_real_time();
	rt_task_delete(task);

	return 0;
}
Ejemplo n.º 2
0
void *thread_fun(void *arg)
{
	RTIME start_time, period;
	RTIME t0, t;
	SEM *sem;
	RT_TASK *mytask;
	unsigned long mytask_name;
	int mytask_indx, jit, maxj, maxjp, count;

	mytask_indx = *((int *)arg);
	mytask_name = taskname(mytask_indx);
	cpus_allowed = 1 - cpus_allowed; 
 	if (!(mytask = rt_task_init_schmod(mytask_name, 1, 0, 0, SCHED_FIFO, 1 << cpus_allowed))) {
		printf("CANNOT INIT TASK %lu\n", mytask_name);
		exit(1);
	}
	printf("THREAD INIT: index = %d, name = %lu, address = %p.\n", mytask_indx, mytask_name, mytask);
	mlockall(MCL_CURRENT | MCL_FUTURE);

 	if (!(mytask_indx%2)) {
		rt_make_hard_real_time();
	}
	rt_receive(0, (unsigned long *)((void *)&sem));

	period = nano2count(PERIOD);
	start_time = rt_get_time() + nano2count(10000000);
	rt_task_make_periodic(mytask, start_time + (mytask_indx + 1)*period, ntasks*period);

// start of task body
	{
		count = maxj = 0;
		t0 = rt_get_cpu_time_ns();
		while(count++ < LOOPS) {
			rt_task_wait_period();
			t = rt_get_cpu_time_ns();
			if ((jit = t - t0 - ntasks*(RTIME)PERIOD) < 0) {
				jit = -jit;
			}
			if (count > 1 && jit > maxj) {
				maxj = jit;
			}
			t0 = t;
//			rtai_print_to_screen("THREAD: index = %d, count %d\n", mytask_indx, count);
		}
		maxjp = (maxj + 499)/1000;
	}
// end of task body

	rt_sem_signal(sem);
 	if (!(mytask_indx%2)) {
		rt_make_soft_real_time();
	}

	rt_task_delete(mytask);
	printf("THREAD %lu ENDS, LOOPS: %d MAX JIT: %d (us)\n", mytask_name, count, maxjp);
	return 0;
}
Ejemplo n.º 3
0
Archivo: rtmain.c Proyecto: ArcEye/RTAI
static void *rt_BaseRate(void *args)
{
	char name[7];
	int i;
	static RTIME t0;


	for (i = 0; i < MAX_NTARGETS; i++) {
		sprintf(name,"BRT%d",i);
		if (!rt_get_adr(nam2num(name))) break;
	}
	if (!(rt_BaseRateTask = rt_task_init_schmod(nam2num(name), *((int *)args), 0, 0, SCHED_FIFO, CpuMap))) {
		fprintf(stderr,"Cannot init rt_BaseRateTask.\n");
		return (void *)1;
	}

	sem_post(&err_sem);

	iopl(3);
	rt_task_use_fpu(rt_BaseRateTask, 1);

	MXmain();
	grow_and_lock_stack(stackinc);
	if (UseHRT) {
		rt_make_hard_real_time();
	}

	rt_rpc(rt_MainTask, 0, (void *)name);
	t0 = rt_get_cpu_time_ns();
	rt_task_make_periodic(rt_BaseRateTask, rt_get_time() + rt_BaseRateTick, rt_BaseRateTick);
	while (!endBaseRate) {
#ifdef TASKDURATION
		RTTSKper=rt_get_cpu_time_ns()-RTTSKinit;
#endif
		WaitTimingEvent(TimingEventArg);

		if (endBaseRate) break;
		APPLICATION_Process_Event(TIME_EV);

		SIM_TIME = (rt_get_cpu_time_ns() - t0)*1.0E-9;
#ifdef TASKDURATION
		RTTSKinit=rt_get_cpu_time_ns();
#endif

	}
	if (UseHRT) {
		rt_make_soft_real_time();
	}

	rt_task_delete(rt_BaseRateTask);

	return 0;
}
Ejemplo n.º 4
0
static void Fast_Thread(long dummy)
{
	int jit;
	RTIME svt, t;
	svt = rt_get_cpu_time_ns() - FASTMUL*TICK_TIME;
	while (1) {
		jit = (int) ((t = rt_get_cpu_time_ns()) - svt - FASTMUL*TICK_TIME);
		svt = t;
		if (jit) { jit = - jit; }
		if (jit > fastjit) { fastjit = jit; }
		cpu_used[hard_cpu_id()]++;
		rt_busy_sleep(FASTMUL/2*TICK_TIME);
		rt_task_wait_period();
	}
}
Ejemplo n.º 5
0
static void fun(long thread) {

	struct sample { long min, max, avrg, jitters[2]; } samp;
	int diff;
	int skip;
	int average;
	int min_diff;
	int max_diff;
	RTIME svt, t;

	t = 0;
	min_diff = 1000000000;
	max_diff = -1000000000;
	while (1) {
		unsigned long flags;
		average = 0;

		svt = rt_get_cpu_time_ns();
		for (skip = 0; skip < NAVRG; skip++) {
			cpu_used[hard_cpu_id()]++;
			expected += period;
			rt_task_wait_period();

			rt_global_save_flags(&flags);
#ifndef ONESHOT_MODE
			diff = (int) ((t = rt_get_cpu_time_ns()) - svt - TICK_TIME);
			svt = t;
#else
			diff = (int) count2nano(rt_get_time() - expected);
#endif
			if (diff < min_diff) {
				min_diff = diff;
			}
			if (diff > max_diff) {
				max_diff = diff;
			}
		average += diff;
		}
		samp.min = min_diff;
		samp.max = max_diff;
		samp.avrg = average/NAVRG;
		samp.jitters[0] = fastjit;
		samp.jitters[1] = slowjit;
		rtf_ovrwr_put(FIFO, &samp, sizeof(samp));
	}
}
Ejemplo n.º 6
0
Archivo: mbx.c Proyecto: cjecho/RTAI
static void mfun(long t)
{
	RTIME time;
	unsigned long long msg;
	int size, maxt = 0, itime;

	while (1) {
		time = rt_get_cpu_time_ns();
		cpu_used[hard_cpu_id()]++;

		mstat[t] = 's';
		if ((size = rt_mbx_send_timed(&smbx, &name[t], sizeof(long long), nano2count(TIMEOUT)))) {
			rt_printk("SEND TIMEDOUT TASK: %d, UNSENT: %d, STAT: %c %c %c, OVERTIME: %d.\n", t, size, mstat[0], mstat[1], bstat, (int)(rt_get_cpu_time_ns() - time));
			goto prem;
		}

		msg = 0;
		mstat[t] = 'r';
		if ((size = rt_mbx_receive_timed(&rmbx[t], &msg, sizeof(msg), nano2count(TIMEOUT)))) {
			rt_printk("RECEIVE TIMEDOUT TIME: %d, NOTRECEIVED: %d, MSG: %x%x, STAT: %c %c %c, OVERTIME: %d.\n", t, size, ((int *)&msg)[1], ((int *)&msg)[0], mstat[0], mstat[1], bstat, (int)(rt_get_cpu_time_ns() - time));
			goto prem;
		}

		if (msg != 0xccccccccccccccccLL) {
                        rt_printk("WRONG REPLY TO TASK: %d, MSG: %x %x.\n", t, ((int *)&msg)[0], ((int *)&msg)[1]);
                        goto prem;
		}
		mstat[t] = 'd';
		if ((itime = rt_get_cpu_time_ns() - time) > maxt) {
			rt_printk("TASK: %d, MAXWAITIME: %d.\n", t, maxt = itime);
		}
		meant[t] = (itime + meant[t]) >> 1;
		rt_sleep(nano2count(SLEEP_TIME));
	}

prem:
	premature = PREMATURE;
	rt_printk("TASK # %d ENDS PREMATURELY\n", t);
}
Ejemplo n.º 7
0
int echo_rcv(int s,void *arg)
{
	int			ret=0;
	struct msghdr		msg;
	struct iovec		iov;
	struct sockaddr_in	addr;
	

	memset(&msg,0,sizeof(msg));
	iov.iov_base=&buffer;
	iov.iov_len=BUFSIZE;

	msg.msg_name=&addr;
	msg.msg_namelen=sizeof(addr);
	msg.msg_iov=&iov;
	msg.msg_iovlen=1;
	msg.msg_control=NULL;
	msg.msg_controllen=0;

	ret=rt_socket_recvmsg(sock, &msg, 0);

	if ( (ret>0) && (msg.msg_namelen==sizeof(struct sockaddr_in)) ) {
		
		union { unsigned long l; unsigned char c[4]; } rcv;
		struct sockaddr_in *sin = msg.msg_name;
		
		/* get the time    */
		rx_time = rt_get_cpu_time_ns();
		memcpy (&tx_time, buffer, sizeof(RTIME));
		rcvd++;

		ret = rtf_put(PRINT, &rx_time, sizeof(RTIME));
		ret = rtf_put(PRINT, &tx_time, sizeof(RTIME));
		if (0 == ret) {
			rt_task_suspend(&rt_task);
			rt_printk("fifo full, sending task suspended\n");
		}

		/* copy the address */
		rcv.l = sin->sin_addr.s_addr;
	}

	return 0;
}
Ejemplo n.º 8
0
void echo_rcv(struct rtdm_dev_context *context, void *arg)
{
    int                 ret=0;
    struct msghdr       msg;
    struct iovec        iov;
    struct sockaddr_in  addr;


    iov.iov_base=&buffer;
    iov.iov_len=BUFSIZE;

    msg.msg_name=&addr;
    msg.msg_namelen=sizeof(addr);
    msg.msg_iov=&iov;
    msg.msg_iovlen=1;
    msg.msg_control=NULL;
    msg.msg_controllen=0;

    /* This demonstrates the fast path to the RTnet API for kernel modules.
       Note that this method depends on using the correct RTDM version. An
       alternative is to take the file descriptor from a global variable or
       pass it in the callback argument "arg" and then call the official API
       functions. */
    ret = context->ops->recvmsg_rt(context, 0, &msg, 0);

    if ((ret > 0) && (msg.msg_namelen == sizeof(struct sockaddr_in))) {

        union { unsigned long l; unsigned char c[4]; } rcv;
        struct sockaddr_in *sin = msg.msg_name;

        /* get the time    */
        rx_time = rt_get_cpu_time_ns();
        memcpy (&tx_time, buffer, sizeof(RTIME));
        rcvd++;

        rtf_put(PRINT, &rx_time, sizeof(RTIME));
        rtf_put(PRINT, &tx_time, sizeof(RTIME));

        /* copy the address */
        rcv.l = sin->sin_addr.s_addr;
    }
}
Ejemplo n.º 9
0
void *process(void * arg)
{
	int ret = 0;

	while(1) {

	        /* wait one period */ 
	        rt_task_wait_period();

                /* get time        */
		tx_time = rt_get_cpu_time_ns();

		memcpy (sendbuffer, &tx_time, sizeof(tx_time));

                /* send the time   */    
		ret=rt_socket_sendto
			(sock, &sendbuffer, packetsize, 0, (struct sockaddr *) &server_addr, sizeof(struct sockaddr_in));
		if (ret) sent++;
	}
}
Ejemplo n.º 10
0
long long int _rtapi_get_time_hook(void) {
    //struct timeval tv;

    //AJ: commenting the following code out, as it seems on some systems it
    // really breaks
    
    /* call the kernel's internal implementation of gettimeofday() */
    /* unfortunately timeval has only usec, struct timespec would be
       better, it has nsec resolution.  Doing this right probably
       involves a number of ifdefs based on kernel version and such */
    /*do_gettimeofday(&tv);*/
    /* convert to nanoseconds */
    /*return (tv.tv_sec * 1000000000LL) + (tv.tv_usec * 1000L);*/
    
    //reverted to old code for now
    /* this is a monstrosity that seems to take several MICROSECONDS!!!
       on some boxes.  Why the RTAI folks even bothered I have no idea!
       If you have any need for speed at all use _rtapi_get_clocks()!!
    */
    return rt_get_cpu_time_ns();
}
Ejemplo n.º 11
0
static void *fun(int idx)
{
	unsigned int loops = LOOPS;
	while(loops--) {
		rt_printk("TASK %d 1 COND/TIMED PREWLOCKED\n", idx);
		if (idx%2) {
			if (pthread_rwlock_trywrlock(&rwl)) {
				rt_printk("TASK %d 1 COND PREWLOCKED FAILED GO UNCOND\n", idx);
				pthread_rwlock_wrlock(&rwl);
			}
		} else {
			struct timespec abstime;
			abstime.tv_sec = llimd(rt_get_cpu_time_ns(), 1, 1000000000);
			abstime.tv_nsec = 20000;
			if (pthread_rwlock_timedwrlock(&rwl, &abstime) < 0) {
				rt_printk("TASK %d 1 TIMED PREWLOCKED FAILED GO UNCOND\n", idx);
				pthread_rwlock_wrlock(&rwl);
			}
		}
		rt_printk("TASK %d 1 WLOCKED\n", idx);
		rt_busy_sleep(100000);
		rt_printk("TASK %d 2 COND PREWLOCK\n", idx);
		if (pthread_rwlock_trywrlock(&rwl)) {
			rt_printk("TASK %d 2 COND PREWLOCK FAILED GO UNCOND\n", idx);
			pthread_rwlock_wrlock(&rwl);
		}
		rt_printk("TASK %d 2 WLOCK\n", idx);
		rt_busy_sleep(100000);
		rt_printk("TASK %d 3 PREWLOCK\n", idx);
		pthread_rwlock_wrlock(&rwl);
		rt_printk("TASK %d 3 WLOCK\n", idx);
		rt_busy_sleep(100000);
		rt_printk("TASK %d 3 PREWUNLOCK\n", idx);
		pthread_rwlock_unlock(&rwl);
		rt_printk("TASK %d 3 WUNLOCK\n", idx);
		rt_busy_sleep(100000);
		rt_printk("TASK %d 2 PREWUNLOCK\n", idx);
		pthread_rwlock_unlock(&rwl);
		rt_printk("TASK %d 2 WUNLOCK\n", idx);
		rt_busy_sleep(100000);
		rt_printk("TASK %d 1 PREWUNLOCKED\n", idx);
		pthread_rwlock_unlock(&rwl);
		rt_printk("TASK %d 1 WUNLOCKED\n", idx);
		rt_busy_sleep(100000);
		rt_printk("TASK %d 1 COND/TIMED PRERDLOCKED\n", idx);
		if (idx%2) {
			if (pthread_rwlock_tryrdlock(&rwl)) {
				rt_printk("TASK %d 1 COND PRERDLOCKED FAILED GO UNCOND\n", idx);
				pthread_rwlock_rdlock(&rwl);
			}
		} else {
			struct timespec abstime;
			abstime.tv_sec = llimd(rt_get_cpu_time_ns(), 1, 1000000000);
			abstime.tv_nsec = 20000;
			if (pthread_rwlock_timedrdlock(&rwl, &abstime) < 0) {
				rt_printk("TASK %d 1 TIMED PRERDLOCKED FAILED GO UNCOND\n", idx);
				pthread_rwlock_rdlock(&rwl);
			}
		}
		rt_printk("TASK %d 1 RDLOCKED\n", idx);
		rt_busy_sleep(100000);
		rt_printk("TASK %d 2 COND PRERDLOCK\n", idx);
		if (pthread_rwlock_tryrdlock(&rwl)) {
			rt_printk("TASK %d 2 COND PRERDLOCK FAILED GO UNCOND\n", idx);
			pthread_rwlock_rdlock(&rwl);
		}
		rt_printk("TASK %d 2 RDLOCK\n", idx);
		rt_busy_sleep(100000);
		rt_printk("TASK %d 3 PRERDLOCK\n", idx);
		pthread_rwlock_rdlock(&rwl);
		rt_printk("TASK %d 3 RDLOCK\n", idx);
		rt_busy_sleep(100000);
		rt_printk("TASK %d 3 PRERDUNLOCK\n", idx);
		pthread_rwlock_unlock(&rwl);
		rt_printk("TASK %d 3 RDUNLOCK\n", idx);
		rt_busy_sleep(100000);
		rt_printk("TASK %d 2 PRERDUNLOCK\n", idx);
		pthread_rwlock_unlock(&rwl);
		rt_printk("TASK %d 2 RDUNLOCK\n", idx);
		rt_busy_sleep(100000);
		rt_printk("TASK %d 1 PRERDUNLOCK\n", idx);
		pthread_rwlock_unlock(&rwl);
		rt_printk("TASK %d 1 RDUNLOCK\n", idx);
		rt_busy_sleep(100000);
	}
	extcnt[idx - 1] = loops;
	rt_sleep(nano2count(10000000));
	rt_printk("TASK %d EXITED\n", idx);
	cleanup = 1;
	return 0;
}
Ejemplo n.º 12
0
void
fun(long thread)
{

    int diff = 0;
    int i;
    int average;
    int min_diff = 0;
    int max_diff = 0;
    RTIME t, svt;

#ifdef CONFIG_RTAI_FPU_SUPPORT
    if (use_fpu) {
        for(i = 0; i < MAXDIM; i++) {
            a[i] = b[i] = 3.141592;
        }
    }
#endif

    svt = rt_get_cpu_time_ns();
    samp.ovrn = 0;
    while (1) {

        min_diff =  1000000000;
        max_diff = -1000000000;

        average = 0;

        for (i = 0; i < loops; i++) {
            cpu_used[hard_cpu_id()]++;
            expected += period_counts;

            if (!rt_task_wait_period()) {
                if (timer_mode) {
                    diff = (int) ((t = rt_get_cpu_time_ns()) - svt - period);
                    svt = t;
                } else {
                    diff = (int) count2nano(rt_get_time() - expected);
                }
            } else {
                samp.ovrn++;
                diff = 0;
                if (timer_mode) {
                    svt = rt_get_cpu_time_ns();
                }
            }

            if (diff < min_diff) {
                min_diff = diff;
            }
            if (diff > max_diff) {
                max_diff = diff;
            }
            average += diff;
#ifdef CONFIG_RTAI_FPU_SUPPORT
            if (use_fpu) {
                dotres = dot(a, b, MAXDIM);
            }
#endif
        }
        samp.min = min_diff;
        samp.max = max_diff;
        samp.index = average / loops;
        rtf_put(DEBUG_FIFO, &samp, sizeof (samp));
    }
    rt_printk("\nDOT PRODUCT RESULT = %lu\n", (unsigned long)dotres);
}
Ejemplo n.º 13
0
int main(void)
{
	RTIME tsr, tss, tsm, trpc;
	RT_TASK *mainbuddy;
	int i, k, s;
	unsigned long msg;

	printf("\n\nWait for it ...\n");
	if (!(mainbuddy = rt_thread_init(nam2num("MASTER"), 1000, 0, SCHED_FIFO, 0x1))) {
		printf("CANNOT INIT TASK %lu\n", nam2num("MASTER"));
		exit(1);
	}

	sem = rt_sem_init(nam2num("SEMAPH"), 1);
	change =  0;

	for (i = 0; i < NR_RT_TASKS; i++) {
		indx[i] = i;
		if (!(thread[i] = rt_thread_create(thread_fun, indx + i, 0))) {
			printf("ERROR IN CREATING THREAD %d\n", indx[i]);
			exit(1);
 		}
 	}

	do {
		msleep(50);
		s = 0;
		for (i = 0; i < NR_RT_TASKS; i++) {
			s += hrt[i];
		}
	} while (s != NR_RT_TASKS);
	mlockall(MCL_CURRENT | MCL_FUTURE);

	rt_make_hard_real_time();
	tsr = rt_get_cpu_time_ns();
	for (i = 0; i < LOOPS; i++) {
		for (k = 0; k < NR_RT_TASKS; k++) {
			rt_task_resume(mytask[k]);
		}
	}
	tsr = rt_get_cpu_time_ns() - tsr;

	change = 1;

	for (k = 0; k < NR_RT_TASKS; k++) {
		rt_task_resume(mytask[k]);
	}

	tss = rt_get_cpu_time_ns();
	for (i = 0; i < LOOPS; i++) {
		for (k = 0; k < NR_RT_TASKS; k++) {
			rt_sem_signal(sem);
		}
	}
	tss = rt_get_cpu_time_ns() - tss;

	change = 2;

	for (k = 0; k < NR_RT_TASKS; k++) {
		rt_sem_signal(sem);
	}

	tsm = rt_get_cpu_time_ns();
	for (i = 0; i < LOOPS; i++) {
		for (k = 0; k < NR_RT_TASKS; k++) {
			rt_send(mytask[k], 0);
		}
	}
	tsm = rt_get_cpu_time_ns() - tsm;

	change = 3;

	for (k = 0; k < NR_RT_TASKS; k++) {
		rt_send(mytask[k], 0);
	}

	trpc = rt_get_cpu_time_ns();
	for (i = 0; i < LOOPS; i++) {
		for (k = 0; k < NR_RT_TASKS; k++) {
			rt_rpc(mytask[k], 0, &msg);
		}
	}
	trpc = rt_get_cpu_time_ns() - trpc;

	rt_make_soft_real_time();

	printf("\n\nFOR %d TASKS: ", NR_RT_TASKS);
	printf("TIME %d (ms), SUSP/RES SWITCHES %d, ", (int)(tsr/1000000), 2*NR_RT_TASKS*LOOPS);
	printf("SWITCH TIME %d (ns)\n", (int)(tsr/(2*NR_RT_TASKS*LOOPS)));

	printf("\nFOR %d TASKS: ", NR_RT_TASKS);
	printf("TIME %d (ms), SEM SIG/WAIT SWITCHES %d, ", (int)(tss/1000000), 2*NR_RT_TASKS*LOOPS);
	printf("SWITCH TIME %d (ns)\n", (int)(tss/(2*NR_RT_TASKS*LOOPS)));

	printf("\nFOR %d TASKS: ", NR_RT_TASKS);
	printf("TIME %d (ms), SEND/RCV SWITCHES %d, ", (int)(tsm/1000000), 2*NR_RT_TASKS*LOOPS);
	printf("SWITCH TIME %d (ns)\n", (int)(tsm/(2*NR_RT_TASKS*LOOPS)));

	printf("\nFOR %d TASKS: ", NR_RT_TASKS);
	printf("TIME %d (ms), RPC/RCV-RET SWITCHES %d, ", (int)(tsm/1000000), 2*NR_RT_TASKS*LOOPS);
	printf("SWITCH TIME %d (ns)\n\n", (int)(trpc/(2*NR_RT_TASKS*LOOPS)));

	fflush(stdout);

	end = 1;
	for (i = 0; i < NR_RT_TASKS; i++) {
		rt_rpc(mytask[i], 0, &msg);
	}
	do {
		msleep(50);
		s = 0;
		for (i = 0; i < NR_RT_TASKS; i++) {
			s += hrt[i];
		}
	} while (s);

	rt_sem_delete(sem);
	rt_task_delete(mainbuddy);
	for (i = 0; i < NR_RT_TASKS; i++) {
		rt_thread_join(thread[i]);
	}

	return 0;
}
Ejemplo n.º 14
0
void *init_task(void *arg)
{
	int idTask = ((struct thread_param*) arg)->idTask;

	// Variaveis para realizar os calculos de tempo...
	struct tm *newtime;
	time_t aclock;

	RTIME inicioExecucao = 0;
	RTIME terminoExecucao = 0;
	RTIME terminoPeriodo = 0;
	RTIME Tperiodo, Tinicio;
	float tempo_processamento_tarefa;
	float periodo_tarefa;
	int prioridade = idTask + 1;

	double tempoProcessamento = 0.0;
	unsigned int cpu_frequency;

	if(!(arrayTasks[idTask] = rt_task_init_schmod(idTask, prioridade, STACK_SIZE, 0, SCHED_FIFO, CPU_ALLOWED)))
	{
		printf("[ERRO] Não foi possível criar a tarefa 1.\n");
		exit(1);
	}

	rt_allow_nonroot_hrt();

	Tinicio = timeline_sched;

	switch (idTask) {
		case 0:
			Tperiodo = tick_period * 6; // 3 segundos
		break;

		case 1:
			Tperiodo = tick_period * 8; // 4 segundos
		break;

		default:
			printf("[ERRO] A Tarefa %d nao possui periodo definido.\n", idTask);
			exit(1);
		break;
	}

	//rt_change_prio(arrayTasks[idTask], idTask);
	rt_task_make_periodic(arrayTasks[idTask], Tinicio, Tperiodo);

	printf("[TASK %d] Criada com Sucesso  =======> %llu\n", idTask, Tperiodo);

	while (1)
	{
		cpu_frequency = 2300000;
		rt_cfg_init_info(arrayTasks[idTask], 100, cpu_frequency, cpu_frequency, 3003); // Lugar correto...

		time(&aclock); // Pega tempo em segundos.
		newtime = localtime(&aclock);

		printf("%s[TASK %d] Processando...  0%% => %s", arrayTextoCorIdTask[idTask], idTask, asctime(newtime));

		inicioExecucao = rt_get_cpu_time_ns();
		cpu_frequency = 1800000;
		rt_cfg_set_cpu_frequency(arrayTasks[idTask], cpu_frequency);
		consumirProcessamento(cpu_frequency, tempoProcessamento = 1.0); //CODIGO PARA CONSUMIR PROCESSAMENTO...FREQ EM HZ E TEMPO EM SEGUNDOS...

		cpu_frequency = 800000;
		rt_cfg_set_cpu_frequency(arrayTasks[idTask], cpu_frequency);
		consumirProcessamento(cpu_frequency, tempoProcessamento = 1.0); //CODIGO PARA CONSUMIR PROCESSAMENTO...FREQ EM HZ E TEMPO EM SEGUNDOS...
		terminoExecucao = rt_get_cpu_time_ns();

		time(&aclock); // Pega tempo em segundos.
		newtime = localtime(&aclock);

		tempo_processamento_tarefa = (terminoExecucao - inicioExecucao) / 1000000000.0; // Transformando de nanosegundo para segundo (10^9).
		printf("%s[TASK %d] Processando... 100%% =======> Tempo processamento: %.10f => %s", arrayTextoCorIdTask[idTask], idTask, tempo_processamento_tarefa, asctime(newtime));

		rt_task_wait_period(); // **** WAIT
	}
}
Ejemplo n.º 15
0
int main(int argc, char *argv[])
{
	int diff;
	int sample;
	long average;
	int min_diff;
	int max_diff;
	int period;
	int i;
	RTIME t, svt;
	RTIME expected, exectime[3];
	MBX *mbx;
	RT_TASK *task, *latchk;
	struct sample { long long min; long long max; int index, ovrn; } samp;
	double s = 0.0, sref;
	long long max = -1000000000, min = 1000000000;

	signal(SIGINT, endme);
	signal(SIGKILL, endme);
	signal(SIGTERM, endme);

 	if (!(mbx = rt_mbx_init(nam2num("LATMBX"), 20*sizeof(samp)))) {
		printf("CANNOT CREATE MAILBOX\n");
		exit(1);
	}

 	if (!(task = rt_task_init_schmod(nam2num("LATCAL"), 0, 0, 0, SCHED_FIFO, 0xF))) {
		printf("CANNOT INIT MASTER LATENCY TASK\n");
		exit(1);
	}

	printf("\n## RTAI latency calibration tool ##\n");
	printf("# period = %i (ns) \n", PERIOD);
	printf("# average time = %i (s)\n", (int)AVRGTIME);
	printf("# use the FPU\n");
	printf("#%sstart the timer\n", argc == 1 ? " " : " do not ");
	printf("# timer_mode is %s\n", TIMER_MODE ? "periodic" : "oneshot");
	printf("\n");

	if (!(hard_timer_running = rt_is_hard_timer_running())) {
		if (TIMER_MODE) {
			rt_set_periodic_mode();
		} else {
			rt_set_oneshot_mode();
		}
		period = start_rt_timer(nano2count(PERIOD));
	} else {
		period = nano2count(PERIOD);
	}

	for(i = 0; i < MAXDIM; i++) {
		a[i] = b[i] = 3.141592;
	}
	sref = dot(a, b, MAXDIM);

	mlockall(MCL_CURRENT | MCL_FUTURE);

	rt_make_hard_real_time();
	expected = rt_get_time() + 200*period;
	rt_task_make_periodic(task, expected, period);
	svt = rt_get_cpu_time_ns();
	i = 0;
	samp.ovrn = 0;
	while (!end) {
		min_diff = 1000000000;
		max_diff = -1000000000;
		average = 0;

		for (sample = 0; sample < SMPLSXAVRG && !end; sample++) {
			expected += period;
			if (!rt_task_wait_period()) {
				if (TIMER_MODE) {
					diff = (int) ((t = rt_get_cpu_time_ns()) - svt - PERIOD);
					svt = t;
				} else {
					diff = (int) count2nano(rt_get_time() - expected);
				}
			} else {
				samp.ovrn++;
				diff = 0;
				if (TIMER_MODE) {
					svt = rt_get_cpu_time_ns();
				}
			}
			outb(i = 1 - i, 0x378);

			if (diff < min_diff) {
				min_diff = diff;
			}
			if (diff > max_diff) {
				max_diff = diff;
			}
			average += diff;
			s = dot(a, b, MAXDIM);
			if (fabs((s - sref)/sref) > 1.0e-15) {
				printf("\nDOT PRODUCT RESULT = %20.16e %20.16e %20.16e\n", s, sref, fabs((s - sref)/sref));
				return 0;
			}
		}
		samp.min = min_diff;
		samp.max = max_diff;
		samp.index = average/SMPLSXAVRG;
#if SOLO
		if (max < samp.max) max = samp.max;
		if (min > samp.min) min = samp.min;
		rt_printk("SOLO * min: %lld/%lld, max: %lld/%lld average: %d (%d) <Hit [RETURN] to stop> *\n", samp.min, min, samp.max, max, samp.index, samp.ovrn);
#else
		rt_mbx_send_if(mbx, &samp, sizeof(samp));
		if ((latchk = rt_get_adr(nam2num("LATCHK"))) && (rt_receive_if(latchk, &average) || end)) {
			rt_return(latchk, average);
			break;
		}
#endif
	}

	while (rt_get_adr(nam2num("LATCHK"))) {
		rt_sleep(nano2count(1000000));
	}
	rt_make_soft_real_time();
	if (!hard_timer_running) {
		stop_rt_timer();
	}
	rt_get_exectime(task, exectime);
	if (exectime[1] && exectime[2]) {
		printf("\n>>> S = %g, EXECTIME = %G\n", s, (double)exectime[0]/(double)(exectime[2] - exectime[1]));
	}
	rt_task_delete(task);
	rt_mbx_delete(mbx);

	return 0;
}
Ejemplo n.º 16
0
int main(int argc, char *argv[])
{
	int diff;
	int skip;
	long average;
	int min_diff;
	int max_diff;
	int period;
	int i;
	RTIME t, svt;
	RTIME expected, exectime[3];
	MBX *mbx;
	RT_TASK *task, *latchk;
	struct sample { long long min; long long max; int index, ovrn; } samp;
	double s;

	signal(SIGHUP,  endme);
	signal(SIGKILL, endme);
	signal(SIGTERM, endme);
	signal(SIGALRM, endme);

 	if (!(mbx = rt_mbx_init(nam2num("LATMBX"), 20*sizeof(samp)))) {
		printf("CANNOT CREATE MAILBOX\n");
		exit(1);
	}

 	if (!(task = rt_task_init_schmod(nam2num("LATCAL"), 0, 0, 0, SCHED_FIFO, 0xF))) {
		printf("CANNOT INIT MASTER TASK\n");
		exit(1);
	}

	printf("\n## RTAI latency calibration tool ##\n");
	printf("# period = %i (ns) \n", PERIOD);
	printf("# average time = %i (s)\n", (int)AVRGTIME);
	printf("# use the FPU\n");
	printf("#%sstart the timer\n", argc == 1 ? " " : " do not ");
	printf("# timer_mode is %s\n", TIMER_MODE ? "periodic" : "oneshot");
	printf("\n");

	if (argc == 1) {
		if (TIMER_MODE) {
			rt_set_periodic_mode();
		} else {
			rt_set_oneshot_mode();
		}
		period = start_rt_timer(nano2count(PERIOD));
	} else {
		period = nano2count(PERIOD);
	}

        for(i = 0; i < MAXDIM; i++) {
                a[i] = b[i] = 3.141592;
        }
	s = dot(a, b, MAXDIM);

	mlockall(MCL_CURRENT | MCL_FUTURE);

	rt_make_hard_real_time();
	rt_task_make_periodic(task, expected = rt_get_tscnt() + 10*period, period);

	svt = rt_get_cpu_time_ns();
	samp.ovrn = i = 0;
	while (!end) {
		min_diff = 1000000000;
		max_diff = -1000000000;
		average = 0;

		for (skip = 0; skip < SKIP && !end; skip++) {
			expected += period;

			if (!rt_task_wait_period()) {
				if (TIMER_MODE) {
					diff = (int) ((t = rt_get_cpu_time_ns()) - svt - PERIOD);
					svt = t;
				} else {
					diff = (int) count2nano(rt_get_tscnt() - expected);
				}
			} else {
				samp.ovrn++;
				diff = 0;
				if (TIMER_MODE) {
					svt = rt_get_cpu_time_ns();
				}
			}
			if (diff < min_diff) {
				min_diff = diff;
			}
			if (diff > max_diff) {
				max_diff = diff;
			}
			average += diff;
			s = dot(a, b, MAXDIM);
		}
		samp.min = min_diff;
		samp.max = max_diff;
		samp.index = average/SKIP;
		rt_mbx_send_if(mbx, &samp, sizeof(samp));
		if ((latchk = rt_get_adr(nam2num("LATCHK"))) && (rt_receive_if(latchk, (unsigned long *)&average) || end)) {
			rt_return(latchk, (unsigned long)average);
			break;
		}
	}

	while (rt_get_adr(nam2num("LATCHK"))) {
		rt_sleep(nano2count(1000000));
	}
	rt_make_soft_real_time();
	if (argc == 1) {
		stop_rt_timer();	
	}
	rt_get_exectime(task, exectime);
	if (exectime[1] && exectime[2]) {
		printf("\n>>> S = %g, EXECTIME = %G\n", s, (double)exectime[0]/(double)(exectime[2] - exectime[1]));
	}
	rt_task_delete(task);
	rt_mbx_delete(mbx);

	return 0;
}
static void* rt_system_thread(void * arg)
{	
	SEM * status_sem;
	SEM * command_sem;
	RT_TASK *task;
	int cntr=0;
	M3Sds * sds = (M3Sds *)arg;
	printf("Starting real-time thread\n");
		
	
	sds_status_size = sizeof(M3LedMatrixEcShmSdsStatus);
	sds_cmd_size = sizeof(M3LedMatrixEcShmSdsCommand);
	
	memset(&cmd, 0, sds_cmd_size);
	
	task = rt_task_init_schmod(nam2num("LSHMP"), 0, 0, 0, SCHED_FIFO, 0xF);
	rt_allow_nonroot_hrt();
	if (task==NULL)
	{
		printf("Failed to create RT-TASK LSHMP\n");
		return 0;
	}
	status_sem=(SEM*)rt_get_adr(nam2num(MEKA_LED_STATUS_SEM));
	command_sem=(SEM*)rt_get_adr(nam2num(MEKA_LED_CMD_SEM));
	if (!status_sem)
	{
		printf("Unable to find the %s semaphore.\n",MEKA_LED_STATUS_SEM);
		rt_task_delete(task);
		return 0;
	}
	if (!command_sem)
	{
		printf("Unable to find the %s semaphore.\n",MEKA_LED_CMD_SEM);
		rt_task_delete(task);
		return 0;
	}
	
	
	RTIME tick_period = nano2count(RT_TIMER_TICKS_NS_MEKA_LED_SHM); 
	RTIME now = rt_get_time();
	rt_task_make_periodic(task, now + tick_period, tick_period); 
	mlockall(MCL_CURRENT | MCL_FUTURE);
	rt_make_hard_real_time();
	long long start_time, end_time, dt;
	long long step_cnt = 0;
	sys_thread_active=1;
	
	while(!sys_thread_end)
	{
		start_time = nano2count(rt_get_cpu_time_ns());
		rt_sem_wait(status_sem);
		memcpy(&status, sds->status, sds_status_size);		
		rt_sem_signal(status_sem);
		
		StepShm(cntr);		
		
		rt_sem_wait(command_sem);		
		memcpy(sds->cmd, &cmd, sds_cmd_size);		
		rt_sem_signal(command_sem);
				
		end_time = nano2count(rt_get_cpu_time_ns());
		dt=end_time-start_time;
		/*
		Check the time it takes to run components, and if it takes longer
		than our period, make us run slower. Otherwise this task locks
		up the CPU.*/
		if (dt > tick_period && step_cnt>10) 
		{
			printf("Step %lld: Computation time of components is too long. Forcing all components to state SafeOp.\n",step_cnt);
			printf("Previous period: %f. New period: %f\n", (double)count2nano(tick_period),(double)count2nano(dt));
 			tick_period=dt;
			//rt_task_make_periodic(task, end + tick_period,tick_period);			
		}
		step_cnt++;
		if (cntr++ == CYCLE_TIME_SEC * 2 * RT_TIMER_TICKS_NS_MEKA_LED_SHM)
		  cntr = 0;
		rt_task_wait_period();
	}	
	printf("Exiting RealTime Thread...\n",0);
	rt_make_soft_real_time();
	rt_task_delete(task);
	sys_thread_active=0;
	return 0;
}
Ejemplo n.º 18
0
int main(int argc, char *argv[])
{
	MBX *mbx;
	SEM *sem;
	RT_TASK *task;
	RTIME t0, t;
	int i, count, jit, maxj, test_time, hdlnode, hdlport;
	struct sockaddr_in addr;
	unsigned long run;
	char c;

 	if (!(task = rt_task_init_schmod(nam2num("PRCTSK"), 1, 0, 0, SCHED_FIFO, TASK_CPU))) {
		printf("CANNOT INIT PROCESS TASK\n");
		exit(1);
	}

        hdlnode = 0;
        if (argc == 2 && strstr(argv[1], "HdlNode=")) {
                inet_aton(argv[1] + 8, &addr.sin_addr);
                hdlnode = addr.sin_addr.s_addr;
        }
        if (!hdlnode) {
                inet_aton("127.0.0.1", &addr.sin_addr);
                hdlnode = addr.sin_addr.s_addr;
        }
	while ((hdlport = rt_request_port(hdlnode)) <= 0 && hdlport != -EINVAL);
	mbx = RT_get_adr(hdlnode, hdlport, "HDLMBX");
        sem = rt_sem_init(nam2num("PRCSEM"), 0);

	printf("USE: SEM SEND/WAIT (s), TASK RESM/SUSP (r), INTERTASK MSG (m): [s|r|m]? ");
	scanf("%c", &c);
	switch (c) {
		case 'r': printf("TESTING TASK SUSPEND/RESUME TIME (s): ");
			  run = 2;
			  break;
		case 'm': printf("TESTING INTERTASK SEND/RECEIVE TIME (s): ");
			  run = 3;
			  break;
		default:  printf("TESTING SEMAPHORE WAIT/SEND TIME (s): ");
			  run = 1;
			  break;
	}
	scanf("%d", &test_time);
	printf("... WAIT FOR %d SECONDS (RUNNING AT %d hz).\n", test_time, 1000000000/PERIOD);
	mlockall(MCL_CURRENT | MCL_FUTURE);
	rt_make_hard_real_time();

        RT_mbx_send(hdlnode, hdlport, mbx, &task, sizeof(task));
        RT_mbx_send(hdlnode, hdlport, mbx, &sem, sizeof(sem));
        RT_mbx_send(hdlnode, hdlport, mbx, &run, sizeof(run));

	count = maxj = 0;
	t0 = rt_get_cpu_time_ns();
	while(++count <= test_time*(1000000000/(PERIOD*100))) {
		for (i = 0; i < 100; i++) {
			t = rt_get_cpu_time_ns();
			if ((jit = t - t0 - PERIOD) < 0) {
				jit = -jit;
			}
			if (count > 1 && jit > maxj) {
				maxj = jit;
			}
			t0 = t;
			switch (run) {
				case 1: rt_sem_wait(sem);
					break;
				case 2: rt_task_suspend(task);
					break;
				case 3: rt_receive(0, &run);
					break;
			}
		}
	}

	run = 0;
        RT_mbx_send(hdlnode, hdlport, mbx, &run, sizeof(run));
	rt_make_soft_real_time();
	rt_release_port(hdlnode, hdlport);
	rt_task_delete(task);
	printf("***** MAX JITTER %3d (us) *****\n", (maxj + 499)/1000);
	exit(0);
}
Ejemplo n.º 19
0
Archivo: client.c Proyecto: ArcEye/RTAI
int main(void)
{
	char s[BUFSIZE];
	int i, fd;
	long long ll;
	float f;
	double d;
	RT_TASK *mytask;
	RTIME t;
        struct timeval timout;
	struct timespec tns;

 	if (!(mytask = rt_task_init_schmod(nam2num("HRTSK"), 0, 0, 0, SCHED_FIFO, 0x1))) {
		printf("CANNOT INIT TEST BUDDY TASK\n");
		exit(1);
	}
	rt_sync_async_linux_syscall_server_create(NULL, SYNC_LINUX_SYSCALL, NULL, NRECS + 1);
	rt_grow_and_lock_stack(40000);
	rt_make_hard_real_time();
	rt_task_use_fpu(mytask, 1);

#if 1
	printf("(SCANF) Input a: string, integer, long long, float, double: ");
	scanf("%s %i %lld %f %lf", s, &i, &ll, &f, &d);
	printf("(SELECT) Got string, integer, long long, now we wait %d s using select.\n", WAITIME);
        timout.tv_sec = WAITIME;
        timout.tv_usec = 0;
	select(1, NULL, NULL, NULL, &timout);
	printf("(PRINTF) Select expired and we print what we read: %s %i %lld %f %lf\n", s, i, ll, f, d);
	printf("(OPEN) Open a file.\n");
	fd = open("rtfile", O_RDWR | O_CREAT | O_TRUNC, 0666);
	printf("(WRITE) Write a %d bytes header of 'B's.\n", HDRSIZE);
	memset(s, 'B', HDRSIZE);
	write(fd, s, HDRSIZE);
	memset(s, 'A', BUFSIZE);
	printf("(WRITE) Write %d records of %d 'A's to the opened file.\n", NRECS, BUFSIZE);

#if SYNC_ASYNC == ASYNC_LINUX_SYSCALL
	printf("!!!!! The writing will be executed asynchronously !!!!!\n");
#else
	printf("!!!!! The writing will be executed synchronously !!!!!\n");
#endif
	rt_set_linux_syscall_mode(SYNC_ASYNC, NULL);
	t = rt_get_cpu_time_ns();
	for (i = 0; i < NRECS; i++) {
		write(fd, s, BUFSIZE);
	}
	rt_set_linux_syscall_mode(SYNC_LINUX_SYSCALL, NULL);

	printf("(PRINTF) WRITE TIME: %lld (ms).\n", (rt_get_cpu_time_ns() - t + 500000)/1000000);
	printf("(WRITE) Write a %d bytes trailer of 'E's.\n", HDRSIZE);
	memset(s, 'E', HDRSIZE);
	write(fd, s, HDRSIZE);
	printf("(SYNC) Sync file to disk.\n");
	sync();
	printf("(LSEEK) Position file at its beginning.\n");
	lseek(fd, 0, SEEK_SET);
	printf("(POLL) Got the beginning, now we wait %d s using poll.\n", WAITIME);
	poll(0, 0, WAITIME*1000);
	printf("(READ) Poll expired, read the first %d bytes (header + first 'A').\n", HDRSIZE + 1);
	read(fd, s, HDRSIZE + 1);
	s[HDRSIZE + 1] = 0;
	printf("(READ) Here is the header  %s.\n", s);
	lseek(fd, -1, SEEK_CUR);
	printf("(READ) Read the written %d MB of 'A's back.\n", BUFSIZE*NRECS);

#if SYNC_ASYNC == ASYNC_LINUX_SYSCALL
	printf("!!!!! The reading will be executed asynchronously !!!!!\n");
#else
	printf("!!!!! The reading will be executed asynchronously !!!!!\n");
#endif
	rt_set_linux_syscall_mode(ASYNC_LINUX_SYSCALL, NULL);
	t = rt_get_cpu_time_ns();
	for (i = 0; i < NRECS; i++) {
		read(fd, s, BUFSIZE);
	}
	rt_set_linux_syscall_mode(SYNC_LINUX_SYSCALL, NULL);

	printf("(PRINTF) READ TIME %lld (ms).\n", (rt_get_cpu_time_ns() - t + 500000)/1000000);
	lseek(fd, -1, SEEK_CUR);
	printf("(READ) Read the last %d bytes (last 'A' + trailer).\n", HDRSIZE + 1);
	read(fd, s, HDRSIZE + 1);
	s[HDRSIZE + 1] = 0;
	printf("(READ) Here is the trailer %s.\n", s);
	printf("(CLOSE) Close the file and end the test.\n");
	close(fd);
	printf("(NANOWAITIME) File closed, let's wait %d s using nanosleep.\n", WAITIME);
	tns.tv_sec = WAITIME;
	tns.tv_nsec = 0;
        nanosleep(&tns, NULL);
	printf("(PRINTF) Test done, exiting.\n");
#endif

	rt_make_soft_real_time();
	rt_task_delete(mytask);
	return 0;
}
Ejemplo n.º 20
0
int main(int argc, char *argv[])
{
	int sock;
	struct sockaddr_in SPRT_ADDR;
	char buf[200];
	int fd;

	int diff;
	int sample;
	int average;
	int min_diff;
	int max_diff;
	int period;
	int i;
	int cnt;
	RTIME t, svt;
	RTIME expected, exectime[3];
	RT_TASK *task;
	long long max = -1000000000, min = 1000000000;
	struct sample { long long min; long long max; int index, ovrn; } samp;
	double s, sref;

 	if (!(task = rt_task_init_schmod(nam2num("LATCAL"), 0, 0, 0, SCHED_FIFO, 0xF))) {
		printf("CANNOT INIT MASTER LATENCY TASK\n");
		exit(1);
	}

	sock = socket(AF_INET, SOCK_DGRAM, 0);
	bzero(&SPRT_ADDR, sizeof(struct sockaddr_in));
	SPRT_ADDR.sin_family = AF_INET;
	SPRT_ADDR.sin_port = htons(5000);
	SPRT_ADDR.sin_addr.s_addr = inet_addr("127.0.0.1");

	fd = open("echo", O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);

	printf("\n## RTAI latency calibration tool ##\n");
	printf("# period = %i (ns) \n", PERIOD);
	printf("# average time = %i (s)\n", (int)AVRGTIME);
	printf("# use the FPU\n");
	printf("#%sstart the timer\n", argc == 1 ? " " : " do not ");
	printf("# timer_mode is %s\n", TIMER_MODE ? "periodic" : "oneshot");
	printf("\n");

	rt_sync_async_linux_syscall_server_create(NULL, ASYNC_LINUX_SYSCALL, async_callback, 120);

	if (!(hard_timer_running = rt_is_hard_timer_running())) {
		if (TIMER_MODE) {
			rt_set_periodic_mode();
		} else {
			rt_set_oneshot_mode();
		}
		period = start_rt_timer(nano2count(PERIOD));
	} else {
		period = nano2count(PERIOD);
	}

        for(i = 0; i < MAXDIM; i++) {
                a[i] = b[i] = 3.141592;
        }
	sref = dot(a, b, MAXDIM);
	s = 0.0;

	mlockall(MCL_CURRENT | MCL_FUTURE);

	rt_make_hard_real_time();
	expected = rt_get_time() + 200*period;
	rt_task_make_periodic(task, expected, period);
	svt = rt_get_cpu_time_ns();
	cnt = 0;
	i = 0;
	samp.ovrn = 0;
	while (!end) {
		min_diff = 1000000000;
		max_diff = -1000000000;
		average = 0;

		for (sample = 0; sample < SMPLSXAVRG && !end; sample++) {
			expected += period;
			if (!rt_task_wait_period()) {
				if (TIMER_MODE) {
					diff = (int) ((t = rt_get_cpu_time_ns()) - svt - PERIOD);
					svt = t;
				} else {
					diff = (int) count2nano(rt_get_time() - expected);
				}
			} else {
				samp.ovrn++;
				diff = 0;
				if (TIMER_MODE) {
					svt = rt_get_cpu_time_ns();
				}
			}

			if (diff < min_diff) {
				min_diff = diff;
			}
			if (diff > max_diff) {
				max_diff = diff;
			}
			average += diff;
			s = dot(a, b, MAXDIM);
			if (fabs(s/sref - 1.0) > 1.0e-16) {
				printf("\nDOT PRODUCT RESULT = %lf %lf %lf\n", s, sref, sref - s);
				return 0;
			}
		}
		samp.min = min_diff;
		samp.max = max_diff;
		samp.index = average/SMPLSXAVRG;
		if (max < samp.max) max = samp.max;
		if (min > samp.min) min = samp.min;
		++cnt;
//		printf("* %d - min: %lld/%lld, max: %lld/%lld average: %d <RET to stop> %d *\n", cnt, samp.min, min, samp.max, max, samp.index, samp.ovrn);
		sprintf(buf, "* %d - min: %lld/%lld, max: %lld/%lld average: %d <RET to stop> %d *\n", cnt, samp.min, min, samp.max, max, samp.index, samp.ovrn);
		i = write(STDOUT_FILENO, buf, strlen(buf));
		fdatasync(STDOUT_FILENO);
		i = write(fd, buf, strlen(buf));
		fsync(fd);
		sendto(sock, buf, strlen(buf), 0, (struct sockaddr *)&SPRT_ADDR, sizeof(struct sockaddr_in));
	}

	close(sock);
	close(fd);
	rt_make_soft_real_time();
	if (!hard_timer_running) {
		stop_rt_timer();
	}
	rt_get_exectime(task, exectime);
	if (exectime[1] && exectime[2]) {
		printf("\n>>> S = %g, EXECTIME = %G\n", s, (double)exectime[0]/(double)(exectime[2] - exectime[1]));
	}
	rt_task_delete(task);

	return 0;
}
Ejemplo n.º 21
0
void
fun(long thread)
{

	int diff = 0;
	int i;
	int average;
	int min_diff = 0;
	int max_diff = 0;
	RTIME t, svt;

	/* If we want to make overall statistics */
	/* we have to reset min/max here         */
	if (overall) {
		min_diff =  1000000000;
		max_diff = -1000000000;
	}
#ifdef CONFIG_RTAI_FPU_SUPPORT
	if (use_fpu) {
		for(i = 0; i < MAXDIM; i++) {
			a[i] = b[i] = 3.141592;
		}
	}
	refres = dot(a, b, MAXDIM);
#endif
	svt = rt_get_cpu_time_ns();
	while (1) {

		/* Not overall statistics: reset min/max */
		if (!overall) {
			min_diff =  1000000000;
			max_diff = -1000000000;
		}

		average = 0;
		for (i = 0; i < loops; i++) {
			cpu_used[hard_cpu_id()]++;
			expected += period_counts;
			rt_task_wait_period();

			if (timer_mode) {
				diff = (int) ((t = rt_get_cpu_time_ns()) - svt - period);
				svt = t;
			} else {
				diff = (int) count2nano(rt_get_time() - expected);
			}

			if (diff < min_diff) { min_diff = diff; }
			if (diff > max_diff) { max_diff = diff; }
			average += diff;
#ifdef CONFIG_RTAI_FPU_SUPPORT
			if (use_fpu) {
				dotres = dot(a, b, MAXDIM);
                        	if ((tdif = dotres/refres - 1.0) < 0.0) {
                        		tdif = -tdif;
				}
	                        if (tdif > 1.0e-16) {
      	                          	rt_printk("\nDOT PRODUCT ERROR\n");
                	                return;
                        	}
			}
#endif
		}
		samp.min = min_diff;
		samp.max = max_diff;
		samp.index = average / loops;
		rtf_put(DEBUG_FIFO, &samp, sizeof (samp));
//while(1);
	}
}
Ejemplo n.º 22
0
int main(void)
{
	RT_TASK *rtask;
	MBX *mbx;
	int maxisr = 0, maxsched = 0, ovrun = 0, semcnt;
	struct { int isr, sched; } latency;
	int timer_period, timer_freq, h_timer_freq;
	int count, perns, pervar, maxpervar = 0;
	RTIME tp, t;

	rt_allow_nonroot_hrt();
	pthread_create(&thread, NULL, endt, NULL);
	iopl(3);

	if (!(rtask = rt_task_init_schmod(nam2num("RTASK"), 0, 0, 0, SCHED_FIFO, ALLOWED_CPUS))) {
		printf("CANNOT CREATE REAL TIME TASK\n");
		return 1;
	}
	if (!(mbx = rt_named_mbx_init("LATMBX", 100*sizeof(latency)))) {
		printf("CANNOT CREATE MAILBOX\n");
		rt_task_delete(rtask);
		return 1;
	}

	rt_make_hard_real_time();
	timer_period = 1;
	timer_freq = 2;
	tmr_get_setup(timer, &timer_period, &timer_freq);
	h_timer_freq = timer_freq/2;
	perns = timer_period*1000000000LL/timer_freq;
 	tmr_start(timer);
#ifdef USE_EXT_WAIT
 	printf("Wait_on_timer returns timer count and cpu time.\n");
#else
 	printf("Wait_on_timer does just that.\n");
#endif
#ifdef DISABLE_INTR
 	printf("Wait_on_timer and times getting with interrupt disabled.\n");
#else
 	printf("Wait_on_timer and times getting with interrupt enabled.\n");
#endif
 	printf("Timer setup completed, running.\n\n");

	mlockall(MCL_CURRENT | MCL_FUTURE);
	rt_make_hard_real_time();

	while (wait_on_timer(timer) > 0);
	tp = rt_get_cpu_time_ns();
	while (!end) {
		CLI();
		CHECK_FLAGS();
#ifdef USE_EXT_WAIT
		if ((semcnt = wait_on_timer_ext(timer, &count, &t)) > 0) {
			rt_printk("OVRN: %d, TOT: %d\n", semcnt, ++ovrun);
		}
#else
		if ((semcnt = wait_on_timer(timer)) > 0) {
			rt_printk("OVRN: %d, TOT: %d\n", semcnt, ++ovrun);
		}
		count = tmr_get_count(timer, &t);
#endif
		CHECK_FLAGS();
		STI();
		if ((latency.sched = ((timer_period - count)*1000000 + h_timer_freq)/timer_freq) > maxsched) {
			maxsched = latency.sched;
		}
		if ((latency.isr = ((timer_period - tmr_get_isr_count(timer))*1000000 + h_timer_freq)/timer_freq) > maxisr) {
			maxisr = latency.isr;
		}
		pervar = abs((int)(t - tp));
		tp = t;
		if (pervar > maxpervar) {
			maxpervar = pervar;
		}
		rt_mbx_send_if(mbx, &latency, sizeof(latency));
//		rt_printk("MXI %d, MXS %d\n", maxisr, maxsched);
	}
 	tmr_stop(timer);

	rt_make_soft_real_time();
	rt_task_delete(rtask);
	rt_mbx_delete(mbx);
	printf("*** MAX LATENCIES: ISR %d (us), SCHED %d (us) ***\n", maxisr, maxsched);
	printf("*** MAX PERIOD VARIATION %d (us) ***\n", (maxpervar - perns + 500)/1000);
	return 0;
}
Ejemplo n.º 23
0
Archivo: sender.c Proyecto: ArcEye/RTAI
int main(void)
{
	RT_TASK *sending_task ;
	SEM *shmsem, *agentsem;
	int i, *shm, shm_size, count;
	void *heap;
	unsigned int chksum;

	sending_task = rt_task_init_schmod(nam2num("STSK"), 0, 0, 0, SCHED_FIFO, 0xF);
	heap = rt_heap_open(nam2num("HEAP"), 0, SUPRT);
	rt_global_heap_open();
	shm = rt_named_halloc(nam2num("MEM"), 0);
	mlockall(MCL_CURRENT | MCL_FUTURE);
	rt_make_hard_real_time();
	rt_halloc(9);
	rt_halloc(13);
	shm_size = shm[0];
printf("SHMSIZE %d\n", shm_size);
	shmsem   = rt_get_adr(nam2num("SHSM"));
	agentsem = rt_get_adr(nam2num("AGSM"));
	count = COUNT;
	rt_malloc(23);
	while(count--) {
		RTIME t;
		void *inloop1, *inloop2;
		t = rt_get_cpu_time_ns();
		inloop1 = rt_named_halloc(nam2num("piopio"), count);
		inloop2 = rt_malloc(count + 3);
		PRINTF("SENDER ALLOC TIME %d %d\n", (int)(rt_get_cpu_time_ns() - t), count + count + 3);
		PRINTF("SENDING TASK WAIT ON SHMSEM\n");
		rt_sem_wait(shmsem);
		PRINTF("SENDING TASK SIGNALLED ON SHMSEM\n");
			if (!(shm[0] = ((float)rand()/(float)RAND_MAX)*shm_size) || shm[0] > shm_size) {
				shm[0] = shm_size;
			}
			chksum = 0;
			for (i = 1; i <= shm[0]; i++) {
				shm[i] = rand();
				chksum += shm[i];
			}
			shm[shm[0] + 1] = chksum;
			PRINTF("STSK: %d CHECKSUM = %x\n", count, chksum);
		PRINTF("SENDING TASK SIGNAL AGENTSEM\n");
		rt_sem_signal(agentsem);
		t = rt_get_cpu_time_ns();
		rt_named_hfree(inloop1);
		rt_free(inloop2);
		PRINTF("SENDER FREE TIME %d\n", (int)(rt_get_cpu_time_ns() - t));
	}
	PRINTF("SENDING TASK LAST WAIT ON SHMSEM\n");
	rt_sem_wait(shmsem);
	PRINTF("SENDING TASK SIGNALLED ON SHMSEM\n");
	shm[0] = 0;
	PRINTF("SENDING TASK LAST SIGNAL TO AGENTSEM\n");
	rt_sem_signal(agentsem);
	rt_named_hfree(shm);
	rt_heap_close(nam2num("HEAP"), heap);
	rt_global_heap_close();
	printf("SENDING TASK DELETES ITSELF\n");
	rt_make_soft_real_time();
	rt_task_delete(sending_task);
	printf("END SENDING TASK %d\n", getpid());
	return 0;
}