Skip to content

Commit

Permalink
Fixed leak and blocked VMs
Browse files Browse the repository at this point in the history
  • Loading branch information
dehydratedpotato committed Jan 16, 2023
1 parent 4bf4f8c commit d8b8e5e
Show file tree
Hide file tree
Showing 3 changed files with 255 additions and 154 deletions.
248 changes: 172 additions & 76 deletions SocPowerBuddy/sampler.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,32 @@ void sample(iorep_data* iorep, // holds our channel subs from the iorep
variating_data* vd, // for get/setting variating data (i.e. residencies from the iorep)
cmd_data* cmd) // command line arg data (i.e. intervals)
{
CFDictionaryRef cpu_delta = NULL;
CFDictionaryRef pwr_delta = NULL;
CFDictionaryRef clpc_delta = NULL;
CFDictionaryRef cpusamp_a = NULL;
CFDictionaryRef cpusamp_b = NULL;
CFDictionaryRef pwrsamp_a = NULL;
CFDictionaryRef pwrsamp_b = NULL;
CFDictionaryRef clpcsamp_a = NULL;
CFDictionaryRef clpcsamp_b = NULL;

NSString* ptype_state = @"P";
NSString* vtype_state = @"V";
NSString* idletype_state = @"IDLE";
NSString* offtype_state = @"OFF";

cpusamp_a = IOReportCreateSamples(iorep->cpusub, iorep->cpusubchn, NULL);
pwrsamp_a = IOReportCreateSamples(iorep->pwrsub, iorep->pwrsubchn, NULL);
clpcsamp_a = IOReportCreateSamples(iorep->clpcsub, iorep->clpcsubchn, NULL);
CFDictionaryRef cpusamp_a = IOReportCreateSamples(iorep->cpusub, iorep->cpusubchn, NULL);
CFDictionaryRef pwrsamp_a = IOReportCreateSamples(iorep->pwrsub, iorep->pwrsubchn, NULL);
CFDictionaryRef clpcsamp_a = IOReportCreateSamples(iorep->clpcsub, iorep->clpcsubchn, NULL);

[NSThread sleepForTimeInterval: (cmd->interval * 1e-3)];

if (cmd->interval > 0) [NSThread sleepForTimeInterval:(cmd->interval*1e-3)];
CFDictionaryRef cpusamp_b = IOReportCreateSamples(iorep->cpusub, iorep->cpusubchn, NULL);
CFDictionaryRef pwrsamp_b = IOReportCreateSamples(iorep->pwrsub, iorep->pwrsubchn, NULL);
CFDictionaryRef clpcsamp_b = IOReportCreateSamples(iorep->clpcsub, iorep->clpcsubchn, NULL);

cpusamp_b = IOReportCreateSamples(iorep->cpusub, iorep->cpusubchn, NULL);
pwrsamp_b = IOReportCreateSamples(iorep->pwrsub, iorep->pwrsubchn, NULL);
clpcsamp_b = IOReportCreateSamples(iorep->clpcsub, iorep->clpcsubchn, NULL);
// deltas
CFDictionaryRef cpu_delta = IOReportCreateSamplesDelta(cpusamp_a, cpusamp_b, NULL);
CFDictionaryRef pwr_delta = IOReportCreateSamplesDelta(pwrsamp_a, pwrsamp_b, NULL);
CFDictionaryRef clpc_delta = IOReportCreateSamplesDelta(clpcsamp_a, clpcsamp_b, NULL);

cpu_delta = IOReportCreateSamplesDelta(cpusamp_a, cpusamp_b, NULL);
pwr_delta = IOReportCreateSamplesDelta(pwrsamp_a, pwrsamp_b, NULL);
clpc_delta = IOReportCreateSamplesDelta(clpcsamp_a, clpcsamp_b, NULL);
CFRelease(cpusamp_a);
CFRelease(cpusamp_b);
CFRelease(pwrsamp_a);
CFRelease(pwrsamp_b);
CFRelease(clpcsamp_a);
CFRelease(clpcsamp_b);

IOReportIterate(cpu_delta, ^(IOReportSampleRef sample) {
for (int i = 0; i < IOReportStateGetCount(sample); i++) {
Expand All @@ -50,35 +48,79 @@ void sample(iorep_data* iorep, // holds our channel subs from the iorep
uint64_t residency = IOReportStateGetResidency(sample, i);

for (int ii = 0; ii < [sd->complex_freq_channels count]; ii++) {
if ([subgroup isEqual: @"CPU Complex Performance States"] || [subgroup isEqual: @"GPU Performance States"]) {
if ([chann_name isEqual:sd->complex_freq_channels[ii]]) {
if ([subgroup isEqual: @"CPU Complex Performance States"] || [subgroup isEqual: @"GPU Performance States"])
{
if ([chann_name isEqual:sd->complex_freq_channels[ii]])
{
if ([idx_name rangeOfString:ptype_state].location != NSNotFound ||
[idx_name rangeOfString:vtype_state].location != NSNotFound)
{
vd->cluster_sums[ii] = [NSNumber numberWithUnsignedLongLong:([vd->cluster_sums[ii] unsignedLongLongValue] + residency)];
[vd->cluster_residencies[ii] addObject:[NSNumber numberWithUnsignedLongLong:residency]];
NSNumber* a = [[NSNumber alloc] initWithUnsignedLongLong:([vd->cluster_sums[ii] unsignedLongLongValue] + residency)];
NSNumber* b = [[NSNumber alloc] initWithUnsignedLongLong:residency];

vd->cluster_sums[ii] = a;
[vd->cluster_residencies[ii] addObject:b];

a = nil;
b = nil;

} else if ([idx_name rangeOfString:idletype_state].location != NSNotFound ||
[idx_name rangeOfString:offtype_state].location != NSNotFound)
vd->cluster_use[ii] = [NSNumber numberWithUnsignedLongLong:residency];
{
NSNumber* a = [[NSNumber alloc] initWithUnsignedLongLong:residency];

vd->cluster_use[ii] = a;

a = nil;
}
}
} else if ([subgroup isEqual:@"CPU Core Performance States"]) {
if (ii <= ([sd->cluster_core_counts count]-1)) {
for (int iii = 0; iii < [sd->cluster_core_counts[ii] intValue]; iii++) {
if ([chann_name isEqual:[NSString stringWithFormat:@"%@%d",sd->core_freq_channels[ii], iii, nil]]) {
if ([idx_name rangeOfString:ptype_state].location != NSNotFound ||
[idx_name rangeOfString:vtype_state].location != NSNotFound)
{
vd->core_sums[ii][iii] = [NSNumber numberWithUnsignedLongLong:([vd->core_sums[ii][iii] unsignedLongLongValue] + residency)];
[vd->core_residencies[ii][iii] addObject:[NSNumber numberWithUnsignedLongLong:residency]];
} else if ([idx_name rangeOfString:idletype_state].location != NSNotFound ||
[idx_name rangeOfString:offtype_state].location != NSNotFound)
vd->core_use[ii][iii] = [NSNumber numberWithUnsignedLongLong:residency];
}
else if ([subgroup isEqual:@"CPU Core Performance States"])
{
if (ii <= ([sd->cluster_core_counts count]-1))
{
for (int iii = 0; iii < [sd->cluster_core_counts[ii] intValue]; iii++)
{
@autoreleasepool {
NSString* key = [NSString stringWithFormat:@"%@%d",sd->core_freq_channels[ii], iii, nil];

if ([chann_name isEqual:key]) {
if ([idx_name rangeOfString:ptype_state].location != NSNotFound ||
[idx_name rangeOfString:vtype_state].location != NSNotFound)
{
NSNumber* a = [[NSNumber alloc] initWithUnsignedLongLong:([vd->core_sums[ii][iii] unsignedLongLongValue] + residency)];
NSNumber* b = [[NSNumber alloc] initWithUnsignedLongLong:residency];

vd->core_sums[ii][iii] = a;
[vd->core_residencies[ii][iii] addObject:b];

a = nil;
b = nil;

} else if ([idx_name rangeOfString:idletype_state].location != NSNotFound ||
[idx_name rangeOfString:offtype_state].location != NSNotFound)
{
NSNumber* a = [[NSNumber alloc] initWithUnsignedLongLong:residency];

vd->core_use[ii][iii] = a;

a = nil;

}
}

key = nil;
}
}
}
}
}

chann_name = nil;
subgroup = nil;
idx_name = nil;
}

return kIOReportIterOk;
});

Expand All @@ -88,14 +130,37 @@ void sample(iorep_data* iorep, // holds our channel subs from the iorep
long value = IOReportSimpleGetIntegerValue(sample, 0);

for (int ii = 0; ii < [sd->complex_freq_channels count]; ii++) {
if ([group isEqual:@"Energy Model"]) {
if ([group isEqual:@"Energy Model"])
{
if ([chann_name isEqual:sd->complex_pwr_channels[ii]])
vd->cluster_pwrs[ii] = [NSNumber numberWithFloat:(float)value/(cmd->interval/1e+3)];
{
NSNumber* a = [[NSNumber alloc] initWithFloat:(float)value/(cmd->interval*1e-3)];

vd->cluster_pwrs[ii] = a;

a = nil;
}

if (ii <= ([sd->cluster_core_counts count]-1)) {
for (int iii = 0; iii < [sd->cluster_core_counts[ii] intValue]; iii++) {
if ([chann_name isEqual:[NSString stringWithFormat:@"%@%d",sd->core_pwr_channels[ii], iii, nil]])
vd->core_pwrs[ii][iii] = [NSNumber numberWithFloat:(float)value/(cmd->interval/1e+3)];
if (ii <= ([sd->cluster_core_counts count]-1))
{
for (int iii = 0; iii < [sd->cluster_core_counts[ii] intValue]; iii++)
{
@autoreleasepool
{
NSString* val = [[NSString alloc] initWithFormat:@"%@%d",sd->core_pwr_channels[ii], iii, nil];

if ([chann_name isEqual:val])
{
NSNumber* a = [[NSNumber alloc] initWithFloat:(float)value/(cmd->interval*1e-3)];

vd->core_pwrs[ii][iii] = a;

a = nil;
}

val = nil;

}
}
}
}
Expand All @@ -104,14 +169,23 @@ void sample(iorep_data* iorep, // holds our channel subs from the iorep
* the GPU entry doen't seem to exist on Apple M1 Energy Model (probably same with M2)
* so we are grabbing that from the PMP
*/
if ([sd->extra[0] isEqual:@"Apple M1"] || [sd->extra[0] isEqual:@"Apple M2"]) {
if ([sd->extra[0] isEqual:@"Apple M1"] || [sd->extra[0] isEqual:@"Apple M2"]){
if ([group isEqual:@"PMP"]) {
if ([chann_name isEqual:@"GPU"])
vd->cluster_pwrs[[vd->cluster_pwrs count]-1] = [NSNumber numberWithFloat:(float)value/(cmd->interval/1e+3)];
{
NSNumber* a = [[NSNumber alloc] initWithFloat:(float)value/(cmd->interval*1e-3)];

vd->cluster_pwrs[[vd->cluster_pwrs count]-1] = a;

a = nil;
}
}
}
}

chann_name = nil;
group = nil;

return kIOReportIterOk;
});

Expand All @@ -121,28 +195,27 @@ void sample(iorep_data* iorep, // holds our channel subs from the iorep
for (int i = 0; i < [sd->cluster_core_counts count]; i++) {
long value = IOReportArrayGetValueAtIndex(sample, i);

NSNumber* a = [[NSNumber alloc] initWithLong:value];

if ([chann_name isEqual:@"CPU cycles, by cluster"])
[vd->cluster_instrcts_clk addObject:[NSNumber numberWithLong:value]];
[vd->cluster_instrcts_clk addObject:a];
if ([chann_name isEqual:@"CPU instructions, by cluster"])
[vd->cluster_instrcts_ret addObject:[NSNumber numberWithLong:value]];
[vd->cluster_instrcts_ret addObject:a];

a = nil;
}

if ([vd->cluster_instrcts_ret count] == [sd->cluster_core_counts count])
return kIOReportIterFailed; // probably not correct, but allows stopping the loop once the data is here

chann_name = nil;

return kIOReportIterOk;
});

CFRelease(cpu_delta);
CFRelease(pwr_delta);
CFRelease(clpc_delta);

CFRelease(cpusamp_a);
CFRelease(cpusamp_b);
CFRelease(pwrsamp_a);
CFRelease(pwrsamp_b);
CFRelease(clpcsamp_a);
CFRelease(clpcsamp_b);
}

void format(static_data* sd, variating_data* vd)
Expand All @@ -152,41 +225,64 @@ void format(static_data* sd, variating_data* vd)

for (int i = 0; i < [sd->complex_freq_channels count]; i++) {
/* formatting cpu freqs */
for (int ii = 0; ii < [vd->cluster_residencies[i] count]; ii++) {
res = [vd->cluster_residencies[i][ii] unsignedLongLongValue];
if (res != 0) {
float perc = (res / [vd->cluster_sums[i] floatValue]);
NSNumber* res = [[NSNumber alloc] initWithFloat: perc];
NSNumber* freq = [[NSNumber alloc] initWithFloat: ([vd->cluster_freqs[i] floatValue] + ([sd->dvfm_states[i][ii] floatValue]*perc))];
for (int ii = 0; ii < [vd->cluster_residencies[i] count]; ii++)
{
@autoreleasepool
{
res = [vd->cluster_residencies[i][ii] unsignedLongLongValue];

vd->cluster_freqs[i] = freq;
[vd->cluster_residencies[i] replaceObjectAtIndex:ii withObject:res];
}

if (i <= ([sd->cluster_core_counts count]-1)) {
for (int iii = 0; iii < [sd->cluster_core_counts[i] intValue]; iii++) {
core_res = [vd->core_residencies[i][iii][ii] unsignedLongLongValue];
if (core_res != 0) {
float core_perc = (core_res / [vd->core_sums[i][iii] floatValue]);
NSNumber* res = [[NSNumber alloc] initWithFloat:core_perc];
NSNumber* freq = [[NSNumber alloc] initWithFloat:([vd->core_freqs[i][iii] floatValue] + ([sd->dvfm_states[i][ii] floatValue]*core_perc))];
if (res != 0)
{
float perc = (res / [vd->cluster_sums[i] floatValue]);

NSNumber* a = [[NSNumber alloc] initWithFloat: perc];
NSNumber* b = [[NSNumber alloc] initWithFloat: ([vd->cluster_freqs[i] floatValue] + ([sd->dvfm_states[i][ii] floatValue]*perc))];

vd->cluster_freqs[i] = b;
[vd->cluster_residencies[i] replaceObjectAtIndex:ii withObject:a];

a = nil;
b = nil;
}

if (i <= ([sd->cluster_core_counts count]-1)) {
for (int iii = 0; iii < [sd->cluster_core_counts[i] intValue]; iii++) {

core_res = [vd->core_residencies[i][iii][ii] unsignedLongLongValue];

vd->core_freqs[i][iii] = freq;
[vd->core_residencies[i][iii] replaceObjectAtIndex:ii withObject:res];
if (core_res != 0)
{
float core_perc = (core_res / [vd->core_sums[i][iii] floatValue]);

NSNumber* a = [[NSNumber alloc] initWithFloat:core_perc];
NSNumber* b = [[NSNumber alloc] initWithFloat: ([vd->core_freqs[i][iii] floatValue] +
([sd->dvfm_states[i][ii] floatValue] *
core_perc))];

vd->core_freqs[i][iii] = b;
[vd->core_residencies[i][iii] replaceObjectAtIndex:ii withObject:a];

a = nil;
b = nil;
}
}
}
}
}

/* formatting idle residency */
vd->cluster_use[i] = [NSNumber numberWithFloat:(([vd->cluster_use[i] floatValue]/([vd->cluster_sums[i] floatValue]+[vd->cluster_use[i] floatValue]))*100)];
vd->cluster_use[i] = [[NSNumber alloc] initWithFloat:(([vd->cluster_use[i] floatValue] /
([vd->cluster_sums[i] floatValue] +
[vd->cluster_use[i] floatValue])) * 100)];

if (i <= ([sd->cluster_core_counts count]-1)) {
for (int iii = 0; iii < [sd->cluster_core_counts[i] intValue]; iii++) {
NSNumber* numb = [[NSNumber alloc] initWithFloat:(100 - ([vd->core_use[i][iii] floatValue] /
NSNumber* a = [[NSNumber alloc] initWithFloat:(100 - ([vd->core_use[i][iii] floatValue] /
([vd->core_sums[i][iii] floatValue] +
[vd->core_use[i][iii] floatValue])) * 100)];
vd->core_use[i][iii] = numb;
vd->core_use[i][iii] = a;

a = nil;
}
}
}
Expand Down
Loading

0 comments on commit d8b8e5e

Please sign in to comment.