examples/example.stats/example.stats/main.cpp

File members: examples/example.stats/example.stats/main.cpp

// Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved.
//
// NVIDIA CORPORATION and its licensors retain all intellectual property
// and proprietary rights in and to this software, related documentation
// and any modifications thereto. Any use, reproduction, disclosure or
// distribution of this software and related documentation without an express
// license agreement from NVIDIA CORPORATION is strictly prohibited.
//
#ifndef DOXYGEN_BUILD
#    include <carb/stats/IStats.h>

#    include <omni/core/OmniInit.h>

// example-begin carbonite-interfaces-walkthrough-host-app-globals
CARB_GLOBALS("example.stats");
// example-end carbonite-interfaces-walkthrough-host-app-globals

int main(int argc, char* argv[])
{
    carb::stats::IStats* stats;
    carb::stats::StatId ids[5];
    carb::stats::Value value;

    // example-begin carbonite-interfaces-walkthrough-acquire-interface
    OMNI_CORE_INIT(argc, argv);

    stats = carb::getCachedInterface<carb::stats::IStats>();

    if (stats == nullptr)
    {
        fputs("ERROR-> failed to acquire the IStats interface.\n", stderr);
        return EXIT_FAILURE;
    }
    // example-end carbonite-interfaces-walkthrough-acquire-interface

    // create some stats in the table.  We'll make one stat for each aggregation type.
    ids[0] = stats->addStat({ "FPS",
                              "average frame rate",
                              carb::stats::AggregationType::eAverage,
                              { carb::stats::StatType::eDouble, 0, 0.0 } });
    ids[1] = stats->addStat(
        { "peak FPS", "peak frame rate", carb::stats::AggregationType::eMax, { carb::stats::StatType::eDouble, 0, 0.0 } });
    ids[2] = stats->addStat({ "min FPS",
                              "minimum frame rate",
                              carb::stats::AggregationType::eMin,
                              { carb::stats::StatType::eDouble, INT64_MAX, 100000000.0 } });
    ids[3] = stats->addStat({ "frames",
                              "total frame count",
                              carb::stats::AggregationType::eAccumulate,
                              { carb::stats::StatType::eInt, 0, 0.0 } });
    ids[4] = stats->addStat({ "last FPS",
                              "most recent frame rate",
                              carb::stats::AggregationType::eReplace,
                              { carb::stats::StatType::eDouble, 0, 0.0 } });

    if (ids[0] == carb::stats::kBadStatId || ids[1] == carb::stats::kBadStatId || ids[2] == carb::stats::kBadStatId ||
        ids[3] == carb::stats::kBadStatId || ids[4] == carb::stats::kBadStatId)
    {
        fputs("ERROR-> failed to create one of the stats objects.", stderr);
        return EXIT_FAILURE;
    }

    // collect some values for the frame rate.  Pretend we've been collecting them once every
    // few seconds.
    puts("collecting some random statistics...\n");

    for (size_t i = 0; i < 1000; i++)
    {
        double fps;

        fps = 15.0 + ((rand() / (double)RAND_MAX) * 18.0);

        value = { carb::stats::StatType::eDouble, 0, fps };
        stats->addValue(ids[0], value);
        stats->addValue(ids[1], value);
        stats->addValue(ids[2], value);
        stats->addValue(ids[4], value);

        value = { carb::stats::StatType::eInt, 1, 0.0 };
        stats->addValue(ids[3], value);
    }

    puts("current statistics:\n");

    for (size_t i = 0; i < CARB_COUNTOF(ids); i++)
    {
        carb::stats::StatDesc desc;

        if (!stats->getValue(ids[i], desc))
        {
            printf("    failed to retrieve stat %zu.\n", i);
            continue;
        }

        switch (desc.value.type)
        {
            case carb::stats::StatType::eInt:
                printf("    %s: %" PRId64 "\n", desc.name, desc.value.intValue);
                break;

            case carb::stats::StatType::eDouble:
                printf("    %s: %g\n", desc.name, desc.value.doubleValue);
                break;

            default:
                printf("    unknown value type %zu.\n", (size_t)desc.value.type);
                break;
        }
    }

    return EXIT_SUCCESS;
}
#endif