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

Initial implementation of C api #915

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

Conversation

erer1243
Copy link

@erer1243 erer1243 commented Sep 12, 2024

Implement a C interface to some of libswsscommon in support of sonic-dash-ha.

Related:
sonic-net/sonic-dash-ha#6
#921

Copy link

linux-foundation-easycla bot commented Sep 12, 2024

CLA Signed

The committers listed above are authorized under a signed CLA.

common/c-api/util.h Show resolved Hide resolved
common/c-api/util.h Outdated Show resolved Hide resolved
common/Makefile.am Outdated Show resolved Hide resolved
return;
}
}
std::lock_guard<std::mutex> lock(m_receivedQueueMutex);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm concerned about the performance impact of this change. The original code only locks when operating on m_receivedOperationQueue. This is because vkco.push_back(std::move(kco)) will copy a large amount of data and take a long time.

With this change, it may affect the ZMQ channel throughput.

Copy link
Author

@erer1243 erer1243 Sep 27, 2024

Choose a reason for hiding this comment

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

There are two race conditions in the code as-is if 2+ threads are running pops(): count can be invalidated, leading to auto& kco = m_receivedOperationQueue.front() giving a null reference; kco can also be popped and freed while still being pushed onto vkco, leading to use-after-free. Unfortunately we have to move the data while holding the lock.

Copy link
Contributor

Choose a reason for hiding this comment

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

to avoid any potential perf issue. let's try this -

  1. define a new temporary queue, which will be empty.
  2. Lock the member queue and swap with the empty queue.
  3. pop all items from the temporary queue and push them all into the output queue.

the item 3, there is no lock in place, which should minimize the overall block/lock time.

Copy link
Contributor

Choose a reason for hiding this comment

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

hope this solves the concern. @erer1243

Copy link
Author

Choose a reason for hiding this comment

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

Riff's suggestion is a good idea. I think that will fix both sides of the issue.

@@ -0,0 +1,34 @@
#include <cstdlib>
Copy link
Contributor

Choose a reason for hiding this comment

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

Why create this c wrapper? Is there some issue to use swss-common lib with https://github.com/rust-lang/rust-bindgen?

Copy link
Author

@erer1243 erer1243 Sep 27, 2024

Choose a reason for hiding this comment

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

bindgen does not really help you call common C++. it only works well for C. It doesn't support types of undefined sizes (bool) and several common classes (vector, string). I created this C wrapper so that I can use bindgen

r12f
r12f previously approved these changes Oct 1, 2024
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.

3 participants