Program Listing for File metrics/types.h

Return to documentation for file (include\metrics\types.h)

/*
    Copyright (c) 2018 Intel Corporation.

    This software and the related documents are Intel copyrighted materials,
    and your use of them is governed by the express license under which they
    were provided to you ("License"). Unless the License provides otherwise,
    you may not use, modify, copy, publish, distribute, disclose or transmit
    this software or the related documents without Intel's prior written
    permission.

    This software and the related documents are provided as is, with no express
    or implied warranties, other than those that are expressly stated in the
    License.
*/

#pragma once

#include "metrics/metrics-config.h"
#include "metrics/utility/enum-mask.h"

#include <cstdint>
#include <cstring>
#include <limits>
#include <string>

namespace gpa {
namespace metrics {

using MetricId = uint64_t;
using MetricValue = double;

GPA_METRICS_EXPORT
bool IsValidMetric(MetricValue v);

const MetricValue kInvalidMetricValue = std::numeric_limits<MetricValue>::quiet_NaN();

uint32_t const kInvalidMetricIndex = UINT32_MAX;
uint32_t const kInvalidGroupIndex = UINT32_MAX;
int32_t const kInvalidGroup = INT32_MAX;

enum class Result {
    kSuccess,
    kFailed,
    kNotFound,
    kIncomplete,
    kInvalidOperation,
    kInvalidParameter,
    kNoMetricsAvailable,
    kOutOfMemory,
    kUnsupported,
};

inline bool IsSuccess(Result const res)
{
    return (res == Result::kSuccess);
}

enum Category {
    kNone = 0,

    kCompute = 1 << 0,
    kGraphics = 1 << 1,
    kTimeStamp = 1 << 2,
    kUndefined = 1 << 3,

    kAll = kCompute | kGraphics | kTimeStamp | kUndefined,
    kAllDefined = kCompute | kGraphics | kTimeStamp,
    kDefault = kAll
};
ENUM_AS_BITSET(Category)


enum class Type {
    Unknown = 0,

    Percent,

    Time,

    Value,

    Count,

    Number
};

struct GPA_METRICS_CLASS_EXPORT MetricOption
{
    enum class Type {
        kDisaggregation,
        kLatency,
        kNormalizationAverage,
        kNormalizationRate,
        kNormalizationUtilization,

        kCount,
        kInvalid = Type::kCount
    };

    enum StringLength : uint16_t {
        kSymbolicNameLength = 64
    };

    // Stucture that contains a metric option value.
    struct Value
    {
        enum class Type {
            kRangeUint32,
            kRangeUint64,
            kUint32,
            kUint64,

            kCount,
            kInvalid = Type::kCount
        };

        Type type;
        union
        {
            struct
            {
                uint32_t min;
                uint32_t max;
            } rangeUint32;
            struct
            {
                uint64_t min;
                uint64_t max;
            } rangeUint64;
            uint32_t uint32;
            uint64_t uint64;
        } data;
    };

    Type type;
    char symbolicName[StringLength::kSymbolicNameLength];  //<! Symbolic name of the metric option.
    uint32_t valueCount;
    Value* pValueArray;

    bool operator==(MetricOption const& rhs) const
    {
        if (type != rhs.type ||
            strncmp(symbolicName, rhs.symbolicName, StringLength::kSymbolicNameLength) != 0 ||
            valueCount != rhs.valueCount) {
            return false;
        }

        uint32_t valuesFound = 0;
        for (uint32_t valueIndex = 0; valueIndex < valueCount; ++valueIndex) {
            for (uint32_t rhsValueIndex = 0; rhsValueIndex < rhs.valueCount; ++rhsValueIndex) {
                if (pValueArray[valueIndex].type == rhs.pValueArray[rhsValueIndex].type &&
                    pValueArray[valueIndex].data.rangeUint64.min == rhs.pValueArray[rhsValueIndex].data.rangeUint64.min &&
                    pValueArray[valueIndex].data.rangeUint64.max == rhs.pValueArray[rhsValueIndex].data.rangeUint64.max) {
                    ++valuesFound;
                    break;
                }
            }
        }

        return valuesFound == valueCount;
    }
};

struct GPA_METRICS_CLASS_EXPORT MetricInstanceOption
{
    MetricOption::Type type;
    MetricOption::Value value;
};

struct GPA_METRICS_CLASS_EXPORT MetricDesc
{
    enum StringLength : uint16_t {
        kUriLength = 256,
        kNameLength = 128,
        kDescriptionLength = 256,
        kUnitsLength = 32,
        kSymbolicNameLength = kNameLength,
        kGroupLength = 64,

        // Aliases.
        kSymbolicName = kSymbolicNameLength,  // Deprecated.
    };

    uint32_t index;
    uint32_t groupId;
    Type type;

    char uri[StringLength::kUriLength];
    char name[StringLength::kNameLength];
    char description[StringLength::kDescriptionLength];
    char units[StringLength::kUnitsLength];

    char symbolicName[StringLength::kSymbolicNameLength];  //<! Metric name in the driver
    char group[StringLength::kGroupLength];

    Category category;

    uint32_t optionCount;
    MetricOption* pOptionArray;

    bool operator==(MetricDesc const& rhs) const
    {
        if (strncmp(uri, rhs.uri, StringLength::kUriLength) != 0 ||
            optionCount != rhs.optionCount) {
            return false;
        }

        uint32_t optionsFound = 0;
        for (uint32_t optionIndex = 0; optionIndex < optionCount; ++optionIndex) {
            for (uint32_t rhsOptionIndex = 0; rhsOptionIndex < rhs.optionCount; ++rhsOptionIndex) {
                if (pOptionArray[optionIndex] == rhs.pOptionArray[rhsOptionIndex]) {
                    ++optionsFound;
                    break;
                }
            }
        }

        return optionsFound == optionCount;
    }

    bool IsNormalizedUnits() const
    {
        return type == Type::Percent;
    }
};

enum class GraphicsApi {
    kNone,
    kD3d11,
    kD3d12,
    kMetal,
    kVulkan,

    kGraphicsApiCount = kVulkan
};

GPA_METRICS_EXPORT
char const* TypeToString(Type type);

GPA_METRICS_EXPORT
char const* GraphicsApiToString(GraphicsApi graphicsApi);

}  // namespace metrics
}  // namespace gpa