0

I've been studying spaghetti-based state-machine, meaning that it's composed of a bunch of if-else statements scattered everywhere, in large kernel module recently in order to port this module to other platform.

To understand how control flow goes between each state, I use printk() in the first place to print every line number so that I can see every detail of program behavior in run-time. However, too many printk() increase too much processing load and result in wrong behavior.

Afterward, I try printk_once() and printk_ratelimited() to reduce line number. But this way causes another problem, especially at the time during transition.

Let me explain why:

Suppose there are four states A, B, C, and D, and each state's control flow is overlapped partly with each other. Also, there are two events e1 and e2 that I can trigger by plugging/unplugging hardware.

Furthermore, we define stable state as a state being stable, meaning that the path of control flow for this state is not changing until event occurs; We also define unstable state as a state being unstable, meaning that this state is just a transition state for other states.

event    current state    next state

x              A              A   <---- always start from A
e1             A              B
x              B              C
e2             C              D
x              D              A

A, C: stable state
B, D: unstable state

If I use printk_once(), I'm not able to get all path of control flow from A to C as a whole, because some part of it are the same, and already be printed.

If I use printk_ratelimited(), it's not guaranteed to capture the path of control flow for transition from A to C because the span is too short to be captured.

What I want is a control flow that clearly shows how program runs step by step from state A to B then to C, so that I can totally understand what's going on between each state.

That's it.

I would like to know how people deal with this. Thanks!

Andy Lin
  • 397
  • 1
  • 2
  • 21
  • "meaning that this state is just a transition state for other states" What does that matter? Overall I don't really understand the question - you have a messy state machine, and...? Do you wish to rewrite it from scratch? Do you wish to salvage it? – Lundin Mar 02 '22 at 13:52
  • @Lundin I refined the post, and it should be more understandable now. Please take a look. – Andy Lin Mar 03 '22 at 02:47
  • Not sure how by just tracing **events** you want to collect information about **states**. If you find `printk` mechanism too time consumable (so you need to use `printk_once` or `printk_ratelimited` for omit some events), then you could try other mechanisms. E.g. you could look into tracepoints. Stack Overflow is a **bad place** for open-ended questions, when you ask for directions towards the "great" purpose. – Tsyvarev Mar 03 '22 at 07:39
  • @Tsyvarev Thanks for mentioning tracepoints, I will look at it. BTW, what place do you suggest when asking open-ended questions? – Andy Lin Mar 03 '22 at 09:42
  • I don't think that some of Stack Exchange site will accept open-ended questions like yours. In the current state your question requires a lot of **discussion** to be taken before one could understand your goal and could suggest a solution. But Question/Answer (QA) format implies none or very little discussion. – Tsyvarev Mar 03 '22 at 09:49
  • @Tsyvarev Could you please tell me which part is not clear enough at once so that I can try make it close to closed-ended question? – Andy Lin Mar 03 '22 at 10:04
  • Actually, the only thing I get from your post is that you want to collect information about hitting many lines of code. E.g I don't understand how do you want to "understand what's going on between each state" by having this information. Well, if having information about **all hits**, then one probably could understand what is going on. But if you want to **reduce** that information, then it is unclear how much hits do you need for "understand". – Tsyvarev Mar 03 '22 at 12:56

0 Answers0