Skip to content

Commit

Permalink
Simplify the average calculation
Browse files Browse the repository at this point in the history
Signed-off-by: Carles Pey <[email protected]>
  • Loading branch information
cpey committed Nov 24, 2024
1 parent 37996ad commit 80247ca
Showing 1 changed file with 4 additions and 17 deletions.
21 changes: 4 additions & 17 deletions chipsec/modules/tools/smm/smm_ptr.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,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, code, data, gprs, confirmed=False):
self.code = code
outlier = self.is_outlier(duration)
if not outlier:
self.acc_smi_duration += duration
self.acc_smi_num += 1
self.update_stdev(duration)
if duration > self.max.duration:
self.max.update(duration, code, data, gprs.copy())
Expand All @@ -284,22 +280,16 @@ def add(self, duration, 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

#
# Computes the standard deviation using the Welford's online algorithm
#
def update_stdev(self, value):
self.avg_smi_num += 1
self.hist_smi_num += 1
difference = value - self.avg_smi_duration
difference_hist = value - self.hist_smi_duration
self.avg()
self.avg_smi_duration += difference / self.avg_smi_num
self.hist_smi_duration += difference_hist / self.hist_smi_num
self.m2 += difference * (value - self.avg_smi_duration)
self.m2_hist += difference_hist * (value - self.hist_smi_duration)
variance = self.m2 / self.avg_smi_num
Expand All @@ -310,8 +300,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 @@ -353,7 +341,6 @@ def get_total_outliers(self):
return self.outliers_hist

def get_info(self):
self.avg()
avg = self.avg_smi_duration or self.hist_smi_duration
info = f'average {round(avg)} stddev {self.stdev:.2f} checked {self.avg_smi_num + self.outliers}'
if self.outliers:
Expand Down

0 comments on commit 80247ca

Please sign in to comment.