1 /*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "src/traced/probes/statsd_client/common.h"
18 #include "perfetto/protozero/scattered_heap_buffer.h"
19 #include "perfetto/tracing/core/data_source_config.h"
20
21 #include "protos/perfetto/config/statsd/statsd_tracing_config.pbzero.h"
22 #include "protos/perfetto/trace/statsd/statsd_atom.pbzero.h"
23 #include "protos/perfetto/trace/trace_packet.pbzero.h"
24 #include "protos/third_party/statsd/shell_config.pbzero.h"
25
26 using ::perfetto::protos::pbzero::StatsdPullAtomConfig;
27 using ::perfetto::protos::pbzero::StatsdShellSubscription;
28 using ::perfetto::protos::pbzero::StatsdTracingConfig;
29
30 namespace perfetto {
31 namespace {
32
AddPullAtoms(const StatsdPullAtomConfig::Decoder & cfg,protozero::RepeatedFieldIterator<int32_t> it,StatsdShellSubscription * msg)33 void AddPullAtoms(const StatsdPullAtomConfig::Decoder& cfg,
34 protozero::RepeatedFieldIterator<int32_t> it,
35 StatsdShellSubscription* msg) {
36 constexpr int32_t kDefaultPullFreqMs = 5000;
37 int32_t pull_freq_ms = kDefaultPullFreqMs;
38 if (cfg.has_pull_frequency_ms()) {
39 pull_freq_ms = cfg.pull_frequency_ms();
40 }
41
42 for (; it; ++it) {
43 auto* pulled_msg = msg->add_pulled();
44 pulled_msg->set_freq_millis(pull_freq_ms);
45
46 for (auto package = cfg.packages(); package; ++package) {
47 pulled_msg->add_packages(*package);
48 }
49
50 auto* matcher_msg = pulled_msg->set_matcher();
51 matcher_msg->set_atom_id(*it);
52 }
53 }
54
AddPushAtoms(protozero::RepeatedFieldIterator<int32_t> it,StatsdShellSubscription * msg)55 void AddPushAtoms(protozero::RepeatedFieldIterator<int32_t> it,
56 StatsdShellSubscription* msg) {
57 for (; it; ++it) {
58 auto* matcher_msg = msg->add_pushed();
59 matcher_msg->set_atom_id(*it);
60 }
61 }
62
63 } // namespace
64
CreateStatsdShellConfig(const DataSourceConfig & config)65 std::string CreateStatsdShellConfig(const DataSourceConfig& config) {
66 StatsdTracingConfig::Decoder cfg(config.statsd_tracing_config_raw());
67 protozero::HeapBuffered<StatsdShellSubscription> msg;
68 for (auto pull_it = cfg.pull_config(); pull_it; ++pull_it) {
69 StatsdPullAtomConfig::Decoder pull_cfg(*pull_it);
70 AddPullAtoms(pull_cfg, pull_cfg.raw_pull_atom_id(), msg.get());
71 AddPullAtoms(pull_cfg, pull_cfg.pull_atom_id(), msg.get());
72 }
73 AddPushAtoms(cfg.push_atom_id(), msg.get());
74 AddPushAtoms(cfg.raw_push_atom_id(), msg.get());
75 return msg.SerializeAsString();
76 }
77
78 } // namespace perfetto
79