.NET Forum / Languages / Managed C++ / June 2005
CPU usage
|
|
Thread rating:  |
Paulo Eduardo - 24 Jun 2005 19:28 GMT 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.
Jochen Kalmbach [MVP] - 24 Jun 2005 19:56 GMT > Is there an API to set % of CPU Usage? No. What are you trying to do?
If your prcess has an endless-loop, then remove it and switch to event-driven architecture...
If the work of your process is not important, then reduce the priority.
See: SetPriorityClass(..., BELOW_NORMAL_PRIORITY_CLASS) http://msdn.microsoft.com/library/en-us/dllproc/base/setpriorityclass.asp
 Signature Greetings Jochen
My blog about Win32 and .NET http://blog.kalmbachnet.de/
Paulo Eduardo - 24 Jun 2005 20:36 GMT 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.
> > Is there an API to set % of CPU Usage? > [quoted text clipped - 8 lines] > See: SetPriorityClass(..., BELOW_NORMAL_PRIORITY_CLASS) > http://msdn.microsoft.com/library/en-us/dllproc/base/setpriorityclass.asp Jochen Kalmbach [MVP] - 24 Jun 2005 21:01 GMT 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 [quoted text clipped - 3 lines] > 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/ge tprocesstimes.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
 Signature Greetings Jochen
My blog about Win32 and .NET http://blog.kalmbachnet.de/
Paulo Eduardo - 24 Jun 2005 21:39 GMT 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.
> Hi Paulo! > [quoted text clipped - 25 lines] > See: GetSystemInfo > http://msdn.microsoft.com/library/en-us/sysinfo/base/getsysteminfo.asp Ronald Laeremans [MSFT] - 24 Jun 2005 22:02 GMT > Jochen, > our app will be running in background (background process). Actually, we are [quoted text clipped - 34 lines] >>See: GetSystemInfo >>http://msdn.microsoft.com/library/en-us/sysinfo/base/getsysteminfo.asp 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/prodtechnol/windowsserver2003/library/TechRef/c 3541e6e-342d-45d2-a211-44c556306e91.mspx
Ronald Laeremans Visual C++ team
Willy Denoyette [MVP] - 24 Jun 2005 22:08 GMT 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.
> Jochen, > our app will be running in background (background process). Actually, we [quoted text clipped - 43 lines] >> See: GetSystemInfo >> http://msdn.microsoft.com/library/en-us/sysinfo/base/getsysteminfo.asp Paulo Eduardo - 24 Jun 2005 22:47 GMT 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.
> 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 [quoted text clipped - 54 lines] > >> See: GetSystemInfo > >> http://msdn.microsoft.com/library/en-us/sysinfo/base/getsysteminfo.asp Tamas Demjen - 24 Jun 2005 23:29 GMT 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 > [quoted text clipped - 7 lines] > Best Regards, > Paulo Eduardo. Severian [MVP] - 25 Jun 2005 08:24 GMT >Hi, Willy > [quoted text clipped - 3 lines] >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.
>> 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 [quoted text clipped - 54 lines] >> >> See: GetSystemInfo >> >> http://msdn.microsoft.com/library/en-us/sysinfo/base/getsysteminfo.asp -- Phillip Crews aka Severian Microsoft MVP, Windows SDK Posting email address is real, but please post replies on the newsgroup.
Paulo Eduardo - 24 Jun 2005 23:24 GMT 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.
> Hi Paulo! > [quoted text clipped - 25 lines] > See: GetSystemInfo > http://msdn.microsoft.com/library/en-us/sysinfo/base/getsysteminfo.asp Jochen Kalmbach [MVP] - 25 Jun 2005 10:18 GMT Hi Paulo!
> In your response you mentioned 'resolution'. Are you speaking about time > slice of a process? The others have suggested what you can further do... I don´t need to add something...
 Signature Greetings Jochen
My blog about Win32 and .NET http://blog.kalmbachnet.de/
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|