Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[components][finsh] add cputh cmd #8091

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

leesinebee
Copy link

@leesinebee leesinebee commented Sep 26, 2023

拉取/合并请求描述:(PR description)

[

为什么提交这份PR (why to submit this PR)

增加cpu使用率的功能,能够方便的查看每个线程在单位时间内的cpu使用率

你的解决方案是什么 (what is your solution)

  1. 使用scheduler hook函数统计每个线程占用cpu的时间。
  2. 通过定时器事件周期性计算cpu使用率并更新显示。
  3. 命令开始执行时注册hook函数,动态创建统计所需的内存并绑定到线程的userdata,命令退出时清除hook函数,并释放内存。命令退出后不占用cpu和内存。
  4. 默认情况下使用tick计数统计cpu使用率,如果用户重写了三个weak函数,则能够使用硬件计数精确的统计每个线程的cpu使用率和cpu cycle。
  5. 使用pingpang buffer统计cpu占用时间,更新显示过程中不会丢失统计。

在什么测试环境下测试通过 (what is the test environment)

使用at32f437-starter开发板和stm32f429-atk-apollo开发板测试通过
]

当前拉取/合并请求的状态 Intent for your PR

必须选择一项 Choose one (Mandatory):

  • 本拉取/合并请求是一个草稿版本 This PR is for a code-review and is intended to get feedback
  • 本拉取/合并请求是一个成熟版本 This PR is mature, and ready to be integrated into the repo

代码质量 Code Quality:

我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:

  • 已经仔细查看过代码改动的对比 Already check the difference between PR and old code
  • 代码风格正确,包括缩进空格,命名及其他风格 Style guide is adhered to, including spacing, naming and other styles
  • 没有垃圾代码,代码尽量精简,不包含#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up
  • 所有变更均有原因及合理的,并且不会影响到其他软件组件代码或BSP All modifications are justified and not affect other components or BSP
  • 对难懂代码均提供对应的注释 I've commented appropriately where code is tricky
  • 代码是高质量的 Code in this PR is of high quality
  • 已经使用formatting 等源码格式化工具确保格式符合RT-Thread代码规范 This PR complies with RT-Thread code specification

@CLAassistant
Copy link

CLAassistant commented Sep 26, 2023

CLA assistant check
All committers have signed the CLA.

@leesinebee
Copy link
Author

leesinebee commented Sep 26, 2023

默认使用tick 统计cpu使用率
image
重写了weak函数后,精确统计
image

Copy link
Member

@BernardXiong BernardXiong left a comment

Choose a reason for hiding this comment

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

感觉那几个weak函数的实现并不是完全合适的。另外,关于cpu usage,应该有软件包了的吧(不完全确定)。cpu usage实现了,放软件包中也挺好的

@leesinebee
Copy link
Author

  • 之前看到有个在idle线程中统计总体cpu usage的函数,但是无法知道某一个线程的使用率。
  • 三个weak函数如果没有实现,那么所有的线程使用单位时间内tick计数统计使用率,这样粒度比较粗。如果实现了话,那么所有的线程使用单位时间内tick*timer_prd计数来统计,会更准确。这个timer在stm32上就是systick。
  • 三个weak函数分别:1、获取timer_prd,在命令启动时使用。2、获取timer的counter数值,在hook中调用。3、获取timer是否溢出,仍在hook中调用。这是目前想到的比较精确统计每个线程使用率的方法。
  • 是的,这个可放在package中比较好。

@leesinebee
Copy link
Author

leesinebee commented Oct 9, 2023

感觉那几个weak函数的实现并不是完全合适的。另外,关于cpu usage,应该有软件包了的吧(不完全确定)。cpu usage实现了,放软件包中也挺好的

rtt的package中确实有一个cpu_usage,可以显示cpu的总体使用率,范围大概从0~100。
rtt的bsp中有一个cpuusage.c的文件,和软件包中的类似,也是显示总体使用率。
cputh能显示thread的使用率,功能上应该和上面的软件包不重复。
看了您的回复,我目前还没想出关于这三个weak函数更好的方法。不知您对这几个weak函数有什么建议,或者其他的改动建议?
另外感觉如果能作为msh内置的命令来显示cpu的使用率会更方便些。

@BernardXiong
Copy link
Member

感觉那几个weak函数的实现并不是完全合适的。另外,关于cpu usage,应该有软件包了的吧(不完全确定)。cpu usage实现了,放软件包中也挺好的

rtt的package中确实有一个cpu_usage,可以显示cpu的总体使用率,范围大概从0~100。 rtt的bsp中有一个cpuusage.c的文件,和软件包中的类似,也是显示总体使用率。 cputh能显示thread的使用率,功能上应该和上面的软件包不重复。 看了您的回复,我目前还没想出关于这三个weak函数更好的方法。不知您对这几个weak函数有什么建议,或者其他的改动建议? 另外感觉如果能作为msh内置的命令来显示cpu的使用率会更方便些。

可以一样的放到软件包中;选择、编译了软件包后,命令也会自动导出到msh中。

@armink
Copy link
Member

armink commented Oct 11, 2023

单独放到软件包里很多人都不知道这个功能,是不是把结果集成到 ps / list thread 命令里,这样更加方便呀

@leesinebee
Copy link
Author

如果放在软件包中,是否要关闭这个PR,然后重新提交。我看到软件包只有索引,不包含代码

@armink
Copy link
Member

armink commented Oct 12, 2023

我建议可以放在 RTT utilities 里,而且最好不要占用各种系统 hook 。避免开启这个功能,导致系统 hook 没法被其他软件包使用

@BernardXiong
Copy link
Member

我建议可以放在 RTT utilities 里,而且最好不要占用各种系统 hook 。避免开启这个功能,导致系统 hook 没法被其他软件包使用

👌 那么可以完善好一套易用的cpu usage出来吧

@leesinebee
Copy link
Author

我建议可以放在 RTT utilities 里,而且最好不要占用各种系统 hook 。避免开启这个功能,导致系统 hook 没法被其他软件包使用

单独放到软件包里很多人都不知道这个功能,是不是把结果集成到 ps / list thread 命令里,这样更加方便呀

集成到ps命令里不太方便,目前的实现方法需要经过指定的时间后才能计算统计结果,会影响ps的显示

@leesinebee
Copy link
Author

我建议可以放在 RTT utilities 里,而且最好不要占用各种系统 hook 。避免开启这个功能,导致系统 hook 没法被其他软件包使用

是指路径rt-thread\components\utilities?目前的实现方法必须要使用rt_scheduler_hook这个系统hook。或者我们增加一个额外hook供cpuusage使用?

@armink
Copy link
Member

armink commented Oct 18, 2023

PS 效果可以参考 ucos 。hook 去掉后,可以直接在调度器源码位置加代码,通过宏隔离

@HelloByeAll
Copy link
Contributor

加入内核的话 是不是得考虑smp的场景也加进来

@polarvid polarvid mentioned this pull request Apr 2, 2024
9 tasks
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.

None yet

5 participants