LinuxBeginner82 asked:
Is there a source code or anything similar that can handle the following method
If you are a beginner in linux, as your forum name suggests, then the following answer may or may not be of any use to you, but here goes anyway, as I understand it. It assumes familiarity with the command line in the terminal.
The source code is freely available and can be found on github. Unless you wish to implement some new and hitherto undeveloped software capability, there really is no need for software development at the source code level because there are numerous programs to deal with the outputs that most users are interested in, including all the ones you have mentioned.
determining the current performance state of a processor
The current performance state of a processor is available from the output of the cpufreq-info command which is from the cpufrequtils package. For example:
Code:
[flip@flop ~]$ cpufreq-info
cpufrequtils 008: cpufreq-info (C) Dominik Brodowski 2004-2009
Report errors and bugs to [email protected], please.
analyzing CPU 0:
driver: intel_pstate
CPUs which run at the same hardware frequency: 0
CPUs which need to have their frequency coordinated by software: 0
maximum transition latency: 4294.55 ms.
hardware limits: 800 MHz - 3.70 GHz
available cpufreq governors: performance, powersave
current policy: frequency should be within 800 MHz and 3.70 GHz.
The governor "powersave" may decide which speed to use
within this range.
current CPU frequency is 3.70 GHz.
<snip>
This is the output from a computer here showing just the first cpu of a 4 cpu core processor with the rest of the output snipped off. It shows that the governor is "powersave" and that the "performance" governor is also available. Another computer here has a larger selection of governors: conservative, powersave, ondemand, userspace, performance, schedutil, so there's more to choose from on that machine. Info on the governors are in the kernel docs here:
https://www.kernel.org/doc/Documentation/cpu-freq/governors.txt. The cpu can have its governor altered, and also be set to any value within the Hz ranges shown in the output.
The performance of the cpu can be monitored with the mpstat command which is from the sysstat package. It can be run in many different ways to gather stats at once or at intervals. Simplest usage is as follows which shows output that is a summary of the 4 cpus, but each can be targeted in multiple ways:
Code:
[flip@flop ~]$ mpstat
Linux 6.1.0-7-amd64 (fen) 04/05/23 _x86_64_ (4 CPU)
19:42:26 CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
19:42:26 all 9.95 0.00 3.13 0.44 0.00 0.03 0.00 0.00 0.00 86.44
There are other programs that show cpu usage as well including top, htop, btop and others too.
comparing the current performance state to a requested/desired new performance state
if the requested/desired new performance state is lower than the current performance state, then the processor switches to this new performance state
These tasks are probably best performed by a shell script designed for the purpose using the outputs of the various commands whose info one is interested in. In particular the command: cpufreq-set, can alter cpu behaviour and can be triggered to do so within a script. The command: cpufreq-aperf, can calculate average frequencies over various time periods. They are included in the cpufrequtils package. Output from any and all commands mentioned so far can be manipulated in a shell script.
The man pages for the commands are fairly informative, if terse, but there's lots online about using these tools.