def getSystemRebootStartTime(rebootTimes):
preloaderTimeList = []
lkTimeList = []
linuxInitTimeList = []
sumTimeList = []
bTimeList = []
if not os.path.exists("System"):
os.mkdir("System")
systemRebootStartTimeFile = open("./System/systemRebootStartTime.txt", mode="w+")
while 1:
if rebootTimes is None:
times = int(input("请输入需要reboot的次数(1分钟/次):"))
else:
times = rebootTimes
if times > 0:
print("Reboot")
os.system("adb reboot")
else:
continue
i = 1
line0 = "preloaderTime lkTime linuxInitTime sumTime btime\n"
systemRebootStartTimeFile.write(line0)
systemRebootStartTimeFile.flush()
timeList.append("SystemRebootStartTimes=" + str(times * 60))
while i <= times:
filename = "./device.txt"
print("adb devices")
os.system("adb devices>" + filename)
file = open(filename, mode="r+")
alist = file.readlines()
print(alist)
print(len(alist))
if len(alist) == 3:
print("Start")
sn = alist[1].split("\t")[0]
print(sn)
d = u2.connect(sn)
os.system("adb shell cat /proc/bootprof > ./System/bootprof_" + str(i) + ".txt")
out = d.shell("cat /proc/bootprof")
preloaderTime = str(out).split("preloader")[0].split("\\n")[3].replace(":", "").strip()
lkTime = str(out).split("lk")[0].split("\\n")[4].replace(":", "").strip()
# print(str(out).split("OFF")[0].split("\\n")[len(str(out).split("OFF")[0].split("\\n")) - 1].replace(":",
# "").strip())
linuxInitTime = str(out).split("OFF")[0].split("\\n")[
len(str(out).split("OFF")[0].split("\\n")) - 1].replace(":", "").strip()
preloaderTimeList.append(int(preloaderTime))
lkTimeList.append(int(lkTime))
linuxInitTimeList.append(float(linuxInitTime))
sumTime = int(preloaderTime) + int(lkTime) + float(linuxInitTime)
sumTimeList.append(sumTime)
bout = d.shell("cat /proc/stat")
bTime = str(bout).split("btime")[1].split("\\n")[0].strip()
bTimeList.append(bTime)
line = "%8s" % preloaderTime + " %8s" % lkTime + " %8s" % linuxInitTime + " %8s" % sumTime + " %12s" % bTime + "\n"
systemRebootStartTimeFile.write(line)
systemRebootStartTimeFile.flush()
time.sleep(10)
timestr = time.asctime(time.localtime(time.time()))
file.close()
os.remove(filename)
os.system("adb reboot")
print(timestr + "======Reboot。。。" + str(i))
i += 1
continue
else:
time.sleep(5)
file.close()
print("Remove file")
os.remove(filename)
print("continue")
continue
# 绘制曲线图
plt.figure(figsize=(40, 20))
plt.xlabel('Times\n red:sumTime green:linuxInitTime blue:lkTime yellow:preloaderTime', fontsize=40) # x轴的label
plt.ylabel('StartTime(ms)', fontsize=40) # y轴的label
plt.title("System Reboot StartTime", fontsize=80)
plt.tick_params(labelsize=30)
plt.plot(preloaderTimeList[:], linestyle="-", color='y')
plt.plot(lkTimeList[:], linestyle="-", color='b')
plt.plot(linuxInitTimeList[:], linestyle="-", color='g')
plt.plot(sumTimeList[:], linestyle="-", color='r')
plt.draw()
figname = "./System/SystemReboot_StartTimeFigure.png"
plt.savefig(figname)
plt.pause(1)
plt.close()
line1 = "preloaderTime:MAX=" + str(np.max(preloaderTimeList)) + " Min=" + str(
np.min(preloaderTimeList)) + " AVG=" + str(np.mean(preloaderTimeList)) + "\n"
line2 = "lkTime:MAX=" + str(np.max(lkTimeList)) + " Min=" + str(np.min(lkTimeList)) + " AVG=" + str(
np.mean(lkTimeList)) + "\n"
line3 = "linuxInitTime:MAX=" + str(np.max(linuxInitTimeList)) + " Min=" + str(
np.min(linuxInitTimeList)) + " AVG=" + str(np.mean(linuxInitTimeList)) + "\n"
line4 = "sumTime:MAX=" + str(np.max(sumTimeList)) + " Min=" + str(
np.min(sumTimeList)) + " AVG=" + str(np.mean(sumTimeList)) + "\n"
systemRebootStartTimeFile.write(line1)
systemRebootStartTimeFile.write(line2)
systemRebootStartTimeFile.write(line3)
systemRebootStartTimeFile.write(line4)
# reboot StartTime 最大值和最小值差异超过10s时标记次数输出。
if int(np.max(sumTimeList)) - int(np.min(sumTimeList)) > 10:
maxIndex = sumTimeList.index(np.max(sumTimeList)) + 1
minIndex = sumTimeList.index(np.min(sumTimeList)) + 1
line5 = "max reboot startTimes:" + str(maxIndex) + "min reboot startTimes:" + str(minIndex)
systemRebootStartTimeFile.write(line5)
systemRebootStartTimeFile.flush()
systemRebootStartTimeFile.close()
break