Standard utility functions
Replacements for common libc utility functions.
Since metric library functions need to be async signal safe most standard libc functions can not be used. In addition, even basic syscalls (such as read and write) cannot be used without risking corruption of some other metrics that the enclosing profiler might be tracking (for example, bytes read or bytes written).
The following functions can be safely called inside signal handlers and accommodates I/O being done by the metric plugin without corrupting I/O metrics that are tracked by the enclosing profiler.
struct timespec allinea_get_current_time(void)Gets the current time using the same clock as the enclosing profiler (
async-signal-safe).A replacement for
clock_gettimethat uses the enclosing profiler-preferred system clock (for example,CLOCK_MONOTONIC).Returns
The current time.
Examples
See custom1.c.
const char * allinea_get_custom_data (metric_id_t_t metricId)Returns the
customDataattribute of the source element from the metric definition defined in the xml file. See metric_id_t_t.Parameters
Parameter
Description
metricIdThe metric id.
Returns
The custom data for the given metric id. A zero length C string if not available.
void allinea_safe_fprintf(int fd, const char * format,...)An async-signal-safe version of
fprintf.void allinea_safe_fprintf ( int fd, const char * format, ... )
Parameters
Parameter
Description
fdThe file descriptor to write to.
formatThe format string.
...Zero or more values to be substituted into the
formatstring in the same manner asprintf.int allinea_safe_open(const char * file, int oflags,...)Opens the given file for reading or writing (async-signal-safe).
int allinea_safe_open ( const char * file, int oflags, ... )
A replacement for
open. When used in conjunction withallinea_safe_read()andallinea_safe_write(), the bytes read or bytes written is not included in the I/O accounting of the enclosing profiler.Parameters
Parameter
Description
fileThe name of the file to open (might be an absolute or a relative path).
oflagsFlags specifying how the file must be opened. Accepts all the flags that can be given to the libc
openfunction, such asO_RDONLY,O_WRONLY, orO_RDWR.Returns
The file descriptor of the open file;
-1on failure anderrno set.Examples
See custom1.c.
void allinea_safe_printf(const char * format,...)An async-signal-safe replacement for
printf.void allinea_safe_printf ( const char * format, ... )
Parameters
Parameter
Description
formatThe format string.
...Zero or more values to be substituted into the
formatstring in the same manner asprintf.ssize_t allinea_safe_read(int fd, void * buf, size_t count)Reads up to
countbytes frombuftofd(async-signal-safe).ssize_t allinea_safe_read ( int fd, void * buf, size_t count )
A replacement for
read. When used in conjunction withallinea_safe_open()andallinea_safe_close(), the read bytes are excluded from the enclosing profiler’s I/O accounting.Parameters
Parameter
Description
fdThe file descriptor to read from.
bufThe buffer to read to.
countThe maximum number of bytes to read.
Returns
The number of bytes actually read;
-1on failure anderrno set.ssize_t allinea_safe_read_all(int fd, void * buf, size_t count)Reads the entire contents of
fdintobuf(async-signal-safe).ssize_t allinea_safe_read_all ( int fd, void * buf, size_t count )
When used in conjunction with
allinea_safe_open()andallinea_safe_close(), the read bytes are excluded from the enclosing profiler’s I/O accounting.Parameters
Parameter
Description
fdThe file descriptor to read from.
bufBuffer in which to copy the contents.
countSize of the buffer. At most, this many bytes are written to
buf.Returns
If successful, shows the number of bytes read, otherwise
-1anderrnois set.ssize_t allinea_safe_read_all_with_alloc(int fd, void * buf, size_t * count)Reads the entire contents of
fdintobuf(async-signal-safe).ssize_t allinea_safe_read_all_with_alloc ( int fd, void ** buf, size_t * count )
When used in conjunction with
allinea_safe_open()andallinea_safe_close(), the read bytes are excluded from the enclosing profiler’s I/O accounting.When this is no longer required, use
allinea_safe_free()to allocate sufficient space for the file contents and add a terminating NULL.Parameters
Parameter
Description
fdThe file descriptor to read from.
bufThe pointer to when the buffer pointer must be stored.
countSize of the buffer allocated.
Returns
If successful, shows the the number of bytes read, otherwise
-1anderrnois set.ssize_t allinea_safe_read_line(int fd, void * buf, size_t count)Reads a line from
fdintobuf(async-signal-safe).ssize_t allinea_safe_read_line ( int fd, void * buf, size_t count )
The final newline
\\nis removed and a final\\0added. When used in conjunction withallinea_safe_open()andallinea_safe_close(), the written bytes are excluded from the enclosing profiler’s I/O accounting.Lines longer than
countare truncated.Parameters
Parameter
Description
fdThe file descriptor to read from.
bufBuffer in which to copy the contents.
countSize of the buffer. At most, this many bytes are written to
buf.Returns
If successful, shows the number of bytes read, otherwise
-1anderrnois set.Examples
See custom1.c.
void allinea_safe_vfprintf(int fd, const char * format, va_list ap)An async-signal-safe version of
vfprintf.void allinea_safe_vfprintf ( int fd, const char * format, va_list ap )
Parameters
Parameter
Description
fdThe file descriptor to write to.
formatThe format string.
apA list of arguments for format.
ssize_t allinea_safe_write(int fd, const void * buf, size_t count)Writes up to
countbytes frombuftofd(async-signal-safe).ssize_t allinea_safe_write ( int fd, const void * buf, size_t count )
A replacement for
writeWhen used in conjunction withallinea_safe_open()andallinea_safe_close(), the written bytes are excluded from the I/O accounting of the enclosing profiler.Parameters
Parameter
Description
fdThe file descriptor to write to.
bufThe buffer to write from.
countThe number of bytes to write.
Returns
The number of bytes actually written;
-1on failure anderrnoset.