|
| 1 | +//////////////////////////////////////////////////////////////////////////////// |
| 2 | +// Copyright (C) 2021 christmann informationstechnik + medien GmbH & Co. KG |
| 3 | +// |
| 4 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +// of this software and associated documentation files (the "Software"), to deal |
| 6 | +// in the Software without restriction, including without limitation the rights |
| 7 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +// copies of the Software, and to permit persons to whom the Software is |
| 9 | +// furnished to do so, subject to the following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included in |
| 12 | +// all copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | +// SOFTWARE. |
| 21 | +//////////////////////////////////////////////////////////////////////////////// |
| 22 | +// Author: Micha vor dem Berge <micha.vordemberge@christmann.info> |
| 23 | +//////////////////////////////////////////////////////////////////////////////// |
| 24 | + |
| 25 | +#include "SensorProviderJetson.h" |
| 26 | + |
| 27 | +#include <cstring> |
| 28 | +#include <cstdlib> |
| 29 | +#include <iostream> |
| 30 | +#include <fstream> |
| 31 | +#include <stdio.h> |
| 32 | +#include <unistd.h> |
| 33 | +#include <sys/types.h> |
| 34 | +#include <object_model.h> |
| 35 | +#include <SensorBean.h> |
| 36 | +#include <fcntl.h> |
| 37 | +#include <sys/mman.h> |
| 38 | +#include <errno.h> |
| 39 | +#include <sys/ioctl.h> |
| 40 | +#include <sys/reboot.h> |
| 41 | +#include <sys/statvfs.h> |
| 42 | + |
| 43 | +using namespace std; |
| 44 | + |
| 45 | +LoggerPtr SensorProviderJetson::logger; |
| 46 | + |
| 47 | +void * SensorProviderJetson::create(PF_ObjectParams *) { |
| 48 | + return new SensorProviderJetson(); |
| 49 | +} |
| 50 | + |
| 51 | +int32_t SensorProviderJetson::destroy(void * p) { |
| 52 | + if (!p) |
| 53 | + return -1; |
| 54 | + delete static_cast<SensorProviderJetson*>(p); |
| 55 | + return 0; |
| 56 | +} |
| 57 | + |
| 58 | +SensorProviderJetson::SensorProviderJetson() : |
| 59 | + mSensors() { |
| 60 | + |
| 61 | + ifstream myfile("/sys/bus/i2c/drivers/ina3221x/1-0040/iio_device/in_power0_input"); |
| 62 | + if (myfile.is_open()) { |
| 63 | + myfile.close(); |
| 64 | + |
| 65 | + // Power Measurements |
| 66 | + SensorBean* sensor = new SensorBean("Power CPU", TYPE_FLOAT, 8, 1, UNIT_POWER, false, false, 0, 0, 0, 0, "", RENDERING_TEXTUAL); |
| 67 | + sensor->setData((uint32_t)0); |
| 68 | + sensor->setUpdateCallback(&SensorProviderJetson::updatePowerCpu); |
| 69 | + mSensors["Power CPU"] = sensor; |
| 70 | + |
| 71 | + sensor = new SensorBean("Power GPU", TYPE_FLOAT, 8, 1, UNIT_POWER, false, false, 0, 0, 0, 0, "", RENDERING_TEXTUAL); |
| 72 | + sensor->setData((uint32_t)0); |
| 73 | + sensor->setUpdateCallback(&SensorProviderJetson::updatePowerGpu); |
| 74 | + mSensors["Power GPU"] = sensor; |
| 75 | + |
| 76 | + sensor = new SensorBean("Power SoC", TYPE_FLOAT, 8, 1, UNIT_POWER, false, false, 0, 0, 0, 0, "", RENDERING_TEXTUAL); |
| 77 | + sensor->setData((uint32_t)0); |
| 78 | + sensor->setUpdateCallback(&SensorProviderJetson::updatePowerSoC); |
| 79 | + mSensors["Power SoC"] = sensor; |
| 80 | + |
| 81 | + sensor = new SensorBean("Power CV", TYPE_FLOAT, 8, 1, UNIT_POWER, false, false, 0, 0, 0, 0, "", RENDERING_TEXTUAL); |
| 82 | + sensor->setData((uint32_t)0); |
| 83 | + sensor->setUpdateCallback(&SensorProviderJetson::updatePowerCv); |
| 84 | + mSensors["Power CV"] = sensor; |
| 85 | + |
| 86 | + sensor = new SensorBean("Power DDR", TYPE_FLOAT, 8, 1, UNIT_POWER, false, false, 0, 0, 0, 0, "", RENDERING_TEXTUAL); |
| 87 | + sensor->setData((uint32_t)0); |
| 88 | + sensor->setUpdateCallback(&SensorProviderJetson::updatePowerSoC); |
| 89 | + mSensors["Power DDR"] = sensor; |
| 90 | + |
| 91 | + sensor = new SensorBean("Power SYS5V", TYPE_FLOAT, 8, 1, UNIT_POWER, false, false, 0, 0, 0, 0, "", RENDERING_TEXTUAL); |
| 92 | + sensor->setData((uint32_t)0); |
| 93 | + sensor->setUpdateCallback(&SensorProviderJetson::updatePowerSys5V); |
| 94 | + mSensors["Power SYS5V"] = sensor; |
| 95 | + |
| 96 | + // Temperature Measurements |
| 97 | + sensor = new SensorBean("Temp SoC", TYPE_FLOAT, 8, 1, UNIT_TEMPERATURE, false, false, 0, 0, 0, 0, "", RENDERING_TEXTUAL); |
| 98 | + sensor->setData((uint32_t)0); |
| 99 | + sensor->setUpdateCallback(&SensorProviderJetson::updateTempSoC); |
| 100 | + mSensors["Temp SoC"] = sensor; |
| 101 | + } else { |
| 102 | + LOG_ERROR(logger, "This plugin only works on Nvidia Jetson Xavier AGX, not initializing."); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +SensorProviderJetson::~SensorProviderJetson() { |
| 107 | +} |
| 108 | + |
| 109 | +map<string, ISensor*> SensorProviderJetson::getSensors(void) { |
| 110 | + return mSensors; |
| 111 | +} |
| 112 | + |
| 113 | +void SensorProviderJetson::updatePowerCpu(SensorBean* sensor) { |
| 114 | + string line; |
| 115 | + uint16_t milli_power; |
| 116 | + |
| 117 | + ifstream myfile("/sys/bus/i2c/drivers/ina3221x/1-0040/iio_device/in_power1_input"); |
| 118 | + if (myfile.is_open()) { |
| 119 | + getline(myfile, line); |
| 120 | + std::istringstream stream(line); |
| 121 | + stream >> milli_power; |
| 122 | + myfile.close(); |
| 123 | + } else { |
| 124 | + LOG_ERROR(logger, "Could not read file '/sys/bus/i2c/drivers/ina3221x/1-0040/iio_device/in_power1_input'"); |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + //LOG_DEBUG(logger, "Power CPU: " << milli_power / 1000.0 << " W"); |
| 129 | + sensor->setData(milli_power / 1000.0); |
| 130 | +} |
| 131 | + |
| 132 | +void SensorProviderJetson::updatePowerGpu(SensorBean* sensor) { |
| 133 | + string line; |
| 134 | + uint16_t milli_power; |
| 135 | + |
| 136 | + ifstream myfile("/sys/bus/i2c/drivers/ina3221x/1-0040/iio_device/in_power0_input"); |
| 137 | + if (myfile.is_open()) { |
| 138 | + getline(myfile, line); |
| 139 | + std::istringstream stream(line); |
| 140 | + stream >> milli_power; |
| 141 | + myfile.close(); |
| 142 | + } else { |
| 143 | + LOG_ERROR(logger, "Could not read file '/sys/bus/i2c/drivers/ina3221x/1-0040/iio_device/in_power0_input'"); |
| 144 | + return; |
| 145 | + } |
| 146 | + |
| 147 | + //LOG_DEBUG(logger, "Power GPU: " << milli_power / 1000.0 << " W"); |
| 148 | + sensor->setData(milli_power / 1000.0); |
| 149 | +} |
| 150 | + |
| 151 | +void SensorProviderJetson::updatePowerSoC(SensorBean* sensor) { |
| 152 | + string line; |
| 153 | + uint16_t milli_power; |
| 154 | + |
| 155 | + ifstream myfile("/sys/bus/i2c/drivers/ina3221x/1-0040/iio_device/in_power2_input"); |
| 156 | + if (myfile.is_open()) { |
| 157 | + getline(myfile, line); |
| 158 | + std::istringstream stream(line); |
| 159 | + stream >> milli_power; |
| 160 | + myfile.close(); |
| 161 | + } else { |
| 162 | + LOG_ERROR(logger, "Could not read file '/sys/bus/i2c/drivers/ina3221x/1-0040/iio_device/in_power2_input'"); |
| 163 | + return; |
| 164 | + } |
| 165 | + |
| 166 | + //LOG_DEBUG(logger, "Power SoC: " << milli_power / 1000.0 << " W"); |
| 167 | + sensor->setData(milli_power / 1000.0); |
| 168 | +} |
| 169 | + |
| 170 | +void SensorProviderJetson::updatePowerCv(SensorBean* sensor) { |
| 171 | + string line; |
| 172 | + uint16_t milli_power; |
| 173 | + |
| 174 | + ifstream myfile("/sys/bus/i2c/drivers/ina3221x/1-0041/iio_device/in_power0_input"); |
| 175 | + if (myfile.is_open()) { |
| 176 | + getline(myfile, line); |
| 177 | + std::istringstream stream(line); |
| 178 | + stream >> milli_power; |
| 179 | + myfile.close(); |
| 180 | + } else { |
| 181 | + LOG_ERROR(logger, "Could not read file '/sys/bus/i2c/drivers/ina3221x/1-0041/iio_device/in_power0_input'"); |
| 182 | + return; |
| 183 | + } |
| 184 | + |
| 185 | + //LOG_DEBUG(logger, "Power CV: " << milli_power / 1000.0 << " W"); |
| 186 | + sensor->setData(milli_power / 1000.0); |
| 187 | +} |
| 188 | +void SensorProviderJetson::updatePowerDDR(SensorBean* sensor) { |
| 189 | + string line; |
| 190 | + uint16_t milli_power; |
| 191 | + |
| 192 | + ifstream myfile("/sys/bus/i2c/drivers/ina3221x/1-0041/iio_device/in_power1_input"); |
| 193 | + if (myfile.is_open()) { |
| 194 | + getline(myfile, line); |
| 195 | + std::istringstream stream(line); |
| 196 | + stream >> milli_power; |
| 197 | + myfile.close(); |
| 198 | + } else { |
| 199 | + LOG_ERROR(logger, "Could not read file '/sys/bus/i2c/drivers/ina3221x/1-0041/iio_device/in_power1_input'"); |
| 200 | + return; |
| 201 | + } |
| 202 | + |
| 203 | + //LOG_DEBUG(logger, "Power DDR: " << milli_power / 1000.0 << " W"); |
| 204 | + sensor->setData(milli_power / 1000.0); |
| 205 | +} |
| 206 | + |
| 207 | +void SensorProviderJetson::updatePowerSys5V(SensorBean* sensor) { |
| 208 | + string line; |
| 209 | + uint16_t milli_power; |
| 210 | + |
| 211 | + ifstream myfile("/sys/bus/i2c/drivers/ina3221x/1-0041/iio_device/in_power2_input"); |
| 212 | + if (myfile.is_open()) { |
| 213 | + getline(myfile, line); |
| 214 | + std::istringstream stream(line); |
| 215 | + stream >> milli_power; |
| 216 | + myfile.close(); |
| 217 | + } else { |
| 218 | + LOG_ERROR(logger, "Could not read file '/sys/bus/i2c/drivers/ina3221x/1-0041/iio_device/in_power2_input'"); |
| 219 | + return; |
| 220 | + } |
| 221 | + |
| 222 | + //LOG_DEBUG(logger, "Power SYS5V: " << milli_power / 1000.0 << " W"); |
| 223 | + sensor->setData(milli_power / 1000.0); |
| 224 | +} |
| 225 | + |
| 226 | +void SensorProviderJetson::updateTempSoC(SensorBean* sensor) { |
| 227 | + string line; |
| 228 | + uint16_t milli_temp; |
| 229 | + |
| 230 | + ifstream myfile("/sys/devices/virtual/thermal/thermal_zone4/temp"); |
| 231 | + if (myfile.is_open()) { |
| 232 | + getline(myfile, line); |
| 233 | + std::istringstream stream(line); |
| 234 | + stream >> milli_temp; |
| 235 | + myfile.close(); |
| 236 | + } else { |
| 237 | + LOG_ERROR(logger, "Could not read file '/sys/devices/virtual/thermal/thermal_zone4/temp'"); |
| 238 | + return; |
| 239 | + } |
| 240 | + |
| 241 | + //LOG_DEBUG(logger, "Temp SoC: " << milli_power / 1000.0 << " C"); |
| 242 | + sensor->setData(milli_temp / 1000.0); |
| 243 | +} |
0 commit comments