Is there a way in qt to get the up time of the application as well as the up time for the system?
Thanks in advance.
You can use the QElapsedTimer class from Qt 4.7 to get uptime for your app. This class will use monotonic clocks if it can.
Just create an instance, and call start on it at the start of your program. From then on, you can get the number of milliseconds your program has been running (or more precisely, since the call to start) by calling
myElapsedTimer.elapsed()
On Windows you can simply calculate by calling Winapi function to get process start datetime.
More information you can find at http://www.codeproject.com/KB/threads/ProcessTime.aspx
On Linux, you can use the times system call to tell you elapsed processor time. This will not count the time your program has been idle waiting for input, or blocked waiting for input, or the time that it's been preempted by other programs also running on the system. (Therefore, this makes it very good for benchmarks.)
Related
I want to measure the runtime of a procedure that will last multiple days. This means the executing process will be interrupted many times since the Linux PC will be suspended to standby repeatedly. I don't want to include those sleep phases in my time measurement so I can't use simple time stamps.
Is there a better way to measure the net runtime of a procedure other than firing a QTimer every second and counting those timeouts?
As G.M. proposed, I used boost::timer::cpu_timer time to solve my issue. It returns the summed up computation time and the run time of the entire process which is fine for my purpose. Boost is not Qt but it is cross-platform anyway.
I am now trying to write a program that waits two minutes and 25 seconds in C++. I use the Sleep function of like that:
Sleep(145000);
Now, my laptop heats up every time I run this function, and the fan starts working.
Now to the question - is this function known for being wasteful? Should I even use it? do I have a better option?
The Windows Sleep() function puts the current thread to sleep. It doesn't run a busy-waiting while-loop or anything like that, it just re-schedules the thread to start up again after the sleep period specified as the function parameter. If your fan is starting up, I suggest looking at the currently running processes using Task Manager.
I have a class which measures the time between calling Start and Stop. I have created a unit test that sleeps using boost::this_thread::sleep between Start and Stop and I test that the result is near the time slept.
However, this test fails on our build agent but not our development machines. The problem is: How do I know whether this is an actual problem of the stopwatch or if it's a "problem" that the build agent (running some other processes, being a virtual machine) might sleep longer than I told it to?
So the question: Is there a robust way to write something like "Do something that takes exactly x seconds?"
Thanks a lot!
There is no way to test something like this reliably on a non-realtime system. The way to go would be to wrap the APIs for getting the system-time that your stop-watch uses and mock them in the tests.
Take the system time before and after the sleep and refer only to the difference between these times and not to the time you slept.
What is the resolution of your stopwatch? If you need to be accurate by seconds then sleeping for 3 seconds and seeing if you are between 2.9 and 3.1 will work for you. If you need mili/nano second accuracy you should just use timestamp mocks as suggestd in the first reply.
That depends on the operating system you're using. On systems that supports multi-programming, the running time of your thread is not deterministic. On some real-time systems however, it's almost accurate if your thread is of top priority. You should be able to disable the interrupt to simulate that case, because your thread will not be preempted by OS scheduler.
In my C++ program, I will start other programs with exec. However, I want to be able to specify a maximum amount of time that the programs can run. How can that be done?
Is setrlimit the right thing to use?
Bit of a brute-force version, but... save/get the handle of the started programm/process, start a timer and kill the other process after the timer has expired?
2 solutions that comes to mind.
1- Send the duration to the second program via the command line and manage the duration internally in the 2nd exe.
2- Create a timer in the first exe and when the timer is triggered kill the 2nd process.
Max.
In general, it can't be done using standard c++ - you will have to use whatever scheduling functions your operating system (which you haven't specified) provides.
Here is what I know about concurrency in OS.
In order to run multi-task in an OS, the CPU will allocate a time slot to each task. When doing task A, other task will "sleep" and so on.
Here is my question:
I have a timer program that count for inactivity of keyboard / mouse. If inactivity continues within 15min, a screen saver program will popup.
If the concurrency theory is as I stated above, then the timer will be inaccurate? Because each program running in OS will have some time "sleep", then the timer program also have chance "sleeping", but in the real world the time is not stop.
You would use services from the OS to provide a timer you would not try to implement yourself. If code had to run simple to count time we would still be in the dark ages as far as computing is concerned.
In most operating systems, your task will not only be put to sleep when its time slice has been used but also while it is waiting for I/O (which is much more common for most programs).
Like AnthonyWJones said, use the operating system's concept of the current time.
The OS kernel's time slices are much too short to introduce any noticeable inaccuracy for a screen saver.
I think your waiting process can be very simple:
activityTime = time of last last keypress or mouse movement [from OS]
now = current time [from OS]
If now >= 15 mins after activityTime, start screensaver
sleep for a few seconds and return to step 1
Because steps 1 and 2 use the OS and not some kind of running counter, you don't care if you get interrupted anytime during this activity.
This could be language-dependent. In Java, it's not a problem. I suspect that all languages will "do the right thing" here. That's with the caveat that such timers are not extremely accurate anyway, and that usually you can only expect that your timer will sleep at least as long as you specify, but might sleep longer. That is, it might not be the active thread when the time runs out, and would therefore resume processing a little later.
See for example http://www.opengroup.org/onlinepubs/000095399/functions/sleep.html
The suspension time may be longer than requested due to the scheduling of other activity by the system.
The time you specify in sleep() is in realtime, not the cpu time your process uses. (As the CPU time is approximately 0 while your program sleeps.)