Apollo 10.0
自动驾驶开放平台
timer_component.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 The Apollo Authors. All Rights Reserved.
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
18
20#include "cyber/timer/timer.h"
21
22namespace apollo {
23namespace cyber {
24
26
28
30 if (is_shutdown_.load()) {
31 return true;
32 }
33 return Proc();
34}
35
37 if (!config.has_name() || !config.has_interval()) {
38 AERROR << "Missing required field in config file.";
39 return false;
40 }
41 node_.reset(new Node(config.name()));
42 LoadConfigFiles(config);
43 if (!Init()) {
44 return false;
45 }
46 interval_ = config.interval();
47
48 auto role_attr = std::make_shared<proto::RoleAttributes>();
49 role_attr->set_node_name(config.name());
50 role_attr->set_channel_name(statistics::TIMER_COMPONENT_CHAN_NAME);
51 statistics::Statistics::Instance()->RegisterChanVar(*role_attr);
52
53 std::shared_ptr<TimerComponent> self =
54 std::dynamic_pointer_cast<TimerComponent>(shared_from_this());
55 auto func = [self, role_attr]() {
56 auto start_time = Time::Now().ToNanosecond();
57 self->Process();
58 auto end_time = Time::Now().ToNanosecond();
59 // sampling proc latency in microsecond
60 statistics::Statistics::Instance()->SamplingProcLatency<
61 uint64_t>(*role_attr, (end_time-start_time)/1000);
62 };
63 timer_.reset(new Timer(config.interval(), func, false));
64 timer_->Start();
65 return true;
66}
67
68void TimerComponent::Clear() { timer_.reset(); }
69
70uint32_t TimerComponent::GetInterval() const { return interval_; }
71
72} // namespace cyber
73} // namespace apollo
std::atomic< bool > is_shutdown_
std::shared_ptr< Node > node_
void LoadConfigFiles(const ComponentConfig &config)
Node is the fundamental building block of Cyber RT.
Definition node.h:44
uint64_t ToNanosecond() const
convert time to nanosecond.
Definition time.cc:83
static Time Now()
get the current time.
Definition time.cc:57
bool Initialize(const TimerComponentConfig &config) override
init the component by protobuf object.
Used to perform oneshot or periodic timing tasks
Definition timer.h:71
#define AERROR
Definition log.h:44
class register implement
Definition arena_queue.h:37