Skip to contentSkip to conversation

Fix bug of the test timer_create.

PATCH In review Bian Naimeng 3 participants 3 messages Jan 15, 2011 – Jan 16, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix bug of the test timer_create.

CLOCK_THREAD_CPUTIME_ID and CLOCK_PROCESS_CPUTIME_ID are CPU-time clock, so when the thread/process invokes nanosleep, these clocks will go sleeping too.

---
 .../conformance/interfaces/timer_create/10-1.c     |   36 +++++++++++++------
 .../conformance/interfaces/timer_create/11-1.c     |   35 +++++++++++++------
 2 files changed, 48 insertions(+), 23 deletions(-)
testcases/open_posix_testsuite/conformance/interfaces/timer_create/10-1.c +24 -12
index 27d5c1c..23a777e 100644
@@ -24,9 +24,12 @@
 #define SLEEPDELTA 3
 #define ACCEPTABLEDELTA 1
 
+int caught = 0;
+
 void handler(int signo)
 {
 	printf("Caught signal\n");
+	caught = 1;
 }
 
 int main(int argc, char *argv[])
@@ -39,8 +42,8 @@ int main(int argc, char *argv[])
 	struct sigaction act;
 	timer_t tid;
 	struct itimerspec its;
-	struct timespec ts, tsleft;
-	int rc;
+	int rc, overrun = 0;
+	time_t stime = 0, etime = 0;
 
 	rc = sysconf(_SC_CPUTIME);
 	printf("sysconf(_SC_CPUTIME) returns: %d\n", rc);
@@ -59,9 +62,6 @@ int main(int argc, char *argv[])
 	its.it_value.tv_sec = TIMERSEC;
 	its.it_value.tv_nsec = 0;
 
-	ts.tv_sec=TIMERSEC+SLEEPDELTA;
-	ts.tv_nsec=0;
-
 	if (sigemptyset(&act.sa_mask) == -1) {
 		perror("Error calling sigemptyset");
 		return PTS_UNRESOLVED;
@@ -76,27 +76,39 @@ int main(int argc, char *argv[])
 		return PTS_UNRESOLVED;
 	}
 
+	if ((time_t)(-1) == time(&stime)) {
+		perror("time() did not return success");
+		return PTS_UNRESOLVED;
+	}
+
 	if (timer_settime(tid, 0, &its, NULL) != 0) {
 		perror("timer_settime() did not return success");
 		return PTS_UNRESOLVED;
 	}
 
-	if (nanosleep(&ts, &tsleft) != -1) {
-		perror("nanosleep() not interrupted");
-		return PTS_FAIL;
+	while(!caught);
+
+	if ((time_t)(-1) == time(&etime)) {
+		perror("time() did not return success");
+		return PTS_UNRESOLVED;
+	}
+
+	if (-1 == (overrun = timer_getoverrun(tid))) {
+		perror("timer_getoverrun() did not return success");
+		return PTS_UNRESOLVED;
 	}
 
-	if (abs(tsleft.tv_sec-SLEEPDELTA) <= ACCEPTABLEDELTA) {
-		printf("Test PASSED");
+	if (etime - stime == TIMERSEC + overrun) {
+		printf("Test PASSED\n");
 		return PTS_PASS;
 	} else {
 		printf("Timer did not last for correct amount of time\n");
 		printf("timer: %d != correct %d\n",
-				(int) ts.tv_sec- (int) tsleft.tv_sec,
+				(int) etime - (int) stime,
 				TIMERSEC);
 		return PTS_FAIL;
 	}
 
 	return PTS_UNRESOLVED;
 #endif
-}
\ No newline at end of file
+}
testcases/open_posix_testsuite/conformance/interfaces/timer_create/11-1.c +24 -11
index bd59e8f..c34b451 100644
@@ -22,9 +22,12 @@
 #define SLEEPDELTA 3
 #define ACCEPTABLEDELTA 1
 
+int caught = 0;
+
 void handler(int signo)
 {
 	printf("Caught signal\n");
+	caught = 1;
 }
 
 int main(int argc, char *argv[])
@@ -38,7 +41,8 @@ int main(int argc, char *argv[])
 	struct sigaction act;
 	timer_t tid;
 	struct itimerspec its;
