Issue created by Martin Werner:
https://gitlab.rtems.org/rtems/rtos/rtems/-/work_items/5834
## Summary
When using `_Timespec_Divide()` (or `rtems_timespec_divide()`), if the `lhs`
timespec parameter is above around 184467, the calculation for the resulting
`ival_percentage` will overflow `uint64_t` and produce an incorrect whole
percentage value.
This seems to be due to the scaling of the lhs by 100000 (which is done to get
a one thousandth of a percent precision, in combination with the scaling by
1000000000 to convert to nanoseconds).
For a lhs of `184468.0` the value ends up as 0x1000032DD5AAD0000 after
multiplying by 1000000000 and 100000, which is larger than `UINT64_MAX`.
CPU usage uses this percentage calculation, so its percentages will be strange
for any tasks which have consumed more than about 2.14 days (184468 seconds /
60 / 60 / 24) of CPU time. (This observation was the point of discovery for the
issue.)
Looking at the Git history, it seems like this behaviour has been the same for
a long time (2007).
## Steps to reproduce
The following application shows the behaviour.
```c
#include <stdio.h>
#include <rtems.h>
#include <rtems/timespec.h>
static rtems_task Init(rtems_task_argument arg)
{
(void)arg;
printf("\n");
int i;
for (i = 184465; i < 184475; ++i) {
struct timespec tss[2];
rtems_timespec_set(tss + 0, i, 0);
rtems_timespec_set(tss + 1, i * 2, 0);
uint32_t percent_whole;
uint32_t percent_fraction;
rtems_timespec_divide(tss + 0, tss + 1, &percent_whole, &percent_fraction);
printf("%lld.%ld / %lld.%ld == %3u.%03u%%\n",
tss[0].tv_sec, tss[0].tv_nsec,
tss[1].tv_sec, tss[1].tv_nsec,
percent_whole, percent_fraction);
}
rtems_shutdown_executive(0);
}
#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
#define CONFIGURE_UNLIMITED_OBJECTS
#define CONFIGURE_UNIFIED_WORK_AREAS
#define CONFIGURE_INIT_TASK_ATTRIBUTES (RTEMS_DEFAULT_ATTRIBUTES |
RTEMS_FLOATING_POINT)
#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
#define CONFIGURE_INIT
#include <rtems/confdefs.h>
```
Output:
```
184465.0 / 368930.0 == 50.000%
184466.0 / 368932.0 == 50.000%
184467.0 / 368934.0 == 50.000%
184468.0 / 368936.0 == 0.000%
184469.0 / 368938.0 == 0.000%
184470.0 / 368940.0 == 0.000%
184471.0 / 368942.0 == 0.000%
184472.0 / 368944.0 == 0.001%
184473.0 / 368946.0 == 0.001%
184474.0 / 368948.0 == 0.001%
```
<!-- Pre-set options
- milestone
-->
--
View it on GitLab: https://gitlab.rtems.org/rtems/rtos/rtems/-/work_items/5834
You're receiving this email because of your account on gitlab.rtems.org.
Unsubscribe from this thread:
https://gitlab.rtems.org/-/namespace/49/sent_notifications/6-2s0ejw825scggfbc118d86w9a-1d/unsubscribe
| Manage all notifications: https://gitlab.rtems.org/-/profile/notifications |
Help: https://gitlab.rtems.org/help
_______________________________________________
bugs mailing list
[email protected]
http://lists.rtems.org/mailman/listinfo/bugs