Overview
| Artifact ID: | 523ddc910ac48d01f506dc01797b9eeb5da7af12 |
|---|---|
| Ticket: | 411603ffffffffffffffffffffffffffffffffff
timeout bug in tclUnixThrd.c |
| User & Date: | ruppert on 2001-03-27 10:49:31 |
Changes
- assignee changed to: "nobody"
- cmimetype changed to: "text/plain"
- comment changed to:
Hi, a collegue of mine has debugged a strange behaviour of a Tk script which was executed in a Tcl interpreter with threads enabled under Solaris 2.6. We observed that even trivial scripts which include an entry widget with a blinking cursor drive the cpu load to substantial values (up to 100% on smaller workstations, i.e. Sparcstation 5), as long as the cursor is blinking! This can be reproduced with the following trivial script: entry .entry -bg white -relief sunken pack .entry -side left -fill x -expand yes To reproduce: Execute these two lines in a wish which has been built with --enable-scripts, click into the entry widget (which makes the cursor start blinking) and observe the cpu load in a perfmeter. We could reproduce this with Tk versions 8.3.0, 8.3.2 and 8.4a2 on Solaris 2.6; a collegue reproduced this also on a Linux box. After some extensive debugging, the ultimate reason for this was found: a timeout value in Tcl_ConditionWait (tclUnixThrd.c) is computed in a way which is, for timeout values < 1sec, almost always incorrect: ------------------------------------------------------------------ void Tcl_ConditionWait(condPtr, mutexPtr, timePtr) Tcl_Condition *condPtr;/* Really (pthread_cond_t **) */ Tcl_Mutex *mutexPtr;/* Really (pthread_mutex_t **) */ Tcl_Time *timePtr;/* Timeout on waiting period */ { pthread_cond_t *pcondPtr; pthread_mutex_t *pmutexPtr; struct timespec ptime; if (*condPtr == NULL) { MASTER_LOCK; /* * Double check inside mutex to avoid race, * then initialize condition variable if necessary. */ if (*condPtr == NULL) { pcondPtr = (pthread_cond_t *)ckalloc(sizeof(pthread_cond_t)); pthread_cond_init(pcondPtr, NULL); *condPtr = (Tcl_Condition)pcondPtr; TclRememberCondition(condPtr); } MASTER_UNLOCK; } pmutexPtr = *((pthread_mutex_t **)mutexPtr); pcondPtr = *((pthread_cond_t **)condPtr); if (timePtr == NULL) { pthread_cond_wait(pcondPtr, pmutexPtr); } else { ptime.tv_sec = timePtr->sec + TclpGetSeconds(); <------ ptime.tv_nsec = 1000 * timePtr->usec; <------ pthread_cond_timedwait(pcondPtr, pmutexPtr, &ptime); } } --------------------------------------------------------------- Explanation: the lines marked with the arrows attempt to calculate a time value from a desired time interval (*timePtr), which is composed from seconds and microseconds, and the current time, which is obtained with TclpGetSeconds, thus completely ignoring fractions of a second. Assume a desired timeout value of 300 ms, which is the default value for cursor blinking, scrollbar repeat rates etc. in Tk: timePtr->sec is zero, and timePtr->usec is 300000. ptime.tv_sec (the desired "firing time" of pthread_cond_timedwait) will thus always be the "current" second value, and ptime.tv_nsec corresponds to 300 ms. Therefore, 70% of the time, pthread_cond_timedwait will return immediately. Because this code is invoked from a loop waiting for events in situations when a blinking cursor has to be updated, the function Tcl_ConditionWait almost never really waits, but instead executes more or less permanently, as long as the cursor is blinking, thus leading to the observed cpu load values. Suggested fix: the timeout value needs to correctly take into account the microsecond component of the current time, including possible overflow situations when microsecond values add up to > 1000000. We suggest to replace the above two lines with the following: Tcl_Time now; TclpGetTime(&now); ptime.tv_sec = timePtr->sec + now.sec + ( timePtr->usec + now.usec ) / 1000000; ptime.tv_nsec = 1000 * (( timePtr->usec + now.usec ) % 1000000); We have tried this fix, and this has removed the problem completely. Best Regards D.Ruppert RTS GmbH Schwieberdingen/Germany [email protected] - foundin changed to: "None"
- priority changed to: "5 Medium"
- resolution changed to: "None"
- status changed to: "Open"
- submitter changed to: "ruppert"
- subsystem changed to: "49. Threading"
- title changed to: "timeout bug in tclUnixThrd.c"
- type changed to: "Bug"