How this code line is reading power consumption information?

117 Views Asked by At

I came across following program that reads power consumed by ARM based raspberry pi and writes it to another file:

#include <iostream>
#include <fstream>
#include <string>
#include <chrono>
#include <ctime>

int main() {
    // Open the power consumption file
    std::ifstream powerFile;
    powerFile.open("/sys/devices/platform/soc/c440000.qcom,spmi/spmi-0/spmi0-02/c440000.qcom,spmi:qcom,pm8150b@2:qpnp,fg/power_supply/bms/power_now");

    if (!powerFile.is_open()) {
        std::cerr << "Failed to open the power file." << std::endl;
        return 1;
    }

    // Open the log file
    std::ofstream logFile;
    logFile.open("power_log.txt", std::ios::app); // Append mode

    if (!logFile.is_open()) {
        std::cerr << "Failed to open the log file." << std::endl;
        return 1;
    }

    // Get the current time
    auto currentTime = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());

    // Read power consumption data
    std::string powerData;
    std::getline(powerFile, powerData);

    if (!powerData.empty()) {
        double powerConsumed = std::stod(powerData) / 1000000.0; // Convert to milliwatts

        // Log the data to the file
        logFile << "Time: " << std::ctime(&currentTime) << "Power Consumed: " << powerConsumed << " mW" << std::endl;
    } else {
        std::cerr << "Failed to read power data from the file." << std::endl;
    }

    // Close the files
    powerFile.close();
    logFile.close();

    return 0;
}

I dont know if its correct or not. How I am puzzled by following line:

powerFile.open("/sys/devices/platform/soc/c440000.qcom,spmi/spmi-0/spmi0-02/c440000.qcom,spmi:qcom,pm8150b@2:qpnp,fg/power_supply/bms/power_now");

It does not seem to be name of the file and it seems like some comma separated value or something. What does this line do?

1

There are 1 best solutions below

0
minorChaos On

/sys is mounted to a certain virtual filesystem (virtual ``disk''). So the file that you name is a virtual file - when you read from it, the kernel does some actions and generates the data. The particular virtual filesystem mounted to /sys is sysfs.

What data are generated can be found in the sources of linux kernel or perhaps in the sources of some kernel module (parts of the /sys are changed dynamically).

The actions to obtain the data depend on the particular device; it may go down to some 2-wire physical communication, for example. Some places where I see pm8150 (or qcom or spmi) are: mfd, dtsi, read, spmi, qcom; qcom,pm8150b in mfd.

It comes to my mind that since the communication can be slow, the information could be cached. But it is equally possible that your read operation could block your program for some time.

Possibly some of the directories are actually symbolic links, so you may get some more information by looking at the output of ls -l sys/devices/platform/soc/c440000.qcom,spmi/spmi-0/spmi0-02/ or longer or shorter paths.