Jump to content
Tuts 4 You
Sign in to follow this  
CodeExplorer

Timer Replacer (Visual C++)

Recommended Posts

CodeExplorer

Time in milliseconds of a timer:
time1 = when timer started
time1 = when timer proc ended

time1=9182437
time2=9182437

time1=9182452

The difference is big: 15 ms!

I know that 1 millisecond is much for computer performance!
I've tested some code and the time in ms was not even 1 ms.

uElapse [in]
    Type: UINT
    The time-out value, in milliseconds.
    If uElapse is less than USER_TIMER_MINIMUM (0x0000000A), the timeout is set to USER_TIMER_MINIMUM.
    If uElapse is greater than USER_TIMER_MAXIMUM (0x7FFFFFFF), the timeout is set to USER_TIMER_MAXIMUM.

I need something much better then a timer!

 

Share this post


Link to post
Share on other sites
CodeExplorer

I found an easy solution: just create a thread with a loop:
 

Quote

DWORD   dwThreadId;
HANDLE hThread = CreateThread(
            NULL,                   // default security attributes
            0,                      // use default stack size  
            MyThreadFunction,       // thread function name
            NULL,          // argument to thread function
            0,                      // use default creation flags
            &dwThreadId);   // returns the thread identifier

Quote

DWORD WINAPI MyThreadFunction( LPVOID lpParam )
{
while (true)
{

// code here

}

return 0;
}

 

 

Share this post


Link to post
Share on other sites
Teddy Rogers

What are you trying to do? If you need a general window event timer then SetTimer which, it looks as if you are using will work fine. If you want to measure the time of something consider using GetTickCount. If you need more timing resolution use QueryPerformanceCounter...

Ted.

Share this post


Link to post
Share on other sites
VirtualPuppet

If you want to be very performance aware, using rdtsc.

Share this post


Link to post
Share on other sites
CodeExplorer

I'm building an allocation API hoocker: will save ESP when API starts on DWORD 1 and EAX when the API ends on DWORD 2,
both DWORDs should be checked in a loop, SetTimer minimum value is of 0x0000000A miliseconds which is way to much for performance.
I want a fast loop.
 

Share this post


Link to post
Share on other sites
CodeExplorer
Posted (edited)
5 hours ago, VirtualPuppet said:

If you want to be very performance aware, using rdtsc.

I found some great articles about rdtsc:
https://msdn.microsoft.com/en-us/library/twchhe95.aspx
https://helloacm.com/the-rdtsc-performance-timer-written-in-c/
I don't need time performance measurement, for that GetTickCount would do the trick;
if the difference is less then 1 (ms) all is ok.
So far the CreateThread on a loop seems to be the fastest solution!

 

Edited by CodeExplorer (see edit history)

Share this post


Link to post
Share on other sites
atom0s

Keep in mind if you loop endlessly like that without any sleep/wait, you are going to cause the CPU usage of your process to spike really high.

Share this post


Link to post
Share on other sites
CodeExplorer
3 hours ago, atom0s said:

Keep in mind if you loop endlessly like that without any sleep/wait, you are going to cause the CPU usage of your process to spike really high.

When no Sleep at all CPU was somewhere around 25% and that crap won't work.
CPU usage 6% with some Sleep,
Why when I use: Sleep(5);
when I measure the difference the ms difference is 10 and even more?
The program won't work without Sleep at all since will report same value multiple times!
 

Share this post


Link to post
Share on other sites
kao

Whatever you're doing, you're doing it wrong.
If your code runs every millisecond (and it takes 1 millisecond to run), there just isn't enough time/resources to run anything else on that CPU core.

 

As for your question, the answer is in MSDN:

Quote

After the sleep interval has passed, the thread is ready to run. ... Note that a ready thread is not guaranteed to run immediately. Consequently, the thread may not run until some time after the sleep interval elapses. For more information, see Scheduling Priorities.

 

Share this post


Link to post
Share on other sites
CodeExplorer

Sleep is very weird:

DWORD time1 = GetTickCount();
Sleep(5);
DWORD time2 = GetTickCount();

time1 = 0x008a0159
time2 = 0x008a0159

How is that possible?
And sometimes the difference is more then 5 ms.
I expect more accuracy!

Quote

If your code runs every millisecond (and it takes 1 millisecond to run), there just isn't enough time/resources to run anything else on that CPU core. 

When I don't use Sleep: 25% of CPU when the program is running is not much I think! I would (even) say that the program doesn't use enough CPU resources.
But in the end I have to use Sleep!
 

Quote


If your code runs every millisecond (and it takes 1 millisecond to run)

That is not even possible!
 

Share this post


Link to post
Share on other sites
kao

25% of CPU is exactly 1 core of the 4-core CPU you have. :) 

Read the MSDN article I linked earlier, it also explains why sometimes thread will sleep less than expected. And some suggestions how to work around it.

 

But in the end, I really think you should go back to drawing board and find another way to do whatever you're trying to do. 

Share this post


Link to post
Share on other sites
CodeExplorer
CodeExplorer

I've thought that there is something wrong with my program: actually is just "slow"
over an Asprotect unpackme with all options enabled.
It takes ~8 minutes to trace that sheet. The get speed is slower directly proportional with number of (allocations) Apis called!
But when you try to build an trace script for Olly for tracing Apis I would say that my tracer is ok.
 

Share this post


Link to post
Share on other sites
atom0s

GetTickCount is not a timer you should use to benchmark differences like that. Also, Windows rounds sleeps up to 10 in most cases due to CPU timing. So values between 1-10 are generally going to give the result of 10 regardless. 

If you want to accurately test the time between the two values you showed above, use a lower level clock like the CPU timer etc.

Share this post


Link to post
Share on other sites
CodeExplorer
Quote

GetTickCount is not a timer you should use to benchmark differences like that.

I know that GetTickCount is not a timer, GetTickCount just returns number of ms passed,
I think for my needs GetTickCount did the trick.

the "Sleep(5);" should Sleep for at last 5 ms not like it does sometimes for 0 ms!
That Sleep for 0 is a shit I have to fix!

 

Share this post


Link to post
Share on other sites
CodeExplorer

I made a solution:

void CriticalTimeSleep(int ms)
{
DWORD time1 = GetTickCount();
DWORD time2 = 0;

do
{
Sleep(ms);
time2 = GetTickCount();
}
while ((time2-time1)<ms);

}

CriticalTimeSleep(10);
I've called that after I've read each dword (they are two dwords to read).

On the loop I've called only Sleep(1),
I don't know exactly what to do in main loop!!!
 

Quote

So values between 1-10 are generally going to give the result of 10 regardless.

It would be great if that would be the case, but sometimes the Sleep time is 0!
 

Share this post


Link to post
Share on other sites
CodeExplorer

Some new info: I use Sleep(1) in main loop: sometime it takes 4 minutes, sometime 8 minutes, sometimes it takes 12 minutes: same Asprotect unpackme
I tend to think that it is because of the random Sleep time of Sleep(1).

I've found these resources:
https://stackoverflow.com/questions/1616392/why-does-an-empty-loop-use-so-much-processor-time
https://stackoverflow.com/questions/175882/whats-the-algorithm-behind-sleep
https://social.msdn.microsoft.com/Forums/vstudio/en-US/facc2b57-9a27-4049-bb32-ef093fbf4c29/threadsleep1-sleeps-for-156-ms?forum=clr

So to sum up my problem: how do to make it Sleep only some milliseconds:
I don't want very high CPU usage but neither low performance like it is when I use Sleep(1) ,
I'm looking for a sort of compromise!
 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×