changeset 42:24af9a14b203 ultra-staging

Included timer files
author Giulio Moro <giuliomoro@yahoo.it>
date Tue, 19 May 2015 16:42:38 +0100
parents 4255ecbb9bec
children f5b5c648cd5d
files core/intervals.cpp include/intervals.h
diffstat 2 files changed, 144 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/core/intervals.cpp	Tue May 19 16:42:38 2015 +0100
@@ -0,0 +1,94 @@
+/*
+ * intervals.h
+ *
+ *  Created on: 18 May 2015
+ *      Author: unmanaged
+ */
+
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <native/timer.h>
+#include <rtdk.h>
+
+#include "../include/intervals.h"
+void Interval::init(int aNumAverages, int aNumFrames, float aSamplingRate, const char *aName){
+	enabled=false;
+	numAverages=aNumAverages;
+	intervals=(RTIME *)malloc(sizeof(RTIME)*numAverages);
+	samplingRate=aSamplingRate;
+	numFrames=aNumFrames;
+	maxTimeus=0;
+	intervalsPointer=0;
+	sum=0;
+	startTime=0;
+
+	if(intervals!=0)
+		enabled=true;
+	int len=strlen(aName);
+	name=(char *)malloc(sizeof(char)*(len+1));
+	strcpy(name, aName);
+	if(name == 0)
+		enabled=false;
+};
+Interval::Interval(){
+	init(100,1,44100,"");
+}
+Interval::Interval(int aNumAverages){
+	init(aNumAverages,1,44100,"");
+}
+Interval::Interval(int aNumAverages, int aNumFrames, float aSamplingRate, const char *aName){
+	init(aNumAverages,aNumFrames,aSamplingRate,aName);
+}
+Interval::~Interval(){
+	free(intervals);
+//			free(name);
+}
+void Interval::setNumFrames(int aNumFrames){
+	numFrames=aNumFrames;
+};
+
+int Interval::start(){
+//	printf("start: intervals: 0x%x, intervalsPointer: %d, numAverages: %d\n", intervals,intervalsPointer,numAverages);
+	if(!enabled)
+		return 0;
+	startTime=rt_timer_read();
+	return 1;
+}
+int Interval::resetMax(){
+	maxTimeus=0;
+	return 1;
+}
+int Interval::split(){ //updates
+	if(!enabled)
+		return 0;
+	int currentInterval=rt_timer_read()-startTime;
+	RTIME *currentPointer=&(intervals[intervalsPointer]);
+	sum-=*currentPointer; //take out the oldest value from the sum
+	*currentPointer=currentInterval;
+	sum+=*currentPointer; //add the new value to the sum
+	timeus=((float)sum)/numAverages/1000.0;
+	maxTimeus=timeus>maxTimeus?timeus:maxTimeus;
+	intervalsPointer++;
+	if(intervalsPointer>=(numAverages-1)){
+		intervalsPointer=0;
+	}
+	return 1;
+}
+void Interval::setEnabled(bool aActive){
+	enabled=aActive;
+}
+float Interval::getTimeus(){
+	return timeus;
+}
+float Interval::getMaxTimeus(){
+	return maxTimeus;
+}
+void Interval::print(){
+	rt_printf(//"sleepTimer time: 29.484us, (1.30 samples, numFrames: 2, 65.01%%). MaxTime: 30.439us, (1.34 samples, 67.12%%)\n");
+"%s time: %.3fus, (%.2f samples, numFrames: %d, %.2f%%). MaxTime: %.3fus, (%.2f samples, %.2f%%)\n",
+			name,
+			timeus, timeus/1000000.0*samplingRate, numFrames, timeus/1000000.0*samplingRate/numFrames * 100,
+			maxTimeus, maxTimeus/1000000.0*samplingRate, maxTimeus/1000000.0*samplingRate/numFrames * 100);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/include/intervals.h	Tue May 19 16:42:38 2015 +0100
@@ -0,0 +1,50 @@
+/*
+ * intervals.h
+ *
+ *  Created on: 18 May 2015
+ *      Author: unmanaged
+ */
+
+#ifndef INTERVALS_H_
+#define INTERVALS_H_
+
+#define TEN_POW_9 1000000000
+
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <native/timer.h>
+#include <rtdk.h>
+
+class Interval
+{
+	private:
+		int intervalsPointer;
+		long sum;
+		RTIME startTime;
+		RTIME *intervals;
+		float maxTimeus;
+		float timeus;
+		float samplingRate; //used for getPrint()
+		int numFrames;
+		bool enabled; //whether it actually reads the clock or not
+		int numAverages;
+		char *name;
+		void init(int aNumAverages, int aNumFrames, float aSamplingRate, const char *aName);
+	public:
+		Interval();
+		Interval(int aNumAverages);
+		Interval(int aNumAverages, int aNumFrames, float aSamplingRate, const char *aName);
+		~Interval();
+		void setNumFrames(int aNumFrames);
+		int start();
+		int resetMax();
+		int split();
+		void setEnabled(bool aActive);
+		float getTimeus();
+		float getMaxTimeus();
+		void print();
+};
+
+#endif /* INTERVALS_H_ */