0

I want to control CPU usage through my application, Can anyone help how to achieve that?

For Ex : I want to write a function in that if we pass argument as 150 then cpu usage for that particular process should be 150.

Sorry for not writing code as, I am not sure how to begin with.

So i am not asking for the code but i am asking, If anyone can help me with the logic/how to achieve that?

Chirag
  • 587
  • 1
  • 4
  • 17

1 Answers1

1

Basically the Linux kernel already includes features to manage resources for any kind of process; and you can exploit that without writing C code.

To the contrary: you simply want to educate yourself how to use cgroups; and then you want to use the concepts behind that with "minimal" efforts on writing custom code on your side.

Do not re-invent the wheel here; as in: your wheel be (most likely) deficient compared to what the existing cgroups can do for you today!

But even when reading your comment, I don't get why you think you have to implement your own solution. If your program needs less than X%; you can't do anything about it anyway. And if it wants more than X; what is the point of creating your own proprietary mechanism of getting there?

Anyhow, maybe the cpulimit tool could help you getting there.

And, as your "real" problem seems to be: "how to generate CPU load"; well there are plenty of ways to get there, see here for example.

Community
  • 1
  • 1
GhostCat
  • 127,190
  • 21
  • 146
  • 218
  • Thank you sir for your valuable reply, But my question is something different. I am very much aware with cgroup feature and functionality. Sir, Basically my question is my application/my program should start CPU usage as provided in argument. For Ex: if i start while(1); CPU utilization = 100% if i put sleep(1) in while(1) the CPU utilization = 0-1%. So what i want is, Whatever the utilization i provide in argument, my application should start using that much cpu. – Chirag Aug 22 '16 at 09:37
  • Sir, Basically i am preparing a test tool for gerating CPU stress as per requirement, I think cpulimit will help for < 100% but what about > 100%? How can i generate CPU usage more than 100%? I think Creating threads only will help, or is there any other way? – Chirag Aug 22 '16 at 09:51