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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
use {
crate::{deprecated_fidl_server::*, table::*},
anyhow::{format_err, Error},
fuchsia_component::server::ServiceFs,
fuchsia_inspect::{self as inspect, ArithmeticArrayProperty, ArrayProperty},
futures::{FutureExt, StreamExt},
std::ops::AddAssign,
structopt::StructOpt,
};
mod deprecated_fidl_server;
mod table;
struct PopulateParams<T> {
floor: T,
step: T,
count: usize,
}
fn populated<H: inspect::HistogramProperty>(histogram: H, params: PopulateParams<H::Type>) -> H
where
H::Type: AddAssign + Copy,
{
let mut value = params.floor;
for _ in 0..params.count {
histogram.insert(value);
value += params.step;
}
histogram
}
#[derive(Debug, StructOpt)]
#[structopt(
name = "example",
about = "Example component to showcase Inspect API objects, including an NxM nested table"
)]
struct Options {
#[structopt(long)]
rows: usize,
#[structopt(long)]
columns: usize,
#[structopt(long = "only-new")]
only_new: bool,
#[structopt(long = "extra-number")]
extra_number: Option<i64>,
}
#[fuchsia::main]
async fn main() -> Result<(), Error> {
let opts = Options::from_args();
if opts.rows == 0 || opts.columns == 0 {
Options::clap().print_help()?;
std::process::exit(1);
}
let inspector = inspect::Inspector::default();
let root = inspector.root();
assert!(inspector.is_valid());
reset_unique_names();
let table_node_name = unique_name("table");
let example_table =
Table::new(opts.rows, opts.columns, &table_node_name, root.create_child(&table_node_name));
let int_array = root.create_int_array(unique_name("array"), 3);
int_array.set(0, 1);
int_array.add(1, 10);
int_array.subtract(2, 3);
let uint_array = root.create_uint_array(unique_name("array"), 3);
uint_array.set(0, 1u64);
uint_array.add(1, 10);
uint_array.set(2, 3u64);
uint_array.subtract(2, 1);
let double_array = root.create_double_array(unique_name("array"), 3);
double_array.set(0, 0.25);
double_array.add(1, 1.25);
double_array.subtract(2, 0.75);
let _int_linear_hist = populated(
root.create_int_linear_histogram(
unique_name("histogram"),
inspect::LinearHistogramParams { floor: -10, step_size: 5, buckets: 3 },
),
PopulateParams { floor: -20, step: 1, count: 40 },
);
let _uint_linear_hist = populated(
root.create_uint_linear_histogram(
unique_name("histogram"),
inspect::LinearHistogramParams { floor: 5, step_size: 5, buckets: 3 },
),
PopulateParams { floor: 0, step: 1, count: 40 },
);
let _double_linear_hist = populated(
root.create_double_linear_histogram(
unique_name("histogram"),
inspect::LinearHistogramParams { floor: 0.0, step_size: 0.5, buckets: 3 },
),
PopulateParams { floor: -1.0, step: 0.1, count: 40 },
);
let _int_exp_hist = populated(
root.create_int_exponential_histogram(
unique_name("histogram"),
inspect::ExponentialHistogramParams {
floor: -10,
initial_step: 5,
step_multiplier: 2,
buckets: 3,
},
),
PopulateParams { floor: -20, step: 1, count: 40 },
);
let _uint_exp_hist = populated(
root.create_uint_exponential_histogram(
unique_name("histogram"),
inspect::ExponentialHistogramParams {
floor: 0,
initial_step: 1,
step_multiplier: 2,
buckets: 3,
},
),
PopulateParams { floor: 0, step: 1, count: 40 },
);
let _double_exp_hist = populated(
root.create_double_exponential_histogram(
unique_name("histogram"),
inspect::ExponentialHistogramParams {
floor: 0.0,
initial_step: 1.25,
step_multiplier: 3.0,
buckets: 3,
},
),
PopulateParams { floor: -1.0, step: 0.1, count: 40 },
);
root.record_lazy_child("lazy-node", || {
async move {
let inspector = inspect::Inspector::default();
inspector.root().record_uint("uint", 3);
Ok(inspector)
}
.boxed()
});
root.record_lazy_values("lazy-values", || {
async move {
let inspector = inspect::Inspector::default();
inspector.root().record_double("lazy-double", 3.25);
Ok(inspector)
}
.boxed()
});
if let Some(extra_number) = opts.extra_number {
root.record_int("extra_number", extra_number);
}
let mut fs = ServiceFs::new();
if !opts.only_new {
fs.dir("diagnostics").add_fidl_service(move |stream| {
spawn_inspect_server(stream, example_table.get_node_object());
});
}
let inspector_clone = inspector.clone();
fs.dir("diagnostics").add_fidl_service(move |stream| {
inspect_runtime::service::spawn_tree_server(
inspector_clone.clone(),
inspect_runtime::service::TreeServerSettings::default(),
stream,
)
.detach();
});
if !opts.only_new {
inspector
.duplicate_vmo()
.ok_or(format_err!("Failed to duplicate VMO"))
.and_then(|vmo| {
fs.dir("diagnostics").add_vmo_file_at("root.inspect", vmo);
Ok(())
})
.unwrap_or_else(|e| {
eprintln!("Failed to expose vmo. Error: {:?}", e);
});
}
fs.take_and_serve_directory_handle()?;
Ok(fs.collect().await)
}