An example metric library using the Arm MAP Metric Plugin API.This implements the functions as defined in Metric Plugin Template and makes calls to the Metric Plugin API. This plugin provides a custom metric showing the number of interrupts handled by the system, as obtained from /proc/interrupts
.
It can be compiled using the command:
gcc -fPIC -I/path/to/arm/metrics/include -shared -o libcustom1.so custom1.c
For the corresponding definition file to enable the libcustom1.so
metric library to be used by compatible profilers, see custom1.xml.
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#define PROC_STAT "/proc/stat"
#define ERROR_NO_PROC_STAT 1000
#define BUFSIZE 256
#define OVERLAP 64
#ifndef min
#define min(x, y) ( ((x) < (y)) ? (x) : (y) )
#endif
static uint64_t previous = 0;
static int have_previous = 0;
{
if (access(PROC_STAT, F_OK) != 0) {
if (errno == ENOENT)
"Not supported (no /proc/interrupts)");
else
"Error accessing %s: %s", PROC_STAT, strerror(errno));
return -1;
}
}
{
}
int sample_interrupts(
metric_id_t metric_id,
struct timespec *in_out_sample_time, uint64_t *out_value)
{
char buf[BUFSIZE + 1];
if (fd == -1) {
"Error opening %s: %d", PROC_STAT, strerror(errno));
return -1;
}
for (;;) {
if (bytes_read == -1) {
"Error opening %s: %d", PROC_STAT, strerror(errno));
break;
}
if (bytes_read == 0) {
break;
}
if (strncmp(buf, "intr ", 5)==0) {
const char *total = buf + 5;
char *space = strchr(total, ' ');
if (space) {
uint64_t current;
*space = '\0';
current = strtoull(total, NULL, 10);
if (have_previous)
*out_value = current - previous;
previous = current;
have_previous = 1;
break;
}
}
}
}