Is there any chance that CLOCK_REALTIME changes time automatically? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
I'm using linux api clock_gettime(realtime) to print current time. Check log index ending with 649 and 917.
The real clock time was 1646948676.999081502(at index 649)
,but after 10 secs it suddenly jumped more than >24 hours back and was 1646860487.614595043(index 917).
Main thing i wanna know is what is VuC offset? Will VucOffset affect this realtime?
and
Could you please explain what is the reason for time jump? and how can we avoid this?
221649 2022/03/10 02:44:37.000000 PF_CLOCK_SYNC CLOCK_MONOTONIC_RAW=35.228995663 s CLOCK_MONOTONIC=35.228996808 s CLOCK_REALTIME=1646948676.999081502 s
230583 2022/03/10 02:44:46.000000 receiveTMPData data received !!
230584 2022/03/10 02:44:46.000000 TMP::Data received is: -56563019
230585 2022/03/10 02:44:46.000000 TMP:TM_OUTPUT_VUC_OFFSET IS SET!!
231917 2022/03/10 02:44:47.000000 PF_CLOCK_SYNC CLOCK_MONOTONIC_RAW=45.230177586 s CLOCK_MONOTONIC=45.230296214 s CLOCK_REALTIME=1646860487.614595043 s

CLOCK_REALTIME is your classic "wall clock", similar in spirit to what is returned by the older gettimeofday() function call. In particular, it is non-monotonic, which means that it can be expected under some circumstances to 'jump' forward or backwards by an arbitrary amount.
Generally "some circumstances" will be limited to "immediately after the local user calls settimeofday() or by some other mechanism changes his computer's clock setting"; but it is also possible for an automated clock-synchronization (like ntpd or ptpd) to automatically adjust the clock forwards or backwards if it feels the need to do so. (I wouldn't expect either of those to adjust the clock by 24 hours unless they decided that the computer's existing clock-value was 24 hours off from the clock they are trying to synchronize it to, of course).

Related

Absolute time since a fixed time point, independent from the OS clock [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I am trying to get the number of seconds since some fixed points cannot be manipulated. Meaning, if the user changed OS date and time, it would still give me the true absolute number of seconds between the fixed time point and the moment of executing the code lines, I've tried chrono::steady_clock, but it seems to be affected by the changes in the OS clock.
Regardless Runtime, Accuracy, or Resolution.
There is no such standard library clock. After all, how would that work? All clocks have to be based on something, set and maintained by someone. The system's clock has to know when it is relative to something else, and this happens when the user sets it.
The only way to achieve something like that is to access an off-system clock, like one of the websites that sends you the current UTC time.

Looking for APIs to measure battery use of a process [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Question Is there an API for Windows and/or Linux that will let me find out which processes are draining battery?
A few notes:
It doesn't have to be precise, I'd be ok with only three levels (low energy cost/fair energy cost/high energy cost) if that's all I can get.
I already have CPU load information, that's not what I am looking for, as experience shows that processes with very low CPU use can nevertheless drain battery by having high I/O, high swapping or frequent wakeups. This is why I'd like to piggyback on whatever the OS is already using: OS developers are much more likely than me to have actually tested this.
I found a partial solution for macOS. See answers.
MSDN doesn't seem to indicate any energy-related API.
I cannot request admin/root rights for running my task manager, so I cannot simply parse the syslogs looking for power usage alerts.
I've found a great reference for how macOS does it.
https://blog.mozilla.org/nnethercote/2015/08/26/what-does-the-os-x-activity-monitors-energy-impact-actually-measure/
That should be fairly easy to follow.

Is there a better way to generator/publish data at given Frequency other then using the clock() function [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have to generate & publish data (as protocol buffer messages) with a frequency of 1Hz and I have been thinking of alternatives to just sticking it into a While loop with a clock() function. Maybe I am over thinking the issue but any advice on the matter would be much appreciated thanks
There are many ways to call a function on a regular interval. Which one is best depends on the situation and needs of the program. There's no single "best way".
A few possibilities (there are many more):
Sit in a loop and sleep for some duration every time round the loop. Simple, easy to understand. But rarely the best solution, since nothing else can happen while you sleep.
Sit in a loop waiting for some event to occur. Like a timer event. Process each event as it arrives - if not enough time has passed, ignore the event (maybe).
Set up a timer with a callback function that will call the function at regular intervals.
If your intervals are very tiny, then maybe spin / busy-wait in a loop and check elapsed time each time around the loop and do something when enough time has passed (rarely a good idea since it will burn a lot of CPU time doing nothing, but sometimes it's the right option).

Efficiency vs Memory tradeoff [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am creating an interactive sudoku board in c++. Whenever the user changes a value, I would like to check if the board is completed. The board will be completed when all spaces on the board are filled. My two ideas of how to do this are:
Create a private data member that holds the amount of filled spaces. To check if the board is completed I will simply have to check if this value equals boardLength^2
Create a member function that iterates through the board and returns false when a blank space is found and true if it goes through the board without finding any blank spaces
Is this a matter of preference, or is there a more accepted/correct way to do this?
Is this a matter of preference, or is there a more accepted/correct way to do this?
There is an accepted and correct way of optimizing, in general:
Optimize for speed or memory footprint when you actually need to, when you identify an actual problem. Your project's unique requirements will govern what constitutes a "problem".
Otherwise, optimize your code for readability and maintainability.
In your particular case:
Chances are that no matter which algorithm you choose, your check will happen so quickly that you will not be able to measure it, and the user will never notice the difference between the simple solution and the "fast" solution. Any attempts to optimize this (at the cost of complexity or readability or time spent writing code) are poor trade-offs.
Use the simplest possible solution. Once finished, if there is a noticeable delay on user input, and you can confirm that it's caused by an inefficient check for board completion, consider ways to improve your algorithm.

Elegant way to excecute many alarm clocks? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Let's say I have a vector of exact future times of multiple events. I want those events to launch when their respective times have been reached. How do I do this without constantly using if statements to check that the current time is one of those set times? This constant checking, especially if many other things are running in my program (and some of those times may be a bit away from now) will reduce the performance of my program. I'm using c++11, latest version of GCC.
If you're using C++11, it's probably easiest to create a thread, have it sleep until the next alarm time. The most efficient way to store the times for the alarms is probably a priority queue.
If you don't have C++ 11 available, you might consider Boost Threads instead. The standard's threads are based closely on Boost threads, so using them will be fairly similar.
If you don't want to use that, you'll pretty much need to use operating system facilities to do the job. With almost any reasonably modern OS, you can create a thread and have it sleep until the next alarm time, but you'll probably also have something that supports what you want a little more directly (call a function at a specified time). The details of that will be specific to the OS though.