-
Notifications
You must be signed in to change notification settings - Fork 1
/
EnergyDebugger.py
1047 lines (914 loc) · 38.4 KB
/
EnergyDebugger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
import Queue
import argparse
import csv
import multiprocessing
import os
import sys
import threading
import time
from PyQt5.QtCore import QSettings, QObject, pyqtSignal, QThread
from PyQt5.QtGui import QTextCursor, QPalette, QColor, QGuiApplication
from PyQt5.QtWidgets import *
import AboutDialog
import MainInterface
import SettingsDialog
from ADBInterface import ADBInterface
from GovernorControler import GovernorController
from PIDTool import PIDTool
from SysLoggerInterface import SysLogger
from SystemMetrics import SystemMetrics
from TraceCMDParser import TracecmdProcessor
from TraceProcessor import TraceProcessor
from Tracer import Tracer
__author__ = "Alex Hoffman"
__copyright__ = "Copyright 2019, Alex Hoffman"
__license__ = "GPL"
__version__ = "1.0"
__maintainer__ = "Alex Hoffman"
__email__ = "[email protected]"
__status__ = "Beta"
parser = argparse.ArgumentParser()
parser.add_argument(
"-c",
"--commandline",
required=False,
help="Enables the use of command line arguments",
)
parser.add_argument("-a",
"--app",
required=False,
help="Specifies the name of the game to be traced")
parser.add_argument("-d",
"--duration",
required=False,
type=float,
help="The duration to trace")
parser.add_argument("-e",
"--events",
required=False,
help="Events that are to be traced")
parser.add_argument("-s",
"--skip-clear",
action="store_true",
help="Skip clearing trace settings")
parser.add_argument(
"-g",
"--draw",
action="store_true",
help="Enables the drawing of the generated graph",
)
parser.add_argument(
"-te",
"--test",
action="store_true",
help="Tests only a few hundred events to speed up testing",
)
parser.add_argument(
"-sub",
"--subgraph",
action="store_true",
help="Enable the drawing of node subgraphs",
)
parser.add_argument(
"-p",
"--preamble",
required=False,
help=
"Specifies the number of seconds that be discarded at the begining of tracing",
)
args = parser.parse_args()
class AboutDialog(QDialog, AboutDialog.Ui_Dialog):
def __init__(self, parent=None):
super(QDialog, self).__init__(parent)
self.setupUi(self)
class SettingsMenu(QDialog, SettingsDialog.Ui_DialogSettings):
def __init__(self, settings, parent=None):
super(QDialog, self).__init__(parent)
self.setupUi(self)
self.settings = settings
# Application
self.lineEditApplicationName.setText(
settings.value("DefaultApplication", defaultValue=""))
self.doubleSpinBoxDuration.setValue(
float(settings.value("DefaultDuration", defaultValue=0.0)))
self.doubleSpinBoxPreamble.setValue(
float(settings.value("DefaultPreamble", defaultValue=0.0)))
self.checkBoxDrawGraph.setChecked(
bool(int(settings.value("DefaultDrawGraph", defaultValue=0))))
self.checkBoxWakeUp.setChecked(
bool(int(settings.value("DefaultEventWakeUp", defaultValue=0))))
self.checkBoxSchedSwitch.setChecked(
bool(int(settings.value("DefaultEventSchedSwitch",
defaultValue=1))))
self.checkBoxCPUIdle.setChecked(
bool(int(settings.value("DefaultEventCPUIdle", defaultValue=0))))
self.checkBoxBinderTransaction.setChecked(
bool(
int(
settings.value("DefaultEventBinderTransaction",
defaultValue=1))))
self.checkBoxSyslogger.setChecked(
bool(int(settings.value("DefaultEventSyslogger", defaultValue=1))))
self.radioButtonMultiThreaded.setChecked(
bool(int(settings.value("OptimizeWithThreads", defaultValue=1))))
self.radioButtonMultiProcessing.setChecked(
bool(int(settings.value("OptimizeWithProcesses", defaultValue=1))))
self.radioButtonNonthreaded.setChecked(
bool(int(settings.value("OptimizeNone", defaultValue=1))))
self.checkBoxUseUIConsole.setChecked(
bool(int(settings.value("UseUIConsole", defaultValue=1))))
# Syslogger
self.spinBoxCPU.setValue(
int(settings.value("DefaultSysloggerCPU", defaultValue=2)))
self.spinBoxInterval.setValue(
int(settings.value("DefaultSysloggerInterval", defaultValue=5)))
self.checkBoxCPUInfo.setChecked(
bool(int(settings.value("DefaultSysloggerCPUInfo",
defaultValue=1))))
self.checkBoxCPUFrequency.setChecked(
bool(int(settings.value("DefaultSysloggerCPUFreq",
defaultValue=1))))
self.checkBoxPower.setChecked(
bool(int(settings.value("DefaultSysloggerPower", defaultValue=1))))
self.checkBoxMali.setChecked(
bool(int(settings.value("DefaultSysloggerMali", defaultValue=1))))
self.checkBoxTemp.setChecked(
bool(int(settings.value("DefaultSysloggerTemp", defaultValue=1))))
self.checkBoxNetwork.setChecked(
bool(int(settings.value("DefaultSysloggerNetwork",
defaultValue=0))))
def sysloggerstart_clicked(self):
pass
def sysloggersetup_clicked(self):
pass
def sysloggerstop_clicked(self):
pass
def sysloggerfinish_clicked(self):
pass
def sysloggerpull_clicked(self):
if self.lineEditSyslogPullFilename.text():
pull_location = (self.lineEditSyslogPullFolder.text() + "/" +
self.lineEditSyslogPullFilename.text())
else:
pull_location = self.lineEditSyslogPullFolder.text() + "/trace.dat"
try:
adb = ADBInterface()
adb.pull_file("/data/local/tmp/trace.dat", pull_location)
except Exception, e:
QMessageBox.critical(
self, "Error",
"Pulling file via ADB failed\n\n Error: {}".format(e))
raise Exception(e)
self.close()
def sysloggerpullfile_clicked(self):
options = QFileDialog.Options()
dialog = QFileDialog(self)
dialog.setFileMode(QFileDialog.DirectoryOnly)
filename = dialog.getExistingDirectory(self, options=options)
self.lineEditSyslogPullFolder.setText(filename)
def sysloggerfiletoconvert_clicked(self):
options = QFileDialog.Options()
filename, _ = QFileDialog.getOpenFileName(
self,
"QFileDialog.getOpenFileName()",
"",
"All Files (*)",
options=options)
self.lineEditConvertSource.setText(filename)
def sysloggerfiletoconvertdestination_clicked(self):
options = QFileDialog.Options()
dialog = QFileDialog(self)
dialog.setFileMode(QFileDialog.DirectoryOnly)
filename = dialog.getExistingDirectory(self, options=options)
self.lineEditConvertDestination.setText(filename)
def sysloggerconverttrace_clicked(self):
error = False
error_str = "The following required parameters are missing:\n\n"
if not self.lineEditConvertSource.text():
error = True
error_str += "Application Name\n"
if self.lineEditConvertDestination.text():
error = True
error_str += "Duration"
if error:
QMessageBox.warning(self, "Error", error_str, QMessageBox.Ok)
if not os.path.isfile(self.lineEditConvertSource.text()):
QMessageBox.critical(self, "Error",
"The trace source file does not exist",
QMessageBox.Ok)
os.system("trace_conv.py -i " + self.lineEditConvertSource.text() +
" -o " + self.lineEditConvertDestination.text())
QMessageBox.information(self, "Trace convert",
"Converting trace complete", QMessageBox.Ok)
def accept(self):
# Application
self.settings.setValue("DefaultApplication",
self.lineEditApplicationName.text())
self.settings.setValue("DefaultDuration",
self.doubleSpinBoxDuration.value())
self.settings.setValue("DefaultPreamble",
self.doubleSpinBoxPreamble.value())
self.settings.setValue("DefaultDrawGraph",
int(self.checkBoxDrawGraph.isChecked()))
self.settings.setValue("DefaultEventSchedSwitch",
int(self.checkBoxSchedSwitch.isChecked()))
self.settings.setValue("DefaultEventCPUIdle",
int(self.checkBoxCPUIdle.isChecked()))
self.settings.setValue(
"DefaultEventBinderTransaction",
int(self.checkBoxBinderTransaction.isChecked()),
)
self.settings.setValue("DefaultEventSyslogger",
int(self.checkBoxSyslogger.isChecked()))
self.settings.setValue("DefaultEventWakeUp",
int(self.checkBoxWakeUp.isChecked()))
self.settings.setValue("OptimizeWithThreads",
int(self.radioButtonMultiThreaded.isChecked()))
self.settings.setValue(
"OptimizeWithProcesses",
int(self.radioButtonMultiProcessing.isChecked()))
self.settings.setValue("OptimizeNone",
int(self.radioButtonNonthreaded.isChecked()))
self.settings.setValue("UseUIConsole",
int(self.checkBoxUseUIConsole.isChecked()))
# Syslogger
self.settings.setValue("DefaultSysloggerCPU", self.spinBoxCPU.value())
self.settings.setValue("DefaultSysloggerInterval",
self.spinBoxInterval.value())
self.settings.setValue("DefaultSysloggerCPUInfo",
int(self.checkBoxCPUInfo.isChecked()))
self.settings.setValue("DefaultSysloggerCPUFreq",
int(self.checkBoxCPUFrequency.isChecked()))
self.settings.setValue("DefaultSysloggerPower",
int(self.checkBoxPower.isChecked()))
self.settings.setValue("DefaultSysloggerMali",
int(self.checkBoxMali.isChecked()))
self.settings.setValue("DefaultSysloggerTemp",
int(self.checkBoxTemp.isChecked()))
self.settings.setValue("DefaultSysloggerNetwork",
int(self.checkBoxNetwork.isChecked()))
super(SettingsMenu, self).accept()
def reject(self):
super(SettingsMenu, self).reject()
class EmittingStream(QObject):
textWritten = pyqtSignal(str)
def write(self, text):
self.textWritten.emit(str(text))
def flush(self):
sys.stdout.flush()
sys.stderr.flush()
class MainInterface(QMainWindow, MainInterface.Ui_MainWindow):
results_path = ""
changed_progress = pyqtSignal(int)
changed_test_count = pyqtSignal(str)
changed_test_progress = pyqtSignal(int)
def __init__(self, parent=None):
super(QMainWindow, self).__init__(parent)
self.setupUi(self)
self.show()
self.buttonkilladb()
self.setupbuttons()
self.setupmenu()
self.settings = None
self.setupsettings()
self.getsettings()
console_colour = QPalette(QColor(255, 255, 255, 255),
QColor(0, 0, 0, 255))
self.textEditConsole.setPalette(console_colour)
self.UI_console = bool(
int(self.settings.value("UseUIConsole", defaultValue=1)))
if self.UI_console:
sys.stdout = EmittingStream(textWritten=self.normal_output_written)
sys.stderr = EmittingStream(textWritten=self.normal_output_written)
else:
self.textEditConsole.setText(
"Standard console being used, this console can be enabled in the settings "
"menu")
# Persistant settings
self.checkBoxSkipTracing.setChecked(
bool(int(self.settings.value("SkipTracing", defaultValue=0))))
self.checkBoxSkipTracing.stateChanged.connect(
self.save_persistent_settings)
# Threading
self.jobs = []
self.console_queue = Queue.Queue()
self.console_task = threading.Thread(target=self.console,
args=()).start()
self.debug_task = None
self.changed_progress.connect(self.progressBar.setValue)
self.changed_test_count.connect(self.labelTestsCompleted.setText)
self.changed_test_progress.connect(
self.progressBarTestsCompleted.setValue)
self.results_path = os.path.join(
os.path.dirname(os.path.realpath(__file__)), "results/")
self.trace_file = None
self.results_file = None
self.graph_file = None
self.binder_log_file = None
self.binder_log = []
self.application_name = None
self.events = []
self.duration = 0 # TODO defaults
self.events_to_process = 0
self.preamble = 2
self.graph = False
self.subgraph = False
self.skip_tracing = False
self.pid = None
# Governor control
self.governorRadioButtons = []
# try:
# self.adb = ADBInterface()
# except Exception, e:
# print("Could not open an ADB connection to device")
# self.close()
# sys.exit()
self.governor_controller = GovernorController()
self.governor = self.setup_governors()
def __del__(self):
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
for job in self.jobs:
job.terminate()
self.console_task._stop_event.set()
def save_persistent_settings(self):
self.settings.setValue("SkipTracing",
int(self.checkBoxSkipTracing.isChecked()))
def setup_governors(self):
governors = self.governor_controller.get_governors()
current_governor = self.governor_controller.get_current_governor()
self.labelCurrentLittleGovernor.setText(str(current_governor[0]))
self.labelCurrentBigGovernor.setText(str(current_governor[1]))
for governor in governors:
rb = QRadioButton(governor)
self.governorRadioButtons.append(rb)
if governor == current_governor[0]:
rb.setChecked(True)
else:
rb.setChecked(False)
rb.clicked.connect(lambda: self.governor_changed())
self.verticalLayoutGovernors.addWidget(rb)
self.update_frequency_display()
self.pushButtonSetLittleFreqs.clicked.connect(
self.set_little_frequencies)
self.pushButtonResetLittleFreqs.clicked.connect(
self.reset_little_frequencies)
self.pushButtonSetBigFreqs.clicked.connect(self.set_big_frequencies)
self.pushButtonResetBigFreqs.clicked.connect(
self.reset_big_frequencies)
return str(current_governor[0])
def update_frequency_display(self):
little_min = str(self.governor_controller.get_min_freq(0))
little_max = str(self.governor_controller.get_max_freq(0))
self.labelLittleMinFreq.setText(little_min)
self.labelLittleMaxFreq.setText(little_max)
self.lineEditLittleMinFreq.setText(little_min)
self.lineEditLittleMaxFreq.setText(little_max)
big_min = str(self.governor_controller.get_min_freq(4))
big_max = str(self.governor_controller.get_max_freq(4))
self.labelBigMinFreq.setText(big_min)
self.labelBigMaxFreq.setText(big_max)
self.lineEditBigMinFreq.setText(big_min)
self.lineEditBigMaxFreq.setText(big_max)
def set_little_frequencies(self):
self.governor_controller.set_min_freq(
0, self.lineEditLittleMinFreq.text())
self.governor_controller.set_max_freq(
0, self.lineEditLittleMaxFreq.text())
self.update_frequency_display()
def set_big_frequencies(self):
self.governor_controller.set_min_freq(4,
self.lineEditBigMinFreq.text())
self.governor_controller.set_max_freq(4,
self.lineEditBigMaxFreq.text())
self.update_frequency_display()
def reset_little_frequencies(self):
self.governor_controller.reset_cpu_frequencies(0)
self.update_frequency_display()
def reset_big_frequencies(self):
self.governor_controller.reset_cpu_frequencies(4)
self.update_frequency_display()
def governor_changed(self):
new_governor = ""
for rb in self.governorRadioButtons:
if rb.isChecked():
new_governor = rb.text()
break
self.governor_controller.set_governor(new_governor)
self.labelCurrentLittleGovernor.setText(new_governor)
self.labelCurrentBigGovernor.setText(new_governor)
self.governor = str(new_governor)
def console(self):
cursor = self.textEditConsole.textCursor()
while True:
try:
new_line = self.console_queue.get()
cursor.movePosition(QTextCursor.End)
cursor.insertText(new_line)
self.textEditConsole.setTextCursor(cursor)
self.textEditConsole.ensureCursorVisible()
except Exception, e:
print("Updating console failed, %s" % e)
def normal_output_written(self, text):
try:
self.console_queue.put(str(text))
except Exception, e:
print("Writing to UI console failed, %s" % e)
def getsettings(self):
self.lineEditApplicationName.setText(
self.settings.value("DefaultApplication", defaultValue=""))
self.doubleSpinBoxDuration.setValue(
float(self.settings.value("DefaultDuration", defaultValue=0.0)))
self.doubleSpinBoxPreamble.setValue(
float(self.settings.value("DefaultPreamble", defaultValue=0.0)))
if float(self.settings.value("DefaultPreamble",
defaultValue=0.0)) != 0.0:
self.checkBoxPreamble.setChecked(True)
self.checkBoxDrawGraph.setChecked(
bool(int(self.settings.value("DefaultDrawGraph", defaultValue=0))))
self.checkBoxWakeUp.setChecked(
bool(int(self.settings.value("DefaultEventWakeUp",
defaultValue=0))))
self.checkBoxSchedSwitch.setChecked(
bool(
int(
self.settings.value("DefaultEventSchedSwitch",
defaultValue=1))))
self.checkBoxCPUIdle.setChecked(
bool(
int(self.settings.value("DefaultEventCPUIdle",
defaultValue=0))))
self.checkBoxBinderTransaction.setChecked(
bool(
int(
self.settings.value("DefaultEventBinderTransaction",
defaultValue=1))))
self.checkBoxSyslogger.setChecked(
bool(
int(
self.settings.value("DefaultEventSyslogger",
defaultValue=1))))
def setupsettings(self):
self.settings = QSettings("HoffSoft", "Android Energy Debugger")
def setupbuttons(self):
self.pushButtonRun.clicked.connect(self.buttonrun)
self.pushButtonKillADB.clicked.connect(self.buttonkilladb)
self.pushButtonDisplayResults.clicked.connect(self.openresults)
self.pushButtonDisplayOptimizations.clicked.connect(
self.openoptimizations)
self.pushButtonDisplayBinderLog.clicked.connect(self.openbinderlog)
def setupmenu(self):
self.actionOpenResults.triggered.connect(self.openresults)
self.actionOpenBinderLog.triggered.connect(self.openbinderlog)
self.actionOpenSettings.triggered.connect(self.opensettingsmenu)
self.actionAbout.triggered.connect(self.openaboutdialog)
def openallresults(self, subdir=None):
self.openresults(subdir)
self.openoptimizations(subdir)
def openresults(self, subdir=None):
self.openfile(self.tableWidgetResults, "_results.csv", subdir=subdir)
def openoptimizations(self, subdir=None):
self.openfile(self.tableWidgetOptimizations,
"_optimizations.csv",
subdir=subdir)
def openfile(self, table_widget, file_suffix, subdir=None):
if not table_widget:
return
table_widget.setRowCount(0)
if self.lineEditApplicationName.text() is None:
QMessageBox.warning(self, "Error", "Application is not specified",
QMessageBox.Ok)
return
self.application_name = self.lineEditApplicationName.text()
if self.application_name == "":
return
if subdir is not None:
results_filename = os.path.join(
self.results_path,
subdir + self.application_name + file_suffix)
else:
results_filename = os.path.join(
self.results_path, self.application_name + file_suffix)
try:
col_count = 0
row_count = 0
with open(results_filename, "r") as fileInput:
for row in csv.reader(fileInput):
row_count += 1
if len(row) > col_count:
col_count = len(row)
fileInput.seek(0)
table_widget.setColumnCount(col_count)
table_widget.setRowCount(row_count)
for i, row in enumerate(csv.reader(fileInput)):
for j, col in enumerate(row):
table_widget.setItem(i, j, QTableWidgetItem(col))
except Exception, e:
QMessageBox.warning(self, "Error", "{}".format(e), QMessageBox.Ok)
def opensettingsmenu(self):
settings_menu = SettingsMenu(self.settings)
try:
settings_menu.exec_()
except Exception, e:
print(e)
self.close()
sys.exit()
def openaboutdialog(self):
dialog = AboutDialog()
dialog.exec_()
def opengraph(self):
pass
def openbinderlog(self):
if self.lineEditApplicationName.text() is None:
QMessageBox.warning(self, "Error", "Application is not specified",
QMessageBox.Ok)
return
self.application_name = self.lineEditApplicationName.text()
try:
self.binder_log_file = open(
os.path.join(self.results_path,
self.application_name + ".tlog"), "r")
except Exception:
QMessageBox.warning(self, "Error", "Binder log does not exist",
QMessageBox.Ok)
return
self.binder_log = []
for line in self.binder_log_file:
self.binder_log.append(line)
self.textBrowserBinderLog.append(line)
self.textBrowserBinderLog.verticalScrollBar().setValue(0)
def checkrun(self):
error = False
error_str = "The following required parameters are missing:\n\n"
if not self.lineEditApplicationName.text():
error = True
error_str += "Application Name\n"
if self.doubleSpinBoxDuration.value() == 0.0:
error = True
error_str += "Duration"
if error:
raise Exception(error_str)
return error
def buttonrun(self):
try:
self.checkrun()
if self.lineEditAppPID.text() != "":
self.pid = self.lineEditAppPID.text()
self.application_name = self.lineEditApplicationName.text()
self.duration = self.doubleSpinBoxDuration.value()
self.events = []
if self.checkBoxBinderTransaction.isCheckable():
self.events.append("binder_transaction")
if self.checkBoxCPUIdle.isChecked():
self.events.append("cpu_idle")
if self.checkBoxSchedSwitch.isChecked():
self.events.append("sched_switch")
if self.checkBoxSyslogger.isChecked():
self.events.append("sys_logger")
if self.checkBoxWakeUp.isChecked():
self.events.append("sched_wakeup")
if self.checkBoxEvents.isChecked():
self.events_to_process = self.spinBoxEvents.value()
else:
self.events_to_process = 0
self.preamble = self.doubleSpinBoxPreamble.value()
self.subgraph = self.checkBoxSubGraph.isChecked()
self.graph = self.checkBoxDrawGraph.isChecked()
self.skip_tracing = self.checkBoxSkipTracing.isChecked()
if (self.checkBoxTestAutomation.isChecked()
): # TODO allow test automation to be multithreaded
no_of_tests = self.spinBoxNoOfTests.value()
test_start_value = self.spinBoxStartTestValue.value()
self.labelTestsCompleted.setText("0")
self.progressBarTestsCompleted.setValue(0)
for x in range(test_start_value,
test_start_value + no_of_tests):
try:
buttonrunprocess(self.application_name,
self.governor[0],
self.duration,
self.events,
self.events_to_process,
self.preamble,
self.subgraph,
self.graph,
self.skip_tracing,
progress_signal=self.changed_progress,
open_func=self.openallresults,
subdir=self.application_name + "/" +
self.governor + "/" + str(x) + "/",
pid=self.pid)
if self.checkBoxTestAutomationPrompt.isChecked():
QMessageBox.information(
self,
"Test finished",
"Press OK to continue",
QMessageBox.Ok,
)
self.changed_test_count.emit(
str(x - test_start_value + 1))
self.changed_test_progress.emit(
round(
float(x - test_start_value + 1) /
float(no_of_tests) * 100))
except Exception, e:
print("test failed, retrying")
x -= 1
else:
if bool(
int(
self.settings.value("OptimizeWithThreads",
defaultValue=1))):
print("Running debugger with multithreading")
if self.debug_task:
try:
self.debug_task.terminate()
except Exception:
print("Could not terminate current debug task")
return
self.debug_task = QDebuggerThread(
self.application_name,
self.governor[0],
self.duration,
self.events,
self.events_to_process,
self.preamble,
self.subgraph,
self.graph,
self.skip_tracing,
self.openallresults,
)
self.debug_task.changed_progress.connect(
self.progressBar.setValue)
self.debug_task.start()
elif bool(
int(
self.settings.value("OptimizeWithProcesses",
defaultValue=1))):
print(
"Running debugger with multiprocessing, outputing to system console"
)
# Separate processes means that the generated process cannot access the UI console created in the
# main program's process
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
proc = multiprocessing.Process(
target=buttonrunprocess,
args=(
self.application_name,
self.governor[0],
self.duration,
self.events,
self.events_to_process,
self.preamble,
self.subgraph,
self.graph,
self.skip_tracing,
self.changed_progress,
self.openallresults,
),
)
self.jobs.append(proc)
proc.start()
if self.UI_console:
sys.stdout = EmittingStream(
textWritten=self.normal_output_written)
sys.stderr = EmittingStream(
textWritten=self.normal_output_written)
else:
buttonrunprocess(
self.application_name,
self.governor[0],
self.duration,
self.events,
self.events_to_process,
self.preamble,
self.subgraph,
self.graph,
self.skip_tracing,
progress_signal=self.changed_progress,
open_func=self.openallresults,
)
except Exception, e:
QMessageBox.critical(self, "Error", e, QMessageBox.Ok)
@staticmethod
def buttonkilladb():
os.system("killall adb")
def buttonrunprocess(
application_name,
governor,
duration,
events,
events_to_process,
preamble,
subgraph,
graph,
skip_tracing,
progress_signal=None,
open_func=None,
subdir=None,
pid=None,
):
try:
current_debugger = EnergyDebugger(application=application_name,
governor=governor,
duration=duration,
events=events,
event_count=events_to_process,
preamble=preamble,
graph=graph,
subgraph=subgraph,
skip_tracing=skip_tracing,
progress_signal=progress_signal,
results_subdir=subdir,
pid=pid)
current_debugger.run()
if open_func is not None:
open_func(subdir)
except Exception, e:
print("Error: {}".format(e))
print(" ------ FINISHED ------")
class QDebuggerThread(QThread):
changed_progress = pyqtSignal(int)
def __init__(
self,
application_name,
governor,
duration,
events,
events_to_process,
preamble,
subgraph,
graph,
skip_tracing,
open_func,
):
QThread.__init__(self)
self.application_name = application_name
self.governor = governor
self.duration = duration
self.events = events
self.events_to_process = events_to_process
self.preamble = preamble
self.subgraph = subgraph
self.graph = graph
self.skip_tracing = skip_tracing
self.open_func = open_func
def run(self):
buttonrunprocess(
self.application_name,
self.governor,
self.duration,
self.events,
self.events_to_process,
self.preamble,
self.subgraph,
self.graph,
self.skip_tracing,
self.changed_progress,
self.open_func,
)
class CommandInterface:
def __init__(self):
pass
def checkrun(self):
if not args.app:
return False
if not args.duration:
return False
def run(self):
pass
class EnergyDebugger:
def __init__(self,
application,
governor,
duration,
events,
event_count,
preamble,
graph,
subgraph,
skip_tracing,
progress_signal,
results_subdir,
pid=None):
self.adb = ADBInterface()
self.application = application
self.governor = governor
self.duration = duration
self.events = []
self.event_count = event_count
self.preamble = preamble
self.graph = graph
self.subgraph = subgraph
self.tc_processor = None
self.skip_tracing = skip_tracing
self.progress_signal = progress_signal
self.results_subdir = results_subdir
self.pid = pid
""" Required objects for tracking system metrics and interfacing with a target system, connected
via an ADB connection.
"""
start_time = time.time()
try:
self.pid_tool = PIDTool(self.adb, self.application, self.pid)
except Exception, e:
raise Exception("Trace failed: {}".format(e))
print("PIDs gathered --- %s Sec" % (time.time() - start_time))
start_time = time.time()
self.trace_processor = TraceProcessor(self.pid_tool, self.application)
print("Trace processor created --- %s Sec" %
(time.time() - start_time))
start_time = time.time()
self.sys_metrics = SystemMetrics(self.adb)
print("System metrics initialized --- %s Sec" %
(time.time() - start_time))
""" The tracer object stores the configuration for the ftrace trace that is to be performed on the
target system.
"""
start_time = time.time()
self.tracer = Tracer(
adb_device=self.adb,
name=application,
metrics=self.sys_metrics,
events=events,
duration=duration,
)
print("Tracer created --- %s Sec" % (time.time() - start_time))
if self.duration > 6:
print "WARNING: Running traces over 6 seconds can cause issue due to data loss from trace buffer size " "limitations"
QMessageBox.warning(
self,
"Warning",
"Running traces over 6 seconds can cause issue due to data loss from trace buffer "
"size limitations",
QMessageBox.Ok,
)
start_time = time.time()
self.sys_logger = SysLogger(self.adb)
print("Syslogger created --- %s Sec" % (time.time() - start_time))
def normalOutputWritten(self, text):
cursor = self.textEditConsole.textCursor()
cursor.movePosition(QTextCursor.End)
cursor.insertText(text)
self.textEditConsole.setTextCursor(cursor)
self.textEditConsole.ensureCursorVisible()
def clear(self):
# TODO
pass
def run(self):
""" Entry point into the debugging tool.
"""
""" As the energy debugger depends on the custom trace points implemented in the syslogger module,
it must be loaded before tracing begins. It must then be unloaded and finished before the results
are pulled from the target system.
"""
start_time = time.time()
if not self.skip_tracing:
self.sys_logger.start()
self.tracer.run_tracer(self.preamble, args.skip_clear)
self.sys_logger.stop()
try: