Skip to content

Commit 6866e30

Browse files
authored
Merge pull request #48 from microsoft/mac_ioreg_test
Mac ioreg test
2 parents bc9cd42 + 61e43a4 commit 6866e30

5 files changed

Lines changed: 26 additions & 6 deletions

File tree

core/app_scenario.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,7 @@ def monitorLife(self):
781781
logging.info("Life monitoring thread started")
782782
# Poll DUT to see if it's still responsive, if not, raise timeout exception
783783
file_path = self.result_dir + os.sep + 'battery_level.txt'
784+
csv_path = self.result_dir + os.sep + 'battery_level.csv'
784785
while(True):
785786
try:
786787
if self.platform.lower() == 'windows':
@@ -795,13 +796,26 @@ def monitorLife(self):
795796
# logging.info("RTC Wake timer reset")
796797
time.sleep(900)
797798
elif self.platform.lower() == 'macos':
798-
result = self._call(["pmset", "-g batt"], blocking=True)
799-
level = result.split("\n")[1].split("\t")[1].split("%")[0]
799+
# Get AppleRawCurrentCapacity level via ioreg
800+
raw_current_capacity_result = self._call(["bash", '-c "ioreg -r -c AppleSmartBattery -a | plutil -extract 0.AppleRawCurrentCapacity raw -"'], blocking=True)
801+
raw_current_capacity_level = raw_current_capacity_result.strip()
802+
803+
# Get AppleRawMaxCapacity level via ioreg
804+
raw_max_capacity_result = self._call(["bash", '-c "ioreg -r -c AppleSmartBattery -a | plutil -extract 0.AppleRawMaxCapacity raw -"'], blocking=True)
805+
raw_max_capacity_level = raw_max_capacity_result.strip()
806+
807+
# Calculate Battery level to 2 decimal places
808+
level = round(float(raw_current_capacity_level) / float(raw_max_capacity_level) * 100, 2)
809+
800810
current_time = datetime.now()
801811
time_s = current_time.strftime("%m/%d/%Y %I:%M:%S %p")
802-
logging.info(f"Battery level: {level}")
812+
logging.info(f"Battery level : {str(level)}")
813+
814+
# Write human-readable txt log
803815
with open(file_path, 'a', newline='') as f:
804-
f.write(f"{time_s}: total battery: {level}\n")
816+
f.write(f"{time_s}: total battery: {str(level)}\n")
817+
818+
805819
if int(level) <= int(self.stop_soc):
806820
break
807821
time.sleep(int(self.poll_rate))

utilities/open_source/config_check.sh

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,10 @@ if [ "$PreRun" = true ]; then
115115
# add_to_table "Scenario" ""
116116
add_to_table "Run Start Time" "$(date +'%Y-%m-%d %H:%M:%S')"
117117
# Add current battery level without the percent sign
118-
battery_level=$(pmset -g batt | grep -o '[0-9]\+%' | head -n 1 | tr -d '%')
118+
# battery_level=$(pmset -g batt | grep -o '[0-9]\+%' | head -n 1 | tr -d '%')
119+
raw_current_capacity_level=$(ioreg -r -c AppleSmartBattery -a | plutil -extract 0.AppleRawCurrentCapacity raw -)
120+
raw_max_capacity_level=$(ioreg -r -c AppleSmartBattery -a | plutil -extract 0.AppleRawMaxCapacity raw -)
121+
battery_level=$(printf "%.2f" $(echo "$raw_current_capacity_level / $raw_max_capacity_level * 100" | bc -l))
119122
add_to_table "Run Start Battery State (%)" "$battery_level"
120123
battery_status=$(pmset -g batt | grep -o 'discharging\|charging\|charged' | head -n 1)
121124
add_to_table "Run Start Charge State" "$battery_status"
@@ -144,7 +147,10 @@ elif [ "$PostRun" = true ]; then
144147
run_stop_time=$(date +'%Y-%m-%d %H:%M:%S')
145148
add_to_table "Run Stop Time" "$run_stop_time"
146149
# Add current battery level without the percent sign
147-
battery_level=$(pmset -g batt | grep -o '[0-9]\+%' | head -n 1 | tr -d '%')
150+
# battery_level=$(pmset -g batt | grep -o '[0-9]\+%' | head -n 1 | tr -d '%')
151+
raw_current_capacity_level=$(ioreg -r -c AppleSmartBattery -a | plutil -extract 0.AppleRawCurrentCapacity raw -)
152+
raw_max_capacity_level=$(ioreg -r -c AppleSmartBattery -a | plutil -extract 0.AppleRawMaxCapacity raw -)
153+
battery_level=$(printf "%.2f" $(echo "$raw_current_capacity_level / $raw_max_capacity_level * 100" | bc -l))
148154
add_to_table "Run Stop Battery State (%)" "$battery_level"
149155
battery_status=$(pmset -g batt | grep -o 'discharging\|charging\|charged' | head -n 1)
150156
add_to_table "Run Stop Charge State" "$battery_status"
2 KB
Binary file not shown.
0 Bytes
Binary file not shown.
360 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)