From: Sam Bradshaw <sbradshaw@micron.com>
To: Jens Axboe <axboe@kernel.dk>
Cc: "fio@vger.kernel.org" <fio@vger.kernel.org>
Subject: Re: [PATCH] gettime: minimize integer division
Date: Thu, 20 Dec 2012 11:23:22 -0800 [thread overview]
Message-ID: <50D365AA.9080202@micron.com> (raw)
In-Reply-To: <50D352D7.2090901@kernel.dk>
>> diff --git a/gettime.c b/gettime.c
>> index 035d275..89f3e27 100644
>> --- a/gettime.c
>> +++ b/gettime.c
>> @@ -168,17 +168,23 @@ void fio_gettime(struct timeval *tp, void
>> fio_unused *caller)
>> }
>> #ifdef ARCH_HAVE_CPU_CLOCK
>> case CS_CPUCLOCK: {
>> - unsigned long long usecs, t;
>> + unsigned long long usecs, t, delta = 0;
>>
>> t = get_cpu_clock();
>> if (tv && t < tv->last_cycles) {
>> dprint(FD_TIME, "CPU clock going back in time\n");
>> t = tv->last_cycles;
>> - } else if (tv)
>> + } else if (tv) {
>> + if (tv->last_tv_valid)
>> + delta = t - tv->last_cycles;
>> tv->last_cycles = t;
>> + }
>>
>> usecs = t / cycles_per_usec;
>> - tp->tv_sec = usecs / 1000000;
>> + if (delta && delta < 1000000)
>> + tp->tv_sec = tv->last_tv.tv_sec;
>> + else
>> + tp->tv_sec = usecs / 1000000;
>> tp->tv_usec = usecs % 1000000;
>> break;
>> }
>
> I was thinking about this... Is it actually guarenteed to work. If
> tv->last_tv.tv_usec is eg 900,000, you'd only need a 100k usec diff to
> need to wrap, not 1000k. And since this is about avoiding costly divs,
> since we know the number of cycles last time, it might make more sense
> to just do the single div to go from cycles to usecs, then add that to
> the tv->last_tv.
>
Something like this might work, though that amount of logic may
be equivalent in terms of cycles to the divide.
diff --git a/gettime.c b/gettime.c
index 035d275..955a0a1 100644
--- a/gettime.c
+++ b/gettime.c
@@ -168,18 +168,25 @@ void fio_gettime(struct timeval *tp, void
fio_unused *caller)
}
#ifdef ARCH_HAVE_CPU_CLOCK
case CS_CPUCLOCK: {
- unsigned long long usecs, t;
+ unsigned long long usecs, t, delta = 0;
t = get_cpu_clock();
if (tv && t < tv->last_cycles) {
dprint(FD_TIME, "CPU clock going back in time\n");
t = tv->last_cycles;
- } else if (tv)
+ } else if (tv) {
+ if (tv->last_tv_valid)
+ delta = t - tv->last_cycles;
tv->last_cycles = t;
+ }
usecs = t / cycles_per_usec;
- tp->tv_sec = usecs / 1000000;
tp->tv_usec = usecs % 1000000;
+ if (delta && delta < 1000000 &&
+ tv->last_tv.tv_usec < tp->tv_usec)
+ tp->tv_sec = tv->last_tv.tv_sec;
+ else
+ tp->tv_sec = usecs / 1000000;
break;
}
#endif
next prev parent reply other threads:[~2012-12-20 19:23 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-12-20 0:52 [PATCH] gettime: minimize integer division Sam Bradshaw
2012-12-20 8:14 ` Jens Axboe
2012-12-20 17:18 ` Sam Bradshaw
2012-12-20 18:03 ` Jens Axboe
2012-12-20 18:58 ` Sam Bradshaw (sbradshaw)
2012-12-20 19:23 ` Sam Bradshaw [this message]
2012-12-21 15:33 ` Jens Axboe
2012-12-21 15:45 ` Jens Axboe
2012-12-21 21:28 ` Sam Bradshaw (sbradshaw)
2012-12-21 21:30 ` Jens Axboe
2012-12-21 21:53 ` Sam Bradshaw (sbradshaw)
2012-12-23 20:49 ` Jens Axboe
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=50D365AA.9080202@micron.com \
--to=sbradshaw@micron.com \
--cc=axboe@kernel.dk \
--cc=fio@vger.kernel.org \
/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