C Version
Create the file util1.c
with the following content to increment the counter statistics in this example. See the libsstore
(3LIB) and sstore_data_attach
(3SSTORE) man pages for information about the libsstore
C interfaces and types.
/* * Example program to provide statistics values using sstore_data_attach(). */ #include <libsstore.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <fcntl.h> #include <sys/stat.h> #include <sys/types.h> #define NUM_STATS 3 /* libsstore handle */ sstore_handle_t hdl; /* statistic identifiers */ char *ids[NUM_STATS] = { "//:class.app/util1//:stat.reads", "//:class.app/util1//:stat.writes", "//:class.app/util1//:stat.errors" }; /* structure where values are stored */ struct mystats { uint64_t reads; uint64_t writes; uint64_t errors; }; int main() { int iterations = 500; struct mystats *stats; /* Allocate a libsstore handle. */ if ((hdl = sstore_alloc()) == NULL) { (void) printf("Failed to allocate handle."); return (-1); } /* * These statistics already have metadata in a common location, * so sstore knows how to create them. sstore_data_attach() will * create a shared-memory region between sstore and this program. */ if (sstore_data_attach(hdl, (const char **)&ids, NUM_STATS, (uint64_t **)&stats) != ESSTORE_OK) { (void) fprintf(stderr, "sstore_data_attach() failed because %s\n", sstore_err_description(hdl)); return (-1); } /* * Update the values in the structure. * The new values will be stored when sstore reads them. */ while (iterations-- > 0) { stats->reads += rand() % 6; stats->writes += rand() % 5; stats->errors += rand() % 2; sleep(1); } /* * Free the libsstore handle. * The statistics are marked as not being actively provided. */ sstore_free(hdl); return (0); }
Compile this sample application:
$ cc -lsstore -o util1 util1.c