forked from petermilne/ACQ420FMC
-
Notifications
You must be signed in to change notification settings - Fork 3
/
acq400_stream.cpp
5133 lines (4463 loc) · 130 KB
/
acq400_stream.cpp
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
/* ------------------------------------------------------------------------- */
/* acq400_stream.cpp */
/* ------------------------------------------------------------------------- */
/* Copyright (C) 2012 pgm, D-TACQ Solutions Ltd *
* <peter dot milne at D hyphen TACQ dot com> *
* Created on: Mar 31, 2013 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of Version 2 of the GNU General Public License *
* as published by the Free Software Foundation; *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
/* ------------------------------------------------------------------------- */
/*
* acq400_stream.cpp
*
* Created on: Mar 31, 2013
* Author: pgm
*
* - Usage
* - No args: stream and copy to stdout eg the 4210 service
* - --null-copy : no copy
* - --pre/--post : pre/post transient BOS - data left in KBUFS
* -- --pre=0 : easy, first N KBUFS
* -- --pre!=0 : need to find the cursor.
* - --demux : controls post shot demux
* - --oversampling : controls sub-rate streaming (EPICS feed).
*
*
* Dropped: copy/demux (SOS mode).
*
* Demux: has to be done in-situ in the kbufs.
* Then allow an overlay VFS to extract the data.
* Demux --pre==0 : demux in situ starting at ibuf=0
* Demux --pre!-0 : demux and write back to ibuf=0
* Easy to read from linear mapped buffers. Copy to local, DMA back
* Driver provides a pair of temp buffers?. Allow ping/pong copy back.
*
* All the kbufs are mapped to a linear thing: this makes it easy for app-code.
* Less easy for kernel code ..
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/sendfile.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include "popt.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include <vector>
#include <libgen.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include <errno.h>
#include <cstdarg>
#include <sched.h>
//#define BUFFER_IDENT 6
#define VERID "B1051"
#define NCHAN 4
#include <semaphore.h>
#include <syslog.h>
#include <sys/statvfs.h>
#include "local.h" /* chomp() hopefully, not a lot of other garbage */
#include "knobs.h"
#include "acq-util.h"
#include "ES.h"
#include "Knob.h"
#include <bitset>
#include "hex_char_to_bin.h"
#define _PFN __PRETTY_FUNCTION__
#define STDOUT 1
using namespace std;
int timespec_subtract (timespec *result, timespec *x, timespec *y) {
const int nsps = 1000000000;
/* Perform the carry for the later subtraction by updating y. */
if (x->tv_nsec < y->tv_nsec) {
int nsec = (y->tv_nsec - x->tv_nsec) / 1000000 + 1;
y->tv_nsec -= nsps * nsec;
y->tv_sec += nsec;
}
if (x->tv_nsec - y->tv_nsec > nsps) {
int nsec = (x->tv_nsec - y->tv_nsec) / nsps;
y->tv_nsec += nsps * nsec;
y->tv_sec -= nsec;
}
/* Compute the time remaining to wait. tv_nsec is certainly positive. */
if (result){
result->tv_sec = x->tv_sec - y->tv_sec;
result->tv_nsec = x->tv_nsec - y->tv_nsec;
}
/* Return 1 if result is negative. */
return x->tv_sec < y->tv_sec;
}
#define BM_NOT_SPECIFIED '\0'
#define BM_NULL 'n'
#define BM_RAW 'r' /* no demux */
#define BM_DEMUX 'h'
#define BM_TEST 't'
#define BM_PREPOST 'P'
#define SM_STREAM 'S'
#define SM_TRANSIENT 'T'
/* all globals in one namespace : G */
namespace G {
unsigned nchan = NCHAN;
unsigned nchan_ES;
unsigned wordsize = 2; /** choice sizeof(short) or sizeof(int) */
#define FULL_MASK 0xffffffff
unsigned mask = FULL_MASK; /** mask data with this value */
unsigned m1 = 0; /** start masking from here */
int oversampling = 0;
int devnum = 0;
const char* script_runs_on_completion = 0;
const char* aggregator_sites = 0;
unsigned nsam = 4096;
int control_handle;
int pre;
int post;
int demux;
int buffer_mode = BM_NOT_SPECIFIED;
int stream_mode = SM_STREAM;
bool soft_trigger;
sem_t* aggsem;
char* aggsem_name;
char* state_file;
bool show_es;
FILE* state_fp;
char* pre_demux_script;
int show_first_sample;
int es_diagnostic = getenv_default("ES_DIAGNOSTIC");
int report_es;
int nsites;
int the_sites[6];
char* progress;
int corner_turn_delay; // test pre/post caps on corner turn, buffers
int exit_on_trigger_fail;
char* subset;
char* sum;
bool null_copy;
bool fillk; // fill output scaled by 1k
int is_spy; // spy task runs independently of main capture
int show_events;
bool double_up; // channel data appears 2 in a row (ACQ480/4)
unsigned naxi; // 0: none, 1=1 channel, 2= 2channels
int stream_sob_sig; // insert start of buffer signature
unsigned find_event_mask = 0x1; // 1: EV0, 2:EV1, 3:EV0|EV1
char* multi_event; // multi-event definintion [pre,]post
int live_instance = 1; // clear if live_instance NOT required
};
int verbose = getenv_default("VERBOSE");
unsigned nb_cat =1; /* number of buffers to concatenate */
static unsigned get_nchan_ES(unsigned nchan);
unsigned sample_size() {
return G::nchan * G::wordsize;
}
unsigned es_size() {
return G::nchan_ES * G::wordsize;
}
unsigned s2b(unsigned samples) {
return samples*sample_size();
}
unsigned b2s(unsigned bytes) {
return bytes/sample_size();
}
static int createOutfile(const char* fname) {
int fd = open(fname,
O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU|S_IRGRP|S_IROTH);
if (fd < 0){
perror(fname);
exit(1);
}
return fd;
}
#include "Buffer.h"
#define FMT_OUT_ROOT "/dev/shm/AI.%d.wf"
#define FMT_OUT_ROOT_NEW "/dev/shm/AI.%d.new"
#define FMT_OUT_ROOT_OLD "/dev/shm/AI.%d.old"
enum DemuxBufferType {
DB_REGULAR,
DB_2D_REGULAR, // 2DMA, single sample
DB_DOUBLE, // double sample: acq480-4
DB_2D_DOUBLE // double sample, double AXI, acq480x80
};
class DemuxBufferCommon: public Buffer {
protected:
const unsigned nchan;
const unsigned nchan_es;
AbstractES& evX;
public:
DemuxBufferCommon(Buffer* cpy): Buffer(cpy),
nchan(G::nchan), nchan_es(G::nchan_ES),
evX(*AbstractES::evX_instance())
{}
static int demux_size;
static int verbose;
static int show_es;
};
unsigned get_nchan_ES(unsigned nchan)
/* get number of channels in ES. We assume same module in each agg site
* duplicates logic epics load.records load_rgm()
*/
{
unsigned nchan_es = nchan;
if (!ISACQ480()){
FILE* fp = fopen("/var/log/run0.log", "r");
if (fp){
int agg_site;
if (fscanf(fp, "%*s %d", &agg_site) == 1){
unsigned active_chan;
unsigned data32;
if (Knob(agg_site, "active_chan").get(&active_chan) == 1 &&
Knob(agg_site, "data32").get(&data32) == 1){
unsigned nlongs = active_chan/(data32? 1: 2);
unsigned esp = nlongs >= 8? 1: nlongs >= 4? 2: 4;
nchan_es *= esp;
}
}
fclose(fp);
}
}
syslog(LOG_DEBUG, "%d %s nchan:%u nchan_es:%u\n", getpid(), __FUNCTION__,
nchan, nchan_es);
return nchan_es;
}
int DemuxBufferCommon::demux_size = ::getenv_default("DemuxBufferSize");
int DemuxBufferCommon::verbose = ::getenv_default("DemuxBufferVerbose");
int DemuxBufferCommon::show_es = ::getenv_default("DemuxBufferShowES");
template <class T, DemuxBufferType N>
class DemuxBuffer: public DemuxBufferCommon {
private:
unsigned* mask;
unsigned nsam;
static T** dddata;
static T** ddcursors;
bool data_fits_buffer;
char** new_names;
char** old_names;
char OUT_ROOT[128];
char OUT_ROOT_NEW[128];
char OUT_ROOT_OLD[128];
char CLEAN_COMMAND[160];
char FIN_FILE[160];
static u32 ID_MASK;
static int startchan;
int lchan(int ichan){
return ichan+1;
}
static void init() {
if (ID_MASK) return;
if (getenv("NOSID")){
ID_MASK = 0x1f;
fprintf(stderr, "ID_MASK set %02x\n", ID_MASK);
}else{
ID_MASK = 0xff;
}
}
void _make_names(const char* root, char** names){
char buf[160];
for (unsigned ic = 0; ic < nchan; ++ic){
int len = sprintf(buf, "%s/CH%02d", root, lchan(ic));
names[ic] = new char[len+1];
strcpy(names[ic], buf);
}
}
void make_names() {
sprintf(OUT_ROOT, FMT_OUT_ROOT, G::devnum);
sprintf(OUT_ROOT_NEW, FMT_OUT_ROOT_NEW, G::devnum);
sprintf(OUT_ROOT_OLD, FMT_OUT_ROOT_OLD, G::devnum);
sprintf(FIN_FILE, "%s.fin", OUT_ROOT);
_make_names(OUT_ROOT_NEW, new_names = new char* [nchan]);
_make_names(OUT_ROOT_OLD, old_names = new char* [nchan]);
}
void start() {
mkdir(OUT_ROOT_NEW, 0777);
for (unsigned ic = 0; ic < nchan; ++ic){
ddcursors[ic] = dddata[ic];
}
startchan = 0;
}
void clean_old() {
for (unsigned ic = 0; ic < nchan; ++ic){
unlink(old_names[ic]);
}
rmdir(OUT_ROOT_OLD);
}
void fin_cmd() {
FILE *fp = fopen(FIN_FILE, "w");
time_t now;
time(&now);
fprintf(fp, "%s\n", ctime(&now));
fclose(fp);
}
void finish() {
clean_old();
rename(OUT_ROOT, OUT_ROOT_OLD);
rename(OUT_ROOT_NEW, OUT_ROOT);
fin_cmd();
if (G::script_runs_on_completion != 0){
system(G::script_runs_on_completion);
}
}
static unsigned ch_id(T data)
{
return data&ID_MASK;
}
void dump(T* src, int nwords){
char cmd[80];
sprintf(cmd, "hexdump -ve '%d/%d \"%%08x \" \"\\n\" '",
nchan, sizeof(T));
FILE *pp = popen(cmd, "w");
fwrite(src, sizeof(T), nwords, pp);
pclose(pp);
}
/* NB: this is an out-of-place demux, source is unchanged */
bool demux(bool start, int start_off, int len);
bool writeChan(int ic){
FILE* fp = fopen(new_names[ic], "w");
if (fp ==0){
perror(new_names[ic]);
return true;
}
int nelems = ddcursors[ic]-dddata[ic];
int nwrite = fwrite(dddata[ic], sizeof(T), nelems, fp);
fclose(fp);
if (nwrite != nelems || (verbose&&ic==0) || verbose>1){
fprintf(stderr, "DemuxBuffer::writeChan(%s) ic:%d %d %d %s\n",
new_names[ic], ic, nwrite, nelems, nwrite!=nelems? "ERROR":"");
}
return false;
}
public:
virtual int writeBuffer(int out_fd, int b_opts) {
int demux_len = demux_size? demux_size: buffer_len;
if ((b_opts&BO_START) != 0){
start();
}
demux((b_opts&BO_START),0, demux_len);
if ((b_opts&BO_FINISH) != 0){
for (unsigned ic = 0; ic < nchan; ++ic){
if (writeChan(ic)){
// links take out from under
return -1;
}
}
finish();
}
return demux_len;
}
virtual int writeBuffer(int out_fd, int b_opts, int start_off, int len)
{
if ((b_opts&BO_START) != 0){
start();
}
if (verbose) fprintf(stderr, "DemuxBuffer::writeBuffer %s%s %p + 0x%08x + %d %p..%p\n",
(b_opts&BO_START)? "S":"", (b_opts&BO_FINISH)? "F":"",
pdata, start_off, len, pdata+start_off, pdata+start_off+len);
demux((b_opts&BO_START), start_off, len);
if ((b_opts&BO_FINISH) != 0){
for (unsigned ic = 0; ic < nchan; ++ic){
if (writeChan(ic)){
if (verbose) fprintf(stderr, "writeChan() fail\n");
// links take out from under
return -1;
}
}
finish();
}
return buffer_len;
}
DemuxBuffer(Buffer* cpy, unsigned _mask) :
DemuxBufferCommon(cpy)
{
if (verbose) fprintf(stderr, "DemuxBuffer T=%d N=%u\n", sizeof(T), N);
nsam = buffer_len/sizeof(T)/G::nchan;
init();
mask = new unsigned[nchan];
for (unsigned ic = 0; ic < nchan; ++ic){
mask[ic] = ic < G::m1? FULL_MASK: _mask;
}
if (dddata == 0){
dddata = new T*[nchan];
ddcursors = new T*[nchan];
for (unsigned ic = 0; ic < nchan; ++ic){
ddcursors[ic] = dddata[ic] = new T[nsam*nb_cat];
}
}
make_names();
data_fits_buffer = nsam*sizeof(T)*nchan == buffer_len;
if (verbose>2){
fprintf(stderr, "nsam:%d _buffer_len:%d data_fits_buffer? %s\n",
nsam, buffer_len, data_fits_buffer? "YES": "NO");
}
}
virtual unsigned getItem(int ii) {
T* src = reinterpret_cast<T*>(pdata);
return src[ii];
}
virtual unsigned getSizeofItem() { return sizeof(T); }
};
template <class T, DemuxBufferType N>
bool DemuxBuffer<T, N>::demux(bool start, int start_off, int len) {
T* src1 = reinterpret_cast<T*>(pdata+start_off);
T* src = reinterpret_cast<T*>(pdata+start_off);
int Tlen = len/sizeof(T);
int isam = 0;
if (verbose > 1) fprintf(stderr, "demux() start_off:%08x src:%p\n",
start_off, src);
if (verbose > 1 && start && ch_id(src[0]) != 0x00){
fprintf(stderr, "handling misalign at [0] %08x data_fits_buffer:%d\n",
src[0], data_fits_buffer);
}
if (sizeof(T)==4 && start && !data_fits_buffer){
/* search for start point - site 1 */
for (; !(ch_id(src[0]) == ch_id(0x20) &&
ch_id(src[1]) == ch_id(0x21) &&
ch_id(src[2]) == ch_id(0x22) &&
ch_id(src[3]) == ch_id(0x23) ); ++src){
if (src - src1 > 256){
if (verbose > 1){
dump(src1, src-src1);
}
fprintf(stderr, "failed to channel align\n");
return true;
}
}
}
int startoff = (src - src1);
if (verbose && startoff){
fprintf(stderr, "realign: %p -> %p %d %d\n",
src1, src, src-src1, startoff);
}
if (verbose && startchan != 0){
fprintf(stderr, "start:%d startchan:%d data[0]:%08x\n",
start, startchan, *src);
}
unsigned ichan = startchan;
/* run to the end of buffer. nsam could be rounded down,
* so do not use it.
*/
if (verbose) fprintf(stderr, "%s will %s skip ES\n", _PFN, show_es? "NOT":"");
for (isam = startoff/nchan; true; ++isam, ichan = 0){
if (!show_es && evX.isES(reinterpret_cast<unsigned*>(src))){
if (verbose) fprintf(stderr, "skip ES\n");
src += nchan_es;
}
for (; ichan < nchan; ++ichan){
*ddcursors[ichan]++ = (*src++)&mask[ichan];
if (src-src1 >= Tlen){
if (verbose){
fprintf(stderr,
"demux() END buf ch:%d src:%p len:%d\n",
ichan, src, ddcursors[ichan]-dddata[ichan]);
}
if (++ichan >= nchan) ichan = 0;
startchan = ichan;
if (verbose && startchan != 0){
fprintf(stderr,
"demux() END buf startchan:%d\n", startchan);
}
return false;
}
}
}
/* does not happen */
return true;
}
template<class T, DemuxBufferType N> unsigned DemuxBuffer<T, N>::ID_MASK;
template<class T, DemuxBufferType N> int DemuxBuffer<T, N>::startchan;
template<class T, DemuxBufferType N> T** DemuxBuffer<T, N>::dddata;
template<class T, DemuxBufferType N> T** DemuxBuffer<T, N>::ddcursors;
template <>
bool DemuxBuffer<short, DB_REGULAR>::demux(bool start, int start_off, int len) {
short* src1 = reinterpret_cast<short*>(pdata+start_off);
short* src = reinterpret_cast<short*>(pdata+start_off);
int shortlen = len/sizeof(short);
int isam = 0;
if (verbose > 1) fprintf(stderr, "demux() start_off:%08x src:%p\n",
start_off, src);
if (verbose > 1 && start && ch_id(src[0]) != 0x00){
fprintf(stderr, "handling misalign at [0] %08x data_fits_buffer:%d\n",
src[0], data_fits_buffer);
}
int startoff = 0;
if (verbose && startchan != 0){
fprintf(stderr, "start:%d startchan:%d data[0]:%08x\n",
start, startchan, *src);
}
unsigned ichan = startchan;
/* run to the end of buffer. nsam could be rounded down,
* so do not use it.
*/
if (verbose) fprintf(stderr, "%s will %s skip ES\n", _PFN, show_es? "NOT":"");
for (isam = startoff/nchan; true; ++isam, ichan = 0){
if (!show_es && evX.isES(reinterpret_cast<unsigned*>(src))){
if (verbose) fprintf(stderr, "skip ES\n");
src += nchan_es;
}
for (; ichan < nchan; ++ichan){
*ddcursors[ichan]++ = (*src++)&mask[ichan];
if (src-src1 >= shortlen){
if (verbose){
fprintf(stderr,
"demux() END buf ch:%d src:%p len:%d\n",
ichan, src, ddcursors[ichan]-dddata[ichan]);
}
if (++ichan >= nchan) ichan = 0;
startchan = ichan;
if (verbose && startchan != 0){
fprintf(stderr,
"demux() END buf startchan:%d\n", startchan);
}
return false;
}
}
}
/* does not happen */
return true;
}
template <>
bool DemuxBuffer<short, DB_DOUBLE>::demux(bool start, int start_off, int len) {
short* src1 = reinterpret_cast<short*>(pdata+start_off);
short* src = reinterpret_cast<short*>(pdata+start_off);
int shortlen = len/sizeof(short);
int isam = 0;
if (verbose > 1) fprintf(stderr, "demux() start_off:%08x src:%p\n",
start_off, src);
if (verbose > 1 && start && ch_id(src[0]) != 0x00){
fprintf(stderr, "handling misalign at [0] %08x data_fits_buffer:%d\n",
src[0], data_fits_buffer);
}
int startoff = 0;
if (verbose && startchan != 0){
fprintf(stderr, "start:%d startchan:%d data[0]:%08x\n",
start, startchan, *src);
}
unsigned ichan = startchan;
/* run to the end of buffer. nsam could be rounded down,
* so do not use it.
*/
if (verbose) fprintf(stderr, "%s will %s skip ES\n", _PFN, show_es? "NOT":"");
for (isam = startoff/nchan; true; ++isam, ichan = 0){
if (!show_es && evX.isES(reinterpret_cast<unsigned*>(src))){
if (verbose) fprintf(stderr, "skip ES\n");
src += nchan_es;
}
for (; ichan < nchan; ++ichan){
*ddcursors[ichan]++ = (*src++)&mask[ichan];
*ddcursors[ichan]++ = (*src++)&mask[ichan];
if (src-src1 >= shortlen){
if (verbose){
fprintf(stderr,
"demux() END buf ch:%d src:%p len:%d\n",
ichan, src, ddcursors[ichan]-dddata[ichan]);
}
if (++ichan >= nchan) ichan = 0;
startchan = ichan;
if (verbose && startchan != 0){
fprintf(stderr,
"demux() END buf startchan:%d\n", startchan);
}
return false;
}
}
}
/* does not happen */
return true;
}
/* DB_2D_REGULAR .. 6 x 8 channels
* DMA0: Sites 1,3,5 # SITE1 => CH01->CH08
* DMA1: Sites 2,4,6 # SITE2 => CH25->CH32
*
* LUTs index from zero, for each offset per DMA block.
*/
const u8 DB_2D_R_MEM_PIN_LUT_48_DMA0[] = {
[ 0] = 0, [ 1] = 1, [ 2] = 2, [ 3] = 3, [ 4] = 4, [ 5] = 5, [ 6] = 6, [ 7] = 7,
[ 8] = 16, [ 9] = 17, [10] = 18, [11] = 19, [12] = 20, [13] = 21, [14] = 22, [15] = 23,
[16] = 32, [17] = 33, [18] = 34, [19] = 35, [20] = 36, [21] = 37, [22] = 38, [23] = 39,
};
const u8 DB_2D_R_MEM_PIN_LUT_48_DMA1[] = {
[ 0] = 8, [ 1] = 9, [ 2] = 10, [ 3] = 11, [ 4] = 12, [ 5] = 13, [ 6] = 14, [ 7] = 15,
[ 8] = 24, [ 9] = 25, [10] = 26, [11] = 27, [12] = 28, [13] = 29, [14] = 30, [15] = 31,
[16] = 40, [17] = 41, [18] = 42, [19] = 43, [20] = 44, [21] = 45, [22] = 46, [23] = 47,
};
template <>
bool DemuxBuffer<short, DB_2D_REGULAR>::demux(bool start, int start_off, int len) {
short* src000 = reinterpret_cast<short*>(pdata+start_off);
short* src = src000;
const int shortlen = len/sizeof(short)/2;
const unsigned NC2 = nchan/2;
const unsigned BUFSHORTS = Buffer::bufferlen/sizeof(short);
if (verbose) fprintf(stderr, "%s start_off:%08x src:%p len:%d\n",
_PFN, start_off, src, len);
int b1 = MapBuffer::getBuffer(reinterpret_cast<char*>(src));
if (b1&1){
if (verbose) fprintf(stderr, "%s ODD buffer skip\n", _PFN);
return true;
}
/* run to the end of buffer. nsam could be rounded down,
* so do not use it.
*/
if (verbose) fprintf(stderr, "%s will %s skip ES\n", _PFN, show_es? "NOT":"");
for (unsigned isam = 0; true; ++isam){
if (!show_es && evX.isES(reinterpret_cast<unsigned*>(src))){
if (verbose) fprintf(stderr, "skip ES\n");
src += nchan_es;
}
{
short* src0 = src;
for (unsigned ichan = 0; ichan < NC2; ++ichan){
*ddcursors[DB_2D_R_MEM_PIN_LUT_48_DMA0[ichan]]++ =
src0[ichan]&mask[DB_2D_R_MEM_PIN_LUT_48_DMA0[ichan]];
}
}
{
short* src1 = src+BUFSHORTS;
for (unsigned ichan = 0; ichan < NC2; ++ichan){
*ddcursors[DB_2D_R_MEM_PIN_LUT_48_DMA1[ichan]]++ =
src1[ichan]&mask[DB_2D_R_MEM_PIN_LUT_48_DMA1[ichan]];
}
}
src += nchan;
if (src-src000 >= shortlen){
if (verbose){
fprintf(stderr,
"demux() END buf ch:%d src:%p len:%d\n",
nchan-1, src, ddcursors[nchan-1]-dddata[nchan-1]);
}
return false;
}
}
if (verbose) fprintf(stderr, "%s 99 DOES NOT HAPPEN\n", _PFN);
/* does not happen */
return true;
}
template <>
bool DemuxBuffer<short, DB_2D_DOUBLE>::demux(bool start, int start_off, int len) {
short* src1 = reinterpret_cast<short*>(pdata+start_off);
short* src = reinterpret_cast<short*>(pdata+start_off);
int shortlen = len/sizeof(short)/2;
int isam = 0;
unsigned NC2 = nchan/2;
const unsigned BUFSHORTS = Buffer::bufferlen/sizeof(short);
if (verbose) fprintf(stderr, "%s start_off:%08x src:%p len:%d\n",
_PFN, start_off, src, len);
int b1 = MapBuffer::getBuffer(reinterpret_cast<char*>(src));
if (b1&1){
if (verbose) fprintf(stderr, "%s ODD buffer skip\n", _PFN);
return true;
}
/*
if (b1 > 100){
if (verbose) fprintf(stderr, "%s no successor buffer skip\n", _PFN);
return true;
}
*/
int startoff = 0;
unsigned ichan = 0;
/* run to the end of buffer. nsam could be rounded down so do not use it. */
if (verbose) fprintf(stderr, "%s will %s skip ES\n", _PFN, show_es? "NOT":"");
for (isam = startoff/nchan; true; ++isam, ichan = 0){
if (!show_es && evX.isES(reinterpret_cast<unsigned*>(src))){
if (verbose) fprintf(stderr, "skip ES\n");
src += nchan_es;
}
short* src0 = src;
for (; ichan < NC2; ++ichan){
*ddcursors[ichan]++ = (*src++)&mask[ichan];
*ddcursors[ichan]++ = (*src++)&mask[ichan];
}
for (short *src2 = src0+BUFSHORTS; ichan < nchan; ++ichan){
*ddcursors[ichan]++ = (*src2++)&mask[ichan];
*ddcursors[ichan]++ = (*src2++)&mask[ichan];
}
if (src-src1 >= shortlen){
if (verbose){
fprintf(stderr,
"demux() END buf ch:%d src:%p len:%d\n",
ichan, src, ddcursors[ichan]-dddata[ichan]);
}
return false;
}
}
if (verbose) fprintf(stderr, "%s 99 DOES NOT HAPPEN\n", _PFN);
/* does not happen */
return true;
}
/** output multiple samples per buffer */
template <class T>
class OversamplingMapBuffer: public Buffer {
const int over, asr;
const int asr1;
const int nsam;
T *outbuf;
int* sums;
public:
OversamplingMapBuffer(Buffer* cpy, int _oversampling, int _asr) :
Buffer(cpy),
over(_oversampling),
asr(_asr),
asr1(sizeof(T)==4?8:0),
nsam(buffer_len/sizeof(T)/G::nchan)
{
static int report;
if (!report &&verbose){
fprintf(stderr, "OversamplingMapBuffer()\n");
++report;
}
outbuf = new T[nsam*G::nchan/over];
sums = new int[G::nchan];
}
virtual ~OversamplingMapBuffer() {
delete [] outbuf;
delete [] sums;
}
virtual int writeBuffer(int out_fd, int b_opts) {
T* src = reinterpret_cast<T*>(pdata);
int nsum = 0;
int osam = 0;
fprintf(stderr, "OversamplingMapBuffer::writeBuffer(<%d>) out_fd:%d\n", sizeof(T), out_fd);
memset(sums, 0, G::nchan*sizeof(int));
for (int isam = 0; isam < nsam; ++isam){
for (unsigned ic = 0; ic < G::nchan; ++ic){
sums[ic] += src[isam*G::nchan+ic] >> asr1;
}
if (++nsum >= over){
for (unsigned ic = 0; ic < G::nchan; ++ic){
outbuf[osam*G::nchan+ic] = sums[ic] >> asr;
sums[ic] = 0;
}
++osam;
nsum = 0;
}
}
return write(out_fd, outbuf, osam*G::nchan*sizeof(T));
}
};
class _OversamplingMapBufferSingleSample: public Buffer {
public:
int* sums;
_OversamplingMapBufferSingleSample(Buffer* cpy):
Buffer(cpy)
{
sums = new int[G::nchan];
}
virtual ~_OversamplingMapBufferSingleSample() {
delete [] sums;
}
static void makeReport(const char* cname, int tsize){
static int report;
if (!report &&verbose){
fprintf(stderr, "%s<%s>()\n", cname, tsize==4? "int": "short");
++report;
}
}
};
/** output one sample per buffer */
template <class T>
class OversamplingMapBufferSingleSample: public _OversamplingMapBufferSingleSample {
const int over, asr;
const int asr1;
const int nsam;
bool checkit_ok(T* src, int isam) {
T checkit = src[isam*G::nchan+0] >> asr1;
for (unsigned ic = 1; ic < 4; ++ic){
T checkit2 = src[isam*G::nchan+ic] >> asr1;
if (checkit2 != checkit){
return true;
}
}
return false;
}
public:
OversamplingMapBufferSingleSample(Buffer* cpy,
int _oversampling, int _asr) :
_OversamplingMapBufferSingleSample(cpy),
over(_oversampling),
asr(sizeof(T)==4&&_asr>8 ? 8: _asr),
asr1(sizeof(T)==4? (8 + _asr - asr):0),
nsam(buffer_len/sizeof(T)/G::nchan)
{
makeReport("OversamplingMapBufferSingleSample", sizeof(T));
}
virtual int writeBuffer(int out_fd, int b_opts) {
T* src = reinterpret_cast<T*>(pdata);
int stride = nsam/over;
if (G::show_first_sample){
memset(sums, 0, G::nchan*sizeof(int));
for (unsigned ic = 0; ic < G::nchan; ++ic){
sums[ic] += src[ic];
}
int fd = createOutfile("/dev/shm/first_sample");
write(fd, sums, G::nchan*sizeof(int));
close(fd);
}
memset(sums, 0, G::nchan*sizeof(int));
for (int isam = 0; isam < nsam; isam += stride){
if (checkiten){
/* runs of samples are bad - could be ES, could be strange 0x0000 .. either way, REJECT */
if (!checkit_ok(src, isam) && !checkit_ok(src, ++isam)){
if (verbose){
fprintf(stderr, "checkit_ok reject\n");
}
return 0;
}
}
for (unsigned ic = 0; ic < G::nchan; ++ic){
sums[ic] += src[isam*G::nchan+ic] >> asr1;
}
}
if (asr){
for (unsigned ic = 0; ic < G::nchan; ++ic){
sums[ic] >>= asr;
}
}
if (verbose) fprintf(stderr, "writeBuffer() 99\n");
return write(out_fd, sums, G::nchan*sizeof(int));
}
};
#define SCOFF 24 /* sample count offset */
#define EFMT "ERROR [%02d] %10llu at offset:%d %08x -> %s:%08x\n"
int u32toB(int nu32) {
return nu32*sizeof(unsigned);
}
class ScratchpadTestBuffer: public MapBuffer {
static bool report_done;
const unsigned sample_size;
const unsigned samples_per_buffer;
static unsigned sample_count;
static bool sample_count_valid;
static unsigned long long samples_elapsed;
static unsigned shim;
static unsigned buffer_count;
int last_sample_offset()
/* offset in u32 */
{
return ((samples_per_buffer-1)*sample_size)/sizeof(unsigned);
}
bool isValidQuad(unsigned *pbuf, unsigned startoff){
return (pbuf[startoff+0]&0x0000001f) == 0x0 &&
(pbuf[startoff+1]&0x0000001f) == 0x1 &&
(pbuf[startoff+2]&0x0000001f) == 0x2 &&
(pbuf[startoff+3]&0x0000001f) == 0x3;
}
int checkAlignment(unsigned *src) {
if (isValidQuad(src, shim)){