-
Notifications
You must be signed in to change notification settings - Fork 1
/
streamtiming_stats.c
277 lines (225 loc) · 7.11 KB
/
streamtiming_stats.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/** @file streamtiming_stats.c
*/
#include <math.h>
#include <ncurses.h>
#include <sched.h>
#include "CommandLineInterface/CLIcore.h"
#include "COREMOD_memory/COREMOD_memory.h"
#include "COREMOD_tools/COREMOD_tools.h"
#include "timediff.h"
// ==========================================
// Forward declaration(s)
// ==========================================
errno_t info_image_streamtiming_stats_disp(
double *tdiffvarray,
long NBsamples,
double tdiffvmax,
long tdiffcntmax);
//
//
errno_t info_image_streamtiming_stats(
imageID ID, int sem, long NBsamplesmax, float samplestimeout, int buffinit)
{
IMAGE *image = &data.image[ID];
static int initflag = 0;
static double *tdiffvarray;
static double *tdiffvarray_sorted;
if(initflag == 0)
{
initflag = 1;
tdiffvarray = (double *) malloc(sizeof(double) * NBsamplesmax);
tdiffvarray_sorted = (double *) malloc(sizeof(double) * NBsamplesmax);
}
// collect timing data
//long cnt0 = image->md[0].cnt0;
struct timespec tstart;
struct timespec t0;
struct timespec t1;
struct timespec t_timeout;
struct timespec tdiff;
double tdiffv;
double tdiffstartv;
static double tdiffvmax = 0.0;
static long tdiffcntmax = 0;
int loopOK = 1;
static long framecnt = 0; // Buffer index for next frame
static long framecntbuff = 0; // Number of bufferized frames so far
//static long NBsamples = 0;
if(buffinit == 1)
{
framecnt = 0;
framecntbuff = 0;
tdiffvmax = 0.0;
tdiffcntmax = 0;
}
// warmup
for(long cnt = 0; cnt < SEMAPHORE_MAXVAL; cnt++)
{
sem_trywait(image->semptr[sem]);
}
clock_gettime(CLOCK_REALTIME, &tstart);
t0 = tstart;
t_timeout = t0;
t_timeout.tv_sec += 2;
sem_timedwait(image->semptr[sem], &t_timeout);
while(loopOK == 1)
{
//for (long framecnt = 0; framecnt < NBsamplesmax; framecnt++)
if(sem_timedwait(image->semptr[sem], &t_timeout))
{
return RETURN_FAILURE;
}
clock_gettime(CLOCK_REALTIME, &t1);
tdiff = info_time_diff(t0, t1);
tdiffv = 1.0 * tdiff.tv_sec + 1.0e-9 * tdiff.tv_nsec;
tdiffvarray[framecnt] = tdiffv;
t0 = t1;
t_timeout = t0;
t_timeout = t0;
t_timeout.tv_sec += 2;
if(tdiffv > tdiffvmax || framecnt == tdiffcntmax)
{
tdiffvmax = tdiffv;
tdiffcntmax = framecnt;
}
++framecnt;
++framecntbuff;
if(framecntbuff > NBsamplesmax)
{
framecntbuff = NBsamplesmax;
}
if(framecnt >= NBsamplesmax)
{
framecnt = 0;
}
tdiff = info_time_diff(tstart, t1);
tdiffstartv = 1.0 * tdiff.tv_sec + 1.0e-9 * tdiff.tv_nsec;
if(tdiffstartv > samplestimeout)
{
loopOK = 0;
}
}
memcpy(tdiffvarray_sorted, tdiffvarray,
NBsamplesmax * sizeof(*tdiffvarray_sorted));
info_image_streamtiming_stats_disp(tdiffvarray_sorted,
framecntbuff,
tdiffvmax,
tdiffcntmax);
return RETURN_SUCCESS;
}
errno_t info_image_streamtiming_stats_disp(
double *tdiffvarray,
long NBsamples,
double tdiffvmax,
long tdiffcntmax)
{
float RMSval = 0.0;
float AVEval = 0.0;
static int percMedianIndex;
static int initflag = 0;
static float *percarray;
static long *percNarray;
static int NBpercbin;
if(initflag == 0)
{
initflag = 1;
// printw("ALLOCATE arrays\n");
NBpercbin = 17;
percMedianIndex = 8;
percarray = (float *) malloc(sizeof(float) * NBpercbin);
percNarray = (long *) malloc(sizeof(long) * NBpercbin);
percarray[0] = 0.001;
percarray[1] = 0.01;
percarray[2] = 0.02;
percarray[3] = 0.05;
percarray[4] = 0.10;
percarray[5] = 0.20;
percarray[6] = 0.30;
percarray[7] = 0.40;
percarray[8] = 0.50;
percarray[9] = 0.60;
percarray[10] = 0.70;
percarray[11] = 0.80;
percarray[12] = 0.90;
percarray[13] = 0.95;
percarray[14] = 0.98;
percarray[15] = 0.99;
percarray[16] = 0.999;
}
for(int pc = 0; pc < NBpercbin; pc++)
{
percNarray[pc] = (long)(percarray[pc] * NBsamples);
}
// process timing data
quick_sort_double(tdiffvarray, NBsamples);
long i;
for(i = 0; i < NBsamples; i++)
{
AVEval += tdiffvarray[i];
RMSval += tdiffvarray[i] * tdiffvarray[i];
}
RMSval = RMSval / NBsamples;
AVEval /= NBsamples;
RMSval = sqrt(RMSval - AVEval * AVEval);
printw("\n NBsamples = %ld \n\n", NBsamples);
for(int percbin = 0; percbin < NBpercbin; percbin++)
{
if(percbin == percMedianIndex)
{
attron(A_BOLD);
printw(
"%2d/%2d %6.3f% \% %6.3f% \% [%6ld] [%6ld] %10.3f us\n",
percbin,
NBpercbin,
100.0 * percarray[percbin],
100.0 * (1.0 - percarray[percbin]),
percNarray[percbin],
NBsamples - percNarray[percbin],
1.0e6 * tdiffvarray[percNarray[percbin]]);
attroff(A_BOLD);
}
else
{
if(tdiffvarray[percNarray[percbin]] >
1.2 * tdiffvarray[percNarray[percMedianIndex]])
{
attron(A_BOLD | COLOR_PAIR(6));
}
if(tdiffvarray[percNarray[percbin]] >
1.5 * tdiffvarray[percNarray[percMedianIndex]])
{
attron(A_BOLD | COLOR_PAIR(5));
}
if(tdiffvarray[percNarray[percbin]] >
1.99 * tdiffvarray[percNarray[percMedianIndex]])
{
attron(A_BOLD | COLOR_PAIR(4));
}
printw(
"%2d/%2d %6.3f% \% %6.3f% \% [%6ld] [%6ld] %10.3f us "
" %+10.3f us\n",
percbin,
NBpercbin,
100.0 * percarray[percbin],
100.0 * (1.0 - percarray[percbin]),
percNarray[percbin],
NBsamples - percNarray[percbin],
1.0e6 * tdiffvarray[percNarray[percbin]],
1.0e6 * (tdiffvarray[percNarray[percbin]] -
tdiffvarray[percNarray[percMedianIndex]]));
}
}
attroff(A_BOLD | COLOR_PAIR(4));
printw("\n Average Time Interval = %10.3f us -> frequ = %10.3f Hz\n",
1.0e6 * AVEval,
1.0 / AVEval);
printw(" RMS = %10.3f us ( %5.3f \%)\n",
1.0e6 * RMSval,
100.0 * RMSval / AVEval);
printw(" Max delay : %10.3f us frame # %ld\n",
1.0e6 * tdiffvmax,
tdiffcntmax);
//free(percarray);
//free(percNarray);
return RETURN_SUCCESS;
}