How to programmatically obtain relative power consumption? - c++

Recently I need to monitor the battery consumption of some process.
The method I already know is to get it through top:
top -stats pid,command,cpu,idlew,power -o power -d
In addition, it can also be obtained in the Activity Monitor of macOS, but I do not know whether there is an operating system interface, so that I can directly obtain a relative value of the power consumption of a process.
I tried to use proc_pid_rusage. The returned parameters are ri_billed_energy and ri_serviced_energy, but they are always 0. I want to know if there are other interfaces or methods that allow me to get this value.

Related

Change Windows time using SetLocalTime() without rebooting

We are setting up an embedded Windows system which needs to have a function to change the system date and time. Our system does not shut down normally just like a normal PC, instead, the user will cut the power directly.
The problem comes when we tried to use the Win API function like SetLocalTime() (with admin rights), the API succeeded when the function just executed, but when the user restarts the machine (i.e. cut the power and turning it on again), the date setting is reverted back to the previous date setting. Looks like the OS cache the setting which requires a normal restart to store the setting to disk.
Does anyone know how to "flush the data cache" immediately without restarting the OS?
Thank you very much.
There are two workaround you can check to see if it helps:
After change Windows time you explicitly execute a reboot to enable your changes to be written to disk before the user cut the power directly. You can use the following API to reboot the system:
ExitWindowsEx(EWX_REBOOT | EWX_FORCE,
SHTDN_REASON_MAJOR_OPERATINGSYSTEM |
SHTDN_REASON_MINOR_UPGRADE |
SHTDN_REASON_FLAG_PLANNED)
Ref: "How to Shut Down the System"
Caching can be turned off for a file is opened by passing FILE_FLAG_NO_BUFFERING as a value for the dwFlagsAndAttributes parameter of CreateFile. When caching is disabled, all read and write operations directly access the physical disk. So you can store the time on a such file and read it when the system reboot and set it again(Starting an Scheduled task on System Boot).

Determining what memory/values a remote application accesses/changes?

