Skip to content

Commit

Permalink
[kernel,libc] Fix precision timer get_ptime
Browse files Browse the repository at this point in the history
  • Loading branch information
ghaerr committed Oct 29, 2024
1 parent 3a662d6 commit df56dfc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
15 changes: 13 additions & 2 deletions elks/arch/i86/lib/prectimer.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
* Precision timer routines for IBM PC and compatibles
* 2 Aug 2024 Greg Haerr
*
* This file is identical in elks/arch/i86/kernel/lib/prectimer.c (kernel)
* and libc/debug/prectimer.c (user mode).
*
* Use 8254 PIT to measure elapsed time in pticks = 0.8381 usecs.
*/

Expand Down Expand Up @@ -109,14 +112,22 @@ unsigned long get_ptime(void)

count = lo | hi;
pticks = lastcount - count;
lastcount = count;
#if 1
if ((int)pticks < 0) { /* wrapped, jiffies is higher */
pticks += MAX_PTICK; /* = MAX_PTICK - count + lastcount */
jdiff--; /* adjust jiffies for wrap, won't ever be negative */
}
if (jdiff < 4286) /* < ~42.86s */
return jdiff * (unsigned long)MAX_PTICK + pticks;
#else /* incorrect (old) version - to be removed */
if ((int)pticks < 0) /* wrapped */
pticks += MAX_PTICK; /* = MAX_PTICK - count + lastcount */
lastcount = count;

if (jdiff < 2) /* < 10ms: 1..11931 */
return pticks;
if (jdiff < 4286) /* < ~42.86s */
return (jdiff - 1) * (unsigned long)MAX_PTICK + pticks;
#endif
return 0; /* overflow displays 0s */
}

Expand Down
15 changes: 13 additions & 2 deletions libc/debug/prectimer.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
* Precision timer routines for IBM PC and compatibles
* 2 Aug 2024 Greg Haerr
*
* This file is identical in elks/arch/i86/kernel/lib/prectimer.c (kernel)
* and libc/debug/prectimer.c (user mode).
*
* Use 8254 PIT to measure elapsed time in pticks = 0.8381 usecs.
*/

Expand Down Expand Up @@ -109,14 +112,22 @@ unsigned long get_ptime(void)

count = lo | hi;
pticks = lastcount - count;
lastcount = count;
#if 1
if ((int)pticks < 0) { /* wrapped, jiffies is higher */
pticks += MAX_PTICK; /* = MAX_PTICK - count + lastcount */
jdiff--; /* adjust jiffies for wrap, won't ever be negative */
}
if (jdiff < 4286) /* < ~42.86s */
return jdiff * (unsigned long)MAX_PTICK + pticks;
#else /* incorrect (old) version - to be removed */
if ((int)pticks < 0) /* wrapped */
pticks += MAX_PTICK; /* = MAX_PTICK - count + lastcount */
lastcount = count;

if (jdiff < 2) /* < 10ms: 1..11931 */
return pticks;
if (jdiff < 4286) /* < ~42.86s */
return (jdiff - 1) * (unsigned long)MAX_PTICK + pticks;
#endif
return 0; /* overflow displays 0s */
}

Expand Down

0 comments on commit df56dfc

Please sign in to comment.