Skip to content

Commit 4e8d59c

Browse files
committed
feat: update
1 parent cbf706a commit 4e8d59c

3 files changed

Lines changed: 118 additions & 22 deletions

File tree

include/resource_monitor/resource_monitor_stats.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class ResourceMonitorStats
2121
AMStat cpu_stats = AMStat("cpu_s", "CPU Stats", 1, 2, 80, 99);
2222
AMStat gpu_stats = AMStat("gpu_s", "GPU Stats", 1, 2, 80, 99);
2323
AMStat ram_stats = AMStat("ram_s", "RAM Stats", 1, 2, 80, 99);
24+
AMStat drive_stats = AMStat("drive_s", "Drive Stats", 1, 2, 80, 99);
2425
AMStat lidar_ip = AMStat("lidar_ip_s", "Lidar IP Stats", 1, 2, 80, 99);
2526
AMStat fl_ip = AMStat("fl_s", "FL IP Stats", 1, 2, 80, 99);
2627
AMStat fr_ip = AMStat("fr_s", "FR IP Stats", 1, 2, 80, 99);
@@ -35,6 +36,7 @@ class ResourceMonitorStats
3536
stat_list.add(&gpu_stats);
3637
stat_list.add(&cpu_stats);
3738
stat_list.add(&ram_stats);
39+
stat_list.add(&drive_stats);
3840
stat_list.add(&lidar_ip);
3941
stat_list.add(&fl_ip);
4042
stat_list.add(&fr_ip);

include/resource_monitor/resource_status_class.h

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include <vb_util_lib/transformer.h>
1313
#include <resource_monitor/resource_monitor_stats.h>
1414
#include <std_msgs/msg/int32.hpp>
15+
#include <sys/statvfs.h> // For statvfs
16+
#include <iomanip> // For std::setprecision
1517

1618

1719
namespace am
@@ -30,9 +32,7 @@ struct GpuInfo
3032
{
3133
std::string gpu_name;
3234
int temp;
33-
int mem_used;
34-
int mem_free;
35-
int util_percent;
35+
int load_percent;
3636
};
3737

3838
struct CpuInfo
@@ -48,6 +48,13 @@ struct CpuInfo
4848
unsigned long long total;
4949
};
5050

