public inbox for [email protected]
 help / color / mirror / Atom feed
From: Ammar Faizi <[email protected]>
To: Alviro Iskandar Setiawan <[email protected]>
Cc: Ammar Faizi <[email protected]>,
	GNU/Weeb Mailing List <[email protected]>
Subject: [PATCH ncns v1 2/4] chnet: ring: Improve `PostCQE()` waiting mechanism
Date: Tue, 16 Aug 2022 23:53:12 +0700	[thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>

Don't use sleep(1) in a loop to wait for the CQE slot be free, instead,
use a conditional variable to reduce the loop cycle and also to reduce
the latency because the wake up doesn't need to wait for a full second.
It just waits until it's signaled whenever the CQE slot becomes
available again.

Signed-off-by: Ammar Faizi <[email protected]>
---
 chnet/chnet_ring.cc | 42 +++++++++++++++++++++++++++++++++++++++++-
 chnet/chnet_ring.h  | 12 +++++++-----
 2 files changed, 48 insertions(+), 6 deletions(-)

diff --git a/chnet/chnet_ring.cc b/chnet/chnet_ring.cc
index 966e60e..c9a3da5 100644
--- a/chnet/chnet_ring.cc
+++ b/chnet/chnet_ring.cc
@@ -34,6 +34,7 @@ CNRingCtx::CNRingCtx(uint32_t entry)
 	state_ = new CNRingState(sq_max, cq_max, 2);
 
 	state_->cqe_to_wait_.store(0, std::memory_order_relaxed);
+	state_->nr_post_cqe_wait_.store(0, std::memory_order_relaxed);
 	sq_head_.store(0, std::memory_order_relaxed);
 	sq_tail_.store(0, std::memory_order_relaxed);
 	cq_head_.store(0, std::memory_order_relaxed);
@@ -128,16 +129,54 @@ bool CNRingCtx::TryPostCQE(CNRingSQE *sqe, int64_t res)
 	return true;
 }
 
+void CNRingCtx::WaitForCQEFreeSlot(void)
+{
+	std::unique_lock<std::mutex> lock(state_->post_cqe_lock_);
+
+	state_->nr_post_cqe_wait_++;
+	state_->post_cqe_cond_.wait(lock, [this]{
+		bool ret;
+
+		state_->cqe_lock_.lock();
+		ret = !!GetCQENoTailIncrement();
+		state_->cqe_lock_.unlock();
+		return ret;
+	});
+	state_->nr_post_cqe_wait_--;
+}
+
 void CNRingCtx::PostCQE(CNRingSQE *sqe, int64_t res)
 {
 	while (1) {
 		if (TryPostCQE(sqe, res))
 			break;
 
-		sleep(1);
+		WaitForCQEFreeSlot();
+	}
+}
+
+void CNRingCtx::NotifyWaitCQEFreeSlot(void)
+{
+	uint32_t nr_wait = state_->nr_post_cqe_wait_.load();
+
+	if (unlikely(nr_wait)) {
+		state_->post_cqe_lock_.lock();
+		if (nr_wait == 1)
+			state_->post_cqe_cond_.notify_one();
+		else
+			state_->post_cqe_cond_.notify_all();
+		state_->post_cqe_lock_.unlock();
 	}
 }
 
+void CNRingCtx::CQAdvance(uint32_t n)
+{
+	state_->cqe_lock_.lock();
+	cq_head_ += n;
+	state_->cqe_lock_.unlock();
+	NotifyWaitCQEFreeSlot();
+}
+
 /*
  * Use this when the caller is not allowed to block.
  */
@@ -335,6 +374,7 @@ void CNRingCtx::WaitCQE(uint32_t to_wait)
 	if (to_wait > max_to_wait)
 		to_wait = max_to_wait;
 
+	NotifyWaitCQEFreeSlot();
 	if (to_wait <= cqe_size())
 		return;
 
diff --git a/chnet/chnet_ring.h b/chnet/chnet_ring.h
index b0cfc50..f41d75b 100644
--- a/chnet/chnet_ring.h
+++ b/chnet/chnet_ring.h
@@ -109,6 +109,10 @@ public:
 	std::mutex	cqe_lock_;
 	std::condition_variable cqe_cond_;
 	std::atomic<uint32_t> cqe_to_wait_;
+
+	std::mutex	post_cqe_lock_;
+	std::condition_variable post_cqe_cond_;
+	std::atomic<uint32_t> nr_post_cqe_wait_;
 };
 #endif
 
@@ -124,11 +128,7 @@ public:
 
 	void PostCQE(CNRingSQE *sqe, int64_t res);
 	void CallPostCQE(CNRingSQE *sqe, int64_t res);
-
-	inline void CQAdvance(uint32_t n)
-	{
-		cq_head_ += n;
-	}
+	void CQAdvance(uint32_t n);
 
 	inline CNRingCQE *HeadCQE(void)
 	{
@@ -158,6 +158,8 @@ private:
 	bool TryPostCQE(CNRingSQE *sqe, int64_t res);
 	CNRingCQE *GetCQENoTailIncrement(void);
 	void NotifyCQEWaiter(void);
+	void NotifyWaitCQEFreeSlot(void);
+	void WaitForCQEFreeSlot(void);
 
 	void IssueSQE(CNRingSQE *sqe);
 	void IssueNopSQE(CNRingSQE *sqe);
-- 
Ammar Faizi


  parent reply	other threads:[~2022-08-16 16:53 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-16 16:53 [PATCH ncns v1 0/4] chnet ring cleanups Ammar Faizi
2022-08-16 16:53 ` [PATCH ncns v1 1/4] chnet: ring: Refactor SQE handling Ammar Faizi
2022-08-16 16:53 ` Ammar Faizi [this message]
2022-08-16 16:53 ` [PATCH ncns v1 3/4] chnet: ring: Make sure we are holding the lock when calling `cqe_size()` Ammar Faizi
2022-08-16 16:53 ` [PATCH ncns v1 4/4] chnet: ring: Only notify CQE free slot when it's available Ammar Faizi
2022-08-16 17:12   ` Alviro Iskandar Setiawan
2022-08-16 17:17     ` Ammar Nofan Faizi
2022-08-16 17:23       ` Alviro Iskandar Setiawan
2022-08-16 17:24 ` [PATCH ncns v1 0/4] chnet ring cleanups Alviro Iskandar Setiawan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox