xref: /aosp_15_r20/external/pigweed/pw_metric/metric_service_nanopb.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker // Copyright 2020 The Pigweed Authors
2*61c4878aSAndroid Build Coastguard Worker //
3*61c4878aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4*61c4878aSAndroid Build Coastguard Worker // use this file except in compliance with the License. You may obtain a copy of
5*61c4878aSAndroid Build Coastguard Worker // the License at
6*61c4878aSAndroid Build Coastguard Worker //
7*61c4878aSAndroid Build Coastguard Worker //     https://www.apache.org/licenses/LICENSE-2.0
8*61c4878aSAndroid Build Coastguard Worker //
9*61c4878aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*61c4878aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11*61c4878aSAndroid Build Coastguard Worker // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12*61c4878aSAndroid Build Coastguard Worker // License for the specific language governing permissions and limitations under
13*61c4878aSAndroid Build Coastguard Worker // the License.
14*61c4878aSAndroid Build Coastguard Worker 
15*61c4878aSAndroid Build Coastguard Worker #include "pw_metric/metric_service_nanopb.h"
16*61c4878aSAndroid Build Coastguard Worker 
17*61c4878aSAndroid Build Coastguard Worker #include <cstring>
18*61c4878aSAndroid Build Coastguard Worker 
19*61c4878aSAndroid Build Coastguard Worker #include "pw_assert/check.h"
20*61c4878aSAndroid Build Coastguard Worker #include "pw_containers/vector.h"
21*61c4878aSAndroid Build Coastguard Worker #include "pw_metric/metric.h"
22*61c4878aSAndroid Build Coastguard Worker #include "pw_metric_private/metric_walker.h"
23*61c4878aSAndroid Build Coastguard Worker #include "pw_preprocessor/util.h"
24*61c4878aSAndroid Build Coastguard Worker #include "pw_span/span.h"
25*61c4878aSAndroid Build Coastguard Worker 
26*61c4878aSAndroid Build Coastguard Worker namespace pw::metric {
27*61c4878aSAndroid Build Coastguard Worker namespace {
28*61c4878aSAndroid Build Coastguard Worker 
29*61c4878aSAndroid Build Coastguard Worker class NanopbMetricWriter : public virtual internal::MetricWriter {
30*61c4878aSAndroid Build Coastguard Worker  public:
NanopbMetricWriter(MetricService::ServerWriter<pw_metric_proto_MetricResponse> & response_writer)31*61c4878aSAndroid Build Coastguard Worker   NanopbMetricWriter(
32*61c4878aSAndroid Build Coastguard Worker       MetricService::ServerWriter<pw_metric_proto_MetricResponse>&
33*61c4878aSAndroid Build Coastguard Worker           response_writer)
34*61c4878aSAndroid Build Coastguard Worker       : response_(pw_metric_proto_MetricResponse_init_zero),
35*61c4878aSAndroid Build Coastguard Worker         response_writer_(response_writer) {}
36*61c4878aSAndroid Build Coastguard Worker 
37*61c4878aSAndroid Build Coastguard Worker   // TODO(keir): Figure out a pw_rpc mechanism to fill a streaming packet based
38*61c4878aSAndroid Build Coastguard Worker   // on transport MTU, rather than having this as a static knob. For example,
39*61c4878aSAndroid Build Coastguard Worker   // some transports may be able to fit 30 metrics; others, only 5.
Write(const Metric & metric,const Vector<Token> & path)40*61c4878aSAndroid Build Coastguard Worker   Status Write(const Metric& metric, const Vector<Token>& path) override {
41*61c4878aSAndroid Build Coastguard Worker     // Nanopb doesn't offer an easy way to do bounds checking, so use span's
42*61c4878aSAndroid Build Coastguard Worker     // type deduction magic to figure out the max size.
43*61c4878aSAndroid Build Coastguard Worker     span<pw_metric_proto_Metric> metrics(response_.metrics);
44*61c4878aSAndroid Build Coastguard Worker     PW_CHECK_INT_LT(response_.metrics_count, metrics.size());
45*61c4878aSAndroid Build Coastguard Worker 
46*61c4878aSAndroid Build Coastguard Worker     // Grab the next available Metric slot to write to in the response.
47*61c4878aSAndroid Build Coastguard Worker     pw_metric_proto_Metric& proto_metric =
48*61c4878aSAndroid Build Coastguard Worker         response_.metrics[response_.metrics_count];
49*61c4878aSAndroid Build Coastguard Worker 
50*61c4878aSAndroid Build Coastguard Worker     // Copy the path.
51*61c4878aSAndroid Build Coastguard Worker     span<Token> proto_path(proto_metric.token_path);
52*61c4878aSAndroid Build Coastguard Worker     PW_CHECK_INT_LE(path.size(), proto_path.size());
53*61c4878aSAndroid Build Coastguard Worker     std::copy(path.begin(), path.end(), proto_path.begin());
54*61c4878aSAndroid Build Coastguard Worker     proto_metric.token_path_count = path.size();
55*61c4878aSAndroid Build Coastguard Worker 
56*61c4878aSAndroid Build Coastguard Worker     // Copy the metric value.
57*61c4878aSAndroid Build Coastguard Worker     if (metric.is_float()) {
58*61c4878aSAndroid Build Coastguard Worker       proto_metric.value.as_float = metric.as_float();
59*61c4878aSAndroid Build Coastguard Worker       proto_metric.which_value = pw_metric_proto_Metric_as_float_tag;
60*61c4878aSAndroid Build Coastguard Worker     } else {
61*61c4878aSAndroid Build Coastguard Worker       proto_metric.value.as_int = metric.as_int();
62*61c4878aSAndroid Build Coastguard Worker       proto_metric.which_value = pw_metric_proto_Metric_as_int_tag;
63*61c4878aSAndroid Build Coastguard Worker     }
64*61c4878aSAndroid Build Coastguard Worker 
65*61c4878aSAndroid Build Coastguard Worker     // Move write head to the next slot.
66*61c4878aSAndroid Build Coastguard Worker     response_.metrics_count++;
67*61c4878aSAndroid Build Coastguard Worker 
68*61c4878aSAndroid Build Coastguard Worker     // If the metric response object is full, send the response and reset.
69*61c4878aSAndroid Build Coastguard Worker     // TODO(keir): Support runtime batch sizes < max proto size.
70*61c4878aSAndroid Build Coastguard Worker     if (response_.metrics_count == metrics.size()) {
71*61c4878aSAndroid Build Coastguard Worker       Flush();
72*61c4878aSAndroid Build Coastguard Worker     }
73*61c4878aSAndroid Build Coastguard Worker 
74*61c4878aSAndroid Build Coastguard Worker     return OkStatus();
75*61c4878aSAndroid Build Coastguard Worker   }
76*61c4878aSAndroid Build Coastguard Worker 
Flush()77*61c4878aSAndroid Build Coastguard Worker   void Flush() {
78*61c4878aSAndroid Build Coastguard Worker     if (response_.metrics_count) {
79*61c4878aSAndroid Build Coastguard Worker       response_writer_.Write(response_)
80*61c4878aSAndroid Build Coastguard Worker           .IgnoreError();  // TODO: b/242598609 - Handle Status properly
81*61c4878aSAndroid Build Coastguard Worker       response_ = pw_metric_proto_MetricResponse_init_zero;
82*61c4878aSAndroid Build Coastguard Worker     }
83*61c4878aSAndroid Build Coastguard Worker   }
84*61c4878aSAndroid Build Coastguard Worker 
85*61c4878aSAndroid Build Coastguard Worker  private:
86*61c4878aSAndroid Build Coastguard Worker   pw_metric_proto_MetricResponse response_;
87*61c4878aSAndroid Build Coastguard Worker   // This RPC stream writer handle must be valid for the metric writer lifetime.
88*61c4878aSAndroid Build Coastguard Worker   MetricService::ServerWriter<pw_metric_proto_MetricResponse>& response_writer_;
89*61c4878aSAndroid Build Coastguard Worker };
90*61c4878aSAndroid Build Coastguard Worker 
91*61c4878aSAndroid Build Coastguard Worker }  // namespace
92*61c4878aSAndroid Build Coastguard Worker 
Get(const pw_metric_proto_MetricRequest &,ServerWriter<pw_metric_proto_MetricResponse> & response)93*61c4878aSAndroid Build Coastguard Worker void MetricService::Get(
94*61c4878aSAndroid Build Coastguard Worker     const pw_metric_proto_MetricRequest& /* request */,
95*61c4878aSAndroid Build Coastguard Worker     ServerWriter<pw_metric_proto_MetricResponse>& response) {
96*61c4878aSAndroid Build Coastguard Worker   // For now, ignore the request and just stream all the metrics back.
97*61c4878aSAndroid Build Coastguard Worker   NanopbMetricWriter writer(response);
98*61c4878aSAndroid Build Coastguard Worker   internal::MetricWalker walker(writer);
99*61c4878aSAndroid Build Coastguard Worker 
100*61c4878aSAndroid Build Coastguard Worker   // This will stream all the metrics in the span of this Get() method call.
101*61c4878aSAndroid Build Coastguard Worker   // This will have the effect of blocking the RPC thread until all the metrics
102*61c4878aSAndroid Build Coastguard Worker   // are sent. That is likely to cause problems if there are many metrics, or
103*61c4878aSAndroid Build Coastguard Worker   // if other RPCs are higher priority and should complete first.
104*61c4878aSAndroid Build Coastguard Worker   //
105*61c4878aSAndroid Build Coastguard Worker   // In the future, this should be replaced with an optional async solution
106*61c4878aSAndroid Build Coastguard Worker   // that puts the application in control of when the response batches are sent.
107*61c4878aSAndroid Build Coastguard Worker   walker.Walk(metrics_).IgnoreError();
108*61c4878aSAndroid Build Coastguard Worker   walker.Walk(groups_).IgnoreError();
109*61c4878aSAndroid Build Coastguard Worker   writer.Flush();
110*61c4878aSAndroid Build Coastguard Worker }
111*61c4878aSAndroid Build Coastguard Worker 
112*61c4878aSAndroid Build Coastguard Worker }  // namespace pw::metric
113