CPU usage

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi, All!

We are developing one app for windows 95/98/Me/NT4.0/2000/XP/2003 using
Visual C++ 6.0. We need to set the % of CPU Usage to app process. Is there
an API to set % of CPU Usage? Can Someone help us?

Thanks in advance.
 
Hi, Jochen

Actually, if I understood you, we are using an event-driven architecture,.
We are using in it c functions and c++ methods that use WMI service. We are
not using one endless loop (for (;;)) too. Is some local of source code we
need to access fixed drives (C:,E:) for search files in them (subdirectories
too). Is it possible to control the cpu usage by process creating one thread
that control the cpu usage of process? For example, If process is using more
than 20% of CPU, the 'thread control' sleep the process for 1s.

Thanks in advance.
 
Hi Paulo!
Actually, if I understood you, we are using an event-driven architecture,.
We are using in it c functions and c++ methods that use WMI service. We are
not using one endless loop (for (;;)) too. Is some local of source code we
need to access fixed drives (C:,E:) for search files in them (subdirectories
too). Is it possible to control the cpu usage by process creating one thread
that control the cpu usage of process? For example, If process is using more
than 20% of CPU, the 'thread control' sleep the process for 1s.

Why do you want to limit your CPU usage? Why not just lower your priority?

To get the process/thread-times you can use

See: GetProcessTimes
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/getprocesstimes.asp

or

See: GetThreadTimes
http://msdn.microsoft.com/library/en-us/dllproc/base/getprocesstimes.asp

You should be aware, that the resolution is about than 10-15 ms...

To implement it correctly you also need to know the number of (logical)
processors...

See: GetSystemInfo
http://msdn.microsoft.com/library/en-us/sysinfo/base/getsysteminfo.asp

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
Jochen,
our app will be running in background (background process). Actually, we are
using the default settings for process. The app run when user log on in
windows. Then, for avoid to increase the cpu usage by process, we are trying
to control the cpu usage. Do you consider that only priority will resolve
this problem?

Thanks a lot in advance.
 
Paulo said:
Jochen,
our app will be running in background (background process). Actually, we are
using the default settings for process. The app run when user log on in
windows. Then, for avoid to increase the cpu usage by process, we are trying
to control the cpu usage. Do you consider that only priority will resolve
this problem?

Thanks a lot in advance.



:
One solution is to: Run at a low priority, see how much CPU time you
used in the last 100 ms (or other reasonable interval) using the APi
sugegsted in this thread and then sleep for the appropriate time if it
exceeded your limit of much much you wanted to load the machine.

Specifically for managing competing processes'resource consumption on
Server 2003 Enterprise and ata Center editions, there is WSRM, you can
read up on what it is and how it works at:
http://www.microsoft.com/technet/pr...Ref/c3541e6e-342d-45d2-a211-44c556306e91.mspx

Ronald Laeremans
Visual C++ team
 
No, it won't, if this is the only process in the system that has runnable
threads, nothing will stop the thread to consume 100% of the CPU, if on the
other hand other applications are running and their threads have higher
priority, your lower priority threads will get much less CPU resources, but
be carefull with this, on consumer windows this can lead to thread
starvation.
I'm also not clear on what your actual CPU consumption is, and why you need
to restrict this while it will probably be done automatically when there are
other programs running.
Willy.
 
Hi, Willy

Actually, the app process are using almost 40% of cpu usage. About % Cpu
Usage, we need this for avoid to decrease performance of system, and
consequently the user interactivity with it(slowly,freeze,etc). Also, we
will go to develop one part of system that scan fixed drives (c:,e:) and,
probably this will consume more cpu.

Thanks a lot in advance.
Best Regards,
Paulo Eduardo.
 
Hi, Jochen

In your response you mentioned 'resolution'. Are you speaking about time
slice of a process?

Thanks a lot in advance.
Best regards,
Paulo Eduardo.
 
The CPU usage is managed by the OS. Just reduce the priority to increase
the system performance. The system does an automatic load balancing. If
nothing else is running, your application will use close to 100% CPU,
but as soon as other processes come up, it may go down to 0%. I managed
to achieve extremely good results with low priority threads. The thread
works as hard as possible when it gets a chance, but it never takes away
any significant CPU time from other processes.

The actual CPU usage always depends on the current load. If you're only
tunning one application, it will have a high CPU usage. The lower the
priority, the lower the CPU usage during heavy load situations.
Hyperthreading helps significantly too.

Tom
 
Hi, Willy

Actually, the app process are using almost 40% of cpu usage. About % Cpu
Usage, we need this for avoid to decrease performance of system, and
consequently the user interactivity with it(slowly,freeze,etc). Also, we
will go to develop one part of system that scan fixed drives (c:,e:) and,
probably this will consume more cpu.

If you reduce the priority, this will prevent your process from
interfering with other processes' CPU use; however, if you're doing
lots of disk I/O, you may need to throttle it manually to prevent
effects on the rest of the system.
 
Back
Top