Skip to content
Flecs v4.1
metrics.h
Go to the documentation of this file.
1/**
2 * @file addons/metrics.h
3 * @brief Metrics module.
4 *
5 * The metrics module extracts metrics from components and makes them available
6 * through a unified component interface.
7 */
8
9#ifdef FLECS_METRICS
10
11/**
12 * @defgroup c_addons_metrics Metrics
13 * @ingroup c_addons
14 * Collect user-defined metrics from ECS data.
15 *
16 * @{
17 */
18
19#ifndef FLECS_METRICS_H
20#define FLECS_METRICS_H
21
22#ifndef FLECS_META
23#define FLECS_META
24#endif
25
26#ifndef FLECS_UNITS
27#define FLECS_UNITS
28#endif
29
30#ifndef FLECS_PIPELINE
31#define FLECS_PIPELINE
32#endif
33
34#ifdef __cplusplus
35extern "C" {
36#endif
37
38/** Flecs metrics module. */
39FLECS_API extern ECS_COMPONENT_DECLARE(FlecsMetrics);
40
41/** Tag added to metrics, and used as the first element of the metric kind pair. */
42FLECS_API extern ECS_TAG_DECLARE(EcsMetric);
43
44/** Metric that has a monotonically increasing value. */
45FLECS_API extern ECS_TAG_DECLARE(EcsCounter);
46
47/** Counter metric that is auto-incremented by the source value. */
48FLECS_API extern ECS_TAG_DECLARE(EcsCounterIncrement);
49
50/** Counter metric that counts the number of entities with an ID. */
51FLECS_API extern ECS_TAG_DECLARE(EcsCounterId);
52
53/** Metric that represents the current value. */
54FLECS_API extern ECS_TAG_DECLARE(EcsGauge);
55
56/** Tag added to metric instances. */
57FLECS_API extern ECS_TAG_DECLARE(EcsMetricInstance);
58
59/** Component with metric instance value. */
61
62/** Component with entity source of metric instance. */
64
65/** Component that stores metric value. */
66typedef struct EcsMetricValue {
67 double value;
69
70/** Component that stores metric source. */
71typedef struct EcsMetricSource {
72 ecs_entity_t entity;
74
75/** Used with ecs_metric_init() to create metric. */
76typedef struct ecs_metric_desc_t {
77 int32_t _canary; /**< Used for validity testing. Do not set. */
78
79 /** Entity associated with metric. */
81
82 /** Entity associated with member that stores metric value. Must not be set
83 * at the same time as id. Cannot be combined with EcsCounterId. */
85
86 /** Member dot expression. Can be used instead of member and supports nested
87 * members. Must be set together with id and should not be set at the same
88 * time as member. */
89 const char *dotmember;
90
91 /** Tracks whether entities have the specified component ID. Must not be set
92 * at the same time as member. */
94
95 /** If id is a (R, *) wildcard and relationship R has the OneOf property,
96 * setting this value to true will track individual targets.
97 * If the kind is EcsCounterId and the id is a (R, *) wildcard, this value
98 * will create a metric per target. */
99 bool targets;
100
101 /** Must be EcsGauge, EcsCounter, EcsCounterIncrement, or EcsCounterId. */
103
104 /** Description of metric. Will only be set if FLECS_DOC addon is enabled. */
105 const char *brief;
107
108/** Create a new metric.
109 * Metrics are entities that store values measured from a range of different
110 * properties in the ECS storage. Metrics provide a single unified interface to
111 * discovering and reading these values, which can be useful for monitoring
112 * utilities, or for debugging.
113 *
114 * Examples of properties that can be measured by metrics are:
115 * - Component member values
116 * - How long an entity has had a specific component
117 * - How long an entity has had a specific target for a relationship
118 * - How many entities have a specific component
119 *
120 * Metrics can either be created as a "gauge" or "counter". A gauge is a metric
121 * that represents the value of something at a specific point in time, for
122 * example "velocity". A counter metric represents a value that is monotonically
123 * increasing, for example "miles driven".
124 *
125 * There are three different counter metric kinds:
126 * - EcsCounter
127 * When combined with a member, this will store the actual value of the member
128 * in the metric. This is useful for values that are already counters, such as
129 * a MilesDriven component.
130 * This kind creates a metric per entity that has the member or ID.
131 *
132 * - EcsCounterIncrement
133 * When combined with a member, this will increment the value of the metric by
134 * the value of the member * delta_time. This is useful for values that are
135 * not counters, such as a Velocity component.
136 * This kind creates a metric per entity that has the member.
137 *
138 * - EcsCounterId
139 * This metric kind will count the number of entities with a specific
140 * (component) ID. This kind creates a single metric instance for regular IDs,
141 * and a metric instance per target for wildcard IDs when targets is set.
142 *
143 * @param world The world.
144 * @param desc Metric description.
145 * @return The metric entity.
146 */
147FLECS_API
149 ecs_world_t *world,
150 const ecs_metric_desc_t *desc);
151
152/** Shorthand for creating a metric with ecs_metric_init().
153 *
154 * Example:
155 *
156 * @code
157 * ecs_metric(world, {
158 * .member = ecs_lookup(world, "Position.x")
159 * .kind = EcsGauge
160 * });
161 * @endcode
162 */
163#define ecs_metric(world, ...)\
164 ecs_metric_init(world, &(ecs_metric_desc_t) __VA_ARGS__ )
165
166/** Metrics module import function.
167 * Usage:
168 * @code
169 * ECS_IMPORT(world, FlecsMetrics)
170 * @endcode
171 *
172 * @param world The world.
173 */
174FLECS_API
176 ecs_world_t *world);
177
178#ifdef __cplusplus
179}
180#endif
181
182#endif
183
184/** @} */
185
186#endif
FLECS_API ecs_entity_t ecs_metric_init(ecs_world_t *world, const ecs_metric_desc_t *desc)
Create a new metric.
FLECS_API void FlecsMetricsImport(ecs_world_t *world)
Metrics module import function.
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:395
struct ecs_world_t ecs_world_t
A world is the container for all ECS data and supporting features.
Definition flecs.h:439
uint64_t ecs_id_t
IDs are the things that can be added to an entity.
Definition flecs.h:388
#define ECS_TAG_DECLARE
Forward declare a tag.
Definition flecs_c.h:29
#define ECS_COMPONENT_DECLARE(id)
Forward declare a component.
Definition flecs_c.h:65
Component that stores metric source.
Definition metrics.h:71
Component that stores metric value.
Definition metrics.h:66
Used with ecs_metric_init() to create metric.
Definition metrics.h:76
const char * brief
Description of metric.
Definition metrics.h:105
ecs_entity_t member
Entity associated with member that stores metric value.
Definition metrics.h:84
ecs_entity_t kind
Must be EcsGauge, EcsCounter, EcsCounterIncrement, or EcsCounterId.
Definition metrics.h:102
int32_t _canary
Used for validity testing.
Definition metrics.h:77
const char * dotmember
Member dot expression.
Definition metrics.h:89
ecs_entity_t entity
Entity associated with metric.
Definition metrics.h:80
bool targets
If id is a (R, *) wildcard and relationship R has the OneOf property, setting this value to true will...
Definition metrics.h:99
ecs_id_t id
Tracks whether entities have the specified component ID.
Definition metrics.h:93