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

Confusing about _times_r implementation and newlibc clock #81

Closed
jolivepetrus opened this issue Nov 7, 2016 · 3 comments
Closed

Confusing about _times_r implementation and newlibc clock #81

jolivepetrus opened this issue Nov 7, 2016 · 3 comments
Labels
Resolution: Done Issue is done internally Status: Done Issue is done internally

Comments

@jolivepetrus
Copy link

I'm a bit confusing about _times_r implementation.

Current implementation:

clock_t IRAM_ATTR _times_r(struct _reent *r, struct tms *ptms)
{
clock_t t = xTaskGetTickCount() * (portTICK_PERIOD_MS * CLK_TCK / 1000);
ptms->tms_cstime = t;
ptms->tms_cutime = 0;
ptms->tms_stime = t;
ptms->tms_utime = 0;
struct timeval tv = {0, 0};
_gettimeofday_r(r, &tv, NULL);
return (clock_t) tv.tv_sec;
}

In newlibc clock implementation is:

clock_t
clock ()
{
struct tms tim_s;
clock_t res;

if ((res = (clock_t) _times_r (_REENT, &tim_s)) != (clock_t) -1)
res = (clock_t) (tim_s.tms_utime + tim_s.tms_stime +
tim_s.tms_cutime + tim_s.tms_cstime);

return res;
}

It seems that ticks returned by clock are multiplied by 2, due to in _times_r tms_cstime and tms_stime are set with the current processor ticks. tms_cstime or tms_stime must be set to 0.

@igrr
Copy link
Member

igrr commented Nov 7, 2016

Thanks, I haven't checked clock, will fix and add a test for it.

@igrr
Copy link
Member

igrr commented Nov 8, 2016

Okay, i have found why i implemented _times_r incorrectly in the first place. I missed one 's' in this line of manpage related to times function:

tms_cstime  The sum of the tms_stimes and tms_cstimes of the child processes.
                                    ^

igrr added a commit that referenced this issue Nov 9, 2016
tms_cstime should only take system time in child processes into account, so has to be zero.
#81
@igrr
Copy link
Member

igrr commented Nov 9, 2016

Fixed in master

@igrr igrr closed this as completed Nov 9, 2016
@espressif-bot espressif-bot added Status: Opened Issue is new Status: In Progress Work is in progress and removed Status: Opened Issue is new labels Jul 7, 2022
@espressif-bot espressif-bot added Resolution: Done Issue is done internally Status: Done Issue is done internally and removed Status: In Progress Work is in progress labels Jul 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: Done Issue is done internally Status: Done Issue is done internally
Projects
None yet
Development

No branches or pull requests

3 participants