cacao  Release 0.1.03-dev
Compute And Control For Adaptive Optics
ImageStreamIO.c File Reference

Read and Create image. More...

#include <math.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>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <semaphore.h>
#include "ImageStreamIO.h"
Include dependency graph for ImageStreamIO.c:

Macros

#define _GNU_SOURCE
 
#define DEBUG_TRACEPOINTLOG(...)
 
#define ULONGLONG_IMG   (80)
 
#define ImageStreamIO_printERROR(code, msg)   if(internal_printError) internal_printError(__FILE__, __func__, __LINE__, code, msg);
 

Functions

void __attribute__ ((constructor))
 Initialize module. More...
 
errno_t init_ImageStreamIO ()
 
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 ImageStreamIO_set_default_printError ()
 Set the error reporting function to the default provided by the library. More...
 
errno_t ImageStreamIO_set_printError (errno_t(*new_printError)(const char *, const char *, int, errno_t, char *))
 Set the error reporting function. The new function supplied by the pointer will be called whenever a library function reports an error. Pass NULL to turn off error reporting from within the library. More...
 
errno_t ImageStreamIO_write_process_log (char *msg)
 Write entry into debug log. More...
 
errno_t ImageStreamIO_printERROR_ (const char *file, const char *func, int line, __attribute__((unused)) errno_t code, char *errmessage)
 
errno_t ImageStreamIO_readBufferAt (const IMAGE *image, const unsigned int slice_index, void **buffer)
 Get the raw pointer to the beginning of the slice slice_index. More...
 
errno_t ImageStreamIO_shmdirname (char *shmdname)
 
errno_t ImageStreamIO_filename (char *file_name, size_t ssz, const char *im_name)
 Get the standard stream filename. More...
 
int ImageStreamIO_typesize (uint8_t datatype)
 Get the size in bytes from the data type code. More...
 
int ImageStreamIO_bitpix (uint8_t datatype)
 Get the FITSIO BITPIX from the data type code. More...
 
uint64_t ImageStreamIO_offset_data (IMAGE *image, void *map)
 
uint64_t ImageStreamIO_initialize_buffer (IMAGE *image)
 
errno_t ImageStreamIO_createIm (IMAGE *image, const char *name, long naxis, uint32_t *size, uint8_t datatype, int shared, int NBkw)
 Create shared memory image stream (legacy API) More...
 
errno_t ImageStreamIO_createIm_gpu (IMAGE *image, const char *name, long naxis, uint32_t *size, uint8_t datatype, int8_t location, int shared, int NBsem, int NBkw, uint64_t imagetype)
 Create shared memory image stream. More...
 
errno_t ImageStreamIO_destroyIm (IMAGE *image)
 Deallocate and remove an IMAGE structure. More...
 
errno_t ImageStreamIO_openIm (IMAGE *image, const char *name)
 Connect to an existing shared memory image stream. More...
 
void * ImageStreamIO_get_image_d_ptr (IMAGE *image)
 
errno_t ImageStreamIO_read_sharedmem_image_toIMAGE (const char *name, IMAGE *image)
 Read / connect to existing shared memory image stream. More...
 
errno_t ImageStreamIO_closeIm (IMAGE *image)
 Close a shared memmory image stream. More...
 
errno_t ImageStreamIO_destroysem (IMAGE *image)
 Destroy shmim semaphores. More...
 
int ImageStreamIO_createsem (IMAGE *image, long NBsem)
 Create shmim semaphores. More...
 
long ImageStreamIO_sempost (IMAGE *image, long index)
 Post all shmim semaphores. More...
 
long ImageStreamIO_sempost_excl (IMAGE *image, long index)
 Post all shmim semaphores except one. More...
 
long ImageStreamIO_sempost_loop (IMAGE *image, long index, long dtus)
 Post shmim semaphores at regular time interval. More...
 
int ImageStreamIO_getsemwaitindex (IMAGE *image, int semindexdefault)
 Get available semaphore index. More...
 
int ImageStreamIO_semwait (IMAGE *image, int index)
 Wait for semaphore. More...
 
int ImageStreamIO_semtrywait (IMAGE *image, int index)
 
int ImageStreamIO_semtimedwait (IMAGE *image, int index, const struct timespec *semwts)
 
