Bug 38666 - Review (and fix) uses of mono_msec_ticks
Summary: Review (and fix) uses of mono_msec_ticks
Status: RESOLVED FIXED
Alias: None
Product: Runtime
Classification: Mono
Component: General ()
Version: unspecified
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Ludovic Henry
URL:
: 38663 ()
Depends on:
Blocks:
 
Reported: 2016-02-11 11:38 UTC by Rolf Bjarne Kvinge [MSFT]
Modified: 2016-03-04 11:50 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Rolf Bjarne Kvinge [MSFT] 2016-02-11 11:38:18 UTC
mono_msec_ticks overflows after ~20 days of uptime (of the machine), which means it's never safe to use for measuring elapsed time unless special care is taken to take overflows into account.

A quick grep shows it's used in several places:

> > git grep mono_msec_ticks
> mono/io-layer/processes.c:      start = mono_msec_ticks ();
> mono/io-layer/processes.c:              now = mono_msec_ticks ();
> mono/io-layer/timefuncs.c:      return mono_msec_ticks ();
> mono/metadata/gc.c:                     guint32 start_ticks = mono_msec_ticks ();
> mono/metadata/gc.c:                             guint32 current_ticks = mono_msec_ticks ();
> mono/metadata/icall-def.h:ICALL(ENV_15, "get_TickCount", mono_msec_ticks)
> mono/metadata/monitor.c:                then = mono_msec_ticks ();
> mono/metadata/monitor.c:                                now = mono_msec_ticks ();
> mono/metadata/threadpool-ms.c:  return mono_msec_ticks () >= threadpool->heuristic_last_dequeue + threshold;
> mono/metadata/threadpool-ms.c:                  ts = mono_msec_ticks ();
> mono/metadata/threadpool-ms.c:                  interval_left -= mono_msec_ticks () - ts;
> mono/metadata/threadpool-ms.c:  threadpool->heuristic_last_dequeue = mono_msec_ticks ();
> mono/metadata/threadpool-ms.c:          guint32 sample_end = mono_msec_ticks ();
> mono/metadata/threadpool-ms.c:                  threadpool->heuristic_last_adjustment = mono_msec_ticks ();
> mono/metadata/threadpool-ms.c:          start = mono_msec_ticks ();
> mono/metadata/threadpool-ms.c:          timeout -= mono_msec_ticks () - start;
> mono/metadata/threadpool-ms.c:                  timeout -= mono_msec_ticks () - start;
> mono/metadata/threads.c:        start_time = mono_msec_ticks ();
> mono/metadata/threads.c:                timeout -= mono_msec_ticks () - start_time;
> mono/metadata/threads.c:                start_time = mono_msec_ticks ();
> mono/mini/debugger-agent.c:                             msecs = mono_msec_ticks ();
> mono/utils/mono-proclib.c:                              boot_time = mono_100ns_datetime () - ((guint64)mono_msec_ticks ()) * 10000;

and none without looking deeply my guess is that most of these are broken (just like bug #38663).

My suggestion would be to use mono_100ns_ticks instead, which overflows in ~500 years (if I got the math right).
Comment 1 Ludovic Henry 2016-02-11 11:39:14 UTC
*** Bug 38663 has been marked as a duplicate of this bug. ***
Comment 2 Ludovic Henry 2016-02-11 11:40:04 UTC
To fix it, I will use mono_nsec_ticks for the threadpool-ms.c and threads.c ones.
Comment 3 Ludovic Henry 2016-03-04 11:50:44 UTC
This is fixed with https://github.com/mono/mono/pull/2721