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

SITL perf improvements #11177

Merged
merged 11 commits into from
Jan 14, 2019
Merged

SITL perf improvements #11177

merged 11 commits into from
Jan 14, 2019

Conversation

bkueng
Copy link
Member

@bkueng bkueng commented Jan 9, 2019

This brings some lockstep/timing/locking-related performance improvements.

In more detail:

  • reduces SITL CPU usage by ~2% (28% -> 26%)
  • reduces locking in hrt_absolute_time and the lockstep scheduler. This reduces the amount of time spent in (un)locking by a few percent, but it's still really high - around 30%. Heaviest remaining users are the driver framework and uORB.
  • hrt_absolute_time used 11% of the total usage, it is now down to 0.35%
  • removed the heavy use of malloc in the lockstep scheduler by using a thread_local object (required some changes to the unit-tests as well).
  • replaced the std::vector with a more efficient linked list in the lockstep scheduler.
  • fixes valgrind & gdb invocation

bkueng added 10 commits January 9, 2019 13:32
This is a small method that is used a lot.
…ce locking

- the loop is not needed
- we optimize for the fast case and lock only if really needed
Not required, since the lock is held during the whole loop iteration.
Previously hrt_absolute_time() was at around 5% of the total CPU usage, now
it's around 0.35%.
- use a linked-list instead of std::vector. Insertion and removal are now
  O(1)
- avoid malloc and use a thread_local instance of TimedWait.
  It gets destroyed when the thread exits, so we have to add protection
  in case a thread exits too quickly. This in turn requires a fix to the
  unit-tests.
@bkueng bkueng requested a review from julianoes January 9, 2019 12:52
Copy link
Contributor

@julianoes julianoes left a comment

Choose a reason for hiding this comment

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

Thanks for looking into this and improving it, that's awsome!

  • removed the heavy use of malloc in the lockstep scheduler by using a thread_local object (required some changes to the unit-tests as well).

Which malloc do you mean? The one for std::vector?

Ah, this thing, I didn't realize, crazy!

static thread_local TimedWait timed_wait;
  • replaced the std::vector with a more efficient linked list in the lockstep scheduler.

How much did the switch from std::vector to the linked list really improve?

@@ -6,6 +6,8 @@ if(NOT PROJECT_NAME STREQUAL "px4")

set (CMAKE_CXX_STANDARD 11)

add_definitions(-DUNIT_TESTS)
Copy link
Contributor

Choose a reason for hiding this comment

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

We should probably move the whole file to tabs.

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, I didn't realize. Commit pushed.

Copy link
Member

Choose a reason for hiding this comment

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

Quick comment because I just noticed this (not PR related). The CMakeLists.txt doesn't need to be different for inclusion in px4 or building standalone.

Copy link
Contributor

Choose a reason for hiding this comment

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

It does because it can be used for standalone unit tests. And I would like to keep these tests until we have figured out a way to port them over into PX4.

void set_absolute_time(uint64_t time_us);
uint64_t get_absolute_time() const;
inline uint64_t get_absolute_time() const { return _time_us; }
Copy link
Contributor

Choose a reason for hiding this comment

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

I like that you made it inline explicitly but it is already inline automatically if defined in the class declaration, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

I like that you made it inline explicitly but it is already inline automatically if defined in the class declaration, right?

Yes, this is just slightly stronger, and sometimes I like to add it to make it clear to a reader that it's important here. Not strictly necessary though.

pthread_cond_t *passed_cond{nullptr};
pthread_mutex_t *passed_lock{nullptr};
uint64_t time_us{0};
bool timeout{false};
bool done{false};
std::atomic<bool> done{false};
std::atomic<bool> removed{true};
Copy link
Contributor

Choose a reason for hiding this comment

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

At first I found the name removed confusing but I think it actually is what it is, and I can't find a better word.

std::atomic<bool> done{false};
std::atomic<bool> removed{true};

TimedWait *next{nullptr}; ///< linked list
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't love the raw linked list. I would prefer an abstraction but I suppose it's easy enough to understand.

}

void LockstepScheduler::set_absolute_time(uint64_t time_us)
{
time_us_ = time_us;
_time_us = time_us;
Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, makes sense to switch it to the PX4 convention.

!temp_timed_wait->done) {
temp_timed_wait->timeout = true;
if (timed_wait->time_us <= time_us &&
!timed_wait->timeout) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Are you sure that we don't need && !temp_timed_wait->done anymore?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, since we check for that above already. If it were needed here, it would mean we have a race condition.

pthread_cond_t cond;
pthread_cond_init(&cond, nullptr);
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
Copy link
Contributor

Choose a reason for hiding this comment

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

I thought these were only allowed for static locks/conds but looks like that's only true for older versions of the POSIX standard. I guess it's a nice optimization to save calls.

std::this_thread::yield(); // usleep is too slow here
}

ls.set_absolute_time(ls.get_absolute_time());
Copy link
Contributor

Choose a reason for hiding this comment

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

Huh, you set the same time again? I don't get it.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's just that the linked list gets cleaned up. Setting the removed flag in particular.


pthread_mutex_unlock(&_hrt_mutex);

}
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice cleanup, missed that.

@@ -334,10 +330,6 @@ extern "C" {

#endif

while (sim_delay) {
px4_usleep(100);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@bkueng
Copy link
Member Author

bkueng commented Jan 9, 2019

Thanks for the review.

How much did the switch from std::vector to the linked list really improve?

Enough to show up in the profiler:
This is before (CPU spent in set_absolute_time and its callees):
before
This PR:
after
In particular the left green area is mostly from the vector (the total CPU reduction is from 2.58% to 0.73%, which also includes the call to free).

@julianoes julianoes merged commit 4bc5909 into master Jan 14, 2019
@bkueng bkueng deleted the locking_improvements branch January 14, 2019 10:10
@MaEtUgR
Copy link
Member

MaEtUgR commented Jan 14, 2019

@bkueng AWSOME!!! I just pulled master and now all the strange issues I reported in #10693 are gone!
I can run the Intel stress test utility next to SITL jmavsim and all that happens is an irregular slow down of the simulation but it runs "forever" and performance is back once the extreme CPU load is removed. Pausing also works fine, QGC connection is only lost during the pause and regained just afterwards.

The only downside is that the shutdown command is now also broken and I always have to kill the task (see #11027). But I'll likely figure that one out.

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.

4 participants