long ImageStreamIO_semflush (IMAGE *image, long index)
 Flush all semaphores of a shmim. More...
 

Variables

static int INITSTATUS_ImageStreamIO = 0
 
errno_t(* internal_printError )(const char *, const char *, int, errno_t, char *) = &ImageStreamIO_printERROR_
 

Detailed Description

Read and Create image.

Read and create images and streams (shared memory)

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE

◆ DEBUG_TRACEPOINTLOG

#define DEBUG_TRACEPOINTLOG (   ...)
Value:
do { \
char msg[1000]; \
sprintf(msg, __VA_ARGS__); \
ImageStreamIO_write_process_log(msg); \
} while(0)

◆ ImageStreamIO_printERROR

#define ImageStreamIO_printERROR (   code,
  msg 
)    if(internal_printError) internal_printError(__FILE__, __func__, __LINE__, code, msg);

◆ ULONGLONG_IMG

#define ULONGLONG_IMG   (80)

Function Documentation

◆ __attribute__()

void __attribute__ ( (constructor)  )

Initialize module.

◆ ImageStreamIO_bitpix()

int ImageStreamIO_bitpix ( uint8_t  atype)

Get the FITSIO BITPIX from the data type code.

Returns
the BITPIX if atype valid
-1 if atype is not valid
Parameters
[in]datatypethe type code (see ImageStruct.h

◆ ImageStreamIO_closeIm()

errno_t ImageStreamIO_closeIm ( IMAGE image)

Close a shared memmory image stream.

For use in clients, detaches and cleans up memory used by non-owner process.

Returns
IMAGESTREAMIO_SUCCESS on success
the appropriate error code otherwise if an error occurs
Parameters
[in]imageA real-time image structure which contains the image data and meta-data.

◆ ImageStreamIO_createIm()

errno_t ImageStreamIO_createIm ( IMAGE image,
const char *  name,
long  naxis,
uint32_t *  size,
uint8_t  datatype,
int  shared,
int  NBkw 
)

Create shared memory image stream (legacy API)

Parameters
[out]imageIMAGE structure which will have its members allocated and initialized.
[in]namethe name of the shared memory file will be data.tmpfsdir/<name>_im.shm
[in]naxisnumber of axes in the image.
[in]sizethe size of the image along each axis. Must have naxis elements.
[in]datatypedata type code
[in]sharedif true then a shared memory buffer is allocated. If false, only local storage is used.
[in]NBkwthe number of keywords to allocate.

◆ ImageStreamIO_createIm_gpu()

errno_t ImageStreamIO_createIm_gpu ( IMAGE image,
const char *  name,
long  naxis,
uint32_t *  size,
uint8_t  datatype,
int8_t  location,
int  shared,
int  NBsem,
int  NBkw,
uint64_t  imagetype 
)

Create shared memory image stream.

Parameters
[out]imageIMAGE structure which will have its members allocated and initialized.
[in]namethe name of the shared memory file will be data.tmpfsdir/<name>_im.shm
[in]naxisnumber of axes in the image.
[in]sizethe size of the image along each axis. Must have naxis elements.
[in]datatypedata type code
[in]locationif -1 then a CPU memory buffer is allocated. If >=0, GPU memory buffer is allocated on devive location.
[in]sharedif true then a shared memory buffer is allocated. If false, only local storage is used.
[in]NBsemthe number of semaphores to allocate.
[in]NBkwthe number of keywords to allocate.
[in]imagetypetype of the stream

◆ ImageStreamIO_createsem()

int ImageStreamIO_createsem ( IMAGE image,
long  NBsem 
)

Create shmim semaphores.

Purpose

Create semaphore of a shmim

Arguments

Parameters
[in]imageIMAGE* pointer to shmim
[in]NBsemnumber of semaphores to be created
Parameters
[in]imagethe name of the shared memory file
[in]NBsemnumber of semaphores to be created

◆ ImageStreamIO_destroyIm()

errno_t ImageStreamIO_destroyIm ( IMAGE image)

Deallocate and remove an IMAGE structure.

For a shared image: Closes all semaphores, deallcoates sem pointers, and removes associated files. Unmaps the shared memory segment, and finally removes the file. Sets the metadata and keyword pointers to NULL.

For a non-shred image: Deallocates all arrays and sets pointers to NULL.

Returns
IMAGESTREAMIO_SUCCESS on success
IMAGESTREAMIO_FAILURE on an error (but currently no checks done)
Parameters
[in]imageThe IMAGE structure to deallocate and remove from the system.

◆ ImageStreamIO_destroysem()

errno_t ImageStreamIO_destroysem ( IMAGE image)

Destroy shmim semaphores.

Purpose

Destroy semaphore of a shmim

Arguments

Parameters
[in]imageIMAGE* pointer to shmim
Parameters
[in]imagethe name of the shared memory file

◆ ImageStreamIO_filename()

errno_t ImageStreamIO_filename ( char *  file_name,
size_t  ssz,
const char *  im_name 
)

Get the standard stream filename.

Fills in the file_name string with the standard shared memory image path, e.g.

char file_name[64];
ImageStreamIO_filename(file_name, 64, "image00");
printf("%s\n", file_name);

produces the output:

*    /milk/shm/image00.im.shm
8  
Returns
IMAGESTREAMIO_SUCCESS on success
IMAGESTREAMIO_FAILURE on error
Parameters
[out]file_namethe file name string to fill in
[in]sszthe allocated size of file_name
[in]im_namethe image name

◆ ImageStreamIO_get_image_d_ptr()

void* ImageStreamIO_get_image_d_ptr ( IMAGE image)
Todo:
should this return a NULL?
Todo:
should this return a NULL?

◆ ImageStreamIO_getsemwaitindex()

int ImageStreamIO_getsemwaitindex ( IMAGE image,
int  semindexdefault 
)

Get available semaphore index.

Purpose

Get available shmim semaphore index

Arguments

Parameters
[in]imageIMAGE* pointer to shmim
[in]indexpreferred semaphore index, if available
Parameters
[in]imagethe name of the shared memory file

◆ ImageStreamIO_initialize_buffer()

uint64_t ImageStreamIO_initialize_buffer ( IMAGE image)
Todo:
Is this really an exit or should we return?
Todo:
should this return an error?

◆ ImageStreamIO_offset_data()

uint64_t ImageStreamIO_offset_data ( IMAGE image,
void *  map 
)

◆ ImageStreamIO_openIm()

errno_t ImageStreamIO_openIm ( IMAGE image,
const char *  name 
)

Connect to an existing shared memory image stream.

Wrapper for ImageStreamIO_read_sharedmem_image_toIMAGE

Parameters
[out]imageIMAGE structure which will be attached to the existing IMAGE
[in]namethe name of the shared memory file will be data.tmpfsdir/<name>_im.shm

◆ ImageStreamIO_printERROR_() [1/2]

errno_t ImageStreamIO_printERROR_ ( const char *  file,
const char *  func,
int  line,
errno_t  code,
char *  errmessage 
)

◆ ImageStreamIO_printERROR_() [2/2]

errno_t ImageStreamIO_printERROR_ ( const char *  file,
const char *  func,
int  line,
__attribute__((unused)) errno_t  code,
char *  errmessage 
)

Print error to stderr

◆ ImageStreamIO_printWARNING()

errno_t ImageStreamIO_printWARNING ( char *  warnmessage)

Print warning to stderr

◆ ImageStreamIO_read_sharedmem_image_toIMAGE()

errno_t ImageStreamIO_read_sharedmem_image_toIMAGE ( const char *  name,
IMAGE image 
)

Read / connect to existing shared memory image stream.

Purpose

Read shared memory image

Details

<

Todo:
can the following code be deleted?
Parameters
[in]namethe name of the shared memory file to access, as in data.tmpfsdir/<name>_im.shm
[out]imagethe IMAGE structure to connect to the stream

◆ ImageStreamIO_readBufferAt()

errno_t ImageStreamIO_readBufferAt ( const IMAGE image,
const unsigned int  slice_index,
void **  buffer 
)

Get the raw pointer to the beginning of the slice slice_index.

Purpose

Return the raw pointer to the beginning of the slice slice_index

Arguments

Parameters
[in]imageIMAGE* pointer to shmim
[in]indecconst int slice_index of the slice to read
[out]buffervoid** pointer to the beginning of the slice
Returns
the error code

◆ ImageStreamIO_semflush()

long ImageStreamIO_semflush ( IMAGE image,
long  index 
)

Flush all semaphores of a shmim.

Purpose

Flush shmim semaphore

Arguments

Parameters
[in]imageIMAGE* pointer to shmim
[in]indexsemaphore index flush all semaphores if index<0
Parameters
[in]imagethe name of the shared memory file

◆ ImageStreamIO_sempost()

long ImageStreamIO_sempost ( IMAGE image,
long  index 
)

Post all shmim semaphores.

Purpose

Posts semaphore of a shmim if index < 0, post all semaphores

Arguments

Parameters
[in]imageIMAGE* pointer to shmim
[in]indexsemaphore index index of semaphore to be posted if index=-1, post all semaphores
Parameters
[in]imagethe name of the shared memory file
[in]indexsemaphore index

◆ ImageStreamIO_sempost_excl()

long ImageStreamIO_sempost_excl ( IMAGE image,
long  index 
)

Post all shmim semaphores except one.

Purpose

Posts all semaphores of a shmim except one

Arguments

Parameters
[in]imageIMAGE* pointer to shmim
[in]indexsemaphore index index of semaphore to be excluded
Parameters
[in]imagethe name of the shared memory file
[in]indexsemaphore index

◆ ImageStreamIO_sempost_loop()

long ImageStreamIO_sempost_loop ( IMAGE image,
long  index,
long  dtus 
)

Post shmim semaphores at regular time interval.

Purpose

Posts all semaphores of a shmim at regular time intervals

Arguments

Parameters
[in]imageIMAGE* pointer to shmim
[in]indexsemaphore index is =-1, post all semaphores
[in]dtustime interval [us]
Parameters
[in]imagethe name of the shared memory file
[in]indexsemaphore index

◆ ImageStreamIO_semtimedwait()

int ImageStreamIO_semtimedwait ( IMAGE image,
int  index,
const struct timespec *  semwts 
)
Parameters
[in]imagethe name of the shared memory file
[in]indexsemaphore index

◆ ImageStreamIO_semtrywait()

int ImageStreamIO_semtrywait ( IMAGE image,
int  index 
)
Parameters
[in]imagethe name of the shared memory file
[in]indexsemaphore index

◆ ImageStreamIO_semwait()

int ImageStreamIO_semwait ( IMAGE image,
int  index 
)

Wait for semaphore.

Purpose

Wait on a shmim semaphore

Arguments

Parameters
[in]imageIMAGE* pointer to shmim
[in]indexsemaphore index
Parameters
[in]imagethe name of the shared memory file
[in]indexsemaphore index

◆ ImageStreamIO_set_default_printError()

errno_t ImageStreamIO_set_default_printError ( )

Set the error reporting function to the default provided by the library.

Returns
IMAGESTREAMIO_SUCCESS on success
IMAGESTREAMIO_FAILURE on an error

◆ ImageStreamIO_set_printError()

errno_t ImageStreamIO_set_printError ( errno_t(*)(const char *, const char *, int, errno_t, char *)  new_printError)

Set the error reporting function. The new function supplied by the pointer will be called whenever a library function reports an error. Pass NULL to turn off error reporting from within the library.

Parameters
new_printErroris a pointer to the function to use for reporting errors. Can be NULL.
Returns
IMAGESTREAMIO_SUCCESS on success
IMAGESTREAMIO_FAILURE on an error

◆ ImageStreamIO_shmdirname()

errno_t ImageStreamIO_shmdirname ( char *  shmdname)

◆ ImageStreamIO_typesize()

int ImageStreamIO_typesize ( uint8_t  atype)

Get the size in bytes from the data type code.

Returns
the size in bytes of the data type if valid
-1 if atype is not valid
Parameters
[in]datatypethe type code (see ImageStruct.h

◆ ImageStreamIO_write_process_log()

errno_t ImageStreamIO_write_process_log ( char *  msg)

Write entry into debug log.

◆ init_ImageStreamIO()

errno_t init_ImageStreamIO ( )

Variable Documentation

◆ INITSTATUS_ImageStreamIO

int INITSTATUS_ImageStreamIO = 0
static

◆ internal_printError

errno_t(* internal_printError) (const char *, const char *, int, errno_t, char *) = &ImageStreamIO_printERROR_