summaryrefslogtreecommitdiffstats
path: root/cpukit/score/src/kern_tc.c (unfollow)
Commit message (Collapse)AuthorFilesLines
2023-03-07pps: Round to closest integer in pps_event()Sebastian Huber1-1/+3
The comment above bintime2timespec() says: When converting between timestamps on parallel timescales of differing resolutions it is historical and scientific practice to round down. However, the delta_nsec value is a time difference and not a timestamp. Also the rounding errors accumulate in the frequency accumulator, see hardpps(). So, rounding to the closest integer is probably slightly better. Reviewed by: imp Pull Request: https://github.com/freebsd/freebsd-src/pull/604
2023-03-07pps: Simplify the nsec calculation in pps_event()Sebastian Huber1-10/+6
Let A be the current calculation of the frequency accumulator (pps_fcount) update in pps_event() scale = (uint64_t)1 << 63; scale /= captc->tc_frequency; scale *= 2; bt.sec = 0; bt.frac = 0; bintime_addx(&bt, scale * tcount); bintime2timespec(&bt, &ts); hardpps(tsp, ts.tv_nsec + 1000000000 * ts.tv_sec); and hardpps(..., delta_nsec): u_nsec = delta_nsec; if (u_nsec > (NANOSECOND >> 1)) u_nsec -= NANOSECOND; else if (u_nsec < -(NANOSECOND >> 1)) u_nsec += NANOSECOND; pps_fcount += u_nsec; This change introduces a new calculation which is slightly simpler and more straight forward. Name it B. Consider the following sample values with a tcount of 2000000100 and a tc_frequency of 2000000000 (2GHz). For A, the scale is 9223372036. Then scale * tcount is 18446744994337203600 which is larger than UINT64_MAX (= 18446744073709551615). The result is 920627651984 == 18446744994337203600 % UINT64_MAX. Since all operands are unsigned the result is well defined through modulo arithmetic. The result of bintime2timespec(&bt, &ts) is 49. This is equal to the correct result 1000000049 % NANOSECOND. In hardpps(), both conditional statements are not executed and pps_fcount is incremented by 49. For the new calculation B, we have 1000000000 * tcount is 2000000100000000000 which is less than UINT64_MAX. This yields after the division with tc_frequency the correct result of 1000000050 for delta_nsec. In hardpps(), the first conditional statement is executed and pps_fcount is incremented by 50. This shows that both methods yield roughly the same results. However, method B is easier to understand and requires fewer conditional statements. Reviewed by: imp Pull Request: https://github.com/freebsd/freebsd-src/pull/604
2023-03-07pps: Directly assign the timestamps in pps_event()Sebastian Huber1-4/+2
Reviewed by: imp Pull Request: https://github.com/freebsd/freebsd-src/pull/604
2023-03-07pps: Move pcount assignment in pps_event()Sebastian Huber1-4/+4
Move the pseq increment. This makes it possible to reuse registers earlier. Reviewed by: imp Pull Request: https://github.com/freebsd/freebsd-src/pull/604
2023-03-07pps: Simplify capture and event processingSebastian Huber1-21/+21
Use local variables for the captured timehand and timecounter in pps_event(). This fixes a potential issue in the nsec preparation for hardpps(). Here the timecounter was accessed through the captured timehand after the generation was checked. Make a snapshot of the relevent timehand values early in pps_event(). Check the timehand generation only once during the capture and event processing. Use atomic_thread_fence_acq() similar to the other readers. Reviewed by: imp Pull Request: https://github.com/freebsd/freebsd-src/pull/604
2023-03-07pps: Load timecounter once in pps_capture()Sebastian Huber1-1/+3
This ensures that the timecounter and the tc_get_timecount handler belong together. Reviewed by: imp Pull Request: https://github.com/freebsd/freebsd-src/pull/604
2023-03-07set_cputicker: use a boolMitchell Horne1-3/+3
The third argument to this function indicates whether the supplied ticker is fixed or variable, i.e. requiring calibration. Give this argument a type and name that better conveys this purpose. Reviewed by: kib, markj MFC after: 1 week Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D35459
2023-03-07kern_tc.c/cputick2usec()firk1-9/+3
(which is used to calculate cputime from cpu ticks) has some imprecision and, worse, huge timestep (about 20 minutes on 4GHz CPU) near 53.4 days of elapsed time. kern_time.c/cputick2timespec() (it is used for clock_gettime() for querying process or thread consumed cpu time) Uses cputick2usec() and then needlessly converting usec to nsec, obviously losing precision even with fixed cputick2usec(). kern_time.c/kern_clock_getres() uses some weird (anyway wrong) formula for getting cputick resolution. PR: 262215 Reviewed by: gnn Differential Revision: https://reviews.freebsd.org/D34558
2022-06-23score: Make SMP only code explicitSebastian Huber1-0/+4
Conditional expressions with inline functions are not optimized away if optimization is disabled. Avoid such expressions to prevent dead branches. It helps also during code review to immediately see if a loop is used or not.
2022-06-23kern_tc.c: Provide a weak hardpps() implementationSebastian Huber1-0/+12
The real implementation of hardpps() is defined in kern_ntptime.c. Use it only if the NTP support is needed by the application. Update #2349.
2022-06-10kern_tc.c: Update pps_event() for uniprocessor configurationsGabriel Moyano1-0/+6
Since pps->capgen equal to zero is not a special value in uniprocessor configurations, there is no need to check for this condition. Update #2349
2022-05-27score: Fix pps_fetch()Sebastian Huber1-1/+2
Return early only if there was a timeout, otherwise return the PPS info. Update #2349.
2022-05-23kern_tc.c: Enable PPS API supportGabriel Moyano1-4/+0
Update #2349.
2022-05-23kern_tc.c: Add definitions required by PPS APIGabriel Moyano1-0/+6
Update #2349.
2022-05-23score: Rename tc_getfrequency()Gabriel Moyano1-1/+1
Rename tc_getfrequency() to _Timecounter_Get_frequency(). Update #2349.
2022-05-23kern_tc.c: Replace FreeBSD event mechanism by adding pointers to functionGabriel Moyano1-0/+41
Update #2349.
2022-05-23kern_tc.c: Add atomic dependencies required by the PPS APIGabriel Moyano1-0/+7
Update #2349.
2022-02-21kern_ntptime.c: Port to RTEMSSebastian Huber1-3/+3
Remove previous adjtime() implementation. Update #2348.
2022-02-21kern_tc: unify timecounter to bintime delta conversionAndriy Gapon1-21/+21
There are two places where we convert from a timecounter delta to a bintime delta: tc_windup and bintime_off. Both functions use the same calculations when the timecounter delta is small. But for a large delta (greater than approximately an equivalent of 1 second) the calculations were different. Both functions use approximate calculations based on th_scale that avoid division. Both produce values slightly greater than a true value, calculated with division by tc_frequency, would be. tc_windup is slightly more accurate, so its result is closer to the true value and, thus, smaller than bintime_off result. As a consequence there can be a jump back in time when time hands are switched after a long period of time (a large delta). Just before the switch the time would be calculated with a large delta from th_offset_count in bintime_off. tc_windup does the switch using its own calculations of a new th_offset using the large delta. As explained earlier, the new th_offset may end up being less than the previously produced binuptime. So, for a period of time new binuptime values may be "back in time" comparing to values just before the switch. Such a jump must never happen. All the code assumes that the uptime is monotonically nondecreasing and some code works incorrectly when that assumption is broken. For example, we have observed sleepq_timeout() ignoring a timeout when the sbinuptime value obtained by the callout code was greater than the expiration value, but the sbinuptime obtained in sleepq_timeout() was less than it. In that case the target thread would never get woken up. The unified calculations should ensure the monotonic property of the uptime. The problem is quite rare as normally tc_windup should be called HZ times per second (typically 1000 or 100). But it may happen in VMs on very busy hypervisors where a VM's virtual CPU may not get an execution time slot for a second or more. Reviewed by: kib MFC after: 2 weeks Sponsored by: Panzura LLC
2022-02-21timecounter: Initialize tc_lock earlierMark Johnston1-1/+2
Hyper-V wants to register its MSR-based timecounter during SI_SUB_HYPERVISOR, before SI_SUB_LOCK, since an emulated 8254 may not be available for DELAY(). So we cannot use MTX_SYSINIT to initialize the timecounter lock. PR: 259878 Reviewed by: kib MFC after: 3 days Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D33014
2021-11-15rtems: Add new clock manager directivesSebastian Huber1-0/+56
Update #4527.
2021-11-15score: Add _Timecounter_Set_NTP_update_second()Sebastian Huber1-5/+25
Allow the installation of an NTP update second handler which may be used by an NTP service. Update #2348.
2021-11-15score: Optimize timehand updates for non-SMPSebastian Huber1-8/+36
In uniprocessor configurations, the timehand updates are done with interrupts disabled. So, it is impossible to observe a generation number of zero.
2021-11-15score: Port large time delta support to RTEMSSebastian Huber1-3/+22
2021-11-15score: Initialize timehand generation to UINT_MAXSebastian Huber1-1/+1
This leads to a timehand generation overflow right at the system start and helps to get code coverage in test programs.
2021-11-15timecounter: Load the currently selected tc once in tc_windup()Mark Johnston1-7/+16
Reported by: Sebastian Huber <sebastian.huber@embedded-brains.de> Reviewed by: kib MFC after: 1 week Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D32729
2021-11-15kern_tc.c: Scaling/large delta recalculationSebastian Huber1-38/+50
This change is a slight performance optimization for systems with a slow 64-bit division. The th->th_scale and th->th_large_delta values only depend on the timecounter frequency and the th->th_adjustment. The timecounter frequency of a timehand only changes when a new timecounter is activated for the timehand. The th->th_adjustment is only changed by the NTP second update. The NTP second update is not done for every call of tc_windup(). Move the code block to recalculate the scaling factor and the large delta of a timehand to the new helper function recalculate_scaling_factor_and_large_delta(). Call recalculate_scaling_factor_and_large_delta() when a new timecounter is activated and a NTP second update occurred. MFC after: 1 week
2021-11-15timecounter: Lock the timecounter listMark Johnston1-10/+30
Timecounter registration is dynamic, i.e., there is no requirement that timecounters must be registered during single-threaded boot. Loadable drivers may in principle register timecounters (which can be switched to automatically). Timecounters cannot be unregistered, though this could be implemented. Registered timecounters belong to a global linked list. Add a mutex to synchronize insertions and the traversals done by (mpsafe) sysctl handlers. No functional change intended. Reviewed by: imp, kib MFC after: 2 weeks Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D32511
2021-11-15timecounter: Let kern.timecounter.stepwarnings be set as a tunableMark Johnston1-1/+1
MFC after: 1 week
2021-11-15Remove "All Rights Reserved" fromEd Maste1-1/+0
FreeBSD Foundation sys/ copyrights These ones were unambiguous cases where the Foundation was the only listed copyright holder (in the associated license block). Sponsored by: The FreeBSD Foundation
2021-11-15kern: clarify boot timeWarner Losh1-1/+7
In FreeBSD, the current time is computed from uptime + boottime. Uptime is a continuous, smooth function that's monotonically increasing. To effect changes to the current time, boottime is adjusted. boottime is mutable and shouldn't be cached against future need. Document the current implementation, with the caveat that we may stop stepping boottime on resume in the future and will step uptime instead (noted in the commit message, but not in the code). Sponsored by: Netflix Reviewed by: phk, rpokala Differential Revision: https://reviews.freebsd.org/D30116
2021-11-15Make kern.timecounter.hardware tunableKonstantin Belousov1-5/+18
Noted and reviewed by: kevans MFC after: 1 week Sponsored by: The FreeBSD Foundation Differential revision: https://reviews.freebsd.org/D29122
2021-11-15Add ddb 'show timecounter' command.Konstantin Belousov1-1/+31
MFC after: 1 week Sponsored by: The FreeBSD Foundation
2021-11-15Changes that improve DTrace FBT reliabilityRobert Watson1-0/+15
on freebsd/arm64: - Implement a dtrace_getnanouptime(), matching the existing dtrace_getnanotime(), to avoid DTrace calling out to a potentially instrumentable function. (These should probably both be under KDTRACE_HOOKS. Also, it's not clear to me that they are correct implementations for the DTrace thread time functions they are used in .. fixes for another commit.) - Don't allow FBT to instrument functions involved in EL1 exception handling that are involved in FBT trap processing: handle_el1h_sync() and do_el1h_sync(). - Don't allow FBT to instrument DDB and KDB functions, as that makes it rather harder to debug FBT problems. Prior to these changes, use of FBT on FreeBSD/arm64 rapidly led to kernel panics due to recursion in DTrace. Reliable FBT on FreeBSD/arm64 is reliant on another change from @andrew to have the aarch64 instrumentor more carefully check that instructions it replaces are against the stack pointer, which can otherwise lead to memory corruption. That change remains under review. MFC after: 2 weeks Reviewed by: andrew, kp, markj (earlier version), jrtc27 (earlier version) Differential revision: https://reviews.freebsd.org/D27766
2021-11-15Remove double-calls to tc_get_timecount()Konstantin Belousov1-3/+0
to warm timecounters. It seems that second call does not add any useful state change for all implemented timecounters. Discussed with: bde Sponsored by: The FreeBSD Foundation MFC after: 3 weeks
2021-11-15Mark more nodes as CTLFLAG_MPSAFEPawel Biernacki1-13/+24
or CTLFLAG_NEEDGIANT (17 of many) r357614 added CTLFLAG_NEEDGIANT to make it easier to find nodes that are still not MPSAFE (or already are but aren’t properly marked). Use it in preparation for a general review of all nodes. This is non-functional change that adds annotations to SYSCTL_NODE and SYSCTL_PROC nodes using one of the soon-to-be-required flags. Mark all obvious cases as MPSAFE. All entries that haven't been marked as MPSAFE before are by default marked as NEEDGIANT Approved by: kib (mentor, blanket) Commented by: kib, gallatin, melifaro Differential Revision: https://reviews.freebsd.org/D23718
2021-11-15Consolidate read code for timecountersKonstantin Belousov1-145/+87
and fix possible overflow in bintime()/binuptime(). The algorithm to read the consistent snapshot of current timehand is repeated in each accessor, including the details proper rollup detection and synchronization with the writer. In fact there are only two different kind of readers: one for bintime()/binuptime() which has to do the in-place calculation, and another kind which fetches some member from struct timehand. Extract the logic into type-checked macros, GETTHBINTIME() for bintime calculation, and GETTHMEMBER() for safe read of a structure' member. This way, the synchronization is only written in bintime_off() and getthmember(). In bintime_off(), use overflow-safe calculation of th_scale * delta(timecounter). In tc_windup, pre-calculate the min delta value which overflows and require slow algorithm, into the new timehands th_large_delta member. This part with overflow fix was written by Bruce Evans. Reported by: Mark Millard <marklmi@yahoo.com> (the overflow issue) Tested by: pho Discussed with: emaste Sponsored by: The FreeBSD Foundation (kib) MFC after: 3 weeks
2021-11-15Remove duplicated empty lines from kern/*.cMateusz Guzik1-1/+0
No functional changes.
2021-11-15Initialize timehands linkage much earlier.Konstantin Belousov1-12/+22
Reported and tested by: trasz Sponsored by: The FreeBSD Foundation MFC after: 1 week
2021-11-15Make timehands count selectable at boottime.Konstantin Belousov1-5/+45
Tested by: O'Connor, Daniel <darius@dons.net.au> Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D21563 This patch was modified by Sebastian Huber <sebastian.huber@embedded-brains.de> to adjust it for RTEMS. See comment in the patch.
2021-11-15Instead of using an incomplete list of platformsOlivier Houchard1-1/+2
that uses 64bits time_t in 32bits mode, special case amd64, as i386 is the only arch that still uses 32bits time_t.
2021-11-15Create a new macro for static DPCPU data.Andrew Turner1-2/+2
On arm64 (and possible other architectures) we are unable to use static DPCPU data in kernel modules. This is because the compiler will generate PC-relative accesses, however the runtime-linker expects to be able to relocate these. In preparation to fix this create two macros depending on if the data is global or static. Reviewed by: bz, emaste, markj Sponsored by: ABT Systems Ltd Differential Revision: https://reviews.freebsd.org/D16140
2021-11-15tc: bcopy -> memcpyMateusz Guzik1-2/+1
2021-11-15Move most of the contents of opt_compat.hBrooks Davis1-1/+0
to opt_global.h. opt_compat.h is mentioned in nearly 180 files. In-progress network driver compabibility improvements may add over 100 more so this is closer to "just about everywhere" than "only some files" per the guidance in sys/conf/options. Keep COMPAT_LINUX32 in opt_compat.h as it is confined to a subset of sys/compat/linux/*.c. A fake _COMPAT_LINUX option ensure opt_compat.h is created on all architectures. Move COMPAT_LINUXKPI to opt_dontuse.h as it is only used to control the set of compiled files. Reviewed by: kib, cem, jhb, jtl Sponsored by: DARPA, AFRL Differential Revision: https://reviews.freebsd.org/D14941
2021-11-15score: Remove FreeBSD identifierSebastian Huber1-1/+1
2021-11-15Use atomic_load(9) to read ppsinfo sequence numbers.Konstantin Belousov1-4/+4
In this case volatile qualifiers enusre that a compiler does not optimize the accesses out. Reviewed by: alc, jhb Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D13534
2021-11-15SPDX: use the Beerware identifier.Pedro F. Giffuni1-0/+2
2021-02-01kern_tc.c: Remove unused codeSebastian Huber1-6/+0
This fix relates to a Coverity issue (PW.DECLARED_BUT_NOT_REFERENCED).
2020-12-02score: Canonicalize Doxygen @file commentsSebastian Huber1-0/+17
Use common phrases for the file brief descriptions. Update #3706.
2019-10-02score: Install timecounter according to qualitySebastian Huber1-0/+2
This makes it possible to install higher quality timecounter in plug-and-play systems and helps to override the clock driver provided timecounter in some test scenarios.