Connecting Tech Pros Worldwide Forums | Help | Site Map

CPU usage

Paulo Eduardo
Guest
 
Posts: n/a
#1: Nov 17 '05
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]
Guest
 
Posts: n/a
#2: Nov 17 '05

re: CPU usage


Paulo Eduardo wrote:
[color=blue]
> Is there an API to set % of CPU Usage?[/color]

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...orityclass.asp

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Paulo Eduardo
Guest
 
Posts: n/a
#3: Nov 17 '05

re: CPU usage


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.



"Jochen Kalmbach [MVP]" wrote:
[color=blue]
> Paulo Eduardo wrote:
>[color=green]
> > Is there an API to set % of CPU Usage?[/color]
>
> 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...orityclass.asp
>
> --
> Greetings
> Jochen
>
> My blog about Win32 and .NET
> http://blog.kalmbachnet.de/
>[/color]
Jochen Kalmbach [MVP]
Guest
 
Posts: n/a
#4: Nov 17 '05

re: CPU usage


Hi Paulo!
[color=blue]
> 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.[/color]

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/de...ocesstimes.asp

or

See: GetThreadTimes
http://msdn.microsoft.com/library/en...ocesstimes.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...systeminfo.asp

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Paulo Eduardo
Guest
 
Posts: n/a
#5: Nov 17 '05

re: CPU usage


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.



"Jochen Kalmbach [MVP]" wrote:
[color=blue]
> Hi Paulo!
>[color=green]
> > 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.[/color]
>
> 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/de...ocesstimes.asp
>
> or
>
> See: GetThreadTimes
> http://msdn.microsoft.com/library/en...ocesstimes.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...systeminfo.asp
>
> --
> Greetings
> Jochen
>
> My blog about Win32 and .NET
> http://blog.kalmbachnet.de/
>[/color]
Ronald Laeremans [MSFT]
Guest
 
Posts: n/a
#6: Nov 17 '05

re: CPU usage


Paulo Eduardo wrote:[color=blue]
> 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.
>
>
>
> "Jochen Kalmbach [MVP]" wrote:
>
>[color=green]
>>Hi Paulo!
>>
>>[color=darkred]
>>>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.[/color]
>>
>>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/de...ocesstimes.asp
>>
>>or
>>
>>See: GetThreadTimes
>>http://msdn.microsoft.com/library/en...ocesstimes.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...systeminfo.asp
>>
>>--
>>Greetings
>> Jochen
>>
>> My blog about Win32 and .NET
>> http://blog.kalmbachnet.de/
>>[/color][/color]
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/pro...556306e91.mspx

Ronald Laeremans
Visual C++ team
Willy Denoyette [MVP]
Guest
 
Posts: n/a
#7: Nov 17 '05

re: CPU usage


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.

"Paulo Eduardo" <PauloEduardo@discussions.microsoft.com> wrote in message
news:36EBA7EC-F512-47A4-A85E-EB42B9B32648@microsoft.com...[color=blue]
> 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.
>
>
>
> "Jochen Kalmbach [MVP]" wrote:
>[color=green]
>> Hi Paulo!
>>[color=darkred]
>> > 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.[/color]
>>
>> 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/de...ocesstimes.asp
>>
>> or
>>
>> See: GetThreadTimes
>> http://msdn.microsoft.com/library/en...ocesstimes.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...systeminfo.asp
>>
>> --
>> Greetings
>> Jochen
>>
>> My blog about Win32 and .NET
>> http://blog.kalmbachnet.de/
>>[/color][/color]


Paulo Eduardo
Guest
 
Posts: n/a
#8: Nov 17 '05

re: CPU usage


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.

"Willy Denoyette [MVP]" wrote:
[color=blue]
> 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.
>
> "Paulo Eduardo" <PauloEduardo@discussions.microsoft.com> wrote in message
> news:36EBA7EC-F512-47A4-A85E-EB42B9B32648@microsoft.com...[color=green]
> > 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.
> >
> >
> >
> > "Jochen Kalmbach [MVP]" wrote:
> >[color=darkred]
> >> 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/de...ocesstimes.asp
> >>
> >> or
> >>
> >> See: GetThreadTimes
> >> http://msdn.microsoft.com/library/en...ocesstimes.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...systeminfo.asp
> >>
> >> --
> >> Greetings
> >> Jochen
> >>
> >> My blog about Win32 and .NET
> >> http://blog.kalmbachnet.de/
> >>[/color][/color]
>
>
>[/color]
Paulo Eduardo
Guest
 
Posts: n/a
#9: Nov 17 '05

re: CPU usage


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.

"Jochen Kalmbach [MVP]" wrote:
[color=blue]
> Hi Paulo!
>[color=green]
> > 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.[/color]
>
> 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/de...ocesstimes.asp
>
> or
>
> See: GetThreadTimes
> http://msdn.microsoft.com/library/en...ocesstimes.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...systeminfo.asp
>
> --
> Greetings
> Jochen
>
> My blog about Win32 and .NET
> http://blog.kalmbachnet.de/
>[/color]
Tamas Demjen
Guest
 
Posts: n/a
#10: Nov 17 '05

re: CPU usage


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

Paulo Eduardo wrote:[color=blue]
> 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.[/color]
Severian [MVP]
Guest
 
Posts: n/a
#11: Nov 17 '05

re: CPU usage


On Fri, 24 Jun 2005 14:47:03 -0700, "Paulo Eduardo"
<PauloEduardo@discussions.microsoft.com> wrote:
[color=blue]
>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.[/color]

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.
[color=blue]
>"Willy Denoyette [MVP]" wrote:
>[color=green]
>> 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.
>>
>> "Paulo Eduardo" <PauloEduardo@discussions.microsoft.com> wrote in message
>> news:36EBA7EC-F512-47A4-A85E-EB42B9B32648@microsoft.com...[color=darkred]
>> > 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.
>> >
>> >
>> >
>> > "Jochen Kalmbach [MVP]" wrote:
>> >
>> >> 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/de...ocesstimes.asp
>> >>
>> >> or
>> >>
>> >> See: GetThreadTimes
>> >> http://msdn.microsoft.com/library/en...ocesstimes.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...systeminfo.asp
>> >>
>> >> --
>> >> Greetings
>> >> Jochen
>> >>
>> >> My blog about Win32 and .NET
>> >> http://blog.kalmbachnet.de/
>> >>[/color]
>>
>>
>>[/color][/color]

--
Phillip Crews aka Severian
Microsoft MVP, Windows SDK
Posting email address is real, but please post replies on the newsgroup.
Jochen Kalmbach [MVP]
Guest
 
Posts: n/a
#12: Nov 17 '05

re: CPU usage


Hi Paulo![color=blue]
> In your response you mentioned 'resolution'. Are you speaking about time
> slice of a process?[/color]

The others have suggested what you can further do... I don“t need to add
something...

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Closed Thread