0

I'm building a VSPackage and my project demands that the package will not consume over then 50% CPU. How can I limit it from the program?

Note: I'm not interesting in prioritizing, I need to set CPU usage to 50% (hard coded).

user247702
  • 23,641
  • 15
  • 110
  • 157
Yogevnn
  • 1,430
  • 2
  • 18
  • 37
  • I don't think it's a simple as that. No more than 50% *ever*? Are should it be no more than 50% for `X` seconds, etc.? The better approach is to profile your application and look for inefficiencies which cause high CPU usage, rather than arbitrarily try and limit CPU usage. Limiting it would only make the operations it was trying to do in the first place take longer, hardly solving the original performance requirement (or what I assume was the spirit of it). – Martin Costello Apr 22 '14 at 15:20
  • You should also remember that for multicore processors total percent will be divided to count of cores. so, 25% if full usages of one core on 4 cores processor – Sergey Litvinov Apr 22 '14 at 15:23
  • Yes i need to limit it under 50% for all actions, i don't care about timing. I know it's a little odd but trying to explain the reason is almost impossible, if i could allow more CPU or not allow it when it comes to 50% will be great, but i will settle for just limiting the whole program to maximum 50% cpu – Yogevnn Apr 22 '14 at 15:24
  • 1
    (made me actually curious about that reasons...) – fast Apr 22 '14 at 15:28
  • 1
    Setting the delay introduced by context switch aside. CPU usage / load is calculated over a period of time so if your time granularity is 0.1s just make sure that your app goes to sleep every 0.05s for 0.05s. Also, see http://stackoverflow.com/questions/2514544/simulate-steady-cpu-load-and-spikes – matcheek Apr 22 '14 at 15:28
  • 2
    What you're asking doesn't make sense. The CPU is either busy or idle. So to measure 50% you have to measure over time. – Brian Rasmussen Apr 22 '14 at 15:30
  • @SergeyLitvinov - I know, my program will run on 4 cores computers, so i need to limit to 50% of all cores – Yogevnn Apr 22 '14 at 15:32
  • So use 2 threads with max (100%) CPU-utilization.. – fast Apr 22 '14 at 15:34
  • if you want to limit to 50% of 4 cores, then you shouldn't use more than two threads in your logic. then it won't be bigger than 50% of all cores, as two threads will use approximately two cores, and it will be 50% of 4 cores machine – Sergey Litvinov Apr 22 '14 at 15:34
  • Thank you all for answering, but it is still not what i'm looking for, it's a little bit weird i know so i will try to explain again, lets say i have a section in my code that i know it is going to take the computer resources, i want to add something next to the code that will tell the computer not to use more then 50% CPU for the following code. Or maybe to limit all program CPU usage to 50% and if needed to allow X time of more then that – Yogevnn Apr 23 '14 at 11:33

1 Answers1

0

Create one or more threads within your extension that run with priority ThreadPriority.Lowest. Even better than manual throttling, this solution will use the CPU to its full extent unless another task (inside or outside Visual Studio) needs to compute something.

Sam Harwell
  • 97,721
  • 20
  • 209
  • 280