Skip to content

Commit 13d9781

Browse files
Improve useful_scripts/cpu_logger.py
1 parent 10f8d1c commit 13d9781

1 file changed

Lines changed: 38 additions & 22 deletions

File tree

useful_scripts/cpu_logger.py

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
# This file is part of eRCaGuy_dotfiles: https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles
44

5-
# STATUS: work-in-progress; NOT YET FUNCTIONAL!
5+
# STATUS: WORKS and is usable, but the comments aren't finalized (ex: see the installation
6+
# instructions below), and the program could use some cleaning up when I have time. Anyway, it does
7+
# work at least!
68

79
# Print out basic stats on cpu load.
810

@@ -59,7 +61,8 @@
5961
"""
6062
To view live log output, run this.
6163
See my ans: https://unix.stackexchange.com/a/687072/114401
62-
less -N +F ~/cpu_log.log
64+
less -N --follow-name +F ~/cpu_log.log
65+
alias gs_cpu_logger_watch='less -N --follow-name +F ~/cpu_log.log'
6366
6467
6568
#########
@@ -110,11 +113,12 @@
110113
while True:
111114
cpu_percent_cores = psutil.cpu_percent(interval=t_measurement_sec, percpu=True)
112115
avg = sum(cpu_percent_cores)/len(cpu_percent_cores)
113-
cpu_percent_overall_str = ('%5.2f' % avg) + '%'
116+
cpu_percent_overall = avg
117+
cpu_percent_overall_str = ('%5.2f' % cpu_percent_overall) + '%'
114118
cpu_percent_cores_str = [('%5.2f' % x) + '%' for x in cpu_percent_cores]
115119
cpu_percent_cores_str = ', '.join(cpu_percent_cores_str)
116120

117-
logger.info(' ==> Overall: {} <==, Individual CPUs: {} '.format(
121+
logger.info(' ==> Overall: {} <==, Individual CPUs: {} '.format(
118122
cpu_percent_overall_str,
119123
cpu_percent_cores_str))
120124

@@ -137,34 +141,46 @@
137141
splitlines = [line.split(maxsplit=1) for line in lines]
138142
# print(splitlines)
139143

140-
# filter output to only retain high-cpu processes
141-
high_cpu_processes_list = []
144+
# Convert to a list of [float cpu_pct, str cmd]
145+
cpu_processes_list = []
142146
for line in splitlines[1:]: # Skip first line since it contains a heading: `%CPU COMMAND`
143147
individual_cpu_usage_pct = float(line[0])
144148
cmd = line[1]
149+
cpu_processes_list.append([individual_cpu_usage_pct, cmd])
150+
cpu_processes_list.sort(reverse=True) # sort highest-cpu-usage first
145151

152+
# Obtain a list of the top 10 processes, and another list of the processes > X% cpu usage
153+
cpu_processes_top10_list = cpu_processes_list[:10]
154+
cpu_processes_above_threshold_list = []
155+
for process in cpu_processes_list:
146156
CPU_THRESHOLD_PCT = 15
147-
if individual_cpu_usage_pct > CPU_THRESHOLD_PCT:
148-
high_cpu_processes_list.append([('%5.2f' % individual_cpu_usage_pct) + "%", cmd])
149-
150-
# print(high_cpu_processes_list)
151-
handler.setFormatter(None) # remove formatter for this log msg only
152-
high_cpu_processes_list.sort(reverse=True) # sort highest-cpu-usage first
153-
for process in high_cpu_processes_list:
154157
individual_cpu_usage_pct = process[0]
155-
cmd = process[1]
158+
if individual_cpu_usage_pct >= CPU_THRESHOLD_PCT:
159+
cpu_processes_above_threshold_list.append(process)
160+
161+
# If overall cpu usage is > Y, log all processes > X, OR the top 10 processes, whichever is
162+
# the greater number of processes. This ensures that when the overall cpu usage is high, we log
163+
# *something*, instead of nothing, in the event no single process is > X
164+
OVERALL_CPU_THRESHOLD_PCT = 50
165+
cpu_processes_list = cpu_processes_above_threshold_list
166+
if cpu_percent_overall > OVERALL_CPU_THRESHOLD_PCT:
167+
if len(cpu_processes_top10_list) > len(cpu_processes_above_threshold_list):
168+
cpu_processes_list = cpu_processes_top10_list
169+
170+
# Log the high-cpu-usage processes
171+
handler.setFormatter(None) # remove formatter for these log msgs only
172+
num = 0
173+
for process in cpu_processes_list:
174+
num += 1
175+
num_str = "%2i" % num
176+
individual_cpu_usage_pct = process[0]
177+
individual_cpu_usage_pct_str = ('%5.2f' % individual_cpu_usage_pct) + "%"
178+
cmd_str = process[1]
156179

157-
logger.info(' {}, cmd: {}'.format(individual_cpu_usage_pct, cmd))
158-
if not high_cpu_processes_list:
159-
# logger.info(' No processes are using > {}% of a single CPU.'.format(CPU_THRESHOLD_PCT))
160-
pass
180+
logger.info(' {}/{}) {}, cmd: {}'.format(num_str, len(cpu_processes_list), individual_cpu_usage_pct_str, cmd_str))
161181
handler.setFormatter(formatter) # restore format for next logs
162182

163183

164-
# time.sleep(t_measurement_sec)
165-
166-
167-
168184

169185
# print("Measuring CPU load for {} seconds...".format(t_measurement_sec))
170186
# cpu_percent_cores = psutil.cpu_percent(interval=t_measurement_sec, percpu=True)

0 commit comments

Comments
 (0)