High-precision timing method and device for windows system real-time simulation, storage medium and electronic equipment
By combining high-resolution performance counters and operating system timing functions, the problem of insufficient precision of Windows system timer functions is solved, high-precision simulation timing is achieved, CPU resource usage is reduced, and system performance and reliability are improved.
Patent Information
- Application Number
- CN202411353430.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-26
- Publication Date
- 2025-10-10
- Estimated Expiration
- 2044-09-26
AI Technical Summary
The timer function of the existing Windows system cannot guarantee high-precision time control, and the operating system scheduling and load affect the execution time of the scheduled tasks, resulting in insufficient accuracy of the simulation process.
By combining high-resolution performance counters and operating system timing functions, the difference between processor clock time and simulation time is calculated, the preset time calibration algorithm is executed cyclically, and the coarse and fine timing functions are combined to ensure timing accuracy.
It achieves high-precision simulation timing under Windows system, reduces CPU resource usage, and improves the overall performance and reliability of the system.
Smart Images

Figure CN119473492B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a high-precision timing method, device, storage medium, and electronic device for real-time simulation of a Windows system. Background Art
[0002] In the field of real-time simulation of digital models of physical systems, each step of the simulation process needs to be precisely timed. In related technologies, dedicated real-time simulation computers can be used for simulation, but this is expensive and the simulation program development time is long.
[0003] Therefore, to reduce costs, ordinary computers can also be used for simulation calculations. However, the timer functions provided by the operating system on ordinary computers generally cannot guarantee high-precision time control. The triggering of these functions may be affected by the operating system scheduling and system load, resulting in deviations between the actual execution time of the scheduled task and the expected time.
[0004] Based on this, there is an urgent need for a high-precision timing method for real-time simulation of Windows systems to improve the timing accuracy of the operating system. Summary of the Invention
[0005] The purpose of this application is to provide a high-precision timing method, device, storage medium and electronic device for real-time simulation of Windows system, which are used to improve the timing accuracy of the operating system.
[0006] This application provides a high-precision timing method for real-time simulation of Windows systems, including:
[0007] Compare the first time corresponding to the current moment with the second time corresponding to the current moment, and generate a first comparison result; the first time is: the processor clock time calculated based on the initial value of the high-resolution performance counter; the second time is: calculated based on the sum of the simulation time at the current moment and the timing accuracy control amount; the simulation time at the current moment is calculated based on the sum of the simulation time and the simulation step size of the previous round of simulation; when the first comparison result indicates that the first time corresponding to the current moment is less than the second time corresponding to the current moment, cyclically execute the preset time calibration algorithm until the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment; execute the target simulation function.
[0008] Optionally, the first time corresponding to the current moment is calculated based on the following steps: obtaining a current value of the high-resolution performance counter and obtaining the current time based on the current value; calculating a target difference between the current time and an initial time, and determining a ratio of the target difference to the frequency of the high-resolution performance counter as the first time corresponding to the current moment;
[0009] The initial time is the time corresponding to the initial value of the high-resolution performance counter.
[0010] Optionally, the loop executes a preset time calibration algorithm until the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment, including: calculating the target time difference between the first time and the simulation time of the current moment, and performing a corresponding target operation based on a second comparison result of the target time difference and the fine timing time, until the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment; wherein, the target operation includes: a first operation and a second operation; the first operation is: when the target time difference is greater than the fine timing time, using a coarse timing function to sleep for a first preset time; the second operation is: when the target time difference is less than or equal to the fine timing time, sleeping for a second preset time by repeatedly querying the processor clock time.
[0011] Optionally, the corresponding target operation is executed based on the second comparison result between the target time difference and the fine timing time, including: judging the size of the target time difference and the fine timing time, and generating a second comparison result; if the second comparison result indicates that the target time difference is greater than the fine timing time, executing the first operation, otherwise, executing the second operation.
[0012] Optionally, executing the first operation includes: creating a target event; calling the rough timing function of the operating system to set a target timer, triggering the target event after the first preset time; and ending the target event when the time object of the target event becomes a signal state.
[0013] Optionally, executing the second operation includes querying the processor clock time of the operating system at a current moment.
[0014] Optionally, after comparing the first time corresponding to the current moment with the second time corresponding to the current moment and generating a first comparison result, the method further includes: executing the target simulation function when the first comparison result indicates that the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment.
[0015] The present application also provides a high-precision timing device for real-time simulation of Windows systems, comprising:
[0016] A comparison module is used to compare a first time corresponding to a current moment with a second time corresponding to the current moment, and generate a first comparison result; the first time is: a processor clock time calculated based on the initial value of a high-resolution performance counter; the second time is: calculated based on the sum of the simulation time at the current moment and a timing accuracy control amount; the simulation time at the current moment is calculated based on the sum of the simulation time and the simulation step size of the previous round of simulation; an execution module is used to cyclically execute a preset time calibration algorithm when the first comparison result indicates that the first time corresponding to the current moment is less than the second time corresponding to the current moment, until the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment; the execution module is also used to execute a target simulation function.
[0017] Optionally, the device also includes: an acquisition module and a calculation module; the acquisition module is used to obtain the current value of the high-resolution performance counter and obtain the current time based on the current value; the calculation module is used to calculate the target difference between the current time and the initial time, and determine the ratio of the target difference to the frequency of the high-resolution performance counter as the first time corresponding to the current moment; wherein the initial time is: the time corresponding to the initial value of the high-resolution performance counter.
[0018] Optionally, the calculation module is also used to calculate the target time difference between the first time and the simulation time of the current moment; the execution module is specifically used to execute the corresponding target operation based on the second comparison result of the target time difference and the fine timing time, until the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment; wherein, the target operation includes: a first operation and a second operation; the first operation is: when the target time difference is greater than the fine timing time, using a coarse timing function to sleep for a first preset time; the second operation is: when the target time difference is less than or equal to the fine timing time, sleeping for a second preset time by repeatedly querying the processor clock time.
[0019] Optionally, the comparison module is also used to determine the size of the target time difference and the fine timing time, and generate a second comparison result; the execution module is specifically used to execute the first operation if the second comparison result indicates that the target time difference is greater than the fine timing time, otherwise, execute the second operation.
[0020] Optionally, the execution module is specifically used to create a target event; the execution module is also specifically used to call the coarse timing function of the operating system to set a target timer, triggering the target event after the first preset time length; the execution module is also specifically used to end the target event when the time object of the target event becomes a signal state.
[0021] Optionally, the execution module is specifically used to query the processor clock time of the operating system at a current moment.
[0022] Optionally, the execution module is further configured to execute the target simulation function when the first comparison result indicates that a first time corresponding to the current moment is greater than or equal to a second time corresponding to the current moment.
[0023] The present application also provides a computer program product, comprising a computer program / instruction, which, when executed by a processor, implements the steps of any of the above-described high-precision timing methods for real-time simulation of Windows systems.
[0024] The present application also provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the program, the steps of the high-precision timing method for real-time simulation of a Windows system as described above are implemented.
[0025] The present application also provides a computer-readable storage medium having a computer program stored thereon. When the computer program is executed by a processor, the steps of any of the above-described high-precision timing methods for real-time simulation of Windows systems are implemented.
[0026] The present application provides a high-precision timing method, device, storage medium, and electronic device for real-time simulation of Windows systems. First, a first time corresponding to the current moment is compared with a second time corresponding to the current moment, and a first comparison result is generated; the first time is: the processor clock time calculated based on the initial value of the high-resolution performance counter; the second time is: calculated based on the sum of the simulation time at the current moment and the timing accuracy control amount; the simulation time at the current moment is calculated based on the sum of the simulation time and the simulation step size of the previous round of simulation; then, when the first comparison result indicates that the first time corresponding to the current moment is less than the second time corresponding to the current moment, a preset time calibration algorithm is executed cyclically until the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment; finally, the target simulation function is executed. In this way, while ensuring high precision, the occupation of CPU resources can be reduced, thereby improving the overall performance and reliability of the system. BRIEF DESCRIPTION OF THE DRAWINGS
[0027] In order to more clearly illustrate the technical solutions in the present application or the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without any creative work.
[0028] Figure 1 This is one of the flow charts of the high-precision timing method for real-time simulation of Windows systems provided by this application;
[0029] Figure 2 This is the second flow chart of the high-precision timing method for real-time simulation of Windows systems provided by this application;
[0030] Figure 3 This is a schematic diagram of the structure of a high-precision timing device for real-time simulation of Windows systems provided by this application;
[0031] Figure 4 It is a structural diagram of the electronic device provided in this application. DETAILED DESCRIPTION
[0032] To make the objectives, technical solutions, and advantages of this application more clear, the technical solutions of this application will be clearly and completely described below in conjunction with the accompanying drawings. Obviously, the embodiments described are only part of the embodiments of this application, not all of them. Based on the embodiments of this application, all other embodiments obtained by ordinary technicians in this field without making any creative efforts are within the scope of protection of this application.
[0033] The terms "first," "second," and the like in the specification and claims of this application are used to distinguish similar objects, and are not used to describe a specific order or precedence. It should be understood that the terms used in this manner are interchangeable where appropriate, so that the embodiments of this application can be implemented in an order other than that illustrated or described herein, and that the objects distinguished by "first," "second," and the like are generally of the same type, and do not limit the number of objects; for example, the first object can be one or more. In addition, the term "and / or" in the specification and claims refers to at least one of the connected objects, and the character " / " generally indicates that the objects connected are in an "or" relationship.
[0034] Taking the Windows system as an example, the existing timer functions under the Windows operating system, such as setTimer, createWaitableTimer and timeSetEvent, usually cannot guarantee high-precision time control. The triggering of these functions may be affected by the operating system scheduling and system load, resulting in a deviation between the actual execution time of the scheduled task and the expected time. Although the QueryPerformanceCounter() function can provide an accurate hardware clock, it may occupy the central processing unit (CPU) resources for a long time during its use, resulting in excessive system load. In view of the accuracy and CPU load problems in the existing technology, an improved time processing mechanism is urgently needed. This mechanism should be able to reduce the occupation of CPU resources while ensuring high accuracy, thereby improving the overall performance and reliability of the system.
[0035] To address the aforementioned technical issues in related technologies, the present invention proposes a high-precision timing method for real-time simulation of Windows systems, aiming to address the aforementioned issues with existing timer functions and time detection application programming interfaces (APIs). This method utilizes a combination of timer functions and time detection APIs to obtain the hardware clock while minimizing CPU resource usage.
[0036] The following describes in detail the high-precision timing method for real-time simulation of Windows systems provided by the embodiments of the present application through specific embodiments and their application scenarios in conjunction with the accompanying drawings.
[0037] like Figure 1 As shown, an embodiment of the present application provides a high-precision timing method for real-time simulation of a Windows system, which may include the following steps 101 to 103:
[0038] Step 101: Compare a first time corresponding to the current moment with a second time corresponding to the current moment, and generate a first comparison result.
[0039] Among them, the first time is: the processor clock time calculated based on the initial value of the high-resolution performance counter; the second time is: calculated based on the sum of the simulation time at the current moment and the timing accuracy control amount; the simulation time at the current moment is calculated based on the sum of the simulation time and the simulation step size of the previous round of simulation.
[0040] Exemplarily, the first time is the processor clock time at the current moment, and the second time is the sum of the simulation time at the current moment and the timing accuracy control amount. It is understandable that the embodiments of the present application include two timelines, namely the processing clock time and the simulation time. When the processor clock time is significantly less than the simulation time, it is necessary to control the processing clock time to catch up with the simulation time through means such as sleep and timing. That is, when the first time is less than the second time, the first time is made greater than or equal to the second time through means such as sleep and timing.
[0041] For example, in an embodiment of the present application, when simulation is performed, in the simulation function startup part, the high-resolution timestamp of the query processor is used as the initial time reference; in the part that needs to be timed in each step of the simulation, the current simulation time is calculated based on the simulation times counter and the simulation time step.
[0042] Specifically, the step of calculating the first time in the above step 101 may include the following steps 101a1 and 101a2:
[0043] Step 101a1: Obtain the current value of the high-resolution performance counter, and obtain the current time based on the current value.
[0044] Step 101a2: Calculate a target difference between the current time and the initial time, and determine the ratio of the target difference to the frequency of the high-resolution performance counter as the first time corresponding to the current moment.
[0045] The initial time is the time corresponding to the initial value of the high-resolution performance counter.
[0046] For example, in an embodiment of the present application, the QueryPerformanceCounter function can be used to obtain the current high-resolution timestamps and convert these timestamps into processor clock time dClockTime. The code is as follows:
[0047] QueryPerformanceCounter(&liInstantTimeCount);
[0048] timeCount = liInstantTimeCount.QuadPart -liInitialTimeCount.QuadPart;
[0049] dClockTime = timeCount / dClockFrequency.
[0050] The QueryPerformanceCounter(&liInstantTimeCount) function retrieves the current value of the high-resolution performance counter and stores the result in the liInstantTimeCount structure.
[0051] timeCount = liInstantTimeCount.QuadPart - liInitialTimeCount.QuadPart. The difference between the current time (liInstantTimeCount.QuadPart) and the initial time (liInitialTimeCount.QuadPart) gives the elapsed time (in count units). liInitialTimeCount is the high-resolution timestamp of the initial moment obtained using the QueryPerformanceCounter function.
[0052] dClockTime = timeCount / dClockFrequency. To obtain the elapsed time in seconds, divide the count value (timeCount) by the frequency of the performance counter (dClockFrequency).
[0053] It should be noted that in the embodiment of the present application, the processor clock time at the current moment, that is, the above-mentioned first time, needs to be calculated multiple times, and each calculation is performed according to the calculation method provided by the above code.
[0054] Step 102: When the first comparison result indicates that the first time corresponding to the current moment is less than the second time corresponding to the current moment, a preset time calibration algorithm is executed cyclically until the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment.
[0055] Exemplarily, in an embodiment of the present application, by comparing the processor clock time dClockTime and the simulation time dSimTime, different timing functions can be used according to the comparison result to achieve high-precision timing.
[0056] Specifically, the above step 102 may further include the following step 102a:
[0057] Step 102a, calculate the target time difference between the first time and the simulation time of the current moment, and perform the corresponding target operation based on the second comparison result of the target time difference and the fine timing time until the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment.
[0058] Among them, the target operation includes: a first operation and a second operation; the first operation is: when the target time difference is greater than the fine timing time, using a coarse timing function to sleep for a first preset time; the second operation is: when the target time difference is less than or equal to the fine timing time, sleeping for a second preset time by repeatedly querying the processor clock time.
[0059] It is understood that, when the first comparison result indicates that the first time corresponding to the current moment is less than the second time corresponding to the current moment, different operations can be performed according to the target time difference between the first time and the simulation time of the current moment. For example, when the time difference is large, that is, the target time difference is greater than the fine timing time, the coarse timing function of the operating system can be called to put the simulation process into hibernation; when the time difference is small, that is, the target time difference is less than or equal to the fine timing time, the timing function of the operating system can be called to put the simulation process into hibernation.
[0060] It should be noted that the above-mentioned rough timing functions may include functions such as setTimer, createWaitableTimer, and timeSetEvent. The above-mentioned timing functions of the Windows operating system may include a QueryPerformanceCounter function.
[0061] Specifically, in the above step 102a, the step of performing the corresponding target operation based on the second comparison result between the target time difference and the fine timing time may further include the following steps 102a1 and 102a2:
[0062] Step 102a1: Determine the size of the target time difference and the fine timing time, and generate a second comparison result.
[0063] Step 102a2: If the second comparison result indicates that the target time difference is greater than the fine timing time, perform the first operation; otherwise, perform the second operation.
[0064] For example, at each step of the simulation, the current processor clock time is checked in a loop to see if it is less than the current simulation time minus the fine timing time. If so, the coarse timing function sleep loop continues; if not, the current processor clock time is queried in a loop.
[0065] It should be noted that the above steps 102a1 and 102a2 are cyclically executed, that is, each time the first operation or the second operation is executed, the above steps 102a1 and 102a2 need to be cyclically executed once, and after the above step 102a2 is executed, the following step 102a3 needs to be executed:
[0066] Step 102a3: Compare the first time corresponding to the current moment with the second time corresponding to the current moment, and generate a third comparison result.
[0067] It is understandable that after executing the above step 102a2, it is necessary to determine whether the first time is greater than or equal to the second time, and if the above third comparison result indicates that the first time is greater than or equal to the second time, execute the code corresponding to the target simulation function.
[0068] Specifically, in the above step 102, the step of performing the first operation may further include the following steps 102b1 to 102b3:
[0069] Step 102b1: Create a target event.
[0070] Step 102b2: Call the rough timing function of the operating system to set a target timer, and trigger the target event after the first preset time period.
[0071] Step 102b3: When the time object of the target event changes to a signal state, end the target event.
[0072] For example, in order to maintain the continuity and accuracy of the simulation, it is necessary to continuously call the Windows timer function in the simulation loop. In the simulation main loop, the timer function is continuously called to reset the timer to ensure the continuity of the simulation. Due to the low accuracy of the timer function timing, this patent only uses the timer function timing for rough timing. Rough timing is set by creating an event object, setting the timer and waiting for the time to be set to the signal state to set a one-time timed event. The specific code is as follows:
[0073] HANDLE hEvent = CreateEvent(NULL,FALSE,FALSE,NULL);
[0074] if (!hEvent)
[0075] {
[0076] printf("CreateEvent failed (%d)\n", GetLastError());
[0077] }
[0078] if (!timeSetEvent(dwMilliseconds,1,(LPTIMECALLBACK)hEvent,0,TIME_ONESHOT | TIME_CALLBACK_EVENT_SET))
[0079] {
[0080] printf("timeSetEvent failed (%d)\n", GetLastError());
[0081] }
[0082] if (WaitForSingleObject(hEvent,INFINITE) != WAIT_OBJECT_0)
[0083] {
[0084] printf("WaitForSingleObject failed (%d)\n", GetLastError());
[0085] }
[0086] CloseHandle(hEvent).
[0087] First, an event object, hEvent, is created. This is a synchronization object used for communication between threads. Then, the timeSetEvent function is called to set a timer. This function triggers a callback function (hEvent in this case) after a specified interval.
[0088] The dwMilliseconds parameter indicates the timer interval in milliseconds. The TIME_ONESHOT flag indicates that this is a one-shot timer event, not a periodic one. The TIME_CALLBACK_EVENT_SET flag indicates that when the timer fires, the hEvent event object will be set to the signaled state. Finally, the function waits for the hEvent event object to become signaled before closing it.
[0089] Specifically, in the above step 102, the step of performing the second operation may further include the following step 102c:
[0090] Step 102c: query the processor clock time of the operating system at the current moment.
[0091] For example, to ensure high precision of the simulation, it is necessary to query the current processor clock time for precise timing in each simulation step and compare it with the simulation time to determine whether to sleep or continue the simulation.
[0092] Step 103: Execute the target simulation function.
[0093] For example, after the simulation process is dormant through the first and second operations, so that the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment, the code corresponding to the simulation function can be executed. The high-precision timing method in the above embodiment can achieve high-precision simulation timing in the operating system while maintaining efficient utilization of CPU resources.
[0094] Illustratively, after step 101, the high-precision timing method for real-time simulation of a Windows system provided in an embodiment of the present application may further include the following step 104:
[0095] Step 104: When the first comparison result indicates that the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment, execute the target simulation function.
[0096] For example, Figure 2 As shown, it is a detailed flow chart of the high-precision timing method for real-time simulation of Windows system provided by an embodiment of the present application. Step 1. Calculate the initial time base of the processor, and calculate the processor clock time at any moment based on the base. Step 2. Calculate the simulation time at the current moment. The current simulation time is calculated by the simulation time and the simulation step size of the previous round. Step 3. Determine whether the processor clock time at the current moment is greater than or equal to the sum of the simulation time at the current moment and the timing precision control value. If so, jump directly to step 5, otherwise, continue to execute step 4. Step 4. Determine whether the processor clock time at the current moment is less than or equal to the difference between the simulation time at the current moment and the fine timing time. If so, cyclically call the operating system timer function (i.e., the above-mentioned coarse timing function). Otherwise, cyclically query the processor clock time at the current moment until the processor clock time at the current moment is greater than or equal to the sum of the simulation time at the current moment and the timing precision control value. Step 5. Call the simulation-related function code.
[0097] In this way, by combining timer function timing and processor cycle query time, we can not only achieve high-precision time control of the simulation process and ensure the accuracy and reliability of the simulation, but also effectively reduce the long-term occupation of CPU resources and improve the overall performance of the system.
[0098] The embodiment of the present application provides a high-precision timing method for real-time simulation of a Windows system. First, a first time corresponding to the current moment is compared with a second time corresponding to the current moment, and a first comparison result is generated. The first time is: the processor clock time calculated based on the initial value of the high-resolution performance counter. The second time is: calculated based on the sum of the simulation time at the current moment and the timing accuracy control amount. The simulation time at the current moment is calculated based on the sum of the simulation time and the simulation step size of the previous round of simulation. Afterwards, if the first comparison result indicates that the first time corresponding to the current moment is less than the second time corresponding to the current moment, a preset time calibration algorithm is executed cyclically until the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment. Finally, the target simulation function is executed. In this way, while ensuring high precision, the occupation of CPU resources can be reduced, thereby improving the overall performance and reliability of the system.
[0099] It should be noted that the high-precision timing method for real-time simulation of a Windows system provided in the embodiments of the present application can be executed by a high-precision timing device for real-time simulation of a Windows system, or a control module in the high-precision timing device for real-time simulation of a Windows system for executing the high-precision timing method for real-time simulation of a Windows system. In the embodiments of the present application, the high-precision timing device for real-time simulation of a Windows system is used as an example to illustrate the high-precision timing device for real-time simulation of a Windows system provided in the embodiments of the present application.
[0100] It should be noted that the high-precision timing method for real-time simulation of Windows systems shown in the above-mentioned method figures in the embodiments of this application are all described by way of example in conjunction with one figure in the embodiments of this application. In specific implementation, the high-precision timing method for real-time simulation of Windows systems shown in the above-mentioned method figures can also be implemented in conjunction with any other combinable figures shown in the above-mentioned embodiments, and no further details will be given here.
[0101] The following describes the high-precision timing device for real-time simulation of Windows systems provided by this application. The high-precision timing method for real-time simulation of Windows systems described below can be referenced to each other.
[0102] Figure 3 This is a schematic diagram of the structure of a high-precision timing device for real-time simulation of Windows systems provided in an embodiment of the present application, as shown in FIG. Figure 3 As shown, specifically including:
[0103] The comparison module 301 is used to compare the first time corresponding to the current moment with the second time corresponding to the current moment, and generate a first comparison result; the first time is: the processor clock time calculated based on the initial value of the high-resolution performance counter; the second time is: calculated based on the simulation time at the current moment and the sum of the timing accuracy control amount; the simulation time at the current moment is calculated based on the sum of the simulation time and the simulation step size of the previous round of simulation; the execution module 302 is used to cyclically execute the preset time calibration algorithm when the first comparison result indicates that the first time corresponding to the current moment is less than the second time corresponding to the current moment, until the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment; the execution module 302 is also used to execute the target simulation function.
[0104] Optionally, the device also includes: an acquisition module and a calculation module; the acquisition module is used to obtain the current value of the high-resolution performance counter and obtain the current time based on the current value; the calculation module is used to calculate the target difference between the current time and the initial time, and determine the ratio of the target difference to the frequency of the high-resolution performance counter as the first time corresponding to the current moment; wherein the initial time is: the time corresponding to the initial value of the high-resolution performance counter.
[0105] Optionally, the calculation module is also used to calculate the target time difference between the first time and the simulation time of the current moment; the execution module 302 is specifically used to execute the corresponding target operation based on the second comparison result of the target time difference and the fine timing time, until the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment; wherein, the target operation includes: a first operation and a second operation; the first operation is: when the target time difference is greater than the fine timing time, using a coarse timing function to sleep for a first preset time; the second operation is: when the target time difference is less than or equal to the fine timing time, sleeping for a second preset time by repeatedly querying the processor clock time.
[0106] Optionally, the comparison module 301 is also used to determine the size of the target time difference and the fine timing time, and generate a second comparison result; the execution module 302 is specifically used to execute the first operation if the second comparison result indicates that the target time difference is greater than the fine timing time, otherwise, execute the second operation.
[0107] Optionally, the execution module 302 is specifically configured to create a target event; the execution module 302 is further specifically configured to call a coarse timing function of an operating system to set a target timer, and trigger the target event after the first preset time length; and the execution module 302 is further specifically configured to end the target event when a time object of the target event becomes a signal state.
[0108] Optionally, the execution module 302 is specifically configured to query a processor clock time of a current time of an operating system.
[0109] Optionally, the execution module 302 is further configured to execute the target simulation function in a case where the first comparison result indicates that the first time corresponding to the current time is greater than or equal to the second time corresponding to the current time.
[0110] The high-precision timing device for real-time simulation of a Windows system provided in the application first compares a first time corresponding to a current time and a second time corresponding to the current time, and generates a first comparison result; the first time is a processor clock time calculated based on an initial value of a high-resolution performance counter; the second time is calculated based on a sum of a simulation time of the current time and a timing precision control quantity; the simulation time of the current time is calculated based on a sum of a simulation time of a previous round of simulation and a simulation step; then, in a case where the first comparison result indicates that the first time corresponding to the current time is less than the second time corresponding to the current time, a preset time calibration algorithm is executed in a loop until the first time corresponding to the current time is greater than or equal to the second time corresponding to the current time; finally, a target simulation function is executed. In this way, the CPU resource occupation can be reduced while ensuring high precision, thereby improving the overall performance and reliability of the system.
[0111] Figure 4 An example of a schematic diagram of a physical structure of an electronic device is shown in FIG. 1. Figure 4As shown, the electronic device may include: a processor 410, a communications interface 420, a memory 430, and a communications bus 440, wherein the processor 410, the communications interface 420, and the memory 430 communicate with each other via the communications bus 440. The processor 410 may call logic instructions in the memory 430 to execute a high-precision timing method for real-time simulation of a Windows system, the method comprising: comparing a first time corresponding to a current moment with a second time corresponding to the current moment, and generating a first comparison result; the first time is the processor clock time calculated based on the initial value of a high-resolution performance counter; the second time is calculated based on the sum of the simulation time at the current moment and a timing accuracy control value; the simulation time at the current moment is calculated based on the sum of the simulation time and the simulation step size of the previous simulation round; if the first comparison result indicates that the first time corresponding to the current moment is less than the second time corresponding to the current moment, cyclically executing a preset time calibration algorithm until the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment; and executing the target simulation function.
[0112] In addition, the logical instructions in the above-mentioned memory 430 can be implemented in the form of a software functional unit and can be stored in a computer-readable storage medium when sold or used as an independent product. Based on this understanding, the technical solution of the present application, or the part that contributes to the existing technology, or the part of the technical solution, can be embodied in the form of a software product. The computer software product is stored in a storage medium and includes several instructions for enabling a computer device (which can be a personal computer, server, or network device, etc.) to execute all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk, and other media that can store program code.
[0113] On the other hand, the present application also provides a computer program product, which includes a computer program stored on a computer-readable storage medium, and the computer program includes program instructions. When the program instructions are executed by a computer, the computer can execute the high-precision timing method for real-time simulation of Windows systems provided by the above methods, the method including: comparing the first time corresponding to the current moment with the second time corresponding to the current moment, and generating a first comparison result; the first time is: the processor clock time calculated based on the initial value of the high-resolution performance counter; the second time is: calculated based on the simulation time at the current moment and the sum of the timing accuracy control amount; the simulation time at the current moment is calculated based on the sum of the simulation time and the simulation step size of the previous round of simulation; when the first comparison result indicates that the first time corresponding to the current moment is less than the second time corresponding to the current moment, the preset time calibration algorithm is executed cyclically until the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment; and the target simulation function is executed.
[0114] On the other hand, the present application also provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, is implemented to execute the above-mentioned high-precision timing method for real-time simulation of Windows systems, the method comprising: comparing a first time corresponding to a current moment with a second time corresponding to the current moment, and generating a first comparison result; the first time is: the processor clock time calculated based on the initial value of a high-resolution performance counter; the second time is: calculated based on the sum of the simulation time at the current moment and the timing accuracy control amount; the simulation time at the current moment is calculated based on the sum of the simulation time and the simulation step size of the previous round of simulation; when the first comparison result indicates that the first time corresponding to the current moment is less than the second time corresponding to the current moment, the preset time calibration algorithm is executed cyclically until the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment; and the target simulation function is executed.
[0115] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate, and the components shown as units may or may not be physical units, i.e., they may be located in one location or distributed across multiple network units. Some or all of the modules may be selected based on actual needs to achieve the objectives of the present embodiment. Persons of ordinary skill in the art will be able to understand and implement the present invention without inventive effort.
[0116] Through the above description of the embodiments, those skilled in the art will clearly understand that each embodiment can be implemented using software plus a necessary general-purpose hardware platform, or of course, hardware. Based on this understanding, the essence of the above technical solution, or the portion that contributes to the prior art, can be embodied in the form of a software product. This computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a magnetic disk, or an optical disk, and includes a number of instructions for causing a computer device (such as a personal computer, server, or network device) to execute the methods described in each embodiment or certain portions of the embodiments.
[0117] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present application.
Claims
1. A high-precision timing method for real-time simulation of Windows systems, characterized in that: include: Comparing a first time corresponding to the current moment with a second time corresponding to the current moment, and generating a first comparison result; The first time is: a processor clock time calculated based on an initial value of a high-resolution performance counter; The second time is calculated based on the sum of the simulation time at the current moment and the timing accuracy control amount; the simulation time at the current moment is calculated based on the sum of the simulation time and the simulation step size of the previous round of simulation; If the first comparison result indicates that the first time corresponding to the current moment is less than the second time corresponding to the current moment, cyclically executing the preset time calibration algorithm until the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment; Execute target simulation function; The method of cyclically executing a preset time calibration algorithm until a first time corresponding to a current moment is greater than or equal to a second time corresponding to the current moment includes: Calculating a target time difference between the first time and the simulation time at the current moment, and performing a corresponding target operation based on a second comparison result of the target time difference and the fine timing time until the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment; Among them, the target operation includes: a first operation and a second operation; the first operation is: when the target time difference is greater than the fine timing time, using a coarse timing function to sleep for a first preset time; the second operation is: when the target time difference is less than or equal to the fine timing time, sleeping for a second preset time by repeatedly querying the processor clock time; the fine timing time is determined by querying the current processor clock time.
2. The method according to claim 1, characterized in that The first time corresponding to the current moment is calculated based on the following steps: Obtaining a current value of the high-resolution performance counter, and obtaining a current time based on the current value; Calculating a target difference between the current time and an initial time, and determining a ratio of the target difference to the frequency of the high-resolution performance counter as a first time corresponding to the current moment; The initial time is the time corresponding to the initial value of the high-resolution performance counter.
3. The method according to claim 1, characterized in that The performing a corresponding target operation based on a second comparison result between the target time difference and the fine timing time includes: Determining the size of the target time difference and the fine timing time, and generating a second comparison result; If the second comparison result indicates that the target time difference is greater than the fine timing time, the first operation is performed; otherwise, the second operation is performed.
4. The method according to claim 3, characterized in that The performing of the first operation includes: Create target event; Calling a rough timing function of the operating system to set a target timer, and triggering the target event after the first preset time period; When the time object of the target event becomes a signal state, the target event is ended.
5. The method according to claim 3, characterized in that The performing of the second operation includes: Query the operating system's current processor clock time.
6. The method according to claim 1, characterized in that After comparing the first time corresponding to the current moment with the second time corresponding to the current moment and generating a first comparison result, the method further includes: In a case where the first comparison result indicates that a first time corresponding to the current moment is greater than or equal to a second time corresponding to the current moment, the target simulation function is executed.
7. A high-precision timing device for real-time simulation of Windows system, characterized in that: The device comprises: a comparison module, configured to compare a first time corresponding to a current moment with a second time corresponding to the current moment, and generate a first comparison result; the first time being a processor clock time calculated based on an initial value of a high-resolution performance counter; the second time being a sum of a simulation time at the current moment and a timing precision control value; the simulation time at the current moment being a sum of a simulation time and a simulation step size of a previous simulation round; an execution module, configured to, if the first comparison result indicates that the first time corresponding to the current moment is less than the second time corresponding to the current moment, cyclically execute a preset time calibration algorithm until the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment; The execution module is further used to execute the target simulation function; The execution module is specifically configured to calculate a target time difference between the first time and the simulation time at the current moment, and execute a corresponding target operation based on a second comparison result of the target time difference and the fine timing time until the first time corresponding to the current moment is greater than or equal to the second time corresponding to the current moment; Among them, the target operation includes: a first operation and a second operation; the first operation is: when the target time difference is greater than the fine timing time, using a coarse timing function to sleep for a first preset time; the second operation is: when the target time difference is less than or equal to the fine timing time, sleeping for a second preset time by repeatedly querying the processor clock time; the fine timing time is determined by querying the current processor clock time.
8. An electronic device, characterized in that: The invention comprises a memory, a processor and a computer program stored in the memory and executable on the processor. When the processor executes the program, the steps of the high-precision timing method for real-time simulation of Windows system as claimed in any one of claims 1 to 6 are implemented.
9. A computer-readable storage medium, characterized in that A computer program is stored thereon, and when the computer program is executed by a processor, the steps of the high-precision timing method for real-time simulation of a Windows system as claimed in any one of claims 1 to 6 are implemented.
Citation Information
Patent Citations
Simulation system clock synchronization method, device and apparatus and storage medium
CN111290477A
Real-time simulation timing method and system based on multi-core processor platform and medium
CN112463328A