Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove call of localtime #3246

Merged
merged 1 commit into from
Dec 14, 2023
Merged

Conversation

beutlich
Copy link
Member

@beutlich beutlich commented Dec 2, 2019

There is a race condition between localtime and ftime/gettimeofday in ModelicaInternal_getTime. In case ftime/gettimeofday is called at a very new second, this can lead to an inaccuracy of (almost) 1 second for the calculated time.

Using gmtime, there is no need to call localtime. Instead the struct tm can be directly computed from the obtained timebuffer and timezone information.

@beutlich beutlich added the L: C-Sources Issue addresses Modelica/Resources/C-Sources label Dec 2, 2019
@beutlich beutlich added this to the MSL4.0.0 milestone Dec 2, 2019
@beutlich beutlich self-assigned this Dec 2, 2019
@HansOlsson
Copy link
Contributor

HansOlsson commented Dec 4, 2019

The ftime handling of time-zones and DST is not clear to me, and thus I am not sure if it is worth the effort.

In detail: as far as I can tell POSIX has removed ftime, and Microsoft does in
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/ftime-ftime32-ftime64?view=vs-2019 refer to
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/tzset?view=vs-2019
which claims that US rules for DST are used, so it is not clear that it is currently useful in Europe.

In particular it is not clear if it is just a flag, or whether other values that 0 and 1 are intended for odd cases (two hour DST, 20 minute DST etc).

(Oh, and for POSIX it isn't even clear if the DST-flag indicates the presence of DST some time during the year or whether is currently active.)

@sjoelund
Copy link
Member

sjoelund commented Dec 6, 2019

Wouldn't it just be easier to copy the tm struct to avoid the race condition? struct tm a = *localtime(&t);, etc? I assume this only happened for the non-secure/re-entrant versions of localtime?

@beutlich
Copy link
Member Author

beutlich commented Dec 6, 2019

It's about the two different calls to timing functions:

  1. localtime to retrieve the tm struct w/o seconds and
  2. ftime/gettimeofday to retrieve the milli- or mircoseconds
    On average, every 1000th call is 1 second of from the actual time since the microseconds of a new second are reported for the previous second.

I am not sure if relevant at all or if the proposed changes are good enough, but what I did:

  • Avoid ftime for POSIX (since deprecated) and use gettimeofday.
  • Do not use tz.tz_dsttime from gettimeofday and rely that DST offset is considered by tz_minuteswest.
  • Consider non-zero dstflag for 1 hour DST offset. (Can test after 2020-03-29 if it works in Europe, too.)

@beutlich beutlich removed this from the MSL4.0.0 milestone Jan 7, 2020
@CLAassistant
Copy link

CLA assistant check
All committers have signed the CLA.

@HansOlsson
Copy link
Contributor

What is the status of this one?
I noticed that:

  • There are some comments indicating other changes - that I don't see in this PR.
  • It still exists (don't know for which version), and is in my review queue.
  • There were some tests that should be done after 2020-03-29 (and before sometime in the autumn). I don't know their status.

@beutlich beutlich force-pushed the update-gettime branch 2 times, most recently from 46daec6 to 42ca6cc Compare July 10, 2020 15:31
@beutlich
Copy link
Member Author

What is the status of this one?

Thanks for reminding me.

Wouldn't it just be easier to copy the tm struct to avoid the race condition?

There is no milliseconds or microseconds in struct tm. Therefore there are two different calls to determine current time in current code. This should be avoided. In the PR I also test for dstflag != 0 only.

The ftime handling of time-zones and DST is not clear to me

The dstflag is nonzero if DST is currently in effect for the local time zone. That is what I tested in DST (now) and 7 months ago (when DST was not in effect).

I can tell POSIX has removed ftime

Yes. gettimeofday is used in the PR for that reason.

so it is not clear that it is currently useful in Europe

I tested successfully in CET with and without DST in effect.

In particular it is not clear if it is just a flag

It is used as a flag.

(Oh, and for POSIX it isn't even clear if the DST-flag indicates the presence of DST some time during the year or whether is currently active.)

Yes, would be good to get some feedback from POSIX expert @sjoelund if the code is formally correct and if it can be successfully tested.

@beutlich beutlich added this to the MSL4.1.0 milestone Jul 10, 2020
@beutlich
Copy link
Member Author

@HansOlsson @sjoelund Any ideas how to proceed?

@HansOlsson
Copy link
Contributor

@HansOlsson @sjoelund Any ideas how to proceed?

I think it is ok, but want to hear from @sjoelund

Copy link
Contributor

@HansOlsson HansOlsson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, then.

@beutlich beutlich removed their assignment Dec 21, 2020
@beutlich beutlich removed this from the MSL4.1.0 milestone Nov 20, 2021
@HansOlsson
Copy link
Contributor

@beutlich Is there a reason to not merge this? (Apart from resolving the conflict).

@beutlich
Copy link
Member Author

(Apart from resolving the conflict)

Done.

@HansOlsson
Copy link
Contributor

@beutlich can you update this from master? I did it like 7 minutes ago, but it is still out of sync and it invalidates my review.

There is a race condition between localtime and ftime/gettimeofday in ModelicaInternal_getTime. In case ftime/gettimeofday is called at a very new second, this can lead to a inaccuracy of (almost) 1 second for the calculated time.

Using gmtime, there is no need to call localtime. Instead the struct tm can be directly computed from the obtained timebuffer and timezone information.
@beutlich
Copy link
Member Author

@beutlich can you update this from master? I did it like 7 minutes ago, but it is still out of sync and it invalidates my review.

Done once again.

@HansOlsson HansOlsson merged commit 349a1fb into modelica:master Dec 14, 2023
2 checks passed
@beutlich beutlich deleted the update-gettime branch December 14, 2023 20:28
@beutlich beutlich added this to the MSL4.1.0 milestone Dec 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
L: C-Sources Issue addresses Modelica/Resources/C-Sources
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants