Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Pinoxchio
apollo
提交
0c13e204
A
apollo
项目概览
Pinoxchio
/
apollo
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
apollo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
0c13e204
编写于
11月 29, 2019
作者:
fengqikai1414
提交者:
fengqikai1414
12月 03, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Cyber: update timer
上级
2b4ce050
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
49 addition
and
37 deletion
+49
-37
cyber/examples/common_component_example/common_component_example.cc
...ples/common_component_example/common_component_example.cc
+0
-3
cyber/examples/common_component_example/common_component_example.h
...mples/common_component_example/common_component_example.h
+0
-1
cyber/timer/timer.cc
cyber/timer/timer.cc
+23
-19
cyber/timer/timer_task.h
cyber/timer/timer_task.h
+3
-3
cyber/timer/timing_wheel.cc
cyber/timer/timing_wheel.cc
+19
-9
cyber/timer/timing_wheel.h
cyber/timer/timing_wheel.h
+4
-2
未找到文件。
cyber/examples/common_component_example/common_component_example.cc
浏览文件 @
0c13e204
...
...
@@ -15,9 +15,6 @@
*****************************************************************************/
#include "cyber/examples/common_component_example/common_component_example.h"
#include "cyber/class_loader/class_loader.h"
#include "cyber/component/component.h"
bool
CommonComponentSample
::
Init
()
{
AINFO
<<
"Commontest component init"
;
return
true
;
...
...
cyber/examples/common_component_example/common_component_example.h
浏览文件 @
0c13e204
...
...
@@ -15,7 +15,6 @@
*****************************************************************************/
#include <memory>
#include "cyber/class_loader/class_loader.h"
#include "cyber/component/component.h"
#include "cyber/examples/proto/examples.pb.h"
...
...
cyber/timer/timer.cc
浏览文件 @
0c13e204
...
...
@@ -65,7 +65,7 @@ bool Timer::InitTimerTask() {
task_
->
callback
=
[
callback
=
this
->
timer_opt_
.
callback
,
task_weak_ptr
]()
{
auto
task
=
task_weak_ptr
.
lock
();
if
(
task
)
{
std
::
lock_guard
<
std
::
mutex
>
lg
(
task
->
m
tx_
);
std
::
lock_guard
<
std
::
mutex
>
lg
(
task
->
m
utex
);
callback
();
}
};
...
...
@@ -76,47 +76,47 @@ bool Timer::InitTimerTask() {
if
(
!
task
)
{
return
;
}
std
::
lock_guard
<
std
::
mutex
>
lg
(
task
->
m
tx_
);
std
::
lock_guard
<
std
::
mutex
>
lg
(
task
->
m
utex
);
auto
start
=
Time
::
MonoTime
().
ToNanosecond
();
callback
();
auto
end
=
Time
::
MonoTime
().
ToNanosecond
();
uint64_t
execute_time_ns
=
end
-
start
;
uint64_t
execute_time_ms
=
#if defined(__aarch64__)
::
llround
(
static_cast
<
double
>
(
execute_time_ns
)
/
1
000000
);
::
llround
(
static_cast
<
double
>
(
execute_time_ns
)
/
1
e6
);
#else
std
::
llround
(
static_cast
<
double
>
(
execute_time_ns
)
/
1
000000
);
std
::
llround
(
static_cast
<
double
>
(
execute_time_ns
)
/
1
e6
);
#endif
if
(
task
->
last_execute_time_ns
_
==
0
)
{
task
->
last_execute_time_ns
_
=
start
;
if
(
task
->
last_execute_time_ns
==
0
)
{
task
->
last_execute_time_ns
=
start
;
}
else
{
task
->
accumulated_error_ns
_
+=
start
-
task
->
last_execute_time_ns
_
-
task
->
interval_ms
*
1000000
;
task
->
accumulated_error_ns
+=
start
-
task
->
last_execute_time_ns
-
task
->
interval_ms
*
1000000
;
}
ADEBUG
<<
"start: "
<<
start
<<
"
\t
last: "
<<
task
->
last_execute_time_ns
_
ADEBUG
<<
"start: "
<<
start
<<
"
\t
last: "
<<
task
->
last_execute_time_ns
<<
"
\t
execut time:"
<<
execute_time_ms
<<
"
\t
accumulated_error_ns: "
<<
task
->
accumulated_error_ns
_
;
task
->
last_execute_time_ns
_
=
start
;
<<
"
\t
accumulated_error_ns: "
<<
task
->
accumulated_error_ns
;
task
->
last_execute_time_ns
=
start
;
if
(
execute_time_ms
>=
task
->
interval_ms
)
{
task
->
next_fire_duration_ms
=
1
;
task
->
next_fire_duration_ms
=
TIMER_RESOLUTION_MS
;
}
else
{
#if defined(__aarch64__)
int64_t
accumulated_error_ms
=
::
llround
(
#else
int64_t
accumulated_error_ms
=
std
::
llround
(
#endif
static_cast
<
double
>
(
task
->
accumulated_error_ns
_
)
/
1000000
);
if
(
static_cast
<
int64_t
>
(
task
->
interval_ms
-
execute_time_ms
-
1
)
>=
accumulated_error_ms
)
{
static_cast
<
double
>
(
task
->
accumulated_error_ns
)
/
1e6
);
if
(
static_cast
<
int64_t
>
(
task
->
interval_ms
-
execute_time_ms
-
TIMER_RESOLUTION_MS
)
>=
accumulated_error_ms
)
{
task
->
next_fire_duration_ms
=
task
->
interval_ms
-
execute_time_ms
-
accumulated_error_ms
;
}
else
{
task
->
next_fire_duration_ms
=
1
;
task
->
next_fire_duration_ms
=
TIMER_RESOLUTION_MS
;
}
ADEBUG
<<
"error ms: "
<<
accumulated_error_ms
<<
" execute time: "
<<
execute_time_ms
<<
" next fire: "
<<
task
->
next_fire_duration_ms
<<
" error ns: "
<<
task
->
accumulated_error_ns
_
;
<<
" error ns: "
<<
task
->
accumulated_error_ns
;
}
TimingWheel
::
Instance
()
->
AddTask
(
task
);
};
...
...
@@ -140,8 +140,12 @@ void Timer::Start() {
void
Timer
::
Stop
()
{
if
(
started_
.
exchange
(
false
)
&&
task_
)
{
AINFO
<<
"stop timer, the timer_id: "
<<
timer_id_
;
std
::
lock_guard
<
std
::
mutex
>
lg
(
task_
->
mtx_
);
task_
.
reset
();
// using a shared pointer to hold task_->mutex before task_ reset
auto
tmp_task
=
task_
;
{
std
::
lock_guard
<
std
::
mutex
>
lg
(
tmp_task
->
mutex
);
task_
.
reset
();
}
}
}
...
...
cyber/timer/timer_task.h
浏览文件 @
0c13e204
...
...
@@ -32,9 +32,9 @@ struct TimerTask {
uint64_t
interval_ms
=
0
;
uint64_t
remainder_interval_ms
=
0
;
uint64_t
next_fire_duration_ms
=
0
;
int64_t
accumulated_error_ns
_
=
0
;
uint64_t
last_execute_time_ns
_
=
0
;
std
::
mutex
m
tx_
;
int64_t
accumulated_error_ns
=
0
;
uint64_t
last_execute_time_ns
=
0
;
std
::
mutex
m
utex
;
};
}
// namespace cyber
...
...
cyber/timer/timing_wheel.cc
浏览文件 @
0c13e204
...
...
@@ -72,8 +72,8 @@ void TimingWheel::AddTask(const std::shared_ptr<TimerTask>& task,
Start
();
}
auto
work_wheel_index
=
current_work_wheel_index
+
task
->
next_fire_duration_ms
;
auto
work_wheel_index
=
current_work_wheel_index
+
task
->
next_fire_duration_ms
/
TIMER_RESOLUTION_MS
;
if
(
work_wheel_index
>=
WORK_WHEEL_SIZE
)
{
auto
real_work_wheel_index
=
GetWorkWheelIndex
(
work_wheel_index
);
task
->
remainder_interval_ms
=
real_work_wheel_index
;
...
...
@@ -83,9 +83,13 @@ void TimingWheel::AddTask(const std::shared_ptr<TimerTask>& task,
work_wheel_
[
real_work_wheel_index
].
AddTask
(
task
);
ADEBUG
<<
"add task to work wheel. index :"
<<
real_work_wheel_index
;
}
else
{
auto
assistant_wheel_index
=
GetAssistantWheelIndex
(
current_assistant_wheel_index_
+
assistant_ticks
);
assistant_wheel_
[
assistant_wheel_index
].
AddTask
(
task
);
auto
assistant_wheel_index
=
0
;
{
std
::
lock_guard
<
std
::
mutex
>
lock
(
current_assistant_wheel_index_mutex_
);
assistant_wheel_index
=
GetAssistantWheelIndex
(
current_assistant_wheel_index_
+
assistant_ticks
);
assistant_wheel_
[
assistant_wheel_index
].
AddTask
(
task
);
}
ADEBUG
<<
"add task to assistant wheel. index : "
<<
assistant_wheel_index
;
}
...
...
@@ -116,11 +120,17 @@ void TimingWheel::TickFunc() {
// AINFO_EVERY(1000) << "Tick " << TickCount();
tick_count_
++
;
rate
.
Sleep
();
current_work_wheel_index_
=
GetWorkWheelIndex
(
current_work_wheel_index_
+
1
);
{
std
::
lock_guard
<
std
::
mutex
>
lock
(
current_work_wheel_index_mutex_
);
current_work_wheel_index_
=
GetWorkWheelIndex
(
current_work_wheel_index_
+
1
);
}
if
(
current_work_wheel_index_
==
0
)
{
current_assistant_wheel_index_
=
GetAssistantWheelIndex
(
current_assistant_wheel_index_
+
1
);
{
std
::
lock_guard
<
std
::
mutex
>
lock
(
current_assistant_wheel_index_mutex_
);
current_assistant_wheel_index_
=
GetAssistantWheelIndex
(
current_assistant_wheel_index_
+
1
);
}
Cascade
(
current_assistant_wheel_index_
);
}
}
...
...
cyber/timer/timing_wheel.h
浏览文件 @
0c13e204
...
...
@@ -35,9 +35,9 @@ struct TimerTask;
static
const
uint64_t
WORK_WHEEL_SIZE
=
512
;
static
const
uint64_t
ASSISTANT_WHEEL_SIZE
=
64
;
static
const
uint64_t
TIMER_RESOLUTION_MS
=
1
;
static
const
uint64_t
TIMER_RESOLUTION_MS
=
2
;
static
const
uint64_t
TIMER_MAX_INTERVAL_MS
=
WORK_WHEEL_SIZE
*
ASSISTANT_WHEEL_SIZE
;
WORK_WHEEL_SIZE
*
ASSISTANT_WHEEL_SIZE
*
TIMER_RESOLUTION_MS
;
class
TimingWheel
{
public:
...
...
@@ -78,7 +78,9 @@ class TimingWheel {
TimerBucket
work_wheel_
[
WORK_WHEEL_SIZE
];
TimerBucket
assistant_wheel_
[
ASSISTANT_WHEEL_SIZE
];
uint64_t
current_work_wheel_index_
=
0
;
std
::
mutex
current_work_wheel_index_mutex_
;
uint64_t
current_assistant_wheel_index_
=
0
;
std
::
mutex
current_assistant_wheel_index_mutex_
;
std
::
thread
tick_thread_
;
DECLARE_SINGLETON
(
TimingWheel
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录