metrics机制

若要启用metrics机制,必须在launch文件里添加参数-collect_metrics true,这个本质也是gflag

源码的顺序如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// node.cc中
if (collect_metrics)
{
metrics_registry_ = absl::make_unique<metrics::FamilyFactory>();
carto::metrics::RegisterAllMetrics(metrics_registry_.get());
}


// register.cc 中定义
void RegisterAllMetrics(FamilyFactory* registry) {
mapping::constraints::ConstraintBuilder2D::RegisterMetrics(registry);
mapping::GlobalTrajectoryBuilderRegisterMetrics(registry);
mapping::LocalTrajectoryBuilder2D::RegisterMetrics(registry);
mapping::PoseGraph2D::RegisterMetrics(registry);
sensor::TrajectoryCollator::RegisterMetrics(registry);
// 不需要可以注释掉
mapping::constraints::ConstraintBuilder3D::RegisterMetrics(registry);
mapping::LocalTrajectoryBuilder3D::RegisterMetrics(registry);
mapping::PoseGraph3D::RegisterMetrics(registry);
}

以上所有RegisterMetricslocal_trajectory_builder_2d.cc, pose_graph_2d.cc, constraint_builder_2d.cc(排除3D情况)定义了所有metrics参数


kLocalSlamCpuRealTimeRatio为例进行说明, 在local_trajectory_builder_2d.cc开始部分的声明:

1
static auto* kLocalSlamCpuRealTimeRatio = metrics::Gauge::Null();`

然后在LocalTrajectoryBuilder2D::RegisterMetrics

1
2
3
4
5
auto* cpu_real_time_ratio = family_factory->NewGaugeFamily(
"mapping_2d_local_trajectory_builder_cpu_real_time_ratio",
"sensor duration / cpu duration.");

kLocalSlamCpuRealTimeRatio = cpu_real_time_ratio->Add({});

最后是赋值部分

1
2
3
4
5
6
7
8
if (last_thread_cpu_time_seconds_.has_value()) {
const double thread_cpu_duration_seconds =
thread_cpu_time_seconds - last_thread_cpu_time_seconds_.value();
if (sensor_duration.has_value()) {
kLocalSlamCpuRealTimeRatio->Set(
common::ToSeconds(sensor_duration.value()) / thread_cpu_duration_seconds );
}
}

如果想查看metrics,只能rosservice call /read_metrics "{}",一次调用的结果部分:

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
name: "mapping_constraints_constraint_builder_2d_constraints"
description: "Constraints computed"
metrics:
type: 0
labels:
-
key: "matcher"
value: "searched"
-
key: "search_region"
value: "global"
value: 4206.0
counts_by_bucket: []
-
type: 0
labels:
-
key: "matcher"
value: "found"
-
key: "search_region"
value: "global"
value: 0.0
counts_by_bucket: []

-
name: "mapping_2d_pose_graph_constraints"
description: "Current number of constraints in the pose graph"
metrics:
-
type: 1
labels:
-
key: "tag"
value: "inter_submap"
-
key: "trajectory"
value: "different"
value: 0.0
counts_by_bucket: []

name: "mapping_constraints_constraint_builder_2d_num_submap_scan_matchers"
description: "Current number of constructed submap scan matchers"
metrics:
-
type: 1
labels: []
value: 18.0
counts_by_bucket: []
-
name: "mapping_2d_pose_graph_constraints"
description: "Current number of constraints in the pose graph"
metrics:
-
type: 1
labels:
-
key: "tag"
value: "inter_submap"
-
key: "trajectory"
value: "different"
value: 27.0
counts_by_bucket: []
-
type: 1
labels:
-
key: "tag"
value: "inter_submap"
-
key: "trajectory"
value: "same"
value: 10.0
counts_by_bucket: []
-
name: "mapping_constraints_constraint_builder_2d_scores"
description: "Constraint scores built"
metrics:
-
type: 2
labels:
-
key: "search_region"
value: "local"

获知不同轨迹之间的查找次数和找到的约束数目,源码在ConstraintBuilder2D::ComputeConstraint中的
1
2
kGlobalConstraintsFoundMetric->Increment();
kGlobalConstraintScoresMetric->Observe(score);

模仿源码,可以根据自己的需要添加 metrics

如果不需要3D的metrics,可以到ConstraintBuilder3D::RegisterMetrics, LocalTrajectoryBuilder3D::RegisterMetrics, PoseGraph3D::RegisterMetrics中return