netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Stephen Hemminger <shemminger@vyatta.com>
To: David Miller <davem@davemloft.net>,
	Robert Olsson <robert.olsson@its.uu.se>
Cc: netdev@vger.kernel.org, Thomas Gleixner <tglx@linutronix.de>
Subject: [PATCH 11/14] pktgen: spin using hrtimer
Date: Thu, 27 Aug 2009 16:55:17 -0700	[thread overview]
Message-ID: <20090827235705.985449360@vyatta.com> (raw)
In-Reply-To: 20090827235506.624381734@vyatta.com

[-- Attachment #1: pktgen-hrtimer-spin.patch --]
[-- Type: text/plain, Size: 2972 bytes --]

This changes how the pktgen thread spins/waits between
packets if delay is configured. It uses a high res timer to
wait for time to arrive.

Signed-off-by: Stephen Hemminger <shemminger@vyatta.com>

---
 kernel/hrtimer.c  |    1 +
 net/core/pktgen.c |   49 ++++++++++++++++++++++++++++---------------------
 2 files changed, 29 insertions(+), 21 deletions(-)

--- a/net/core/pktgen.c	2009-08-27 16:00:52.538250235 -0700
+++ b/net/core/pktgen.c	2009-08-27 16:28:17.520270294 -0700
@@ -131,6 +131,7 @@
 #include <linux/ioport.h>
 #include <linux/interrupt.h>
 #include <linux/capability.h>
+#include <linux/hrtimer.h>
 #include <linux/freezer.h>
 #include <linux/delay.h>
 #include <linux/timer.h>
@@ -2086,33 +2087,40 @@ static void pktgen_setup_inject(struct p
 	pkt_dev->nflows = 0;
 }
 
-static inline s64 delta_ns(ktime_t a, ktime_t b)
-{
-	return ktime_to_ns(ktime_sub(a, b));
-}
 
 static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
 {
-	ktime_t start, now;
-	s64 dt;
+	ktime_t start;
+	s32 remaining;
+	struct hrtimer_sleeper t;
 
-	start = now = ktime_now();
+	hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
+	hrtimer_set_expires(&t.timer, spin_until);
 
-	while ((dt = delta_ns(spin_until, now)) > 0) {
-		/* TODO: optimize sleeping behavior */
-		if (dt > TICK_NSEC)
-			schedule_timeout_interruptible(1);
-		else if (dt > 100*NSEC_PER_USEC) {
-			if (!pkt_dev->running)
-				return;
-			if (need_resched())
+	remaining = ktime_to_us(hrtimer_expires_remaining(&t.timer));
+	if (remaining <= 0)
+		return;
+
+	start = ktime_now();
+	if (remaining < 100)
+		udelay(remaining); 	/* really small just spin */
+	else {
+		/* see do_nanosleep */
+		hrtimer_init_sleeper(&t, current);
+		do {
+			set_current_state(TASK_INTERRUPTIBLE);
+			hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
+			if (!hrtimer_active(&t.timer))
+				t.task = NULL;
+
+			if (likely(t.task))
 				schedule();
-		}
 
-		now = ktime_now();
+			hrtimer_cancel(&t.timer);
+		} while (t.task && pkt_dev->running && !signal_pending(current));
+		__set_current_state(TASK_RUNNING);
 	}
-
-	pkt_dev->idle_acc += ktime_to_ns(ktime_sub(now, start));
+	pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), start));
 }
 
 static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
@@ -3360,8 +3368,7 @@ static void pktgen_xmit(struct pktgen_de
 	int ret;
 
 	if (pkt_dev->delay) {
-		if (ktime_lt(ktime_now(), pkt_dev->next_tx))
-			spin(pkt_dev, pkt_dev->next_tx);
+		spin(pkt_dev, pkt_dev->next_tx);
 
 		/* This is max DELAY, this has special meaning of
 		 * "never transmit"
--- a/kernel/hrtimer.c	2009-08-27 16:00:52.547269763 -0700
+++ b/kernel/hrtimer.c	2009-08-27 16:02:04.543287103 -0700
@@ -1477,6 +1477,7 @@ void hrtimer_init_sleeper(struct hrtimer
 	sl->timer.function = hrtimer_wakeup;
 	sl->task = task;
 }
+EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
 
 static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
 {

-- 


  parent reply	other threads:[~2009-08-27 23:59 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-08-27 23:55 [PATCH 00/14] pktgen update for net-next (2.6.32) Stephen Hemminger
2009-08-27 23:55 ` [PATCH 01/14] pktgen: minor cleanup Stephen Hemminger
2009-08-27 23:55 ` [PATCH 02/14] pktgen: change inlining Stephen Hemminger
2009-08-27 23:55 ` [PATCH 03/14] pktgen: mark read-only/mostly variables Stephen Hemminger
2009-08-27 23:55 ` [PATCH 04/14] pktgen: stop_device cleanup Stephen Hemminger
2009-08-27 23:55 ` [PATCH 05/14] pktgen: xmit logic reorganization Stephen Hemminger
2009-08-27 23:55 ` [PATCH 06/14] pktgen: cleanup clone count test Stephen Hemminger
2009-08-27 23:55 ` [PATCH 07/14] pktgen: use netdev_alloc_skb Stephen Hemminger
2009-08-27 23:55 ` [PATCH 08/14] pktgen: reorganize transmit loop Stephen Hemminger
2009-08-28  3:52   ` Ben Greear
2009-08-28  5:49     ` Stephen Hemminger
2009-08-28 16:01       ` Ben Greear
2009-08-29  6:04       ` David Miller
2009-09-01 21:30         ` Stephen Hemminger
2009-09-02 13:06           ` Patrick McHardy
2009-08-27 23:55 ` [PATCH 09/14] pktgen: avoid calling gettimeofday Stephen Hemminger
2009-08-27 23:55 ` [PATCH 10/14] pktgen: convert to use ktime_t Stephen Hemminger
2009-08-27 23:55 ` Stephen Hemminger [this message]
2009-08-27 23:55 ` [PATCH 12/14] pktgen: use common idle routine Stephen Hemminger
2009-08-27 23:55 ` [PATCH 13/14] pktgen: cleanup checkpatch warnings Stephen Hemminger
2009-08-27 23:55 ` [PATCH 14/14] pktgen: increase version Stephen Hemminger
2009-08-29  6:33 ` [PATCH 00/14] pktgen update for net-next (2.6.32) David Miller
2009-08-29  6:42   ` David Miller
2009-09-08 11:52 ` robert
2009-09-08 12:21   ` Jesper Dangaard Brouer
2009-09-08 15:41     ` robert
2009-09-09  7:53       ` Jesper Dangaard Brouer
2009-09-11 14:29         ` Jesper Dangaard Brouer

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 \
    --in-reply-to=20090827235705.985449360@vyatta.com \
    --to=shemminger@vyatta.com \
    --cc=davem@davemloft.net \
    --cc=netdev@vger.kernel.org \
    --cc=robert.olsson@its.uu.se \
    --cc=tglx@linutronix.de \
    /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;
as well as URLs for NNTP newsgroup(s).