Skip to content

Commit

Permalink
Simplyfing the average calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
cpey committed Nov 23, 2024
1 parent 7e5da09 commit c9ca57e
Showing 1 changed file with 10 additions and 22 deletions.
32 changes: 10 additions & 22 deletions chipsec/modules/tools/smm/smm_ptr.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,6 @@ def clear(self):
self.max = smi_info(0)
self.min = smi_info(2**32-1)
self.outlier = smi_info(0)
self.acc_smi_duration = 0
self.acc_smi_num = 0
self.avg_smi_duration = 0
self.avg_smi_num = 0
self.outliers = 0
Expand All @@ -271,8 +269,6 @@ def add(self, duration, time, code, data, gprs, confirmed=False):
outlier = self.is_outlier(duration)
self.records['deltas'].append(duration)
self.records['times'].append(time)
self.acc_smi_duration += duration
self.acc_smi_num += 1
self.update_stdev(duration)
if not outlier:
if duration > self.max.duration:
Expand All @@ -285,21 +281,15 @@ def add(self, duration, time, code, data, gprs, confirmed=False):
self.outlier.update(duration, code, data, gprs.copy())
self.confirmed = confirmed

def avg(self):
if self.avg_smi_num or self.acc_smi_num:
self.avg_smi_duration = ((self.avg_smi_duration * self.avg_smi_num) + self.acc_smi_duration) / (self.avg_smi_num + self.acc_smi_num)
self.avg_smi_num += self.acc_smi_num
self.hist_smi_duration = ((self.hist_smi_duration * self.hist_smi_num) + self.acc_smi_duration) / (self.hist_smi_num + self.acc_smi_num)
self.hist_smi_num += self.acc_smi_num
self.acc_smi_duration = 0
self.acc_smi_num = 0

def update_stdev(self, value):
difference = value - self.avg_smi_duration
difference_hist = value - self.hist_smi_duration
self.avg()
self.m2 += difference * (value - self.avg_smi_duration)
self.m2_hist += difference_hist * (value - self.hist_smi_duration)
def update_stdev(self, duration):
self.avg_smi_num += 1
self.hist_smi_num += 1
difference = duration - self.avg_smi_duration
difference_hist = duration - self.hist_smi_duration
self.avg_smi_duration += difference / self.avg_smi_num
self.hist_smi_duration += difference_hist / self.hist_smi_num
self.m2 += difference * (duration - self.avg_smi_duration)
self.m2_hist += difference_hist * (duration - self.hist_smi_duration)
variance = self.m2 / self.avg_smi_num
variance_hist = self.m2_hist / self.hist_smi_num
self.stdev = math.sqrt(variance)
Expand All @@ -308,8 +298,6 @@ def update_stdev(self, value):
def update_calibration(self, duration):
if not self.needs_calibration:
return
self.acc_smi_duration += duration
self.acc_smi_num += 1
self.update_stdev(duration)
self.calib_samples += 1
if self.calib_samples >= SCAN_CALIB_SAMPLES:
Expand Down Expand Up @@ -354,7 +342,7 @@ def get_total_outliers(self):

def get_info(self):
avg = self.avg_smi_duration or self.hist_smi_duration
info = f"average {round(avg)} checked {self.avg_smi_num + self.outliers}"
info = f"average {round(avg)} stdev {self.stdev} checked {self.avg_smi_num + self.outliers}"
if self.outliers:
info += f"\n Identified outlier: {self.outlier.get_info()}"
info += f"\nDeltas: {self.records}"
Expand Down

0 comments on commit c9ca57e

Please sign in to comment.