Lets take an example here which is known everywhere in the IT world:
We have a game, for example solitaire, and someone makes and releases a trainer for it that your moves are always '0'.
How do I programatically determine which adresses and what values that "hack" changes?
What way would be the best, if this is possible?
From within the game [injecting/loading my own dll?]
By intercepting traffic between the hack and target process with my own process?
I ask this question because of 2 things:
Protect an application from being "hacked" (at least by the script kiddies)
Reverse engineer a trainer (so you don't have to reinvent the wheel / avoid NIH syndrome)
You can't. Some broken attempts may be setting two addresses and then comparing them (they will find the other address though). Or they can simply remove your compare call.
They can alter any protection function that you use to "programatically determine" to always return false results. They can do anything to your executable, so there is no way.
Unless you hook the kernel functions that open your process to modify the memory. But that is also breakable and if I am not wrong you need to get your "protection kernel driver" digitally signed now.
There is another way in which you load a DLL in every running and newly spawned processes (which will probably alert antiviruses about your program being a virus), with that DLL you hook OpenProcess (and if there is another alternative to it, that too) functions in each process and check if its targeted at your program, and prevent it if so. Search about function hooking. I believe there was something called "MS Detour" or something for it.
And still, the game will not even be close to safe.
To sum up, no way is good to protect your game locally. If you are storing scores or something you should create a server program and client should report every move to server.
Even then, they can create a bot to automatically respond to server. Then the best you can do is somehow verify it is a human that is playing. (maybe captcha or comparing the solving speed with human avarage?)

does WMI cache data in requests?

I've tried the Win32_DesktopMonitor and checked the "Availability", but the value returned is always 3 (powered on), even when the monitor is physically turned off.
Is the data cached and there's a "force refresh" command in WMI, or in this particular case, the "Availability" is just not reliable ?
I think there is caching going on somewhere. I've observed it recently.
I wrote code that was polling for updates to Win32_PnPSignedDriver via SelectQuery / ManagementObjectSearcher and the results appear to be cached because it never realizes that a new device/driver has been added. Running the query from a separate app instantly sees that it was updated.
You may have a look to your driver. According to the documentation, starting with Windows Vista, hardware that is not compatible with Windows Display Driver Model (WDDM) returns inaccurate property values for instances of this class. For me it's an another way to say that it's not reliable.

How to Disable Dynamic Frequency Scaling?

I would like to do some microbenchmarks, and try to do them right. Unfortunately dynamic frequency scaling makes benchmarking highly unreliable.
Is there a way to programmatically (C++, Windows) find out if dynamic frequency scaling is enabled? If, can this be disabled in a program?
Ive tried to just use a warmup phase that uses 100% CPU for a second before the actual benchmark takes place, but this turned out to be not reliable either.
UPDATE: Even when I disable SpeedStep in the BIOS, cpu-z shows that the frequency changes between 1995 and 2826 GHz
In general, you need to do the following steps:
Call CallNtPowerInformation() and pass SystemPowerCapabilities to InformationLevel parameter, set lpInputBuffer and nInputBufferSize to NULL, then set lpOutputBuffer to SYSTEM_POWER_CAPABILITIES structure, and set nOutputBufferSize to the size of the structure. After this first call, SYSTEM_POWER_CAPABILITIES structure containing the current system power capabilities. To check whether the system supports processor throttling, read the value of ProcessorThrottle.
There are other two members we are interested in, they are, ProcessorMinThrottle and ProcessorMaxThrottle; they represents the minimum and maximum level of system processor throttling supported, expressed as a percentage. If both members has already values 100%, this means CPU throttling is currently disabled, so you don't need to reconfigure it.
To disable CPU throttling, you need to set ProcessorMinThrottle and ProcessorMaxThrottle to 100%. To do this, call CallNtPowerInformation() again and pass SystemPowerCapabilities to InformationLevel parameter; but now, set lpInputBuffer to the SYSTEM_POWER_CAPABILITIES structure in which the two members has been set to 100%. I'm sure you know what to do next.
In non-programmatic way, you can also get/set Windows Power Options using the Windows built-in command-line tools, that is, PowerCfg.
Further Reading
Power Management
Power Management Functions
So far, none of the above CallNtPowerInformation options worked for me. The relevant ProcessorThrottle field of SYSTEM_POWER_CAPABILITIES was FALSE and changing some SYSTEM_POWER_POLICYs didn't work.
However, https://www.geeks3d.com/20170213/how-to-disable-intel-turbo-boost-technology-on-a-notebook/#_24 outlines a way to make an option available in the power management settings.
With ProcMon, I was able to trace it back to the following registry manipulations:
Read the ActivePowerScheme SZ value under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Power\User\PowerSchemes to get the active power plan
Set the ACSettingIndex and/or DCSettingIndex DWORD under Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Power\User\PowerSchemes\<above active power plan GUID>\54533251-82be-4824-96c1-47b60b740d00\be337238-0d82-4146-a960-4f3749d470c7 to 0 (Disabled, or whatever you choose) from 2 (High)
Unfortunately, the relevant keys are owned by the system, which either means you have to prompt the user (which has to have admin access) to change the permissions of the key or you have to use powercfg to manipulate the setting. The latter is preferrable and actually seems to work, even without admin access (courtesy of https://learn.microsoft.com/en-us/windows-server/administration/performance-tuning/hardware/power/power-performance-tuning#processor-performance-boost-mode):
powercfg -setacvalueindex scheme_current sub_processor PERFBOOSTMODE 0
powercfg -setdcvalueindex scheme_current sub_processor PERFBOOSTMODE 0
powercfg -setactive scheme_current
In Windows XP and later CPU speed is managed by power policy. Doesn't it turn off the scaling if you set "Max performance" mode in Windows power management dialog?
There're also some third party tools - SpeedSwitchXP for example.
Programmatically this could be done, I suppose, using CallNtPowerInformation function.

How do you get how much memory a program uses?

I have two programs, one in C++, the other in assembler. I want to compare how much memory they use when running respectively. How can I do this?
I am doing the testing on Windows, but I also would like to know how to do it on Linux.
On Linux, try valgrind. It's an amazing tool with too many features for mere mortals to totally comprehend. Have a look at valgrind's massif.
Run the program in one shell. Open another shell and run 'top' command. it will list running processes and home much memory they consume. you can, i guess, poll /proc/yourprocessid/stat to see how much memory it is using over time.
On Windows you can use Performance Monitor.
Performance monitor usage
Start Performance Monitor from Start menu/ Administrative Tools/ Performance
If you want to start the logging:
Select performance log and alert>Current Log option in the left side of the browser.
Select New Log Settings.
Give an apropriate name to the log e.g. performance_Server for Server
It will prompt you one menu. In “general” tabs click on the add button and select the process you want monitor. (Change the performance object to process, for “select counters from list” select “private bytes”, for “select instances from list”, select the process you want to monitor.) After that click on Add and close. Now change the interval as per test case requirement. Now go to “log files” tab change the log file type to either csv or tsv format. Now apply and press OK.
If you want to start/stop the logging:
Select the particular log you want to start and stop.
In toolbar above you will see start and stop button.
If you want to check the content of a log file:
Click Options/Data From…
Select the log file to be viewed, click OK
Go to the chart screen (View/Chart)
Click Edit/Add to chart
Add the required items to the chart. (In case the memory leakage is to be checked, then you need to view the PrivateBytes of the processes and the _Total of them)
Read the values from the chart (Min and Max values are displayed at the bottom of the chart)
If you want to monitor network transfer:
Display the chart screen (View/Chart)
Click Edit/Add to log, and select the items Network Interface\Bytes Sent If you set it in dl
Or Network Inerface\ Bytes Received if you set it in the CRS-PC+
Click Done
Monitor memory usage:
In menu Start/Programs/Administrative Tools/ start the program Performance Monitor
Click on the button to open the window that adds processes
Fill the fields as follows:
Object: Process
Counter: Private Bytes
Instance: a process whose Memory occupation need to be displayed
Click on Add button
Repeat the last two steps for every process the memory needs to be displayed
Close the window that adds processes
On the bottom of the Performance Monitor window, there is the list of the processes previously selected.
How to use the logged data
Now open the file Perfmon_.csv or Perfmon_.tsv using WordPad or Excel.
If you have opened the file using Excel, then using the option Save As, save the file in the Microsoft Excel format.
The Windows Task manager can show you the memory usage of each process. I guess you could use Valgrind instead, but I don't see the point in that. On Linux, use Valgrind or ps.
On Windows you can use the GetProcessMemoryInfo Function.
Here is an example on how to use it:
Collecting Memory Usage Information For a Process
Depends on your operating system - you would expect to have tools to tell you the memory consumed when the apps are running.
Trying to infer the answer by inspecting the code would be very difficult, run the apps, use your platform's diagnoistics.
Depending on the size of the programs, this could be nearly unfeasible.
If they are not very large, then you can see how much memory they allocate; for instance, an int would take up 4 bytes, a char would take 1 byte, etc. Assembly is very transparent in how much memory it is using, even on an x86 machine. Cpp is nearly as transparent, as long as you faithfully track object creation and memory destruction/allocation.
If the program is huge, you'll need to use specific tools for tracking/profiling memory use, like GlowCode (http://www.glowcode.com/summary.htm).
On Windows, you can use Microsoft's Performance Monitor to do it. Start, Run, "perfmon". This tool will report on all sorts of statistics about processes, and provide graphs for you. In general, you're going to be interested in reporting on the "Private working set". This will tell you how much memory your process has reserved for its own use.
If you want to just get your heap usage, and you want to do it programatically, you should look into the CRT Debug Heap.
I'm not sure about Linux though, sorry.
On Windows, I have found Address Space Monitor very useful, especially for looking at how fragmented your memory is.