Hi friends,
I am currently working on an issue where i should write a program which utilizes Cpu as specified by the user. The function should be provided with an argument ( how much percentage of CPU has to be utilized by the process ) for example CPU(75) should utilize 75% of CPU. The function should be terminated by a signal. Is this feasible ???? guys please help me out. Thanks in advance. is there any linux commands ? all NETBSD functions can be used.
Thanks,
Chandra Mohan .S
Executing function is not visible to the kernel but the process in which its wrapped is available to the external world for communication. I guess this is what you meant right?
Keeping pinging for the load average often if it exceeds or equals commit suicide or restart
sub get_system_load {
open(L, '<', '/proc/loadavg') or die "Unable to open /proc/loadavg file <$!>\n";
my $data = <L>;
close(L);
return (split(' ', $data))[0];
}
Look up processor affinity - settask() - will allow you to specify which cpu a process runs on.
A percentage of one single CPU or of all CPUs/Cores/Threads available to it ?
With what sampling rate ? Load is meaningful only as an average value. Instantaneously, a process is either running 100% or 0% of each CPU.
I think am wrong here, what I gave is only a generic load average fetch and controlling based on that. It will really not fetch the utilization of a process bound to a processor.
Load average and CPU load are indeed different metrics.
Oh - ignore the affinity comment. I thought you wanted to designate a cpu for a given process.
Monitoring of cpu usage as a percent by a process on itself is just adding pointless overhead. If you do not want to degrade system performance, then nice the process.
That is why nice exists.
If the process gets 100% cpu, when in a lower priority, it then means nothing else needs it. So it does not degrade overall performance. Plus, 100% cpu utilization in and by itself does not mean the system is overloaded - it could be a background process running at low priority. Things like cpu wait queue length or i/o queue request length are far more important.
On the other hand, if you want to limit CPU usage by a percentage and are using a recent enough Solaris release, you can use per project or per zone CPU capping.
CPU caps (Project rm.cpu-caps) - XWiki
No idea about Linux or netBSD.
Hi friends,
Thanks for your replies. By the way mine is a single processor environment and i need this program to do some stress testing. My task should consume CPU as per the specification ie the amount the task should utilize is given as a parameter eg:utilizeCpu(90) should utilize 90% of the CPU. I dont know whether this is feasible. I am a newbie to linux please let me know if there is any way to sort out this problem ????
Thanks,
Chandru.
Why do you need that ?
Is this homework ?
Why don't you answer the questions asked in post #4 ?
In any case, you cannot guarantee a process will use 90% of a CPU or 90% of all CPUs. That depends on CPU availability/ what other processes and the kernel are doing.
Hi,
The function should take the CPU utilization to the desired percentage and stay there. It should be based on the amount of CPU being used by the processes currently running. now if i give 90% cumulatively all the process taken together along with my process should take 90% of the CPU. Need not prempt any processes running at that time. I could not understand some concepts in post #4 what you mean by "Instantaneously a process is running either 100% or 0%" could you be more clear on that? Thanks for your reply.
Why do you need that ? (You only describe what you need, not why)
Is this homework ? (You failed to answer that one)
What concepts in the questions asked in post #4 are you failing to understand ?
You cannot reliably limit the CPU used to 90% without preempting running processes.
By 100% or 0%, I mean CPU load is computed by sampling. At a given time, a CPU is either idle (0%) or not (100%). Intermediary values are only the result of averaging these values.