|
2 | 2 |
|
3 | 3 | # This file is part of eRCaGuy_dotfiles: https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles |
4 | 4 |
|
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! |
6 | 8 |
|
7 | 9 | # Print out basic stats on cpu load. |
8 | 10 |
|
|
59 | 61 | """ |
60 | 62 | To view live log output, run this. |
61 | 63 | 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' |
63 | 66 |
|
64 | 67 |
|
65 | 68 | ######### |
|
110 | 113 | while True: |
111 | 114 | cpu_percent_cores = psutil.cpu_percent(interval=t_measurement_sec, percpu=True) |
112 | 115 | 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) + '%' |
114 | 118 | cpu_percent_cores_str = [('%5.2f' % x) + '%' for x in cpu_percent_cores] |
115 | 119 | cpu_percent_cores_str = ', '.join(cpu_percent_cores_str) |
116 | 120 |
|
117 | | - logger.info(' ==> Overall: {} <==, Individual CPUs: {} '.format( |
| 121 | + logger.info(' ==> Overall: {} <==, Individual CPUs: {} '.format( |
118 | 122 | cpu_percent_overall_str, |
119 | 123 | cpu_percent_cores_str)) |
120 | 124 |
|
|
137 | 141 | splitlines = [line.split(maxsplit=1) for line in lines] |
138 | 142 | # print(splitlines) |
139 | 143 |
|
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 = [] |
142 | 146 | for line in splitlines[1:]: # Skip first line since it contains a heading: `%CPU COMMAND` |
143 | 147 | individual_cpu_usage_pct = float(line[0]) |
144 | 148 | 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 |
145 | 151 |
|
| 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: |
146 | 156 | 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: |
154 | 157 | 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] |
156 | 179 |
|
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)) |
161 | 181 | handler.setFormatter(formatter) # restore format for next logs |
162 | 182 |
|
163 | 183 |
|
164 | | - # time.sleep(t_measurement_sec) |
165 | | - |
166 | | - |
167 | | - |
168 | 184 |
|
169 | 185 | # print("Measuring CPU load for {} seconds...".format(t_measurement_sec)) |
170 | 186 | # cpu_percent_cores = psutil.cpu_percent(interval=t_measurement_sec, percpu=True) |
|
0 commit comments