forked from cacao-org/AOloopControl_perfTest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AOloopControl_perfTest.c
2906 lines (2143 loc) · 89.1 KB
/
AOloopControl_perfTest.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file AOloopControl_perfTest.c
* @brief Adaptive Optics Control loop engine testing
*
* AO engine uses stream data structure
*
*
* ## Main files
*
* AOloopControl_perfTest_DM.c Test DM speed and examine DM modes
* AOloopControl_perfTest_status.c Report loop metrics, loop performance monitor
*
* @bug No known bugs.
*
*
*/
#define _GNU_SOURCE
// uncomment for test print statements to stdout
//#define _PRINT_TEST
/* =============================================================================================== */
/* =============================================================================================== */
/* HEADER FILES */
/* =============================================================================================== */
/* =============================================================================================== */
#include <string.h>
#include <math.h>
#include <pthread.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h> /* strrchr */
#include "CommandLineInterface/CLIcore.h"
#include "00CORE/00CORE.h"
#include "COREMOD_tools/COREMOD_tools.h"
#include "COREMOD_iofits/COREMOD_iofits.h"
#include "COREMOD_memory/COREMOD_memory.h"
#include "statistic/statistic.h"
#include "AOloopControl/AOloopControl.h"
#include "AOloopControl_perfTest/AOloopControl_perfTest.h"
/* =============================================================================================== */
/* =============================================================================================== */
/* DEFINES, MACROS */
/* =============================================================================================== */
/* =============================================================================================== */
# ifdef _OPENMP
# include <omp.h>
#define OMP_NELEMENT_LIMIT 1000000
# endif
#define MaxNBdatFiles 100000
/* =============================================================================================== */
/* =============================================================================================== */
/* GLOBAL DATA DECLARATION */
/* =============================================================================================== */
/* =============================================================================================== */
//extern long aoloopcontrol_var.aoconfID_wfsim; // declared in AOloopControl.c
//extern long aoloopcontrol_var.aoconfID_dmC; // declared in AOloopControl.c
//extern long aoloopcontrol_var.aoconfID_dmRM; // declared in AOloopControl.c
//extern long aoloopcontrol_var.aoconfID_DMmodes; // declared in AOloopControl.c
//extern long aoloopcontrol_var.aoconfID_gainb; // declared in AOloopControl.c
//extern long aoloopcontrol_var.aoconfID_limitb; // declared in AOloopControl.c
//extern long aoloopcontrol_var.aoconfID_multfb; // declared in AOloopControl.c
//extern long aoloopcontrol_var.aoconfID_DMmode_GAIN; // declared in AOloopControl.c
//extern long aoloopcontrol_var.aoconfID_LIMIT_modes; // declared in AOloopControl.c
//extern long aoloopcontrol_var.aoconfID_MULTF_modes; // declared in AOloopControl.c
//extern long aoloopcontrol_var.aoconfID_cmd_modes; // declared in AOloopControl.c
//extern long aoloopcontrol_var.aoconfID_meas_modes; // declared in AOloopControl.c
//extern long aoloopcontrol_var.aoconfID_RMS_modes; // declared in AOloopControl.c
//extern long aoloopcontrol_var.aoconfID_AVE_modes; // declared in AOloopControl.c
//extern long aoloopcontrol_var.aoconfID_modeARPFgainAuto; // declared in AOloopControl.c
static int wcol, wrow; // window size
// TIMING
static struct timespec tnow;
static struct timespec tdiff;
typedef struct {
char name[500];
double tstart;
double tend;
long cnt;
} StreamDataFile;
/* =============================================================================================== */
/* MAIN DATA STRUCTURES */
/* =============================================================================================== */
static int INITSTATUS_AOloopControl_perfTest = 0;
extern long LOOPNUMBER; // current loop index
extern AOLOOPCONTROL_CONF *AOconf; // declared in AOloopControl.c
extern AOloopControl_var aoloopcontrol_var; // declared in AOloopControl.c
// CLI commands
//
// function CLI_checkarg used to check arguments
// CLI_checkarg ( CLI argument index , type code )
//
// type codes:
// 1: float
// 2: long
// 3: string, not existing image
// 4: existing image
// 5: string
//
/* =============================================================================================== */
/* =============================================================================================== */
/** @name AOloopControl - 9. STATUS / TESTING / PERF MEASUREMENT */
/* =============================================================================================== */
/* =============================================================================================== */
/** @brief CLI function for AOcontrolLoop_TestDMSpeed */
int_fast8_t AOcontrolLoop_perfTest_TestDMSpeed_cli()
{
if(CLI_checkarg(1,4)+CLI_checkarg(2,2)+CLI_checkarg(3,2)+CLI_checkarg(4,1)==0) {
AOcontrolLoop_perfTest_TestDMSpeed( data.cmdargtoken[1].val.string, data.cmdargtoken[2].val.numl, data.cmdargtoken[3].val.numl, data.cmdargtoken[4].val.numf);
return 0;
}
else return 1;
}
/** @brief CLI function for AOcontrolLoop_TestSystemLatency */
int_fast8_t AOcontrolLoop_perfTest_TestSystemLatency_cli() {
if(CLI_checkarg(1,4)+CLI_checkarg(2,4)+CLI_checkarg(3,1)+CLI_checkarg(4,2)==0) {
AOcontrolLoop_perfTest_TestSystemLatency(data.cmdargtoken[1].val.string, data.cmdargtoken[2].val.string, data.cmdargtoken[3].val.numf, data.cmdargtoken[4].val.numl);
return 0;
}
else return 1;
}
/** @brief CLI function for AOloopControl_TestDMmodeResp */
int_fast8_t AOloopControl_perfTest_TestDMmodeResp_cli() {
if(CLI_checkarg(1,4)+CLI_checkarg(2,2)+CLI_checkarg(3,1)+CLI_checkarg(4,1)+CLI_checkarg(5,1)+CLI_checkarg(6,1)+CLI_checkarg(7,1)+CLI_checkarg(8,2)+CLI_checkarg(9,4)+CLI_checkarg(10,4)+CLI_checkarg(11,4)+CLI_checkarg(12,3)==0) {
AOloopControl_perfTest_TestDMmodeResp(data.cmdargtoken[1].val.string, data.cmdargtoken[2].val.numl, data.cmdargtoken[3].val.numf, data.cmdargtoken[4].val.numf, data.cmdargtoken[5].val.numf, data.cmdargtoken[6].val.numf, data.cmdargtoken[7].val.numf, data.cmdargtoken[8].val.numl, data.cmdargtoken[9].val.string, data.cmdargtoken[10].val.string, data.cmdargtoken[11].val.string, data.cmdargtoken[12].val.string);
return 0;
}
else return 1;
}
/** @brief CLI function for AOloopControl_TestDMmodes_Recovery */
int_fast8_t AOloopControl_perfTest_TestDMmodes_Recovery_cli() {
if(CLI_checkarg(1,4)+CLI_checkarg(2,1)+CLI_checkarg(3,4)+CLI_checkarg(4,4)+CLI_checkarg(5,4)+CLI_checkarg(6,4)+CLI_checkarg(7,1)+CLI_checkarg(8,2)+CLI_checkarg(9,3)+CLI_checkarg(10,3)+CLI_checkarg(11,3)+CLI_checkarg(12,3)==0) {
AOloopControl_perfTest_TestDMmodes_Recovery(data.cmdargtoken[1].val.string, data.cmdargtoken[2].val.numf, data.cmdargtoken[3].val.string, data.cmdargtoken[4].val.string, data.cmdargtoken[5].val.string, data.cmdargtoken[6].val.string, data.cmdargtoken[7].val.numf, data.cmdargtoken[8].val.numl, data.cmdargtoken[9].val.string, data.cmdargtoken[10].val.string, data.cmdargtoken[11].val.string, data.cmdargtoken[12].val.string);
return 0;
}
else return 1;
}
/** @brief CLI function for AOloopControl_blockstats */
int_fast8_t AOloopControl_perfTest_blockstats_cli() {
if(CLI_checkarg(1,2)+CLI_checkarg(2,5)==0) {
AOloopControl_perfTest_blockstats(data.cmdargtoken[1].val.numl, data.cmdargtoken[2].val.string);
return 0;
}
else return 1;
}
/** @brief CLI function for AOloopControl_InjectMode */
int_fast8_t AOloopControl_perfTest_InjectMode_cli() {
if(CLI_checkarg(1,2)+CLI_checkarg(2,1)==0) {
AOloopControl_perfTest_InjectMode(data.cmdargtoken[1].val.numl, data.cmdargtoken[2].val.numf);
return 0;
}
else return 1;
}
/** @brief CLI function for AOloopControl_loopMonitor */
int_fast8_t AOloopControl_perfTest_loopMonitor_cli() {
if(CLI_checkarg(1,1)+CLI_checkarg(2,2)==0) {
AOloopControl_perfTest_loopMonitor(LOOPNUMBER, data.cmdargtoken[1].val.numf, data.cmdargtoken[2].val.numl);
return 0;
} else {
AOloopControl_perfTest_loopMonitor(LOOPNUMBER, 1.0, 8);
return 0;
}
}
/** @brief CLI function for AOloopControl_statusStats */
int_fast8_t AOloopControl_perfTest_statusStats_cli() {
if(CLI_checkarg(1,2)+CLI_checkarg(2,2)==0) {
AOloopControl_perfTest_statusStats(data.cmdargtoken[1].val.numl, data.cmdargtoken[2].val.numl);
return 0;
}
else return 1;
}
/** @brief CLI function for AOloopControl_mkTestDynamicModeSeq */
int_fast8_t AOloopControl_perfTest_mkTestDynamicModeSeq_cli()
{
if(CLI_checkarg(1,3)+CLI_checkarg(2,2)+CLI_checkarg(3,2)+CLI_checkarg(4,2)==0) {
AOloopControl_perfTest_mkTestDynamicModeSeq(data.cmdargtoken[1].val.string, data.cmdargtoken[2].val.numl, data.cmdargtoken[3].val.numl, data.cmdargtoken[4].val.numl);
return 0;
}
else return 1;
}
/** @brief CLI function for AOloopControl_AnalyzeRM_sensitivity */
int_fast8_t AOloopControl_perfTest_AnalyzeRM_sensitivity_cli()
{
if(CLI_checkarg(1,4)+CLI_checkarg(2,4)+CLI_checkarg(3,4)+CLI_checkarg(4,4)+CLI_checkarg(4,4)+CLI_checkarg(5,4)+CLI_checkarg(6,1)+CLI_checkarg(7,1)+CLI_checkarg(8,3)==0) {
AOloopControl_perfTest_AnalyzeRM_sensitivity(data.cmdargtoken[1].val.string, data.cmdargtoken[2].val.string, data.cmdargtoken[3].val.string, data.cmdargtoken[4].val.string, data.cmdargtoken[5].val.string, data.cmdargtoken[6].val.numf, data.cmdargtoken[7].val.numf, data.cmdargtoken[8].val.string);
return 0;
}
else return 1;
}
int_fast8_t AOloopControl_LoopTimer_Analysis_cli()
{
if(CLI_checkarg(1,4)+CLI_checkarg(2,5)+CLI_checkarg(3,5)==0)
{
AOloopControl_LoopTimer_Analysis(data.cmdargtoken[1].val.string, data.cmdargtoken[2].val.string, data.cmdargtoken[3].val.string);
return 0;
}
else
return 1;
}
int_fast8_t AOloopControl_perfTest_mkSyncStreamFiles2_cli()
{
if(CLI_checkarg(1,5)+CLI_checkarg(2,5)+CLI_checkarg(3,5)+CLI_checkarg(4,1)+CLI_checkarg(5,1)+CLI_checkarg(6,1)+CLI_checkarg(7,1)==0)
{
AOloopControl_perfTest_mkSyncStreamFiles2(data.cmdargtoken[1].val.string, data.cmdargtoken[2].val.string, data.cmdargtoken[3].val.string, data.cmdargtoken[4].val.numf, data.cmdargtoken[5].val.numf, data.cmdargtoken[6].val.numf, data.cmdargtoken[7].val.numf);
return 0;
}
else
return 1;
}
int_fast8_t AOloopControl_perfTest_ComputeSimilarityMatrix_cli()
{
if(CLI_checkarg(1,4)+CLI_checkarg(2,5) == 0)
{
AOloopControl_perfTest_ComputeSimilarityMatrix(data.cmdargtoken[1].val.string, data.cmdargtoken[2].val.string);
}
else
return 1;
}
int_fast8_t AOloopControl_perfTest_StatAnalysis_2streams_cli()
{
if(CLI_checkarg(1,4)+CLI_checkarg(2,4)+CLI_checkarg(3,4)+CLI_checkarg(4,4)+CLI_checkarg(5,2)+CLI_checkarg(6,2) == 0)
{
AOloopControl_perfTest_StatAnalysis_2streams(data.cmdargtoken[1].val.string, data.cmdargtoken[2].val.string, data.cmdargtoken[3].val.string, data.cmdargtoken[4].val.string, data.cmdargtoken[5].val.numl, data.cmdargtoken[6].val.numl);
}
else
return 1;
}
int_fast8_t AOloopControl_perfTest_SelectWFSframes_from_PSFframes_cli()
{
if(CLI_checkarg(1,4)+CLI_checkarg(2,4)+CLI_checkarg(3,1)+CLI_checkarg(4,2)+CLI_checkarg(5,2)+CLI_checkarg(6,2)+CLI_checkarg(7,2)+CLI_checkarg(8,2)+CLI_checkarg(9,1) == 0)
{
AOloopControl_perfTest_SelectWFSframes_from_PSFframes(data.cmdargtoken[1].val.string, data.cmdargtoken[2].val.string, data.cmdargtoken[3].val.numf, data.cmdargtoken[4].val.numl, data.cmdargtoken[5].val.numl, data.cmdargtoken[6].val.numl, data.cmdargtoken[7].val.numl, data.cmdargtoken[8].val.numl, data.cmdargtoken[9].val.numf);
}
else
return 1;
}
/* =============================================================================================== */
/* =============================================================================================== */
/* FUNCTIONS SOURCE CODE */
/* =============================================================================================== */
/* =============================================================================================== */
/** @name AOloopControl functions */
/* =============================================================================================== */
/* =============================================================================================== */
/** @name AOloopControl - 1. INITIALIZATION, configurations */
/* =============================================================================================== */
/* =============================================================================================== */
void __attribute__ ((constructor)) libinit_AOloopControl_perfTest()
{
if ( INITSTATUS_AOloopControl_perfTest == 0 )
{
init_AOloopControl_perfTest();
RegisterModule(__FILE__, "cacao", "AO loop control performance monitoring and testing");
INITSTATUS_AOloopControl_perfTest = 1;
}
}
int_fast8_t init_AOloopControl_perfTest()
{
FILE *fp;
/* =============================================================================================== */
/* =============================================================================================== */
/** @name AOloopControl - 9. STATUS / TESTING / PERF MEASUREMENT */
/* =============================================================================================== */
/* =============================================================================================== */
RegisterCLIcommand("aoldmtestsp", __FILE__, AOcontrolLoop_perfTest_TestDMSpeed_cli, "test DM speed by sending circular tip-tilt", "<dmname> <delay us [long]> <NB pts> <ampl>", "aoldmtestsp dmdisp2 100 20 0.1", "long AOcontrolLoop_perfTest_TestDMSpeed(char *dmname, long delayus, long NBpts, float ampl)");
RegisterCLIcommand("aoltestlat", __FILE__, AOcontrolLoop_perfTest_TestSystemLatency_cli, "test system latency", "<dm stream> <wfs stream> <ampl [um]> <NBiter>", "aoltestlat dmC wfsim 0.1 5000", "long AOcontrolLoop_perfTest_TestSystemLatency(char *dmname, char *wfsname, float OPDamp, long NBiter)");
RegisterCLIcommand("aoltestmresp", __FILE__, AOloopControl_perfTest_TestDMmodeResp_cli, "Measure system response for a single mode", "<DM modes [3D im]> <mode #> <ampl [um]> <fmin [Hz]> <fmax [Hz]> <fstep> <meas. time [sec]> <time step [us]> <DM mask> <DM in [2D stream]> <DM out [2D stream]> <output [2D im]>", "aoltestmresp DMmodesC 5 0.05 10.0 100.0 1.2 1.0 1000 dmmask dmdisp3 dmC out", "long AOloopControl_perfTest_TestDMmodeResp(char *DMmodes_name, long index, float ampl, float fmin, float fmax, float fmultstep, float avetime, long dtus, char *DMmask_name, char *DMstream_in_name, char *DMstream_out_name, char *IDout_name)");
RegisterCLIcommand("aoltestdmrec", __FILE__, AOloopControl_perfTest_TestDMmodes_Recovery_cli, "Test system DM modes recovery", "<DM modes [3D im]> <ampl [um]> <DM mask [2D im]> <DM in [2D stream]> <DM out [2D stream]> <meas out [2D stream]> <lag time [us]> <NB averages [long]> <out ave [2D im]> <out rms [2D im]> <out meas ave [2D im]> <out meas rms [2D im]>", "aoltestdmrec DMmodesC 0.05 DMmask dmsisp2 dmoutr 2000 20 outave outrms outmave outmrms", "long AOloopControl_perfTest_TestDMmodes_Recovery(char *DMmodes_name, float ampl, char *DMmask_name, char *DMstream_in_name, char *DMstream_out_name, char *DMstream_meas_name, long tlagus, long NBave, char *IDout_name, char *IDoutrms_name, char *IDoutmeas_name, char *IDoutmeasrms_name)");
RegisterCLIcommand("aolresetrms", __FILE__, AOloopControl_perfTest_resetRMSperf, "reset RMS performance monitor", "no arg", "aolresetrms", "int AOloopControl_perfTest_resetRMSperf()");
RegisterCLIcommand("aolinjectmode",__FILE__, AOloopControl_perfTest_InjectMode_cli, "inject single mode error into RM channel", "<index> <ampl>", "aolinjectmode 20 0.1", "int AOloopControl_perfTest_InjectMode()");
RegisterCLIcommand("aolstatusstats", __FILE__, AOloopControl_perfTest_statusStats_cli, "measures distribution of status values", "<update flag [int]> <NBsample [long]>", "aolstatusstats 0 100000", "int AOloopControl_perfTest_statusStats(int updateconf, long NBsample)");
RegisterCLIcommand("aolmon", __FILE__, AOloopControl_perfTest_loopMonitor_cli, "monitor loop", "<frequ> <Nbcols>", "aolmon 10.0 3", "int AOloopControl_perfTest_loopMonitor(long loop, double frequ)");
RegisterCLIcommand("aolblockstats", __FILE__, AOloopControl_perfTest_blockstats_cli, "measures mode stats per block", "<loopnb> <outim>", "aolblockstats 2 outstats", "long AOloopControl_perfTest_blockstats(long loop, const char *IDout_name)");
RegisterCLIcommand("aolmktestmseq", __FILE__, AOloopControl_perfTest_mkTestDynamicModeSeq_cli, "make modal periodic test sequence", "<outname> <number of slices> <number of modes> <firstmode>", "aolmktestmseq outmc 100 50 0", "long AOloopControl_perfTest_mkTestDynamicModeSeq(const char *IDname_out, long NBpt, long NBmodes, long StartMode)");
RegisterCLIcommand("aolzrmsens", __FILE__, AOloopControl_perfTest_AnalyzeRM_sensitivity_cli, "Measure zonal RM sensitivity", "<DMmodes> <DMmask> <WFSref> <WFSresp> <WFSmask> <amplitude[nm]> <lambda[nm]> <outname>", "aolzrmsens DMmodes dmmask wfsref0 zrespmat wfsmask 0.1 outfile.txt", "long AOloopControl_perfTest_AnalyzeRM_sensitivity(const char *IDdmmodes_name, const char *IDdmmask_name, const char *IDwfsref_name, const char *IDwfsresp_name, const char *IDwfsmask_name, float amplimitnm, float lambdanm, const char *foutname)");
RegisterCLIcommand("aoltimingstat", __FILE__, AOloopControl_LoopTimer_Analysis_cli, "Analysis of loop timing data", "<TimingImage> <TimingTXTfile> <outFile>", "aoltimingstat aol0_looptiming timing.txt outfile.txt", "long AOloopControl_LoopTimer_Analysis(char *IDname, char *fnametxt, char *outfname)");
RegisterCLIcommand("aolptmksyncs2",
__FILE__,
AOloopControl_perfTest_mkSyncStreamFiles2_cli,
"synchronize two streams from disk telemetry",
"<datadir> <stream0name> <stream1name> <tstart> <tend> <dt> <dtlag>",
"aolptmksyncs2 \"/media/data/20180701/\" aol2_wfsim aol3_wfsim 1530410732.0 1530410733.0 0.001 0.00001",
"int AOloopControl_perfTest_mkSyncStreamFiles2(char *datadir, char *stream0, char *stream1, double tstart, double tend, double dt, double dtlag)");
RegisterCLIcommand("aolperfcompsimM",
__FILE__,
AOloopControl_perfTest_ComputeSimilarityMatrix_cli,
"compute similarity matrix from image sequence",
"<input cube> <output matrix>",
"aolperfcompsimM imc outM",
"int AOloopControl_perfTest_ComputeSimilarityMatrix(char *IDname, char *IDname_out)");
RegisterCLIcommand("aolperfsimpairs",
__FILE__,
AOloopControl_perfTest_StatAnalysis_2streams_cli,
"Find similarity pairs and perform statistical analysis",
"<input stream0 cube> <input stream1 cube> <input simM0> <input simM1> <min frame dt> <NBselected>",
"aolperfsimpairs imc0 imc1 simM0 simM1 20 1000",
"int AOloopControl_perfTest_StatAnalysis_2streams(char *IDname_stream0, char *IDname_stream1, char *IDname_simM0, char *IDname_simM1, long dtmin, long NBselected)");
RegisterCLIcommand("aolperfselwfsfpsf",
__FILE__,
AOloopControl_perfTest_SelectWFSframes_from_PSFframes_cli,
"select WFS frames from PSF frames (synchronized)",
"<input WFS cube> <input PSF cube> <fraction> <x0> <x1> <y0> <y1> <EvalMode> <alpha>",
"aolperfselwfsfpsf imwfsC impsfC 100 120 100 120 0 2.0",
"int AOloopControl_perfTest_SelectWFSframes_from_PSFframes(char *IDnameWFS, char *IDnamePSF, float frac, long x0, long x1, long y0, long y1, int EvalMode, float alpha)");
}
/**
* ## Purpose
*
* Measure hardware latency between DM and WFS streams
*
*
* ## Arguments
*
* @param[in]
* dmname char*
* DM actuation stream to which function sends pokes
*
* @param[in]
* wfsname char*
* - WFS image stream
*
* @param[in]
* OPDamp FLOAT
* Poke amplitude \[um\]
*
* @param[in]
* NBiter LONG
* Number of poke cycles
*
*/
int_fast8_t AOcontrolLoop_perfTest_TestSystemLatency(
const char *dmname,
char *wfsname,
float OPDamp,
long NBiter) {
long IDdm;
long dmxsize, dmysize;
long IDwfs;
long wfsxsize, wfsysize, wfssize;
// long twait0us = 100000;
double tdouble_start;
double tdouble_end;
long wfscntstart;
long wfscntend;
struct timespec tstart;
// struct timespec tnow;
struct timespec *tarray;
double tdouble;
double dtmax = 1.0; // Max running time per iteration
double dt, dt1;
double *dtarray;
double a, b;
char command[200];
long IDdm0, IDdm1; // DM shapes
long ii, jj;
float x, y;
long IDwfsc;
long wfs_NBframesmax = 50;
long wfsframe;
long twaitus = 30000; // initial wait [us]
double dtoffset0 = 0.002; // 2 ms
long wfsframeoffset = 10;
long IDwfsref;
char *ptr;
long kk, kkmax;
double *valarray;
double tmp;
double dtoffset;
long kkoffset;
long iter;
float *latencyarray;
float *latencysteparray;
float latencyave, latencystepave;
FILE *fp;
float minlatency, maxlatency;
double wfsdt;
uint8_t datatype;
uint32_t naxes[3];
// ===========================
// processinfo support
// ===========================
PROCESSINFO *processinfo;
char pinfoname[200];
sprintf(pinfoname, "mlat-%s-%s", dmname, wfsname);
char descrstring[200];
sprintf(descrstring, "lat %s %s", dmname, wfsname);
char msgstring[200];
sprintf(msgstring, "Measure Latency amp=%f %ld iter", OPDamp, NBiter);
processinfo = processinfo_setup(
pinfoname,
descrstring, // description
msgstring, // message on startup
__FUNCTION__, __FILE__, __LINE__
);
// OPTIONAL SETTINGS
processinfo->MeasureTiming = 1; // Measure timing
processinfo->RT_priority = 80; // RT_priority, 0-99. Larger number = higher priority. If <0, ignore
processinfo->loopcntMax = NBiter;
int loopOK = 1;
processinfo_WriteMessage(processinfo, "Allocating memory");
latencyarray = (float *) malloc(sizeof(float) * NBiter);
latencysteparray = (float *) malloc(sizeof(float) * NBiter);
IDdm = image_ID(dmname);
dmxsize = data.image[IDdm].md[0].size[0];
dmysize = data.image[IDdm].md[0].size[1];
IDdm0 = create_2Dimage_ID("_testdm0", dmxsize, dmysize);
IDdm1 = create_2Dimage_ID("_testdm1", dmxsize, dmysize);
float RMStot = 0.0;
for(ii = 0; ii < dmxsize; ii++)
for(jj = 0; jj < dmysize; jj++) {
x = (2.0 * ii - 1.0 * dmxsize) / dmxsize;
y = (2.0 * jj - 1.0 * dmxsize) / dmysize;
data.image[IDdm0].array.F[jj * dmxsize + ii] = 0.0;
data.image[IDdm1].array.F[jj * dmxsize + ii] = OPDamp * (sin(8.0 * x) * sin(8.0 * y));
RMStot += data.image[IDdm1].array.F[jj * dmxsize + ii] * data.image[IDdm1].array.F[jj * dmxsize + ii];
}
RMStot = sqrt(RMStot / dmxsize / dmysize);
for(ii = 0; ii < dmxsize; ii++)
for(jj = 0; jj < dmysize; jj++) {
data.image[IDdm1].array.F[jj * dmxsize + ii] *= OPDamp / RMStot;
}
if(system("mkdir -p tmp") != 0) {
printERROR(__FILE__, __func__, __LINE__, "system() returns non-zero value");
}
save_fits("_testdm0", "!tmp/_testdm0.fits");
save_fits("_testdm1", "!tmp/_testdm1.fits");
IDwfs = image_ID(wfsname);
sprintf(msgstring, "Connecting to stream %s %ld", wfsname, IDwfs);
processinfo_WriteMessage(processinfo, msgstring);
if(IDwfs == -1) {
sprintf(msgstring, "Cannot connect to stream %s", wfsname);
processinfo_error(processinfo, msgstring);
return RETURN_FAILURE;
}
wfsxsize = data.image[IDwfs].md[0].size[0];
wfsysize = data.image[IDwfs].md[0].size[1];
wfssize = wfsxsize * wfsysize;
datatype = data.image[IDwfs].md[0].datatype;
naxes[0] = wfsxsize;
naxes[1] = wfsysize;
naxes[2] = wfs_NBframesmax;
IDwfsc = create_image_ID("_testwfsc", 3, naxes, datatype, 0, 0);
float FrameRateWait = 5.0;
sprintf(msgstring, "Measuring frame rate over %.1f sec", FrameRateWait);
processinfo_WriteMessage(processinfo, msgstring);
// coarse estimate of frame rate
clock_gettime(CLOCK_REALTIME, &tnow);
tdouble_start = 1.0 * tnow.tv_sec + 1.0e-9 * tnow.tv_nsec;
wfscntstart = data.image[IDwfs].md[0].cnt0;
sleep(FrameRateWait);
clock_gettime(CLOCK_REALTIME, &tnow);
tdouble_end = 1.0 * tnow.tv_sec + 1.0e-9 * tnow.tv_nsec;
wfscntend = data.image[IDwfs].md[0].cnt0;
wfsdt = (tdouble_end - tdouble_start) / (wfscntend - wfscntstart);
printf("wfs dt = %f sec\n", wfsdt);
// update times
dtmax = wfsdt * wfs_NBframesmax * 1.2 + 0.5;
twaitus = 1000000.0 * wfsdt * 3.0;
dtoffset0 = 1.5 * wfsdt;
tarray = (struct timespec *) malloc(sizeof(struct timespec) * wfs_NBframesmax);
dtarray = (double *) malloc(sizeof(double) * wfs_NBframesmax);
if(system("mkdir -p timingstats") != 0) {
printERROR(__FILE__, __func__, __LINE__, "system() returns non-zero value");
}
if((fp = fopen("timingstats/hardwlatency.txt", "w")) == NULL) {
printf("ERROR: cannot create file \"timingstats/hardwlatency.txt\"\\n");
exit(0);
}
clock_gettime(CLOCK_REALTIME, &tnow);
tdouble_start = 1.0 * tnow.tv_sec + 1.0e-9 * tnow.tv_nsec;
wfscntstart = data.image[IDwfs].md[0].cnt0;
wfsframeoffset = (long)(0.3 * wfs_NBframesmax);
printf("WFS size : %ld %ld\n", wfsxsize, wfsysize);
if(datatype == _DATATYPE_FLOAT) {
printf("data type : _DATATYPE_FLOAT\n");
}
if(datatype == _DATATYPE_UINT16) {
printf("data type : _DATATYPE_UINT16\n");
}
if(datatype == _DATATYPE_INT16) {
printf("data type : _DATATYPE_INT16\n");
}
list_image_ID();
// ===========================
// Start loop
// ===========================
processinfo_loopstart(processinfo); // Notify processinfo that we are entering loop
processinfo_WriteMessage(processinfo, "Starting loop");
iter = 0;
while(loopOK == 1) {
//double tlastdouble;
double tstartdouble;
long NBwfsframe;
unsigned long wfscnt0;
double latencymax = 0.0;
double latency;
loopOK = processinfo_loopstep(processinfo);
sprintf(msgstring, "iteration %5ld / %5ld", iter, NBiter);
processinfo_WriteMessage(processinfo, msgstring);
processinfo_exec_start(processinfo);
printf(" - ITERATION %5ld / %5ld\n", iter, NBiter);
fflush(stdout);
for(ii = 0; ii < 10; ii++) {
printf(" %5ld -> %f\n", ii, (float) data.image[IDwfs].array.SI16[ii]);
}
printf("write to %s\n", dmname);
fflush(stdout);
copy_image_ID("_testdm0", dmname, 1);
unsigned int dmstate = 0;
// waiting time
usleep(twaitus);
// and waiting frames
wfscnt0 = data.image[IDwfs].md[0].cnt0;
for(wfsframe = 0; wfsframe < wfs_NBframesmax; wfsframe++) {
while(wfscnt0 == data.image[IDwfs].md[0].cnt0) {
usleep(50);
}
wfscnt0 = data.image[IDwfs].md[0].cnt0;
}
dt = 0.0;
clock_gettime(CLOCK_REALTIME, &tstart);
tstartdouble = 1.0 * tstart.tv_sec + 1.0e-9 * tstart.tv_nsec;
// tlastdouble = tstartdouble;
wfsframe = 0;
wfscnt0 = data.image[IDwfs].md[0].cnt0;
printf("\n");
while((dt < dtmax) && (wfsframe < wfs_NBframesmax)) {
// WAITING for image
while(wfscnt0 == data.image[IDwfs].md[0].cnt0) {
usleep(2);
}
wfscnt0 = data.image[IDwfs].md[0].cnt0;
printf("[%8ld / %8ld] %f %f\n", wfsframe, wfs_NBframesmax, dt, dtmax);
fflush(stdout);
if(datatype == _DATATYPE_FLOAT) {
// copy image to cube slice
ptr = (char *) data.image[IDwfsc].array.F;
ptr += sizeof(float) * wfsframe * wfssize;
memcpy(ptr, data.image[IDwfs].array.F, sizeof(float)*wfssize);
}
if(datatype == _DATATYPE_UINT16) {
// copy image to cube slice
ptr = (char *) data.image[IDwfsc].array.UI16;
ptr += sizeof(short) * wfsframe * wfssize;
memcpy(ptr, data.image[IDwfs].array.UI16, sizeof(short)*wfssize);
}
if(datatype == _DATATYPE_INT16) {
// copy image to cube slice
ptr = (char *) data.image[IDwfsc].array.SI16;
ptr += sizeof(short) * wfsframe * wfssize;
memcpy(ptr, data.image[IDwfs].array.SI16, sizeof(short)*wfssize);
}
clock_gettime(CLOCK_REALTIME, &tarray[wfsframe]);
tdouble = 1.0 * tarray[wfsframe].tv_sec + 1.0e-9 * tarray[wfsframe].tv_nsec;
dt = tdouble - tstartdouble;
// dt1 = tdouble - tlastdouble;
dtarray[wfsframe] = dt;
// tlastdouble = tdouble;
// apply DM pattern #1
if((dmstate == 0) && (dt > dtoffset0) && (wfsframe > wfsframeoffset)) {
usleep((long)(ran1() * 1000000.0 * wfsdt));
printf("\nDM STATE CHANGED ON ITERATION %ld\n\n", wfsframe);
kkoffset = wfsframe;
dmstate = 1;
copy_image_ID("_testdm1", dmname, 1);
clock_gettime(CLOCK_REALTIME, &tnow);
tdouble = 1.0 * tnow.tv_sec + 1.0e-9 * tnow.tv_nsec;
dt = tdouble - tstartdouble;
dtoffset = dt; // time at which DM command is sent
}
wfsframe++;
}
printf("\n\n %ld frames recorded\n", wfsframe);
fflush(stdout);
copy_image_ID("_testdm0", dmname, 1);
dmstate = 0;
// Computing difference between consecutive images
NBwfsframe = wfsframe;
valarray = (double *) malloc(sizeof(double) * NBwfsframe);
double valmax = 0.0;
double valmaxdt = 0.0;
for(kk = 1; kk < NBwfsframe; kk++) {
valarray[kk] = 0.0;
if(datatype == _DATATYPE_FLOAT)
for(ii = 0; ii < wfssize; ii++) {
tmp = data.image[IDwfsc].array.F[kk * wfssize + ii] - data.image[IDwfsc].array.F[(kk - 1) * wfssize + ii];
valarray[kk] += tmp * tmp;
}
if(datatype == _DATATYPE_UINT16)
for(ii = 0; ii < wfssize; ii++) {
tmp = data.image[IDwfsc].array.UI16[kk * wfssize + ii] - data.image[IDwfsc].array.UI16[(kk - 1) * wfssize + ii];
valarray[kk] += 1.0 * tmp * tmp;
}
if(datatype == _DATATYPE_INT16)
for(ii = 0; ii < wfssize; ii++) {
tmp = 0.0;
tmp = data.image[IDwfsc].array.SI16[kk * wfssize + ii] - data.image[IDwfsc].array.SI16[(kk - 1) * wfssize + ii];
valarray[kk] += 1.0 * tmp * tmp;
}
valarray[kk] = sqrt(valarray[kk] / wfssize / 2);
if(valarray[kk] > valmax) {
valmax = valarray[kk];
valmaxdt = 0.5 * (dtarray[kk - 1] + dtarray[kk]);
kkmax = kk - kkoffset;
}
}
//
//
//
for(wfsframe = 1; wfsframe < NBwfsframe; wfsframe++) {
fprintf(fp, "%ld %10.2f %g\n", wfsframe - kkoffset, 1.0e6 * (0.5 * (dtarray[wfsframe] + dtarray[wfsframe - 1]) - dtoffset), valarray[wfsframe]);
}
printf("mean interval = %10.2f ns\n", 1.0e9 * (dt - dtoffset) / NBwfsframe);
fflush(stdout);
free(valarray);
latency = valmaxdt - dtoffset;
// latencystep = kkmax;
printf("... Hardware latency = %f ms = %ld frames\n", 1000.0 * latency, kkmax);
if(latency > latencymax) {
latencymax = latency;
save_fl_fits("_testwfsc", "!./timingstats/maxlatencyseq.fits");
}
fprintf(fp, "# %5ld %8.6f\n", iter, (valmaxdt - dtoffset));
latencysteparray[iter] = 1.0 * kkmax;
latencyarray[iter] = (valmaxdt - dtoffset);
// process signals, increment loop counter
processinfo_exec_end(processinfo);
iter++;
}
fclose(fp);
clock_gettime(CLOCK_REALTIME, &tnow);
tdouble_end = 1.0 * tnow.tv_sec + 1.0e-9 * tnow.tv_nsec;
wfscntend = data.image[IDwfs].md[0].cnt0;
free(dtarray);
free(tarray);
sprintf(msgstring, "Processing Data (%ld iterations)", NBiter);
processinfo_WriteMessage(processinfo, msgstring);
latencyave = 0.0;
latencystepave = 0.0;
minlatency = latencyarray[0];
maxlatency = latencyarray[0];
for(iter = 0; iter < NBiter; iter++) {
if(latencyarray[iter] > maxlatency) {
maxlatency = latencyarray[iter];
}
if(latencyarray[iter] < minlatency) {
minlatency = latencyarray[iter];
}
latencyave += latencyarray[iter];
latencystepave += latencysteparray[iter];
}
latencyave /= NBiter;
latencystepave /= NBiter;
//save__fl_fits("_testwfsc", "!./timingstats/maxlatencyseq.fits");
quick_sort_float(latencyarray, NBiter);
printf("AVERAGE LATENCY = %8.3f ms %f frames\n", latencyave * 1000.0, latencystepave);
printf("min / max over %ld measurements: %8.3f ms / %8.3f ms\n", NBiter, minlatency * 1000.0, maxlatency * 1000.0);
if(sprintf(command, "echo %8.6f > conf/param_hardwlatency.txt", latencyarray[NBiter / 2]) < 1) {
printERROR(__FILE__, __func__, __LINE__, "sprintf wrote <1 char");
}
if(system(command) != 0) {
printERROR(__FILE__, __func__, __LINE__, "system() returns non-zero value");
}
if(sprintf(command, "echo %f %f %f %f %f > timingstats/hardwlatencyStats.txt", latencyarray[NBiter / 2], latencyave, minlatency, maxlatency, latencystepave) < 1) {
printERROR(__FILE__, __func__, __LINE__, "sprintf wrote <1 char");
}
if(system(command) != 0) {
printERROR(__FILE__, __func__, __LINE__, "system() returns non-zero value");
}
dt = tdouble_end - tdouble_start;
printf("FRAME RATE = %.3f Hz\n", 1.0 * (wfscntend - wfscntstart) / dt);
if(sprintf(command, "echo %.3f > conf/param_mloopfrequ.txt", 1.0 * (wfscntend - wfscntstart) / dt) < 1) {
printERROR(__FILE__, __func__, __LINE__, "sprintf wrote <1 char");
}
if(system(command) != 0) {
printERROR(__FILE__, __func__, __LINE__, "system() returns non-zero value");
}
free(latencyarray);
free(latencysteparray);
sprintf(msgstring, "Measured %8.3f ms @ %.3f Hz", latencyave * 1000.0, 1.0 * (wfscntend - wfscntstart));
processinfo_WriteMessage(processinfo, msgstring);
// ==================================
// ENDING LOOP
// ==================================
processinfo_cleanExit(processinfo);
return 0;
}
// waits on semaphore 3
long AOloopControl_perfTest_blockstats(long loop, const char *IDout_name)
{
long IDout;
uint32_t *sizeout;
long NBmodes;
char fname[200];
long IDmodeval;
long m, blk, i;
long cnt;
long IDblockRMS, IDblockRMS_ave;
long NBblock;
float *rmsarray;
int *indexarray;
float alpha = 0.0001;
if(sprintf(fname, "aol%ld_modeval", loop) < 1)
printERROR(__FILE__, __func__, __LINE__, "sprintf wrote <1 char");
IDmodeval = read_sharedmem_image(fname);
NBmodes = data.image[IDmodeval].md[0].size[0];
sizeout = (uint32_t*) malloc(sizeof(uint32_t)*2);
sizeout[0] = NBmodes;
sizeout[1] = 1;
IDout = create_image_ID(IDout_name, 2, sizeout, _DATATYPE_FLOAT, 1, 0);
COREMOD_MEMORY_image_set_createsem(IDout_name, 10);
printf("%ld modes\n", NBmodes);
m = 0;
blk = 0;
while(m<NBmodes)
{
long ID;
long n;
if(sprintf(fname, "aol%ld_DMmodes%02ld", loop, blk) < 1)
printERROR(__FILE__, __func__, __LINE__, "sprintf wrote <1 char");
ID = read_sharedmem_image(fname);
n = data.image[ID].md[0].size[2];
for(i=0; i<n; i++)
{
data.image[IDout].array.F[m] = blk;
m++;
}
blk++;
}
NBblock = blk;
rmsarray = (float*) malloc(sizeof(float)*NBblock);
indexarray = (int*) malloc(sizeof(int)*NBmodes);
for(m=0; m<NBmodes; m++)
indexarray[m] = (int) (0.1 + data.image[IDout].array.F[m]);
printf("NBblock = %ld\n", NBblock);
sizeout[0] = NBblock;
sizeout[1] = 1;
if(sprintf(fname, "aol%ld_blockRMS", loop) < 1)
printERROR(__FILE__, __func__, __LINE__, "sprintf wrote <1 char");
IDblockRMS = create_image_ID(fname, 2, sizeout, _DATATYPE_FLOAT, 1, 0);