Skip to contentSkip to conversation

complib/cl_timer.c: fixing cl_timer calculation

PATCH Superseded Yevgeny Kliteynik 3 participants 6 messages Aug 23, 2010 – Aug 25, 2010 3 messages not in the archive InfiniBand

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 complib/cl_timer.c: fixing cl_timer calculation

When calculating p_timer->timeout.tv_sec and p_timer->timeout.tv_nsec, the carry was ignored, resulting in wrong value in p_timer->timeout.tv_sec, and value > 10^9 in p_timer->timeout.tv_nsec (illegal value).

---
 opensm/complib/cl_timer.c |    8 +++++---
 1 files changed, 5 insertions(+), 3 deletions(-)
opensm/complib/cl_timer.c +5 -3
index 2acdb51..09a5584 100644
@@ -299,7 +299,8 @@ cl_status_t cl_timer_start(IN cl_timer_t * const p_timer,
 {
 	struct timeval curtime;
 	cl_list_item_t *p_list_item;
-	uint32_t delta_time = time_ms;
+	uint32_t delta_time_sec = time_ms / 1000;
+	uint32_t delta_time_usec = (time_ms % 1000) * 1000;
 
 	CL_ASSERT(p_timer);
 	CL_ASSERT(p_timer->state == CL_INITIALIZED);
@@ -324,9 +325,10 @@ cl_status_t cl_timer_start(IN cl_timer_t * const p_timer,
 	/* if (delta_time < 1000.0) {delta_time = 1000;} */
 
 	/* Calculate the timeout. */
-	p_timer->timeout.tv_sec = curtime.tv_sec + (delta_time / 1000);
+	p_timer->timeout.tv_sec = curtime.tv_sec + delta_time_sec +
+		((curtime.tv_usec + delta_time_usec) / 1000000);
 	p_timer->timeout.tv_nsec =
-	    (curtime.tv_usec + ((delta_time % 1000) * 1000)) * 1000;
+	    ((curtime.tv_usec + delta_time_usec) % 1000000) * 1000;
 
 	/* Add the timer to the queue. */
 	if (cl_is_qlist_empty(&gp_timer_prov->queue)) {
Signature · 6 lines
1.6.2.4

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#2 lore raw
In reply to a message not in the archive
Quoted 3 lines · Yevgeny Kliteynik

When calculating p_timer->timeout.tv_sec and p_timer->timeout.tv_nsec, the carry was ignored, resulting in wrong value in p_timer->timeout.tv_sec, and value > 10^9 in p_timer->timeout.tv_nsec (illegal value).

Nice catch. And I also see a similar issue in cl_timer_trim().

See a comments about the patch below.

Quoted 12 lines
---
 opensm/complib/cl_timer.c |    8 +++++---
 1 files changed, 5 insertions(+), 3 deletions(-)
opensm/complib/cl_timer.c +0 -0
index 2acdb51..09a5584 100644

opensm/complib/cl_timer.c cl_status_t cl_timer_start(IN cl_timer_t * const p_timer, · line 303

Quoted 6 lines
opensm/complib/cl_timer.c +2 -1
@@ -299,7 +299,8 @@ cl_status_t cl_timer_start(IN cl_timer_t * const p_timer,
 {
 	struct timeval curtime;
 	cl_list_item_t *p_list_item;
-	uint32_t delta_time = time_ms;
+	uint32_t delta_time_sec = time_ms / 1000;
+	uint32_t delta_time_usec = (time_ms % 1000) * 1000;

In order to prevent compatibility and porting issues would it be better to use struct timeval's types here:

	time_t delta_time_sec;
	suseconds_t delta_time_usec_t delta_time_usec;

, instead of fixed size integers?

opensm/complib/cl_timer.c cl_status_t cl_timer_start(IN cl_timer_t * const p_timer, · line 331

Quoted 13 lines
opensm/complib/cl_timer.c +3 -2
 	CL_ASSERT(p_timer);
 	CL_ASSERT(p_timer->state == CL_INITIALIZED);
@@ -324,9 +325,10 @@ cl_status_t cl_timer_start(IN cl_timer_t * const p_timer,
 	/* if (delta_time < 1000.0) {delta_time = 1000;} */
 
 	/* Calculate the timeout. */
-	p_timer->timeout.tv_sec = curtime.tv_sec + (delta_time / 1000);
+	p_timer->timeout.tv_sec = curtime.tv_sec + delta_time_sec +
+		((curtime.tv_usec + delta_time_usec) / 1000000);
 	p_timer->timeout.tv_nsec =
-	    (curtime.tv_usec + ((delta_time % 1000) * 1000)) * 1000;
+	    ((curtime.tv_usec + delta_time_usec) % 1000000) * 1000;

Or even something like this:

	strcut timeval curtime, deltatime;

	gettimeofday(&curtime, NULL);

	addtime.tv_sec = time_ms / 1000;
	addtime.tv_usec = (time_ms % 1000) * 1000;

	timeradd(&curtime, &addtime, &timer->timeout);

?

Sasha

Quoted 11 lines
opensm/complib/cl_timer.c +0 -0
 	/* Add the timer to the queue. */
 	if (cl_is_qlist_empty(&gp_timer_prov->queue)) {
Signature · 6 lines
1.6.2.4

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#3 lore raw
Quoted 61 lines · Sasha Khapyorsky
Quoted 3 lines · Yevgeny Kliteynik

When calculating p_timer->timeout.tv_sec and p_timer->timeout.tv_nsec, the carry was ignored, resulting in wrong value in p_timer->timeout.tv_sec, and value> 10^9 in p_timer->timeout.tv_nsec (illegal value).

Nice catch. And I also see a similar issue in cl_timer_trim().

See a comments about the patch below.

Quoted 12 lines
---
  opensm/complib/cl_timer.c |    8 +++++---
  1 files changed, 5 insertions(+), 3 deletions(-)
opensm/complib/cl_timer.c +0 -0
index 2acdb51..09a5584 100644

opensm/complib/cl_timer.c cl_status_t cl_timer_start(IN cl_timer_t * const p_timer, · line 303

Quoted 6 lines
opensm/complib/cl_timer.c +2 -1
@@ -299,7 +299,8 @@ cl_status_t cl_timer_start(IN cl_timer_t * const p_timer,
  {
  	struct timeval curtime;
  	cl_list_item_t *p_list_item;
-	uint32_t delta_time = time_ms;
+	uint32_t delta_time_sec = time_ms / 1000;
+	uint32_t delta_time_usec = (time_ms % 1000) * 1000;

In order to prevent compatibility and porting issues would it be better to use struct timeval's types here:

	time_t delta_time_sec;
	suseconds_t delta_time_usec_t delta_time_usec;

, instead of fixed size integers?

opensm/complib/cl_timer.c cl_status_t cl_timer_start(IN cl_timer_t * const p_timer, · line 331

Quoted 13 lines
opensm/complib/cl_timer.c +3 -2
  	CL_ASSERT(p_timer);
  	CL_ASSERT(p_timer->state == CL_INITIALIZED);
@@ -324,9 +325,10 @@ cl_status_t cl_timer_start(IN cl_timer_t * const p_timer,
  	/* if (delta_time<  1000.0) {delta_time = 1000;} */
 
  	/* Calculate the timeout. */
-	p_timer->timeout.tv_sec = curtime.tv_sec + (delta_time / 1000);
+	p_timer->timeout.tv_sec = curtime.tv_sec + delta_time_sec +
+		((curtime.tv_usec + delta_time_usec) / 1000000);
  	p_timer->timeout.tv_nsec =
-	    (curtime.tv_usec + ((delta_time % 1000) * 1000)) * 1000;
+	    ((curtime.tv_usec + delta_time_usec) % 1000000) * 1000;

Or even something like this:

	strcut timeval curtime, deltatime;

	gettimeofday(&curtime, NULL);

	addtime.tv_sec = time_ms / 1000;
	addtime.tv_usec = (time_ms % 1000) * 1000;

	timeradd(&curtime,&addtime,&timer->timeout);
Quoted 1 line

?

Sure, why not. It won't be that clean, because p_timer->timeout and curtime/deltatime are different types, but I get the idea.

-- Yevgeny

Quoted 14 lines

Sasha

Quoted 11 lines
opensm/complib/cl_timer.c +0 -0
  	/* Add the timer to the queue. */
  	if (cl_is_qlist_empty(&gp_timer_prov->queue)) {
Signature · 6 lines
1.6.2.4

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#5 lore raw
In reply to a message not in the archive
Quoted 7 lines · Yevgeny Kliteynik
Quoted 6 lines · Sasha Khapyorsky
Quoted 3 lines · Yevgeny Kliteynik

When calculating p_timer->timeout.tv_sec and p_timer->timeout.tv_nsec, the carry was ignored, resulting in wrong value in p_timer->timeout.tv_sec,
and value> 10^9 in p_timer->timeout.tv_nsec (illegal value).

Nice catch. And I also see a similar issue in cl_timer_trim().

BTW, any idea what are the (obviously historical)
reasons for these lines in the code?

323: /* do not do 0 wait ! */
324: /* if (delta_time < 1000.0) {delta_time = 1000;} */

-- Yevgeny

Quoted 52 lines
Quoted 52 lines

See a comments about the patch below.

Quoted 12 lines
---
   opensm/complib/cl_timer.c |    8 +++++---
   1 files changed, 5 insertions(+), 3 deletions(-)
opensm/complib/cl_timer.c +0 -0
index 2acdb51..09a5584 100644

opensm/complib/cl_timer.c cl_status_t cl_timer_start(IN cl_timer_t * const p_timer, · line 303

Quoted 6 lines
opensm/complib/cl_timer.c +2 -1
@@ -299,7 +299,8 @@ cl_status_t cl_timer_start(IN cl_timer_t * const p_timer,
   {
   	struct timeval curtime;
   	cl_list_item_t *p_list_item;
-	uint32_t delta_time = time_ms;
+	uint32_t delta_time_sec = time_ms / 1000;
+	uint32_t delta_time_usec = (time_ms % 1000) * 1000;

In order to prevent compatibility and porting issues would it be better to use struct timeval's types here:

	time_t delta_time_sec;
	suseconds_t delta_time_usec_t delta_time_usec;

, instead of fixed size integers?

opensm/complib/cl_timer.c cl_status_t cl_timer_start(IN cl_timer_t * const p_timer, · line 331

Quoted 13 lines
opensm/complib/cl_timer.c +3 -2
   	CL_ASSERT(p_timer);
   	CL_ASSERT(p_timer->state == CL_INITIALIZED);
@@ -324,9 +325,10 @@ cl_status_t cl_timer_start(IN cl_timer_t * const p_timer,
   	/* if (delta_time<   1000.0) {delta_time = 1000;} */
 
   	/* Calculate the timeout. */
-	p_timer->timeout.tv_sec = curtime.tv_sec + (delta_time / 1000);
+	p_timer->timeout.tv_sec = curtime.tv_sec + delta_time_sec +
+		((curtime.tv_usec + delta_time_usec) / 1000000);
   	p_timer->timeout.tv_nsec =
-	    (curtime.tv_usec + ((delta_time % 1000) * 1000)) * 1000;
+	    ((curtime.tv_usec + delta_time_usec) % 1000000) * 1000;

Or even something like this:

	strcut timeval curtime, deltatime;

	gettimeofday(&curtime, NULL);

	addtime.tv_sec = time_ms / 1000;
	addtime.tv_usec = (time_ms % 1000) * 1000;
Quoted 25 lines
Quoted 3 lines
	timeradd(&curtime,&addtime,&timer->timeout);

?

Sure, why not. It won't be that clean, because p_timer->timeout and curtime/deltatime are different types, but I get the idea.

-- Yevgeny

Quoted 14 lines

Sasha

Quoted 11 lines
opensm/complib/cl_timer.c +0 -0
   	/* Add the timer to the queue. */
   	if (cl_is_qlist_empty(&gp_timer_prov->queue)) {
Signature · 6 lines
1.6.2.4

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#7 lore raw
In reply to a message not in the archive
Quoted 6 lines · Yevgeny Kliteynik

BTW, any idea what are the (obviously historical)
reasons for these lines in the code?

323: /* do not do 0 wait ! */
324: /* if (delta_time < 1000.0) {delta_time = 1000;} */

Have no idea. This is from day zero of git history.

Sasha
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#8 lore raw
Quoted 9 lines · Sasha Khapyorsky
Quoted 6 lines · Yevgeny Kliteynik

BTW, any idea what are the (obviously historical)
reasons for these lines in the code?

323: /* do not do 0 wait ! */
324: /* if (delta_time< 1000.0) {delta_time = 1000;} */

Have no idea. This is from day zero of git history.

Well, it's not there any more.

-- Yevgeny

Quoted 2 lines

Sasha

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html