-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathImageStreamIO.c
2337 lines (2064 loc) · 65.6 KB
/
ImageStreamIO.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 ImageStreamIO.c
* @brief Read and Create image
*
* Read and create images and streams (shared memory)
*
*
*
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif//_GNU_SOURCE
#include <math.h>
#include <limits.h>
#include <pthread.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <dirent.h>
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h> // for open
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <semaphore.h>
#include <unistd.h> // for close
#include "ImageStreamIO_config.h" // For IMAGESTRUCT_VERSION
#ifdef USE_CFITSIO
#include <fitsio.h>
#endif
// shared memory and semaphores file permission
#define FILEMODE 0666
#if defined NDEBUG
#define DEBUG_TRACEPOINT_LOG(...)
#else
#define DEBUG_TRACEPOINT_LOG(...) \
do \
{ \
char msg[1000]; \
snprintf(msg, 1000, __VA_ARGS__); \
ImageStreamIO_write_process_log(msg); \
} while (0)
#endif
// Handle old fitsios
#ifndef ULONGLONG_IMG
#define ULONGLONG_IMG (80)
#endif
#include "ImageStreamIO.h"
static int INITSTATUS_ImageStreamIO = 0;
void __attribute__((constructor)) libinit_ImageStreamIO()
{
if (INITSTATUS_ImageStreamIO == 0)
{
init_ImageStreamIO();
INITSTATUS_ImageStreamIO = 1;
}
}
errno_t init_ImageStreamIO()
{
// any initialization needed ?
return IMAGESTREAMIO_SUCCESS;
}
// Forward dec'l
errno_t ImageStreamIO_printERROR_(const char *file, const char *func, int line,
errno_t code, char *errmessage);
errno_t ImageStreamIO_printWARNING(char *warnmessage);
errno_t (*internal_printError)(const char *, const char *, int, errno_t,
char *) = &ImageStreamIO_printERROR_;
errno_t ImageStreamIO_set_default_printError()
{
internal_printError = &ImageStreamIO_printERROR_;
return IMAGESTREAMIO_SUCCESS;
}
errno_t ImageStreamIO_set_printError(errno_t (*new_printError)(const char *,
const char *, int, errno_t, char *))
{
internal_printError = new_printError;
return IMAGESTREAMIO_SUCCESS;
}
#define ImageStreamIO_printERROR(code, msg) \
if (internal_printError) \
internal_printError(__FILE__, __func__, __LINE__, code, (char*)msg);
#ifdef HAVE_CUDA
void check(cudaError_t result, char const *const func, const char *const file,
int const line)
{
if (result)
{
cudaDeviceReset();
// Make sure we call CUDA Device Reset
ImageStreamIO_printERROR_(file, func, line, result, "CUDA error");
}
}
// This will output the proper CUDA error strings in the event
// that a CUDA host call returns an error
#define checkCudaErrors(val) check((val), #val, __FILE__, __LINE__)
#endif
/**
* @brief Write entry into debug log
*
* \returns 0
*
*/
errno_t ImageStreamIO_write_process_log(
char *msg)
{
FILE *fplog;
char fname[STRINGMAXLEN_FILE_NAME];
pid_t thisPID;
thisPID = getpid();
snprintf(fname, STRINGMAXLEN_FILE_NAME, "logreport.%05d.log", thisPID);
struct tm *uttime;
time_t tvsec0;
fplog = fopen(fname, "a");
if (fplog != NULL)
{
struct timespec tnow;
// time_t now;
clock_gettime(CLOCK_ISIO, &tnow);
tvsec0 = tnow.tv_sec;
uttime = gmtime(&tvsec0);
fprintf(fplog, "%04d%02d%02dT%02d%02d%02d.%09ld %s\n",
1900 + uttime->tm_year, 1 + uttime->tm_mon, uttime->tm_mday, uttime->tm_hour,
uttime->tm_min, uttime->tm_sec, tnow.tv_nsec,
msg);
fclose(fplog);
}
return 0;
}
/**
* @brief Print error to stderr
*
* \returns IMAGESTREAMIO_SUCCESS
*
*/
errno_t ImageStreamIO_printERROR_(
const char *file,
const char *func,
int line,
__attribute__((unused)) errno_t code,
char *errmessage)
{
fprintf(stderr,
"%c[%d;%dmERROR [ FILE: %s FUNCTION: %s LINE: %d ] %c[%d;m\n",
(char)27, 1, 31, file, func, line, (char)27, 0);
if (errno != 0)
{
char buff[256];
// Test for which version of strerror_r we're using (XSI or GNU)
# if ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && \
!defined(_GNU_SOURCE))
if (strerror_r(errno, buff, sizeof(buff)) == 0)
{
fprintf(stderr, "C Error: %s\n", buff);
}
else
{
fprintf(stderr, "Unknown C Error\n");
}
# else
// GNU strerror_r does not necessarily use buff, and uses errno to report
// errors.
int _errno = errno;
errno = 0;
char *estr = strerror_r(_errno, buff, sizeof(buff));
if (errno == 0)
fprintf(stderr, "%c[%d;%dmC Error: %s%c[%d;m\n", (char)27, 1, 31, estr,
27, 0);
else
fprintf(stderr, "%c[%d;%dmUnknown C Error%c[%d;m\n", (char)27, 1, 31, 27,
0);
errno = _errno; // restore it in case it's used later.
# endif
}
else
fprintf(stderr, "%c[%d;%dmNo C error (errno = 0)%c[%d;m\n", (char)27, 1, 31,
27, 0);
fprintf(stderr, "%c[%d;%dm %s %c[%d;m\n", (char)27, 1, 31, errmessage,
(char)27, 0);
return IMAGESTREAMIO_SUCCESS;
}
/**
* Print warning to stderr
*
* \returns IMAGESTREAMIO_SUCCESS
*
*/
errno_t ImageStreamIO_printWARNING(
char *warnmessage)
{
//int fn = fileno(stderr);
fprintf(stderr, "%c[%d;%dmWARNING %c[%d;m\n",
(char)27, 1, 35, (char)27, 0);
fprintf(stderr, "%c[%d;%dm (PID %d) %s %c[%d;m\n", (char)27, 1, 35, (int) getpid(), warnmessage,
(char)27, 0);
return IMAGESTREAMIO_SUCCESS;
}
/* ============================================================================================================================================================================================== */
/* @name 0. Utilities */
/* ============================================================================================================================================================================================== */
errno_t ImageStreamIO_readBufferAt(
const IMAGE *image,
const unsigned int slice_index,
void **buffer)
{
if ((image->md->imagetype & 0xF) != CIRCULAR_BUFFER)
{
*buffer = (void *)image->array.UI8;
return IMAGESTREAMIO_SUCCESS;
}
if (slice_index >= image->md->size[2])
{
*buffer = NULL;
return IMAGESTREAMIO_FAILURE;
}
const uint64_t frame_size = image->md->size[0] * image->md->size[1];
const int size_element = ImageStreamIO_typesize(image->md->datatype);
*buffer = (void *)(image->array.UI8 + slice_index * frame_size * size_element);
return IMAGESTREAMIO_SUCCESS;
}
/**
* @brief Build directory pathname to use for shared memory files
*
* \returns IMAGESTREAMIO_SUCCESS, or exits process on failure
*
* Use the first directory found:
* - Run-time override: environment variable MILK_SHM_DIR
* - Primary default: macro SHAREDMEMDIR macro, typically "/milk/shm"
* - Secondary default: "/tmp"
*
*/
errno_t ImageStreamIO_shmdirname(
char *shmdname)
{
DIR *tmpdir = NULL; // Initialize to failure (NULL dir stream)
// first, we try the env variable if it exists
char *MILK_SHM_DIR = getenv("MILK_SHM_DIR");
if (MILK_SHM_DIR != NULL)
{
snprintf(shmdname,STRINGMAXLEN_DIR_NAME, "%s", MILK_SHM_DIR);
// does this direcory exist ?
tmpdir = opendir(shmdname);
if (!tmpdir)
{ // Print warning about envvar if envvar has in valid dir
printf(" [ WARNING ] '%s' does not exist\n", MILK_SHM_DIR);
}
}
// second, we try SHAREDMEMDIR default
if (!tmpdir)
{
snprintf(shmdname, STRINGMAXLEN_DIR_NAME, "%s", SHAREDMEMDIR);
tmpdir = opendir(shmdname);
}
// if all above fails, set to /tmp
if (!tmpdir)
{
snprintf(shmdname, STRINGMAXLEN_DIR_NAME, "%s", "/tmp");
tmpdir = opendir(shmdname);
}
// Failure: no directories were found that could be opened
if (!tmpdir)
{
exit(EXIT_FAILURE);
}
// Success: close directory stream; dirname is in shdname
closedir(tmpdir);
return IMAGESTREAMIO_SUCCESS;
}
/**
* @brief Build shared memory file pathname
*
* \returns IMAGESTREAMIO_SUCCESS on success
* \returns IMAGESTREAMIO_FAILURE on failure
*
* <shmdirname(above)>/<name>.im.shm
*
*/
errno_t ImageStreamIO_filename(
char *file_name,
size_t ssz,
const char *im_name)
{
static char shmdirname[STRINGMAXLEN_DIR_NAME];
static int initSHAREDMEMDIR = 0;
if (initSHAREDMEMDIR == 0)
{
ImageStreamIO_shmdirname(shmdirname);
initSHAREDMEMDIR = 1;
}
int rv = snprintf(file_name, ssz, "%s/%s.im.shm", shmdirname, im_name);
if ((rv > 0) && (rv < (int)ssz))
{
return IMAGESTREAMIO_SUCCESS;
}
if (rv < 0)
{
ImageStreamIO_printERROR(IMAGESTREAMIO_FAILURE, strerror(errno));
}
else
{
ImageStreamIO_printERROR(IMAGESTREAMIO_FAILURE,
"string not large enough for file name");
}
return IMAGESTREAMIO_FAILURE;
}
/**
* @brief Size, in bytes, of datatype _DATATYPE_*
*
* \returns size in bytes if datatype is a valid data type
* \returns -1 if datatype is not a valid data type
*
*/
int ImageStreamIO_typesize(
uint8_t datatype)
{
switch (datatype)
{
case _DATATYPE_UINT8:
return SIZEOF_DATATYPE_UINT8;
case _DATATYPE_INT8:
return SIZEOF_DATATYPE_INT8;
case _DATATYPE_UINT16:
return SIZEOF_DATATYPE_UINT16;
case _DATATYPE_INT16:
return SIZEOF_DATATYPE_INT16;
case _DATATYPE_UINT32:
return SIZEOF_DATATYPE_UINT32;
case _DATATYPE_INT32:
return SIZEOF_DATATYPE_INT32;
case _DATATYPE_UINT64:
return SIZEOF_DATATYPE_UINT64;
case _DATATYPE_INT64:
return SIZEOF_DATATYPE_INT64;
case _DATATYPE_HALF:
return SIZEOF_DATATYPE_HALF;
case _DATATYPE_FLOAT:
return SIZEOF_DATATYPE_FLOAT;
case _DATATYPE_DOUBLE:
return SIZEOF_DATATYPE_DOUBLE;
case _DATATYPE_COMPLEX_FLOAT:
return SIZEOF_DATATYPE_COMPLEX_FLOAT;
case _DATATYPE_COMPLEX_DOUBLE:
return SIZEOF_DATATYPE_COMPLEX_DOUBLE;
default:
break;
}
ImageStreamIO_printERROR(IMAGESTREAMIO_INVALIDARG, "invalid type code");
return -1; // This is an in-band error code, so can't be > 0.
}
/**
* @brief Get name of datatype _DATATYPE_*
*
* \returns char* pointer to name of datatype _DATATYPE_*
* \returns "unknown" if datatype is not a valid data type
*
*/
const char *ImageStreamIO_typename(
uint8_t datatype)
{
switch (datatype)
{
case _DATATYPE_UINT8:
return "UINT8";
case _DATATYPE_INT8:
return "INT8";
case _DATATYPE_UINT16:
return "UINT16";
case _DATATYPE_INT16:
return "INT16";
case _DATATYPE_UINT32:
return "UINT32";
case _DATATYPE_INT32:
return "INT32";
case _DATATYPE_UINT64:
return "UINT64";
case _DATATYPE_INT64:
return "INT64";
case _DATATYPE_HALF:
return "FLT16";
case _DATATYPE_FLOAT:
return "FLT32";
case _DATATYPE_DOUBLE:
return "FLT64";
case _DATATYPE_COMPLEX_FLOAT:
return "CPLX32";
case _DATATYPE_COMPLEX_DOUBLE:
return "CPLX64";
default:
break;
}
return "unknown";
}
/**
* @brief Get 7-character name of datatype _DATATYPE_*
*
* \returns 7-character char* pointer, left-padded with spaces if needed
* \returns "unknown" if datatype is not a valid data type
*
*/
const char *ImageStreamIO_typename_7(
uint8_t datatype)
{
switch (datatype)
{
case _DATATYPE_UINT8:
return "UINT8 ";
case _DATATYPE_INT8:
return "INT8 ";
case _DATATYPE_UINT16:
return "UINT16 ";
case _DATATYPE_INT16:
return "INT16 ";
case _DATATYPE_UINT32:
return "UINT32 ";
case _DATATYPE_INT32:
return "INT32 ";
case _DATATYPE_UINT64:
return "UINT64 ";
case _DATATYPE_INT64:
return "INT64 ";
case _DATATYPE_HALF:
return "FLT16 ";
case _DATATYPE_FLOAT:
return "FLOAT ";
case _DATATYPE_DOUBLE:
return "DOUBLE ";
case _DATATYPE_COMPLEX_FLOAT:
return "CFLOAT ";
case _DATATYPE_COMPLEX_DOUBLE:
return "CDOUBLE";
default:
break;
}
return "unknown";
}
/**
* @brief Test whether datatype is a valid _DATATYPE_*
*
* \returns 0 if datatype is a valid non-complex datatype
* \returns 0 if datatype is a valid complex datatype and complex_allowed is 1
* \returns -1 if datatype is not a valid datatype
* \returns -1 if datatype is complex and complex allowed is 0
*
*/
int ImageStreamIO_checktype(uint8_t datatype, int complex_allowed)
{
switch (datatype)
{
case _DATATYPE_UINT8:
case _DATATYPE_INT8:
case _DATATYPE_UINT16:
case _DATATYPE_INT16:
case _DATATYPE_UINT32:
case _DATATYPE_INT32:
case _DATATYPE_UINT64:
case _DATATYPE_INT64:
case _DATATYPE_HALF:
case _DATATYPE_FLOAT:
case _DATATYPE_DOUBLE:
return 0;
case _DATATYPE_COMPLEX_FLOAT:
case _DATATYPE_COMPLEX_DOUBLE:
return complex_allowed ? 0 : -1;
default:
break;
}
ImageStreamIO_printERROR(IMAGESTREAMIO_INVALIDARG, "invalid type code");
return -1; // This is an in-band error code, so can't be > 0.
}
/**
* @brief Get 4-character name of datatype _DATATYPE_*
* @brief Return
*
* \returns char* pointer to 4-char name of datatype, Left-padded with spaces
* \returns " ???" if datatype is not a valid data type
*
*/
const char *ImageStreamIO_typename_short(
uint8_t datatype)
{
switch (datatype)
{
case _DATATYPE_UINT8:
return " UI8";
case _DATATYPE_INT8:
return " I8";
case _DATATYPE_UINT16:
return "UI16";
case _DATATYPE_INT16:
return " I16";
case _DATATYPE_UINT32:
return "UI32";
case _DATATYPE_INT32:
return " I32";
case _DATATYPE_UINT64:
return "UI64";
case _DATATYPE_INT64:
return " I64";
case _DATATYPE_HALF:
return " F16";
case _DATATYPE_FLOAT:
return " FLT";
case _DATATYPE_DOUBLE:
return " DBL";
case _DATATYPE_COMPLEX_FLOAT:
return "CFLT";
case _DATATYPE_COMPLEX_DOUBLE:
return "CDBL";
default:
break;
}
return " ???";
}
/**
* @brief Get suitable floating-point data type to hold int datatype value
*
* \returns floating-point _DATATYPE_* on succes
* \returns -1 if datatype is not a valid data type
*
*
*/
int ImageStreamIO_floattype(
uint8_t datatype)
{
switch (datatype)
{
case _DATATYPE_UINT8:
return _DATATYPE_FLOAT;
case _DATATYPE_INT8:
return _DATATYPE_FLOAT;
case _DATATYPE_UINT16:
return _DATATYPE_FLOAT;
case _DATATYPE_INT16:
return _DATATYPE_FLOAT;
case _DATATYPE_UINT32:
return _DATATYPE_FLOAT;
case _DATATYPE_INT32:
return _DATATYPE_FLOAT;
case _DATATYPE_UINT64:
return _DATATYPE_DOUBLE;
case _DATATYPE_INT64:
return _DATATYPE_DOUBLE;
case _DATATYPE_HALF:
return _DATATYPE_HALF;
case _DATATYPE_FLOAT:
return _DATATYPE_FLOAT;
case _DATATYPE_DOUBLE:
return _DATATYPE_DOUBLE;
case _DATATYPE_COMPLEX_FLOAT:
return _DATATYPE_COMPLEX_FLOAT;
case _DATATYPE_COMPLEX_DOUBLE:
return _DATATYPE_COMPLEX_DOUBLE;
default:
break;
}
ImageStreamIO_printERROR(IMAGESTREAMIO_INVALIDARG, "invalid type code");
return -1; // This is an in-band error code, so can't be > 0.
}
/**
* @brief Get FITSIO data type matching the ImageStreamIO data type arg
*
* \returns FITSIO data type (e.g. TBYTE) on success
* \returns -1 if datatype is not a valid ImageStreamIO data type
*
*/
int ImageStreamIO_FITSIOdatatype(uint8_t datatype)
{
switch (datatype)
{
# ifdef USE_CFITSIO
case _DATATYPE_UINT8:
return TBYTE;
case _DATATYPE_INT8:
return TSBYTE;
case _DATATYPE_UINT16:
return TUSHORT;
case _DATATYPE_INT16:
return TSHORT;
case _DATATYPE_UINT32:
return TUINT;
case _DATATYPE_INT32:
return TINT;
case _DATATYPE_UINT64:
return TULONG;
case _DATATYPE_INT64:
return TLONG;
case _DATATYPE_FLOAT:
return TFLOAT;
case _DATATYPE_DOUBLE:
return TDOUBLE;
# endif
default:
break;
}
ImageStreamIO_printERROR(IMAGESTREAMIO_INVALIDARG,
"bitpix not implemented for type");
return -1; // This is an in-band error code, must be unique from valid BITPIX values.
}
/**
* @brief Get FITSIO bitpix type matching the ImageStreamIO data type arg
*
* \returns FITSIO bitpix type (e.g. BYTE_IMG) on success
* \returns -1 if datatype is invalid or has no matching FITSIO bitpix type
*
*/
int ImageStreamIO_FITSIObitpix(
uint8_t datatype)
{
switch (datatype)
{
# ifdef USE_CFITSIO
case _DATATYPE_UINT8:
return BYTE_IMG;
case _DATATYPE_INT8:
return SBYTE_IMG;
case _DATATYPE_UINT16:
return USHORT_IMG;
case _DATATYPE_INT16:
return SHORT_IMG;
case _DATATYPE_UINT32:
return ULONG_IMG;
case _DATATYPE_INT32:
return LONG_IMG;
case _DATATYPE_UINT64:
return ULONGLONG_IMG;
case _DATATYPE_INT64:
return LONGLONG_IMG;
case _DATATYPE_FLOAT:
return FLOAT_IMG;
case _DATATYPE_DOUBLE:
return DOUBLE_IMG;
# endif
default:
break;
}
ImageStreamIO_printERROR(IMAGESTREAMIO_INVALIDARG,
"bitpix not implemented for type");
return -1; // This is an in-band error code, must be unique from valid BITPIX values.
}
/**
* @brief Assign map to image->array.raw shmim data ptr, get data size
*
* \returns size in bytes of CPU-based shmim data area image->array.raw
* \returns 0 for GPU-based shmim
*
*/
uint64_t ImageStreamIO_offset_data(
IMAGE *image,
void *map)
{
uint8_t datatype = image->md->datatype;
u_int64_t offset = 0;
if (image->md->location >= 0)
{
image->array.raw = ImageStreamIO_get_image_d_ptr(image);
offset = 0;
}
else
{
image->array.raw = map;
offset = ImageStreamIO_typesize(datatype) * image->md->nelement;
}
return offset;
}
/**
* @brief Initialize shmim data, set image->array.raw pointer if needed
*
* \returns size in bytes of CPU-based shmim data area image->array.raw
* \returns 0 for GPU-based shmim
* \returns exits process if calloc of non-shared, process-local memory failed
*
*/
uint64_t ImageStreamIO_initialize_buffer(
IMAGE *image)
{
// void *map; // pointed cast in bytes
const size_t size_element = ImageStreamIO_typesize(image->md->datatype);
if (image->md->location == -1)
{
if (image->md->shared == 1)
{
memset(image->array.raw, 0, image->md->nelement * size_element);
}
else
{
image->array.raw = calloc((size_t)image->md->nelement, size_element);
if (image->array.raw == NULL)
{
ImageStreamIO_printERROR(IMAGESTREAMIO_BADALLOC, "memory allocation failed");
fprintf(stderr, "%c[%d;%dm", (char)27, 1, 31);
fprintf(stderr, "Image name = %s\n", image->name);
fprintf(stderr, "Image size = ");
fprintf(stderr, "%ld", (long)image->md->size[0]);
int i;
for (i = 1; i < image->md->naxis; i++)
{
fprintf(stderr, "x%ld", (long)image->md->size[i]);
}
fprintf(stderr, "\n");
fprintf(stderr, "Requested memory size = %ld elements = %f Mb\n",
(long)image->md->nelement,
1.0 / 1024 / 1024 * image->md->nelement * sizeof(uint8_t));
fprintf(stderr, " %c[%d;m", (char)27, 0);
exit(EXIT_FAILURE); ///\todo Is this really an exit or should we return?
}
}
}
else if (image->md->location >= 0)
{
# ifdef HAVE_CUDA
checkCudaErrors(cudaSetDevice(image->md->location));
checkCudaErrors(
cudaMalloc(&image->array.raw, size_element * image->md->nelement + GPU_IMAGE_PLACEHOLDER));
if (image->md->shared == 1)
{
checkCudaErrors(
cudaIpcGetMemHandle(&image->md->cudaMemHandle, image->array.raw));
}
# else
ImageStreamIO_printERROR(IMAGESTREAMIO_NOTIMPL,
"unsupported location, milk needs to be compiled with -DUSE_CUDA=ON"); ///\todo should this return an error?
# endif
}
return ImageStreamIO_offset_data(image, image->array.raw);
} // uint64_t ImageStreamIO_initialize_buffer(IMAGE *image)
/**
* @brief Get inode using shmim file descriptor, write inode to IMAGE
*
* \returns IMAGESTREAMIO_SUCCESS on success
* \returns IMAGESTREAMIO_INODE on failure
*
*/
errno_t
ImageStreamIO_store_image_inode(IMAGE* image)
{
// - Retrieve status of file referenced by FD; close file on error
struct stat file_stat;
if (fstat(image->shmfd, &file_stat) < 0)
{
close(image->shmfd);
ImageStreamIO_printERROR(IMAGESTREAMIO_INODE, "Error getting inode");
return IMAGESTREAMIO_INODE;
}
// - Save inode as metadata, which metadata are also in the file
image->md->inode = file_stat.st_ino;
return IMAGESTREAMIO_SUCCESS;
}
/**
* @brief Check image->md->inode against inode from shmim name
*
* \returns IMAGESTREAMIO_SUCCESS if ->md->inode matches the shmim inode
* \returns _INODE if ->md->inode does not match the shmim name inode
* \returns _FAILURE if the shmim name inode could not be retrieved
*
*/
errno_t ImageStreamIO_check_image_inode(IMAGE *image)
{
// - Build filename from shmim name image->md->name
char SM_fname[STRINGMAXLEN_FILE_NAME] = {0};
if(IMAGESTREAMIO_SUCCESS
!= ImageStreamIO_filename(SM_fname, sizeof(SM_fname), image->md->name))
{
return IMAGESTREAMIO_FAILURE; // _filename did _printERROR
}
// - Retrieve status of file OR SYMLINK referenced by SM_fname
struct stat file_stat;
if(stat(SM_fname, &file_stat) < 0)
{
ImageStreamIO_printERROR(IMAGESTREAMIO_FAILURE, "Error getting inode");
return IMAGESTREAMIO_FAILURE;
}
// - Return success or failure if inode matches or not, respectively
return image->md->inode == file_stat.st_ino ? IMAGESTREAMIO_SUCCESS :
IMAGESTREAMIO_INODE;
}
/**
* @brief Check image->md->inode against inode from filesystem (post symlink resolution)
*
* \returns IMAGESTREAMIO_SUCCESS if ->md->inode matches the post-symlink file inode
* \returns _INODE if ->md->inode does not match
* \returns _FAILURE if the shmim name inode could not be retrieved
*
*/
errno_t ImageStreamIO_check_image_endpoint_inode(IMAGE *image)
{
// - Build filename from shmim name image->md->name
char SM_fname[STRINGMAXLEN_FILE_NAME] = {0};
if(IMAGESTREAMIO_SUCCESS
!= ImageStreamIO_filename(SM_fname, sizeof(SM_fname), image->md->name))
{
return IMAGESTREAMIO_FAILURE; // _filename did _printERROR
}
// Symlink resolution
char resolved_path[PATH_MAX] = {'\0'};
if(realpath(SM_fname, resolved_path) == NULL)
{
ImageStreamIO_printERROR(IMAGESTREAMIO_FAILURE, "Error getting realpath");
return IMAGESTREAMIO_FAILURE;
}
// - Retrieve status of file referenced by SM_fname
struct stat file_stat;
if(stat(resolved_path, &file_stat) < 0)
{
ImageStreamIO_printERROR(IMAGESTREAMIO_FAILURE, "Error getting realpath inode");
return IMAGESTREAMIO_FAILURE;
}
// - Return success or failure if inode matches or not, respectively
return image->md->inode == file_stat.st_ino ? IMAGESTREAMIO_SUCCESS :
IMAGESTREAMIO_INODE;
}
/**
* @brief Check if an autorelink is needed.
*
* \returns IMAGESTREAMIO_SUCCESS if no need.
* \returns IMAGESTREAMIO_INODE if successful relink to a new [compliant] inode.
* \returns IMAGESTREAMIO_FAILURE if no can.
*/
errno_t ImageStreamIO_autorelink_if_need_if_can(IMAGE *image)
{
int retcode = ImageStreamIO_check_image_endpoint_inode(image);
if (retcode == IMAGESTREAMIO_SUCCESS || retcode == IMAGESTREAMIO_FAILURE) {
// SUCCESS: underlying INODE hasn't changed.
// FAILURE: something messed up that we won't recover
return retcode;
}
// retcode == IMAGESTREAMIO_INODE
// This code below should not be executed if the image we're relinking on is actually the same
// mmap. This would result in a use-after-free.
IMAGE candidate_img = {0}; // stack temp image.
IMAGE* new_candidate_img = &candidate_img;
if (IMAGESTREAMIO_SUCCESS != ImageStreamIO_openIm(new_candidate_img, image->name)) {
printf("_openIm failed @ _autorelink_if_need_if_can\n");
return IMAGESTREAMIO_FAILURE;
}
if (IMAGESTREAMIO_SUCCESS != ImageStreamIO_new_image_compatible(image, new_candidate_img)) {
printf("New image incompatible @ _autorelink_if_need_if_can\n");
return IMAGESTREAMIO_FAILURE;
}
// Sizing works. Let's do the dirty.
// 1. Close the image (old inode) from the caller.
if (IMAGESTREAMIO_SUCCESS != ImageStreamIO_closeIm(image)) {
printf("_closeIm failed @ _autorelink_if_need_if_can\n");
}
// 2. Copy the temp stack frame into the caller struct, including pointers to the new mappings.
memcpy(image, new_candidate_img, sizeof(IMAGE));
return IMAGESTREAMIO_SUCCESS;
}
/**
* @brief Compares if an image fits into another
*
* \returns IMAGESTREAMIO_SUCCESS if source_image would fit into new_image
* \returns IMAGESTREAMIO_FAILURE if not
*/
errno_t ImageStreamIO_new_image_compatible(IMAGE* source_image, IMAGE* new_image){
int compatible = 1;
IMAGE_METADATA* src_md = source_image->md;
IMAGE_METADATA* new_md = new_image->md;
compatible &= (src_md->datatype == new_md->datatype &&
src_md->location == new_md->location &&
src_md->sem == new_md->sem &&
src_md->naxis == new_md->naxis &&
src_md->size[0] == new_md->size[0]);
if (src_md->naxis >= 2){
compatible &= src_md->size[1] == new_md->size[1];
}
if (src_md->naxis >= 3){
compatible &= src_md->size[2] == new_md->size[2];
}
compatible &= (src_md->NBkw <= new_md->NBkw &&
src_md->CBsize <= new_md->CBsize);
if (compatible) {
return IMAGESTREAMIO_SUCCESS;
} else {
return IMAGESTREAMIO_FAILURE;
}
}
/**
* @brief Use metadata to write size and pointer data to IMAGE structure
*
* \returns IMAGESTREAMIO_SUCCESS on success
* \returns IMAGESTREAMIO_INVALIDARG if image->md has any invalid values
*
*/