[Mono-devel-list] [PATCH] Bug Fix for GetTickCount in WAPI IO Layer

Paolo Molaro lupus at ximian.com
Mon Feb 28 05:08:16 EST 2005


On 02/26/05 Jesse Towner wrote:
> This should fix the problem of System.Environment.TickCount reporting
> incorrect values on
> non-windows systems. It now determines the boot time of the system, and
> from there can
> determine the uptime, which is what GetTickCount requires. I haven't
> tested it on all of the

> +/* NetBSD, FreeBSD, OpenBSD and	MacOS X	*/
> +#ifndef	GET_BOOT_TIME_METHOD_FOUND
> +#	if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || \
> +	   defined(__APPLE__) || defined(__DARWIN__)
> +#		define GET_BOOT_TIME_SYSCTL

Could you add configure tests for this, instead of using operating system
defines?

> +/* Ultrix & Irix*/
> +#ifndef	GET_BOOT_TIME_METHOD_FOUND
> +#	if defined(ultrix) || defined(IRIX)
> +#		define GET_BOOT_TIME_KMEM

Don't you need root privileges to access kmem? In this case this
approach doesn't seem useful, since it'd work only for root.

> +/* Needed on Tru64 */
> +#ifdef GET_BOOT_TIME_TBLSYSINFO
> +#	include <sys/table.h>
> +#endif

Do you have access to such systems? If the code is not tested
it's better to not include it.

> +gboolean _wapi_get_boottime (struct timeval *tv)
> +{
> +	if (tv == NULL)
> +		return FALSE;
> +
> +	mono_once (&get_boot_time_once, get_boot_time_init);
> +
> +	if (has_boot_time == FALSE)
> +		return FALSE;
> +
> +	tv->tv_sec = boot_time_cache.tv_sec;
> +	tv->tv_usec = boot_time_cache.tv_usec;
> +	return TRUE;
> +}
> +
> +gboolean _wapi_get_uptime (struct timeval *tv)
> +{
> +	struct timeval bt;
> +	struct timeval now;
> +
> +	if (tv == NULL)
> +		return FALSE;
> +
> +	if (_wapi_get_boottime (&bt) == FALSE)
> +		return FALSE;
> +
> +	if (gettimeofday (&now, NULL) == -1)
> +		return FALSE;
> +
> +	tv->tv_sec = now.tv_sec - bt.tv_sec;
> +	tv->tv_usec = now.tv_usec - bt.tv_usec;
> +	return TRUE;
> +}

Both these functions can be static in the file.

>  guint32 GetTickCount (void)
>  {
>  	struct timeval tv;
> -	guint32 ret;
> +
> +	if (_wapi_get_uptime (&tv) == FALSE)
> +		return 0;

This function needs to be fast, so what you should do is:
*) have an init function that the runtime can call at startup
that gets the boot time. If no boot time is available, just
register the process startup time: this means that the boot time
is always defined.
*) GetTickCount is changed to just call gettimeofday(), subtract
the boot time and convert to milliseconds.

Thanks.
lupus

-- 
-----------------------------------------------------------------
lupus at debian.org                                     debian/rules
lupus at ximian.com                             Monkeys do it better



More information about the Mono-devel-list mailing list