-	struct timespec ts, tsleft;
+	time_t  stime = 0, etime = 0;
+	int overrun = 0;
 
 	if (rc == -1) {
 		printf("_POSIX_THREAD_CPUTIME unsupported\n");
@@ -56,9 +60,6 @@ int main(int argc, char *argv[])
 	its.it_value.tv_sec = TIMERSEC;
 	its.it_value.tv_nsec = 0;
 
-	ts.tv_sec=TIMERSEC+SLEEPDELTA;
-	ts.tv_nsec=0;
-
 	if (sigemptyset(&act.sa_mask) == -1) {
 		perror("Error calling sigemptyset");
 		return PTS_UNRESOLVED;
@@ -73,23 +74,35 @@ int main(int argc, char *argv[])
 		return PTS_UNRESOLVED;
 	}
 
+	if ((time_t)(-1) == time(&stime)) {
+		perror("time() did not return success");
+		return PTS_UNRESOLVED;
+	}
+
 	if (timer_settime(tid, 0, &its, NULL) != 0) {
 		perror("timer_settime() did not return success");
 		return PTS_UNRESOLVED;
 	}
 
-	if (nanosleep(&ts, &tsleft) != -1) {
-		perror("nanosleep() not interrupted");
-		return PTS_FAIL;
+	while(!caught);
+
+	if ((time_t)(-1) == time(&etime)) {
+		perror("time() did not return success");
+		return PTS_UNRESOLVED;
+	}
+
+	if (-1 == (overrun = timer_getoverrun(tid))) {
+		perror("timer_getoverrun() did not return success");
+		return PTS_UNRESOLVED;
 	}
 
-	if (abs(tsleft.tv_sec-SLEEPDELTA) <= ACCEPTABLEDELTA) {
-		printf("Test PASSED");
+	if (etime - stime == TIMERSEC + overrun) {
+		printf("Test PASSED\n");
 		return PTS_PASS;
 	} else {
 		printf("Timer did not last for correct amount of time\n");
 		printf("timer: %d != correct %d\n",
-				(int) ts.tv_sec- (int) tsleft.tv_sec,
+				(int) etime - (int) stime,
 				TIMERSEC);
 		return PTS_FAIL;
 	}
@@ -100,4 +113,4 @@ int main(int argc, char *argv[])
 	return PTS_UNSUPPORTED;
 #endif
 
-}
\ No newline at end of file
+}
Signature · 20 lines
1.7.0.4




-- 
Regards
Bian Naimeng


------------------------------------------------------------------------------
Protect Your Site and Customers from Malware Attacks
Learn about various malware tactics and how to avoid them. Understand 
malware threats, the impact they can have on your business, and how you 
can protect your company and customers by using code signing.
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list
#1 lore raw
Quoted 3 lines · Bian Naimeng

CLOCK_THREAD_CPUTIME_ID and CLOCK_PROCESS_CPUTIME_ID are CPU-time clock, so when the thread/process invokes nanosleep, these clocks will go sleeping too.

Fixed -- thanks!
-Garrett

------------------------------------------------------------------------------
Protect Your Site and Customers from Malware Attacks
Learn about various malware tactics and how to avoid them. Understand malware threats, the impact they can have on your business, and how you can protect your company and customers by using code signing.
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list

#2 lore raw

Hi!

Quoted 3 lines
Quoted 3 lines

CLOCK_THREAD_CPUTIME_ID and CLOCK_PROCESS_CPUTIME_ID are CPU-time clock, so when the thread/process invokes nanosleep, these clocks will go sleeping too.

I've send a sligtly better version of this patch a couple of weeks ago and that was one of the patches I've been constantly reminding you about.

I'm a little worried here that mixing time() that returns time since the epoch and per process cpu timers wouldn't work on machine under load.

My patch uses only per cpu process timers so should be fine.

Could you please have a look at my version?

Signature · 13 lines
Cyril Hrubis
[email protected]

------------------------------------------------------------------------------
Protect Your Site and Customers from Malware Attacks
Learn about various malware tactics and how to avoid them. Understand 
malware threats, the impact they can have on your business, and how you 
can protect your company and customers by using code signing.
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list