Skip to contentSkip to conversation

do_gettimeofday() fails to compensate for lost ticks

PATCH In review Khalid Aziz 1 participant 2 messages Sep 24, 2003 PA-RISC architecture

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 do_gettimeofday() fails to compensate for lost ticks

I saw this problem on ia64 and when I checked parisc code, it also seems to have the same problem.

do_gettimeofday() needs to account for lost ticks before returning current time and it fails to do that. do_gettimeofday() on other architectures compensate for lost ticks correctly. Due to this bug, if you repeatedly do clock_settime() immediately followed by clock_gettime() and compare the time returned by clock_gettime() to the time set by clock_settime(), you will eventually see clock going backwards. I am attaching a test program from POSIX testsuite at the end that exposes this bug. Run this test in a continuous loop that stops when test fails.

Following patch should address this issue.

--
Khalid

=============================

arch/parisc/kernel/time.c +1 -0
@@ -166,6 +166,7 @@
 	
 		sec = xtime.tv_sec;
 		usec += xtime.tv_usec;
+		usec += (jiffies - wall_jiffies) * (1000000 / HZ);
 	}
 	read_unlock_irqrestore(&xtime_lock, flags);
 

==============================
/*
* Copyright (c) 2002, Intel Corporation. All rights reserved.
* Created by: julie.n.fleischer REMOVE-THIS AT intel DOT com
* This file is licensed under the GPL license. For the full content
* of this license, see the COPYING file at the top level of this
* source tree.

* Test that clock_settime() sets clock_id to tp.
*
* The clock_id chosen for this test is CLOCK_REALTIME.
* The date chosen is Nov 12, 2002 ~11:13am (date when test was first
* written).
*/
#include <stdio.h>
#include <time.h>
/*#include "posixtest.h"
#include "helpers.h"*/

#define TESTTIME 1037128358
#define ACCEPTABLEDELTA 1
#define PTS_UNRESOLVED 2
#define PTS_PASS 0
#define PTS_FAIL 1

int getBeforeTime(struct timespec *tpget)

{
	if (clock_gettime(CLOCK_REALTIME, tpget) != 0) {
		perror("clock_gettime() did not return success\n");
		perror("clock may not be reset properly\n");
		return PTS_UNRESOLVED;
	}
	return PTS_PASS;
}

int setBackTime(struct timespec tpset)

{
	if (clock_settime(CLOCK_REALTIME, &tpset) != 0) {
		perror("clock_settime() did not return success\n");
		perror("clock may not be reset properly\n");
		return PTS_UNRESOLVED;
	}
	return PTS_PASS;
}

int main(int argc, char *argv[])

{
	struct timespec tpset, tpget, tpreset;
	int delta;

	getBeforeTime(&tpreset);

	tpset.tv_sec = TESTTIME;
	tpset.tv_nsec = 0;
	if (clock_settime(CLOCK_REALTIME, &tpset) == 0) {
		if (clock_gettime(CLOCK_REALTIME, &tpget) == -1) {
			perror("Error in clock_gettime()");
			return 2;
		}
		delta = tpget.tv_sec-tpset.tv_sec;
		if ( (delta <= ACCEPTABLEDELTA) && (delta >= 0) ) {
			printf("Test PASSED\n");
			setBackTime(tpreset);
			return 0;
		} else {
			printf("delta = %d, tpget=%d,%d, tpset=%d,%d\n", delta, tpget.tv_sec, tpget.tv_nsec, tpset.tv_sec, tpset.tv_nsec);
			printf("clock does not appear to be set\n");
			setBackTime(tpreset);
			return 1;
		}
	} else {
		printf("clock_settime() failed\n");
		return 2;
	}

	printf("This code should not be executed.\n");
	return 2;
}
#1 lore raw

Turns out the problem is not do_gettimeofday() not accounting for lost ticks, rather it is do_settimeofday() double compensating for lost ticks. Here is a patch to fix this problem.

---
Khalid

=========================

arch/parisc/kernel/time.c +0 -1
@@ -191,7 +191,6 @@
 		 * done, and then undo it!
 		 */
 		tv->tv_usec -= gettimeoffset();
-		tv->tv_usec -= (jiffies - wall_jiffies) * (1000000 / HZ);
 
 		while (tv->tv_usec < 0) {
 			tv->tv_usec += 1000000;
Quoted 17 lines

I saw this problem on ia64 and when I checked parisc code, it also seems to have the same problem.

do_gettimeofday() needs to account for lost ticks before returning current time and it fails to do that. do_gettimeofday() on other architectures compensate for lost ticks correctly. Due to this bug, if you repeatedly do clock_settime() immediately followed by clock_gettime() and compare the time returned by clock_gettime() to the time set by clock_settime(), you will eventually see clock going backwards. I am attaching a test program from POSIX testsuite at the end that exposes this bug. Run this test in a continuous loop that stops when test fails.

Following patch should address this issue.

--
Khalid