summaryrefslogtreecommitdiff
path: root/service/include/sc_metrics.h
blob: 42605649ee28731534aa7d158524d9b64dbcd495 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
 * @file sc_metrics.h
 * @brief Header file for SC metrics handle management.
 *
 * This header file defines the data structures and function prototypes for managing
 * SC metrics handles in the system. The SC metrics handle is used to store and process
 * metric values, with functions provided for creating, deleting, and manipulating
 * these handles. Key operations include setting, resetting, and accumulating metric values.
 *
 * The following data structures and functions are defined:
 *
 * Data Structures:
 * - `struct sc_metrics_handle`: Represents a metrics handle with a capacity and a pointer
 *   to an array of metric values.
 *
 * Functions:
 * - `sc_metrics_handle_create(uint16_t capacity)`: Allocates and initializes a new SC metrics
 *   handle with the specified capacity, which is rounded up to the nearest multiple of 8.
 *
 * - `sc_metrics_handle_delete(struct sc_metrics_handle * metrics_handle)`: Deallocates the memory
 *   associated with an SC metrics handle, including the values array.
 *
 * - `sc_metrics_values_set(struct sc_metrics_handle * metrics_handle, uint64_t values[], uint16_t capacity)`:
 *   Sets the metric values in the handle. The provided capacity must not exceed the handle's allocated
 *   capacity.
 *
 * - `sc_metrics_values_reset(struct sc_metrics_handle * metrics_handle)`: Resets all metric values
 *   in the handle to zero.
 *
 * - `sc_metrics_accumulate(struct sc_metrics_handle * metrics_handle, uint64_t inc_values[], uint16_t capacity)`:
 *   Accumulates new values into the existing metric values within the handle. The provided capacity
 *   must not exceed the handle's allocated capacity.
 */

#pragma once

#include <common.h>

#define SC_METRICS_MAX_CAPACITY 32

struct sc_metrics_handle
{
    uint16_t capacity;
    volatile uint64_t * values;
};

struct sc_metrics_handle * sc_metrics_handle_create(uint16_t capacity);
int sc_metrics_handle_delete(struct sc_metrics_handle * metrics_handle);
int sc_metrics_value_get(struct sc_metrics_handle * metrics_handle, uint64_t * value, uint16_t key);
int sc_metrics_values_get(struct sc_metrics_handle * metrics_handle, uint64_t values[], uint16_t capacity);
static inline int sc_metrics_value_set(struct sc_metrics_handle * metrics_handle, uint64_t value, uint16_t key);
int sc_metrics_values_set(struct sc_metrics_handle * metrics_handle, uint64_t values[], uint16_t capacity);
int sc_metrics_values_reset(struct sc_metrics_handle * metrics_handle);
int sc_metrics_accumulate(struct sc_metrics_handle * metrics_handle, uint64_t inc_values[], uint16_t capacity);

/**
 * @brief Set a single value for the SC metrics handle.
 *
 * This function sets a single metric value in the SC metrics handle at the
 * specified key. The function ensures that the key is within the handle's
 * allocated capacity. The value is directly written to the handle's values
 * array at the specified key.
 *
 * @param metrics_handle Pointer to the SC metrics handle where the value will
 *                       be set. This handle must be valid and initialized.
 * @param value The metric value to set in the handle.
 * @param key The key at which to set the metric value. This key must be within
 *            the handle's allocated capacity.
 *
 * @return Return value indicating success:
 *         - RT_SUCCESS (0) if the value is successfully set.
 */
static inline int sc_metrics_value_set(struct sc_metrics_handle * metrics_handle, uint64_t value, uint16_t key)
{
    assert(metrics_handle != NULL);
    assert(metrics_handle->values != NULL);
    assert(key < metrics_handle->capacity);

    metrics_handle->values[key] = value;

    return RT_SUCCESS;
}