1d4afb5ceSopenharmony_ci/*
2d4afb5ceSopenharmony_ci * lws System Metrics
3d4afb5ceSopenharmony_ci *
4d4afb5ceSopenharmony_ci * Copyright (C) 2021 Andy Green <andy@warmcat.com>
5d4afb5ceSopenharmony_ci *
6d4afb5ceSopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy
7d4afb5ceSopenharmony_ci * of this software and associated documentation files (the "Software"), to
8d4afb5ceSopenharmony_ci * deal in the Software without restriction, including without limitation the
9d4afb5ceSopenharmony_ci * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10d4afb5ceSopenharmony_ci * sell copies of the Software, and to permit persons to whom the Software is
11d4afb5ceSopenharmony_ci * furnished to do so, subject to the following conditions:
12d4afb5ceSopenharmony_ci *
13d4afb5ceSopenharmony_ci * The above copyright notice and this permission notice shall be included in
14d4afb5ceSopenharmony_ci * all copies or substantial portions of the Software.
15d4afb5ceSopenharmony_ci *
16d4afb5ceSopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17d4afb5ceSopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18d4afb5ceSopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19d4afb5ceSopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20d4afb5ceSopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21d4afb5ceSopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22d4afb5ceSopenharmony_ci * IN THE SOFTWARE.
23d4afb5ceSopenharmony_ci */
24d4afb5ceSopenharmony_ci
25d4afb5ceSopenharmony_ci/*
26d4afb5ceSopenharmony_ci * Const struct that describes a policy for processing raw metrics to turn them
27d4afb5ceSopenharmony_ci * into events.
28d4afb5ceSopenharmony_ci *
29d4afb5ceSopenharmony_ci * Typically although we want to monitor every event, the data produced can be
30d4afb5ceSopenharmony_ci * too large, and many events that are "normal" just need to be counted as such;
31d4afb5ceSopenharmony_ci * outliers or change-to-continuous outliers may deserve closer recording as
32d4afb5ceSopenharmony_ci * events in their own right.
33d4afb5ceSopenharmony_ci *
34d4afb5ceSopenharmony_ci * Mean computation must "decay" as it ages, we do this by halving the sum and
35d4afb5ceSopenharmony_ci * count after .us_decay_unit us.
36d4afb5ceSopenharmony_ci *
37d4afb5ceSopenharmony_ci * We don't acknowledge outliers until there are at least .min_contributors
38d4afb5ceSopenharmony_ci * in the current mean (which is subject to decaying)
39d4afb5ceSopenharmony_ci *
40d4afb5ceSopenharmony_ci * We decide something is an outlier event if it deviates from the mean by
41d4afb5ceSopenharmony_ci * .pc_outlier_deviation %.
42d4afb5ceSopenharmony_ci */
43d4afb5ceSopenharmony_ci
44d4afb5ceSopenharmony_ci/*
45d4afb5ceSopenharmony_ci * The dynamic counterpart for each static metric policy, this is on heap
46d4afb5ceSopenharmony_ci * one per const lws_metric_policy_t.  It's listed in context->owner_mtr_dynpol
47d4afb5ceSopenharmony_ci */
48d4afb5ceSopenharmony_ci
49d4afb5ceSopenharmony_citypedef struct lws_metric_policy_dyn {
50d4afb5ceSopenharmony_ci	const lws_metric_policy_t	*policy;
51d4afb5ceSopenharmony_ci	/**< the static part of the policy we belong to... can be NULL if no
52d4afb5ceSopenharmony_ci	 * policy matches or the policy was invalidated */
53d4afb5ceSopenharmony_ci
54d4afb5ceSopenharmony_ci	lws_dll2_owner_t		owner;
55d4afb5ceSopenharmony_ci	/**< list of metrics that are using this policy */
56d4afb5ceSopenharmony_ci
57d4afb5ceSopenharmony_ci	lws_dll2_t			list;
58d4afb5ceSopenharmony_ci	/**< context owns us */
59d4afb5ceSopenharmony_ci
60d4afb5ceSopenharmony_ci	lws_sorted_usec_list_t		sul;
61d4afb5ceSopenharmony_ci	/**< schedule periodic reports for metrics using this policy */
62d4afb5ceSopenharmony_ci} lws_metric_policy_dyn_t;
63d4afb5ceSopenharmony_ci
64d4afb5ceSopenharmony_ci/*
65d4afb5ceSopenharmony_ci * A metrics private part, encapsulating the public part
66d4afb5ceSopenharmony_ci */
67d4afb5ceSopenharmony_ci
68d4afb5ceSopenharmony_citypedef struct lws_metric {
69d4afb5ceSopenharmony_ci
70d4afb5ceSopenharmony_ci	lws_dll2_t			list;
71d4afb5ceSopenharmony_ci	/**< owned by either 1) ctx.lws_metric_policy_dyn_t.owner, or
72d4afb5ceSopenharmony_ci	 * 2) ctx.owner_mtr_no_pol */
73d4afb5ceSopenharmony_ci
74d4afb5ceSopenharmony_ci	struct lws_context		*ctx;
75d4afb5ceSopenharmony_ci
76d4afb5ceSopenharmony_ci	/* public part overallocated */
77d4afb5ceSopenharmony_ci} lws_metric_t;
78d4afb5ceSopenharmony_ci
79d4afb5ceSopenharmony_ci
80d4afb5ceSopenharmony_ci#if defined(LWS_WITH_SYS_METRICS)
81d4afb5ceSopenharmony_ci#define lws_metrics_hist_bump_priv(_mt, _name) \
82d4afb5ceSopenharmony_ci		lws_metrics_hist_bump_(lws_metrics_priv_to_pub(_mt), _name)
83d4afb5ceSopenharmony_ci#define lws_metrics_hist_bump_priv_wsi(_wsi, _hist, _name) \
84d4afb5ceSopenharmony_ci		lws_metrics_hist_bump_(lws_metrics_priv_to_pub(_wsi->a.context->_hist), _name)
85d4afb5ceSopenharmony_ci#define lws_metrics_hist_bump_priv_ss(_ss, _hist, _name) \
86d4afb5ceSopenharmony_ci		lws_metrics_hist_bump_(lws_metrics_priv_to_pub(_ss->context->_hist), _name)
87d4afb5ceSopenharmony_ci#define lws_metrics_priv_to_pub(_x) ((lws_metric_pub_t *)&(_x)[1])
88d4afb5ceSopenharmony_ci#else
89d4afb5ceSopenharmony_ci#define lws_metrics_hist_bump_priv(_mt, _name)
90d4afb5ceSopenharmony_ci#define lws_metrics_hist_bump_priv_wsi(_wsi, _hist, _name)
91d4afb5ceSopenharmony_ci#define lws_metrics_hist_bump_priv_ss(_ss, _hist, _name)
92d4afb5ceSopenharmony_ci#define lws_metrics_priv_to_pub(_x) ((lws_metric_pub_t *)NULL)
93d4afb5ceSopenharmony_ci#endif
94d4afb5ceSopenharmony_ci
95d4afb5ceSopenharmony_ci#if defined(LWS_WITH_SECURE_STREAMS_PROXY_API)
96d4afb5ceSopenharmony_ci/*
97d4afb5ceSopenharmony_ci * sspc-specific version that also appends the tag value to the lifecycle tag
98d4afb5ceSopenharmony_ci * used for logging the sspc identity
99d4afb5ceSopenharmony_ci */
100d4afb5ceSopenharmony_ciint
101d4afb5ceSopenharmony_cilws_metrics_tag_sspc_add(struct lws_sspc_handle *ss, const char *name, const char *val);
102d4afb5ceSopenharmony_ci#endif
103d4afb5ceSopenharmony_ci
104d4afb5ceSopenharmony_ciint
105d4afb5ceSopenharmony_cilws_metrics_register_policy(struct lws_context *ctx,
106d4afb5ceSopenharmony_ci			    const lws_metric_policy_t *head);
107d4afb5ceSopenharmony_ci
108d4afb5ceSopenharmony_civoid
109d4afb5ceSopenharmony_cilws_metrics_destroy(struct lws_context *ctx);
110d4afb5ceSopenharmony_ci
111d4afb5ceSopenharmony_civoid
112d4afb5ceSopenharmony_cilws_metric_event(lws_metric_t *mt, char go_nogo, u_mt_t val);
113d4afb5ceSopenharmony_ci
114d4afb5ceSopenharmony_cilws_metric_t *
115d4afb5ceSopenharmony_cilws_metric_create(struct lws_context *ctx, uint8_t flags, const char *name);
116d4afb5ceSopenharmony_ci
117d4afb5ceSopenharmony_ciint
118d4afb5ceSopenharmony_cilws_metric_destroy(lws_metric_t **mt, int keep);
119d4afb5ceSopenharmony_ci
120d4afb5ceSopenharmony_civoid
121d4afb5ceSopenharmony_cilws_metric_policy_dyn_destroy(lws_metric_policy_dyn_t *dm, int keep);
122d4afb5ceSopenharmony_ci
123d4afb5ceSopenharmony_civoid
124d4afb5ceSopenharmony_cilws_metric_rebind_policies(struct lws_context *ctx);
125