Skip to content

Implement Stopwatch #12623

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

Merged
merged 2 commits into from
Apr 7, 2023
Merged

Implement Stopwatch #12623

merged 2 commits into from
Apr 7, 2023

Conversation

KarateBrot
Copy link
Member

@KarateBrot KarateBrot commented Apr 5, 2023

This PR implements a stopwatch for convenient time duration measurements (for example as a debugging tool).

Example code

#include "common/stopwatch.h"

static stopwatch_t watch;

void exampleFunction(void)
{
    stopwatchReset(&watch);  // Init or reset timer
    stopwatchStart(&watch);  // Start timer

    // CPU does something important...

    stopwatchStop(&watch);   // Stop timer. Start again without resetting to continue counting

    // Write elapsed time to some debug channel (for example)
    DEBUG_SET(DEBUG_WHATEVER, 0, stopwatchGetCycles(&watch));           // Elapsed CPU cycles
    DEBUG_SET(DEBUG_WHATEVER, 1, stopwatchGetMicros(&watch));           // Elapsed microseconds
    DEBUG_SET(DEBUG_WHATEVER, 2, lrintf(stopwatchGetMicrosf(&watch)));  // Elapsed microseconds with sub-microsecond precision
}

@KarateBrot KarateBrot added this to the 4.5 milestone Apr 5, 2023
@KarateBrot KarateBrot self-assigned this Apr 5, 2023
@github-actions

This comment has been minimized.

@blckmn
Copy link
Member

blckmn commented Apr 5, 2023

AUTOMERGE: (FAIL)

  • github identifies PR as mergeable -> FAIL
  • assigned to a milestone -> PASS
  • cooling off period lapsed -> PASS
  • commit count less or equal to three -> PASS
  • Don't merge label NOT found -> PASS
  • at least one RN: label found -> PASS
  • Tested label found -> FAIL
  • assigned to an approver -> PASS
  • approver count at least three -> FAIL

@KarateBrot
Copy link
Member Author

@ledvinap Thanks for the ideas, I like it much better now. Anything else you can spot?

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@ledvinap
Copy link
Contributor

ledvinap commented Apr 5, 2023

@KarateBrot : Nice and clean.

@github-actions

This comment has been minimized.

Copy link
Member

@haslinghuis haslinghuis left a comment

Choose a reason for hiding this comment

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

As this is for development instrumentation we should isolate with USE_STOPWATCH like we do with USE_TIMER_MAP_PRINT

@KarateBrot
Copy link
Member Author

KarateBrot commented Apr 5, 2023

It is not isolated to development purposes. My example only highlights a debug use case. My intention behind this PR was to provide this as a tool for the BF firmware in general. You can also use it for looptime measurements or any other short-time measurements which need precise timing in BF.

@github-actions
Copy link

github-actions bot commented Apr 5, 2023

Do you want to test this code? Here you have an automated build:
Assets
WARNING: It may be unstable. Use only for testing! See: https://www.youtube.com/watch?v=I1uN9CN30gw for instructions for unified targets!

@ctzsnooze ctzsnooze merged commit 2792d51 into betaflight:master Apr 7, 2023
@HThuren
Copy link
Member

HThuren commented Apr 7, 2023

@KarateBrot, Just to know, how to activate this, future in lua or another smart features ?

@KarateBrot KarateBrot deleted the stopwatch branch April 10, 2023 09:11
@KarateBrot
Copy link
Member Author

@HThuren I'm not completely sure if I understand your question but this is a programmer's tool, not really interesting to BF end users. Now it's just a bit easier or more convenient to very precisely measure short time durations. I don't think it's useful as a "stopwatch for humans" (not yet), as the variable for CPU cycles rolls over in about 15-30 seconds, depending on the clock speed of the CPU.

@HThuren
Copy link
Member

HThuren commented Apr 10, 2023

@HThuren I'm not completely sure if I understand your question but this is a programmer's tool, not really interesting to BF end users. Now it's just a bit easier or more convenient to very precisely measure short time durations. I don't think it's useful as a "stopwatch for humans" (not yet), as the variable for CPU cycles rolls over in about 15-30 seconds, depending on the clock speed of the CPU.

You just gave the answer, a programmers tool, not intended for end users, thank you.

davidbitton pushed a commit to davidbitton/betaflight that referenced this pull request Feb 5, 2024
* Implement stopwatch to measure time periods

* Add float version of stopwatchGetMicros()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: COMPLETED
Development

Successfully merging this pull request may close these issues.

6 participants