51+
struct DiskInfo {
52+
unsigned long long totalSpace; // Total space in bytes
53+
unsigned long long availableSpace; // Available space in bytes (matches `df`)
54+
unsigned long long usedSpace; // Used space in bytes
55+
double percentUsed; // Percentage used
56+
};
57+
5158
class ResourceStatus
5259
{
5360
public:
@@ -59,10 +66,12 @@ class ResourceStatus
5966

6067
am::CpuInfo getCPUInfo();
6168

62-
void getGPUInfo(std::vector<am::GpuInfo> &gpu_infos);
69+
am::GpuInfo getGPUInfo();
6370

6471
void getCPUInfo(std::vector<am::CpuInfo> &infos);
6572

73+
DiskInfo getDiskInfo(const std::string& path = "/");
74+
6675
double calculateCpuLoad(const am::CpuInfo &ci, const am::CpuInfo &ci_old);
6776

6877
double getUpTime();
@@ -102,6 +111,8 @@ class ResourceStatus
102111

103112
int getCPUCoresCount();
104113

114+
std::string readFile(const std::string& path);
115+
105116
am::CpuInfo parseCpuLine(const std::string &line);
106117

107118
int cpu_cnt_= -1;
@@ -120,7 +131,7 @@ class ResourceStatus
120131

121132
std::vector<am::CpuInfo> cpu_infos_old_;
122133

123-
std::vector<am::GpuInfo> gpu_infos_;
134+
am::GpuInfo gpu_info_;
124135

125136
std::map<std::string, std::string> ip_addresses_; //IPAddress, Name
126137

src/resource_monitor/resource_status_class.cpp

Lines changed: 100 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <cstdio> // For popen and fgets
66
#include <memory> // For std::unique_ptr
77
#include <regex> // For std::regex
8+
#include <boost/filesystem.hpp>
89

910
namespace am
1011
{
@@ -182,7 +183,26 @@ void ResourceStatus::updateInfos()
182183

183184
cpu_infos_old_ = cpu_infos_;
184185

185-
getGPUInfo(gpu_infos_);
186+
gpu_info_ = getGPUInfo();
187+
stats_->gpu_stats = 50;
188+
ROS_INFO("GPU Load Percent: %d , Temp: %d", gpu_info_.load_percent, gpu_info_.temp);
189+
if(gpu_info_.load_percent > 90)
190+
{
191+
stats_->gpu_stats = 100;
192+
ROS_ERROR("GPU Issues: LOAD Percent: %d, Temp: %d", gpu_info_.load_percent, gpu_info_.temp);
193+
}
194+
195+
196+
//check the drive stats
197+
stats_->drive_stats = 50;
198+
am::DiskInfo disk_info = getDiskInfo();
199+
if(disk_info.percentUsed > 98.0)
200+
{
201+
stats_->drive_stats = 100;
202+
int coef = 1024*1024;
203+
ROS_ERROR("Disk total: %lld MB, available: %lld MB, used: %lld MB, percentage: %f", (disk_info.totalSpace/coef), (disk_info.availableSpace/coef), (disk_info.usedSpace/coef), disk_info.percentUsed);
204+
}
205+
186206
}
187207

188208

@@ -224,21 +244,69 @@ am::MemoryInfo& ResourceStatus::getMemoryInfo()
224244
return mi;
225245
}
226246

