Skip to content

Comments

add WaitStrategy#44

Merged
Lwhyz merged 2 commits intoLwhyz:mainfrom
Daveshammer:main
Apr 18, 2025
Merged

add WaitStrategy#44
Lwhyz merged 2 commits intoLwhyz:mainfrom
Daveshammer:main

Conversation

@Daveshammer
Copy link
Contributor

@Daveshammer Daveshammer commented Apr 7, 2025

根据“cpu占用异常 #39”,添加了三种等待策略:
1.低延迟模式 原始空转等待(默认)
2.均衡模式 在一段时间内都无动作 进入线程休眠
3.睡眠模式 loop 线程统一 休眠1毫秒 / 使用条件变量控制loop的循环

Copy link
Owner

@Lwhyz Lwhyz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

很棒!这里有几个建议:

  1. 策略需要放在wsp命名空间内使用
  2. 如果不在均衡模式下用cv.notify_one通知工作线程,也可以用sleep而不是wait_for
  3. 统一下用下划线_命名
  4. 在README中也简要体现一下不同的mode
  5. 有空闲的话可以在Benchmark中修改现有用例,做不同模式下性能的简单标注。

@Daveshammer
Copy link
Contributor Author

很棒!这里有几个建议:

  1. 策略需要放在wsp命名空间内使用
  2. 如果不在均衡模式下用cv.notify_one通知工作线程,也可以用sleep而不是wait_for
  3. 统一下用下划线_命名
  4. 在README中也简要体现一下不同的mode
  5. 有空闲的话可以在Benchmark中修改现有用例,做不同模式下性能的简单标注。

我再接着改改

@Lwhyz
Copy link
Owner

Lwhyz commented Apr 10, 2025

我再接着改改
ok

@Daveshammer
Copy link
Contributor Author

Hi,有空帮忙看看还有什么问题,第一次提PR没啥经验,可能有不少疏漏之处

@Daveshammer Daveshammer changed the title add WaitStrategy: HighPerformance, Balanced, Smooth add WaitStrategy Apr 15, 2025
std::mutex latency_mutex;

auto task = [&latencies, &latency_mutex](std::chrono::steady_clock::time_point submit_time) {
auto start_time = std::chrono::steady_clock::now();
Copy link
Owner

@Lwhyz Lwhyz Apr 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在轻量的框架里面,获取高精度时间这个调用是相当重的,这样测试反映的耗时都在这个调用上而不是框架本身。只能是全部跑完统计。

Copy link
Owner

@Lwhyz Lwhyz Apr 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hanya@localhost:build$ ./bench4 4 1000000
Strategy: lowlatancy | Threads: 4 | Tasks: 1000000 | Avg Latency: 5.19 us | Min Latency: 0 us | Max Latency: 1027 us
Strategy: balance | Threads: 4 | Tasks: 1000000 | Avg Latency: 6.87 us | Min Latency: 0 us | Max Latency: 1256 us
Strategy: blocking | Threads: 4 | Tasks: 1000000 | Avg Latency: 6.14 us | Min Latency: 0 us | Max Latency: 145 us
hanya@localhost:build$ ./bench4 1 1000000
Strategy: lowlatancy | Threads: 1 | Tasks: 1000000 | Avg Latency: 56448.22 us | Min Latency: 28 us | Max Latency: 113657 us
Strategy: balance | Threads: 1 | Tasks: 1000000 | Avg Latency: 34161.14 us | Min Latency: 1 us | Max Latency: 71510 us
Strategy: blocking | Threads: 1 | Tasks: 1000000 | Avg Latency: 83461.47 us | Min Latency: 59 us | Max Latency: 152813 us

单个worker的总耗时是更低的,但是这里平均延迟高出了几个数量级。

Copy link
Owner

@Lwhyz Lwhyz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有些问题我都在里面标注了


namespace wsp {

enum class WaitStrategy {

This comment was marked as resolved.

// record submit_time
for (int i = 0; i < task_nums; ++i) {
auto submit_time = std::chrono::steady_clock::now();
wb.submit([=]() { task(submit_time); });
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[=] 把东西都拷进去了,lambdb这里用着要小心。

Copy link
Owner

@Lwhyz Lwhyz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

除了Bench4有点问题,其它的我测试了一下没有发现问题。👍👍👍

@Daveshammer
Copy link
Contributor Author

除了Bench4有点问题,其它的我测试了一下没有发现问题。👍👍👍

目前bench4采用类似bench2的写法,跑完再计算时间。但是发现了一个问题,yield换成sleep函数后,lowlatency模式的任务执行时间更短了,ubuntu下和win11上都有这个现象。

@Lwhyz Lwhyz closed this Apr 18, 2025
@Lwhyz Lwhyz reopened this Apr 18, 2025
@Lwhyz Lwhyz merged commit be2a489 into Lwhyz:main Apr 18, 2025
@Lwhyz
Copy link
Owner

Lwhyz commented Apr 18, 2025

目前bench4采用类似bench2的写法,跑完再计算时间。但是发现了一个问题,yield换成sleep函数后,lowlatency模式的任务执行时间更短了,ubuntu下和win11上都有这个现象。

估计是超线程导致线程切换太多了,可以perf看一下。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants