Modify the Application to Save Statistic Values for Each Resource

The util2.c file is the same as the util1.c file except for the description at the top of the file and the following changes:

  • Change NUM_STATS from 3 to 9.

  • The ids array has two changes:

    • Change util1 to util2 in the class name of each SSID.

    • Add the three resource instances, each with all three statistics.

  • The while() loop updates nine statistics instead of three.

Note that the stats structure is unchanged and the sstore_data_attach() call is unchanged.

/*
 * Sample program to use sstore_data_attach() to provide values for
 * statistics of statically allocated resources.
 */

#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   9

/* libsstore handle */
sstore_handle_t hdl;

/* statistic identifiers */
char *ids[NUM_STATS] = {
    "//:class.app/util2//:res.inst/Customers//:stat.reads",
    "//:class.app/util2//:res.inst/Customers//:stat.writes",
    "//:class.app/util2//:res.inst/Customers//:stat.errors",
    "//:class.app/util2//:res.inst/Products//:stat.reads",
    "//:class.app/util2//:res.inst/Products//:stat.writes",
    "//:class.app/util2//:res.inst/Products//:stat.errors",
    "//:class.app/util2//:res.inst/Orders//:stat.reads",
    "//:class.app/util2//:res.inst/Orders//:stat.writes",
    "//:class.app/util2//:res.inst/Orders//: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[0].reads += rand() % 6;
stats[0].writes += rand() % 4;
stats[0].errors += rand() % 2;
stats[1].reads += rand() % 8;
stats[1].writes += rand() % 4;
stats[1].errors += rand() % 2;
stats[2].reads += rand() % 9;
stats[2].writes += rand() % 5;
stats[2].errors += rand() % 2;
        sleep(1);
    }

    /*
     * Free the libsstore handle.
     * The statistics are marked as not being actively provided.
     */
    sstore_free(hdl);

    return (0);
}