227-
void ResourceStatus::getGPUInfo(std::vector<am::GpuInfo> &gpu_infos)
247+
// Function to read the content of a file
248+
std::string ResourceStatus::readFile(const std::string& path)
228249
{
229-
gpu_infos.clear();
250+
std::ifstream file(path);
251+
if (!file.is_open()) {
252+
throw std::runtime_error("Error: Unable to open file " + path);
253+
}
254+
255+
std::string content;
256+
std::getline(file, content);
257+
file.close();
258+
return content;
259+
}
260+
261+
am::GpuInfo ResourceStatus::getGPUInfo()
262+
{
263+
am::GpuInfo gpu_info;
264+
265+
// "/sys/devices/gpu.0/load" exists only in Jetpack
266+
//in contrast, nvidia-smi only exists in amd64 architure
267+
const std::string loadPath = "/sys/devices/gpu.0/load";
268+
if (boost::filesystem::exists(loadPath))
269+
{
270+
std::string loadStr = readFile(loadPath);
271+
gpu_info.load_percent = std::stoi(loadStr) / 1000; // Convert to percentage
272+
273+
const std::string baseThermalPath = "/sys/class/thermal/";
274+
const std::string typeSuffix = "/type";
275+
const std::string tempSuffix = "/temp";
276+
277+
for (int i = 0; i < 10; ++i) { // Check up to 10 thermal zones
278+
try {
279+
std::string typePath = baseThermalPath + "thermal_zone" + std::to_string(i) + typeSuffix;
280+
std::string type = readFile(typePath);
281+
if (type.find("GPU") != std::string::npos)
282+
{ // Look for the GPU thermal zone
283+
std::string tempPath = baseThermalPath + "thermal_zone" + std::to_string(i) + tempSuffix;
284+
std::string tempStr = readFile(tempPath);
285+
gpu_info.temp = std::stoi(tempStr) / 1000; // Convert millidegrees to degrees Celsius
286+
}
287+
} catch (...) {
288+
// Ignore errors and continue checking other zones
289+
}
290+
}
291+
throw std::runtime_error("Error: GPU thermal zone not found.");
292+
293+
return gpu_info;
294+
}
295+
296+
230297
// Execute the nvidia-smi command and read the output directly
231298
const std::string command = "nvidia-smi --query-gpu=name,utilization.gpu,temperature.gpu,memory.used,memory.free --format=csv,nounits,noheader";
232299
FILE* pipe = popen(command.c_str(), "r");
233300
if (!pipe)
234301
{
235302
ROS_ERROR("Error: Unable to execute nvidia-smi. Ensure it's installed and available in PATH.");
236-
return;
303+
return gpu_info;
237304
}
238305

239306
char buffer[128];
240307
std::ostringstream result;
241-
while (fgets(buffer, sizeof(buffer), pipe) != nullptr) {
308+
while (fgets(buffer, sizeof(buffer), pipe) != nullptr)
309+
{
242310
result << buffer;
243311
}
244312
pclose(pipe);
@@ -248,10 +316,8 @@ void ResourceStatus::getGPUInfo(std::vector<am::GpuInfo> &gpu_infos)
248316
std::string line;
249317
while (std::getline(iss, line))
250318
{
251-
252319
//ROS_INFO(GREEN "%s" COLOR_RESET, line.c_str());
253320
std::istringstream lineStream(line);
254-
am::GpuInfo gpu_info;
255321
// Parse memory used and free values
256322
std::string gpuName;
257323
int gpuUtilization, gpuTemperature, memoryUsed, memoryFree;
@@ -269,14 +335,15 @@ void ResourceStatus::getGPUInfo(std::vector<am::GpuInfo> &gpu_infos)
269335
lineStream >> memoryFree;
270336

271337
gpu_info.gpu_name = gpuName;
272-
gpu_info.util_percent = gpuUtilization;
338+
gpu_info.load_percent = gpuUtilization;
273339
gpu_info.temp = gpuTemperature;
274-
gpu_info.mem_free = memoryFree;
275-
gpu_info.mem_used = memoryUsed;
276-
stats_->gpu_stats = (gpu_info.util_percent>90?100:50);
277-
278-
gpu_infos.push_back(gpu_info);
340+
341+
return gpu_info;
279342
}
343+
344+
345+
346+
return gpu_info;
280347
}
281348

282349

@@ -385,10 +452,7 @@ void ResourceStatus::print()
385452
ROS_INFO("UpTime: %f", uptime_seconds_);
386453

387454
msg = "";
388-
for(int i = 0; i < gpu_infos_.size(); i++)
389-
{
390-
msg += gpu_infos_[i].gpu_name + ": Temp[C] = " + std::to_string(gpu_infos_[i].temp) + ", Used[%]: " + std::to_string(gpu_infos_[i].util_percent);
391-
}
455+
msg += "GPU: Temp[C] = " + std::to_string(gpu_info_.temp) + ", Used[%]: " + std::to_string(gpu_info_.load_percent);
392456

393457
ROS_INFO("%s", msg.c_str());
394458
}
@@ -567,6 +631,25 @@ std::vector<std::string> ResourceStatus::getInetAddresses()
567631
return inetAddresses;
568632
}
569633

634+
635+
// Function to get disk usage information
636+
DiskInfo ResourceStatus::getDiskInfo(const std::string& path)
637+
{
638+
struct statvfs stat;
639+
640+
if (statvfs(path.c_str(), &stat) != 0) {
641+
throw std::runtime_error("Error: Unable to get disk information for " + path);
642+
}
643+
644+
DiskInfo info;
645+
info.totalSpace = stat.f_blocks * stat.f_frsize; // Total blocks * block size
646+
info.availableSpace = stat.f_bavail * stat.f_frsize; // Available blocks * block size
647+
info.usedSpace = info.totalSpace - info.availableSpace;
648+
info.percentUsed = (info.usedSpace * 100.0) / info.totalSpace;
649+
650+
return info;
651+
}
652+
570653
/*
571654
Timer Callback: this is where everything is updated
572655
*/

0 commit comments

Comments
 (0)