Create an Application that Updates Statistic Values
Create the file data_update.c with the following content to update the statistic values in this example. Differences between this sstore_data_update() program and the sstore_data_attach() program described in Create an Application that Writes Statistic Values include the following:
-
This
sstore_data_update() program has nomystatsstructure. Themystatsstructure is used for the memory map in thesstore_data_attach() program. -
The handle to the statistics store is declared but no memory is allocated.
/*
* Example program data_update.c:
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <libsstore.h>
#define NUM_STATS 2
const char *ssids[NUM_STATS] = {
"//:class.app/example//:stat.one",
"//:class.app/example//:stat.two"
};
int main()
{
sstore_handle_t hdl;
sstore_value_t vals[NUM_STATS] = {0};
int i, j;
if ((hdl = sstore_alloc()) == NULL) {
fprintf(stderr, "Failed to alloc libsstore handle\n");
return (-1);
}
/* Alloc the sstore_value_t's */
for (i = 0; i < NUM_STATS; i++) {
if ((vals[i] = sstore_value_alloc()) == NULL) {
fprintf(stderr, "Failed to alloc sstore_value_t\n");
goto end;
}
vals[i]->sv_type = SSTORE_VALUE_NUMBER;
}
/* Update the stats every second */
for (i = 0;; i++) {
char *id, *desc;
for (j = 0; j < NUM_STATS; j++) {
vals[j]->sv_value.num += j + 1;
}
if (sstore_data_update(hdl, ssids, NUM_STATS,
vals) != ESSTORE_OK) {
fprintf(stderr, "Failed to update stats. "
"Reason: %s\n", sstore_err_description(hdl));
break;
}
/* Check warnings */
while (sstore_warning_next(hdl, &id,
&desc) != SS_WARN_OK) {
fprintf(stderr, "failed to update stat for %s "
"because %s\n", id, desc);
}
sleep(1);
}
end:
for (i = 0; i < NUM_STATS; i++) {
sstore_value_free(vals[i]);
}
sstore_free(hdl);
return (0);
}Compile the application:
$ cc -lsstore -o data_update data_update.c