Fixed leaks in Mem and attempt at fixing leaks in sensors.cpp

This commit is contained in:
aristocratos 2021-10-16 01:59:44 +02:00
parent fbae907720
commit 70b4871062
3 changed files with 19 additions and 10 deletions

View file

@ -236,7 +236,7 @@ namespace Cpu {
void update_sensors() {
current_cpu.temp_max = 95; // we have no idea how to get the critical temp
ThermalSensors sensors;
std::map<int, double> sensor = sensors.getSensors();
auto sensor = sensors.getSensors();
try {
current_cpu.temp.at(0).push_back((long long)sensor[0]);
@ -512,6 +512,7 @@ namespace Mem {
if (bsdNameRef) {
char buf[200];
CFStringGetCString(bsdNameRef, buf, 200, kCFStringEncodingASCII);
CFRelease(bsdNameRef);
return string(buf);
}
return "";
@ -520,12 +521,13 @@ namespace Mem {
bool isWhole(io_registry_entry_t volumeRef) {
CFBooleanRef isWhole = (CFBooleanRef)IORegistryEntryCreateCFProperty(volumeRef, CFSTR("Whole"), kCFAllocatorDefault, 0);
Boolean val = CFBooleanGetValue(isWhole);
CFRelease(isWhole);
return bool(val);
}
class IOObject {
public:
IOObject(string name, io_object_t obj) : name(name), object(obj) {}
IOObject(string name, io_object_t& obj) : name(name), object(obj) {}
virtual ~IOObject() { IOObjectRelease(object); }
private:
string name;
@ -569,7 +571,6 @@ namespace Mem {
if (properties) {
CFDictionaryRef statistics = (CFDictionaryRef)CFDictionaryGetValue(properties, CFSTR("Statistics"));
if (statistics) {
// Logger::debug("device:" + device + " = " + disk.name);
disk_ios++;
int64_t readBytes = getCFNumber(statistics, CFSTR("Bytes read from block device"));
if (disk.io_read.empty())
@ -578,7 +579,6 @@ namespace Mem {
disk.io_read.push_back(max((int64_t)0, (readBytes - disk.old_io.at(0))));
disk.old_io.at(0) = readBytes;
while (cmp_greater(disk.io_read.size(), width * 2)) disk.io_read.pop_front();
// Logger::debug("bytes read:" + std::to_string(readBytes) + " : " + std::to_string(disk.io_read.back()));
int64_t writeBytes = getCFNumber(statistics, CFSTR("Bytes written to block device"));
if (disk.io_write.empty())
@ -587,16 +587,16 @@ namespace Mem {
disk.io_write.push_back(max((int64_t)0, (writeBytes - disk.old_io.at(1))));
disk.old_io.at(1) = writeBytes;
while (cmp_greater(disk.io_write.size(), width * 2)) disk.io_write.pop_front();
// Logger::debug("bytes written:" + std::to_string(writeBytes) + " : " + std::to_string(disk.io_write.back()));
// IOKit does not give us IO times, (use IO read + IO write with 1 MiB being 100% to get some activity indication)
if (disk.io_activity.empty())
disk.io_activity.push_back(0);
else
disk.io_activity.push_back(clamp((long)round((double)(disk.io_write.back() + disk.io_read.back()) / (1 << 20)), 0l, 100l));
while (cmp_greater(disk.io_activity.size(), width * 2)) disk.io_activity.pop_front();
// Logger::debug("io activity:" + std::to_string(disk.io_activity.back()) + '%');
}
}
CFRelease(properties);
}
}
}

View file

@ -37,6 +37,7 @@ CFDictionaryRef matching(int page, int usage) {
nums[1] = CFNumberCreate(0, kCFNumberSInt32Type, &usage);
CFDictionaryRef dict = CFDictionaryCreate(0, (const void **)keys, (const void **)nums, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFRelease(keys);
return dict;
}
@ -57,6 +58,7 @@ CFArrayRef getProductNames(CFDictionaryRef sensors) {
} else {
CFArrayAppendValue(array, CFSTR("noname")); // @ gives a Ref like in "CFStringRef name"
}
CFRelease(name);
}
return array;
}
@ -82,12 +84,14 @@ CFArrayRef getThermalValues(CFDictionaryRef sensors) {
value = CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &temp);
}
CFArrayAppendValue(array, value);
CFRelease(value);
}
CFRelease(system);
return array;
}
}
std::map<int, double> Cpu::ThermalSensors::getSensors() {
std::map<int, double> cpuValues;
unordered_flat_map<int, double> Cpu::ThermalSensors::getSensors() {
unordered_flat_map<int, double> cpuValues;
CFDictionaryRef thermalSensors = matching(0xff00, 5); // 65280_10 = FF00_16
// thermalSensors's PrimaryUsagePage should be 0xff00 for M1 chip, instead of 0xff05
// can be checked by ioreg -lfx
@ -118,6 +122,9 @@ std::map<int, double> Cpu::ThermalSensors::getSensors() {
cpuValues[0] = temp; // package T for Apple Silicon
}
}
CFRelease(thermalSensors);
CFRelease(thermalNames);
CFRelease(thermalValues);
return cpuValues;
}

View file

@ -1,8 +1,10 @@
#include <map>
#include <robin_hood.h>
using robin_hood::unordered_flat_map;
namespace Cpu {
class ThermalSensors {
public:
std::map<int, double> getSensors();
unordered_flat_map<int, double> getSensors();
};
} // namespace Cpu