| 1 |
|
| 2 |
"""psi2log - PSI metrics monitor and logger""" |
| 3 |
|
| 4 |
from argparse import ArgumentParser |
| 5 |
from ctypes import CDLL |
| 6 |
from signal import SIGHUP, SIGINT, SIGQUIT, SIGTERM, signal |
| 7 |
from sys import exit, stdout |
| 8 |
from time import monotonic, sleep |
| 9 |
|
| 10 |
|
| 11 |
def read_path(path): |
| 12 |
""" |
| 13 |
""" |
| 14 |
try: |
| 15 |
fd[path].seek(0) |
| 16 |
except ValueError: |
| 17 |
try: |
| 18 |
fd[path] = open(path, 'rb', buffering=0) |
| 19 |
except FileNotFoundError as e: |
| 20 |
log(e) |
| 21 |
return None |
| 22 |
except KeyError: |
| 23 |
try: |
| 24 |
fd[path] = open(path, 'rb', buffering=0) |
| 25 |
except FileNotFoundError as e: |
| 26 |
log(e) |
| 27 |
return None |
| 28 |
try: |
| 29 |
return fd[path].read(99999).decode() |
| 30 |
except OSError as e: |
| 31 |
log(e) |
| 32 |
fd[path].close() |
| 33 |
return None |
| 34 |
|
| 35 |
|
| 36 |
def form1(num): |
| 37 |
""" |
| 38 |
""" |
| 39 |
s = str(num).split('.') |
| 40 |
return '{}.{:0<2}'.format(s[0], s[1]) |
| 41 |
|
| 42 |
|
| 43 |
def form2(num): |
| 44 |
""" |
| 45 |
""" |
| 46 |
s = str(round(num, 1)).split('.') |
| 47 |
return '{}.{:0<1}'.format(s[0], s[1]) |
| 48 |
|
| 49 |
|
| 50 |
def signal_handler(signum, frame): |
| 51 |
""" |
| 52 |
""" |
| 53 |
def signal_handler_inner(signum, frame): |
| 54 |
pass |
| 55 |
|
| 56 |
for i in sig_list: |
| 57 |
signal(i, signal_handler_inner) |
| 58 |
|
| 59 |
if signum == SIGINT: |
| 60 |
print('') |
| 61 |
|
| 62 |
lpd = len(peaks_dict) |
| 63 |
|
| 64 |
if lpd == 15: |
| 65 |
log('=================================') |
| 66 |
log('Peak values: avg10 avg60 avg300') |
| 67 |
log('----------- ------ ------ ------') |
| 68 |
log('some cpu {:>6} {:>6} {:>6}'.format( |
| 69 |
form1(peaks_dict['c_some_avg10']), |
| 70 |
form1(peaks_dict['c_some_avg60']), |
| 71 |
form1(peaks_dict['c_some_avg300']), |
| 72 |
)) |
| 73 |
log('----------- ------ ------ ------') |
| 74 |
log('some io {:>6} {:>6} {:>6}'.format( |
| 75 |
form1(peaks_dict['i_some_avg10']), |
| 76 |
form1(peaks_dict['i_some_avg60']), |
| 77 |
form1(peaks_dict['i_some_avg300']), |
| 78 |
)) |
| 79 |
|
| 80 |
log('full io {:>6} {:>6} {:>6}'.format( |
| 81 |
form1(peaks_dict['i_full_avg10']), |
| 82 |
form1(peaks_dict['i_full_avg60']), |
| 83 |
form1(peaks_dict['i_full_avg300']), |
| 84 |
)) |
| 85 |
|
| 86 |
log('----------- ------ ------ ------') |
| 87 |
|
| 88 |
log('some memory {:>6} {:>6} {:>6}'.format( |
| 89 |
form1(peaks_dict['m_some_avg10']), |
| 90 |
form1(peaks_dict['m_some_avg60']), |
| 91 |
form1(peaks_dict['m_some_avg300']), |
| 92 |
)) |
| 93 |
|
| 94 |
log('full memory {:>6} {:>6} {:>6}'.format( |
| 95 |
form1(peaks_dict['m_full_avg10']), |
| 96 |
form1(peaks_dict['m_full_avg60']), |
| 97 |
form1(peaks_dict['m_full_avg300']), |
| 98 |
)) |
| 99 |
|
| 100 |
if lpd == 5: |
| 101 |
log('----- | ----- ----- | ----- ----- | --------') |
| 102 |
log('{:>5} | {:>5} {:>5} | {:>5} {:>5} | peaks'.format( |
| 103 |
form2(peaks_dict['avg_cs']), |
| 104 |
form2(peaks_dict['avg_is']), |
| 105 |
form2(peaks_dict['avg_if']), |
| 106 |
form2(peaks_dict['avg_ms']), |
| 107 |
form2(peaks_dict['avg_mf']) |
| 108 |
)) |
| 109 |
|
| 110 |
if target == 'SYSTEM_WIDE': |
| 111 |
log_stall_times() |
| 112 |
|
| 113 |
if separate_log: |
| 114 |
logging.info('') |
| 115 |
|
| 116 |
exit() |
| 117 |
|
| 118 |
|
| 119 |
def log_stall_times(): |
| 120 |
""" |
| 121 |
""" |
| 122 |
total_cs_1 = psi_file_cpu_to_total(cpu_file) |
| 123 |
total_is_1, total_if_1 = psi_file_mem_to_total(io_file) |
| 124 |
total_ms_1, total_mf_1 = psi_file_mem_to_total(memory_file) |
| 125 |
t = monotonic() - t_0 |
| 126 |
|
| 127 |
M = 1000000 |
| 128 |
|
| 129 |
dcs = (total_cs_1 - total_cs_0) / M |
| 130 |
dis = (total_is_1 - total_is_0) / M |
| 131 |
dif = (total_if_1 - total_if_0) / M |
| 132 |
dms = (total_ms_1 - total_ms_0) / M |
| 133 |
dmf = (total_mf_1 - total_mf_0) / M |
| 134 |
|
| 135 |
if mode == '0' or mode == '1': |
| 136 |
log('=================================') |
| 137 |
else: |
| 138 |
log('--') |
| 139 |
|
| 140 |
log('Stall times for the last {}s:'.format(round(t, 1))) |
| 141 |
log('-----------') |
| 142 |
log('some cpu {}s, avg {}%'.format( |
| 143 |
round(dcs, 1), |
| 144 |
round(dcs / t * 100, 1) |
| 145 |
)) |
| 146 |
log('-----------') |
| 147 |
log('some io {}s, avg {}%'.format( |
| 148 |
round(dis, 1), |
| 149 |
round(dis / t * 100, 1) |
| 150 |
)) |
| 151 |
log('full io {}s, avg {}%'.format( |
| 152 |
round(dif, 1), |
| 153 |
round(dif / t * 100, 1) |
| 154 |
)) |
| 155 |
log('-----------') |
| 156 |
|
| 157 |
log('some memory {}s, avg {}%'.format( |
| 158 |
round(dms, 1), |
| 159 |
round(dms / t * 100, 1) |
| 160 |
)) |
| 161 |
|
| 162 |
log('full memory {}s, avg {}%'.format( |
| 163 |
round(dmf, 1), |
| 164 |
round(dmf / t * 100, 1) |
| 165 |
)) |
| 166 |
|
| 167 |
|
| 168 |
def cgroup2_root(): |
| 169 |
""" |
| 170 |
""" |
| 171 |
with open(mounts) as f: |
| 172 |
for line in f: |
| 173 |
if cgroup2_separator in line: |
| 174 |
return line.partition(cgroup2_separator)[0].partition(' ')[2] |
| 175 |
|
| 176 |
|
| 177 |
def mlockall(): |
| 178 |
""" |
| 179 |
""" |
| 180 |
MCL_CURRENT = 1 |
| 181 |
MCL_FUTURE = 2 |
| 182 |
MCL_ONFAULT = 4 |
| 183 |
|
| 184 |
libc = CDLL(None, use_errno=True) |
| 185 |
result = libc.mlockall(MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT) |
| 186 |
|
| 187 |
if result != 0: |
| 188 |
result = libc.mlockall(MCL_CURRENT | MCL_FUTURE) |
| 189 |
if result != 0: |
| 190 |
log('WARNING: cannot lock process memory: [Errno {}]'.format( |
| 191 |
result)) |
| 192 |
else: |
| 193 |
log('Prosess memory locked with MCL_CURRENT | MCL_FUTURE') |
| 194 |
else: |
| 195 |
log('Process memory locked with ' |
| 196 |
'MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT') |
| 197 |
|
| 198 |
|
| 199 |
def psi_file_mem_to_metrics0(psi_path): |
| 200 |
""" |
| 201 |
""" |
| 202 |
with open(psi_path) as f: |
| 203 |
psi_list = f.readlines() |
| 204 |
some_list, full_list = psi_list[0].split(' '), psi_list[1].split(' ') |
| 205 |
some_avg10 = some_list[1].split('=')[1] |
| 206 |
some_avg60 = some_list[2].split('=')[1] |
| 207 |
some_avg300 = some_list[3].split('=')[1] |
| 208 |
full_avg10 = full_list[1].split('=')[1] |
| 209 |
full_avg60 = full_list[2].split('=')[1] |
| 210 |
full_avg300 = full_list[3].split('=')[1] |
| 211 |
return (some_avg10, some_avg60, some_avg300, |
| 212 |
full_avg10, full_avg60, full_avg300) |
| 213 |
|
| 214 |
|
| 215 |
def psi_file_mem_to_metrics(psi_path): |
| 216 |
""" |
| 217 |
""" |
| 218 |
foo = read_path(psi_path) |
| 219 |
|
| 220 |
if foo is None: |
| 221 |
return None |
| 222 |
|
| 223 |
try: |
| 224 |
psi_list = foo.split('\n') |
| 225 |
|
| 226 |
some_list, full_list = psi_list[0].split(' '), psi_list[1].split(' ') |
| 227 |
some_avg10 = some_list[1].split('=')[1] |
| 228 |
some_avg60 = some_list[2].split('=')[1] |
| 229 |
some_avg300 = some_list[3].split('=')[1] |
| 230 |
full_avg10 = full_list[1].split('=')[1] |
| 231 |
full_avg60 = full_list[2].split('=')[1] |
| 232 |
full_avg300 = full_list[3].split('=')[1] |
| 233 |
return (some_avg10, some_avg60, some_avg300, |
| 234 |
full_avg10, full_avg60, full_avg300) |
| 235 |
|
| 236 |
except Exception as e: |
| 237 |
log('{}'.format(e)) |
| 238 |
return None |
| 239 |
|
| 240 |
|
| 241 |
def psi_file_cpu_to_metrics(psi_path): |
| 242 |
""" |
| 243 |
""" |
| 244 |
foo = read_path(psi_path) |
| 245 |
|
| 246 |
if foo is None: |
| 247 |
return None |
| 248 |
|
| 249 |
try: |
| 250 |
psi_list = foo.split('\n') |
| 251 |
|
| 252 |
some_list = psi_list[0].split(' ') |
| 253 |
some_avg10 = some_list[1].split('=')[1] |
| 254 |
some_avg60 = some_list[2].split('=')[1] |
| 255 |
some_avg300 = some_list[3].split('=')[1] |
| 256 |
return (some_avg10, some_avg60, some_avg300) |
| 257 |
|
| 258 |
except Exception as e: |
| 259 |
log('{}'.format(e)) |
| 260 |
return None |
| 261 |
|
| 262 |
|
| 263 |
def psi_file_mem_to_total(psi_path): |
| 264 |
""" |
| 265 |
""" |
| 266 |
foo = read_path(psi_path) |
| 267 |
|
| 268 |
if foo is None: |
| 269 |
return None |
| 270 |
|
| 271 |
try: |
| 272 |
psi_list = foo.split('\n') |
| 273 |
|
| 274 |
some_list, full_list = psi_list[0].split(' '), psi_list[1].split(' ') |
| 275 |
some_total = some_list[4].split('=')[1] |
| 276 |
full_total = full_list[4].split('=')[1] |
| 277 |
|
| 278 |
return int(some_total), int(full_total) |
| 279 |
|
| 280 |
except Exception as e: |
| 281 |
log('{}'.format(e)) |
| 282 |
return None |
| 283 |
|
| 284 |
|
| 285 |
def psi_file_cpu_to_total(psi_path): |
| 286 |
""" |
| 287 |
""" |
| 288 |
foo = read_path(psi_path) |
| 289 |
|
| 290 |
if foo is None: |
| 291 |
return None |
| 292 |
|
| 293 |
try: |
| 294 |
psi_list = foo.split('\n') |
| 295 |
|
| 296 |
some_list = psi_list[0].split(' ') |
| 297 |
some_total = some_list[4].split('=')[1] |
| 298 |
|
| 299 |
return int(some_total) |
| 300 |
|
| 301 |
except Exception as e: |
| 302 |
log('{}'.format(e)) |
| 303 |
return None |
| 304 |
|
| 305 |
|
| 306 |
def print_head_0(): |
| 307 |
""" |
| 308 |
""" |
| 309 |
log('===================================================================' |
| 310 |
'============') |
| 311 |
log(' cpu || io || memory') |
| 312 |
log('============= || ============================= || =================' |
| 313 |
'============') |
| 314 |
log(' some || some | full || some | ' |
| 315 |
' full') |
| 316 |
log('------------- || ------------- | ------------- || ------------- | -' |
| 317 |
'------------') |
| 318 |
log(' avg10 avg60 || avg10 avg60 | avg10 avg60 || avg10 avg60 | ' |
| 319 |
'avg10 avg60') |
| 320 |
log('------ ------ || ------ ------ | ------ ------ || ------ ------ | -' |
| 321 |
'----- ------') |
| 322 |
|
| 323 |
|
| 324 |
def print_head_1(): |
| 325 |
""" |
| 326 |
""" |
| 327 |
log('====================================================================' |
| 328 |
'==============================================') |
| 329 |
log(' cpu || io ' |
| 330 |
'|| memory') |
| 331 |
log('==================== || =========================================== ' |
| 332 |
'|| ===========================================') |
| 333 |
log(' some || some | full ' |
| 334 |
'|| some | full') |
| 335 |
log('-------------------- || -------------------- | -------------------- ' |
| 336 |
'|| -------------------- | --------------------') |
| 337 |
log(' avg10 avg60 avg300 || avg10 avg60 avg300 | avg10 avg60 avg300 ' |
| 338 |
'|| avg10 avg60 avg300 | avg10 avg60 avg300') |
| 339 |
log('------ ------ ------ || ------ ------ ------ | ------ ------ ------ ' |
| 340 |
'|| ------ ------ ------ | ------ ------ ------') |
| 341 |
|
| 342 |
|
| 343 |
def print_head_2(): |
| 344 |
""" |
| 345 |
""" |
| 346 |
log('----- - ----------- - ----------- -') |
| 347 |
log(' cpu | io | memory |') |
| 348 |
log('----- | ----------- | ----------- |') |
| 349 |
log(' some | some full | some full | interval') |
| 350 |
log('----- | ----- ----- | ----- ----- | --------') |
| 351 |
|
| 352 |
|
| 353 |
def log(*msg): |
| 354 |
""" |
| 355 |
""" |
| 356 |
if not SUPPRESS_OUTPUT: |
| 357 |
print(*msg) |
| 358 |
if separate_log: |
| 359 |
logging.info(*msg) |
| 360 |
|
| 361 |
|
| 362 |
def log_head(*msg): |
| 363 |
""" |
| 364 |
""" |
| 365 |
print(*msg) |
| 366 |
if separate_log: |
| 367 |
logging.info(*msg) |
| 368 |
|
| 369 |
|
| 370 |
parser = ArgumentParser() |
| 371 |
|
| 372 |
parser.add_argument( |
| 373 |
'-t', |
| 374 |
'--target', |
| 375 |
help="""target (cgroup_v2 or SYSTEM_WIDE)""", |
| 376 |
default='SYSTEM_WIDE', |
| 377 |
type=str |
| 378 |
) |
| 379 |
|
| 380 |
|
| 381 |
parser.add_argument( |
| 382 |
'-i', |
| 383 |
'--interval', |
| 384 |
help="""interval in sec""", |
| 385 |
default=2, |
| 386 |
type=float |
| 387 |
) |
| 388 |
|
| 389 |
|
| 390 |
parser.add_argument( |
| 391 |
'-l', |
| 392 |
'--log', |
| 393 |
help="""path to log file""", |
| 394 |
default=None, |
| 395 |
type=str |
| 396 |
) |
| 397 |
|
| 398 |
|
| 399 |
parser.add_argument( |
| 400 |
'-m', |
| 401 |
'--mode', |
| 402 |
help="""mode (0, 1 or 2)""", |
| 403 |
default='0', |
| 404 |
type=str |
| 405 |
) |
| 406 |
|
| 407 |
|
| 408 |
parser.add_argument( |
| 409 |
'-s', |
| 410 |
'--suppress-output', |
| 411 |
help="""suppress output""", |
| 412 |
default='False', |
| 413 |
type=str |
| 414 |
) |
| 415 |
|
| 416 |
|
| 417 |
args = parser.parse_args() |
| 418 |
target = args.target |
| 419 |
mode = args.mode |
| 420 |
interval = args.interval |
| 421 |
log_file = args.log |
| 422 |
suppress_output = args.suppress_output |
| 423 |
|
| 424 |
if target != 'SYSTEM_WIDE': |
| 425 |
target = '/' + target.strip('/') |
| 426 |
|
| 427 |
|
| 428 |
if log_file is None: |
| 429 |
separate_log = False |
| 430 |
else: |
| 431 |
separate_log = True |
| 432 |
import logging |
| 433 |
|
| 434 |
if separate_log: |
| 435 |
try: |
| 436 |
logging.basicConfig( |
| 437 |
filename=log_file, |
| 438 |
level=logging.INFO, |
| 439 |
format="%(asctime)s: %(message)s") |
| 440 |
except Exception as e: |
| 441 |
print(e) |
| 442 |
exit(1) |
| 443 |
|
| 444 |
|
| 445 |
if suppress_output == 'False': |
| 446 |
SUPPRESS_OUTPUT = False |
| 447 |
elif suppress_output == 'True': |
| 448 |
SUPPRESS_OUTPUT = True |
| 449 |
else: |
| 450 |
log_head('error: argument -s/--suppress-output: valid values are ' |
| 451 |
'False and True') |
| 452 |
exit(1) |
| 453 |
|
| 454 |
|
| 455 |
if log_file is not None: |
| 456 |
logstring = 'log file: {}, '.format(log_file) |
| 457 |
else: |
| 458 |
logstring = 'log file is not set, ' |
| 459 |
|
| 460 |
|
| 461 |
if interval < 1: |
| 462 |
log_head('error: argument -i/--interval: the value must be greater than or' |
| 463 |
' equal to 1') |
| 464 |
exit(1) |
| 465 |
|
| 466 |
|
| 467 |
if not (mode == '0' or mode == '1' or mode == '2'): |
| 468 |
log_head('ERROR: invalid mode. Valid values are 0, 1 and 2. Exit.') |
| 469 |
exit(1) |
| 470 |
|
| 471 |
|
| 472 |
try: |
| 473 |
psi_file_mem_to_metrics0('/proc/pressure/memory') |
| 474 |
except Exception as e: |
| 475 |
log_head('ERROR: {}'.format(e)) |
| 476 |
log_head('PSI metrics are not provided by the kernel. Exit.') |
| 477 |
exit(1) |
| 478 |
|
| 479 |
|
| 480 |
log_head('Starting psi2log, target: {}, mode: {}, interval: {} sec, {}suppress' |
| 481 |
' output: {}'.format( |
| 482 |
target, mode, round(interval, 3), logstring, suppress_output)) |
| 483 |
|
| 484 |
|
| 485 |
fd = dict() |
| 486 |
|
| 487 |
|
| 488 |
if target == 'SYSTEM_WIDE': |
| 489 |
system_wide = True |
| 490 |
source_dir = '/proc/pressure' |
| 491 |
cpu_file = '/proc/pressure/cpu' |
| 492 |
io_file = '/proc/pressure/io' |
| 493 |
memory_file = '/proc/pressure/memory' |
| 494 |
log_head('PSI source dir: /proc/pressure/, source files: cpu, io, memory') |
| 495 |
else: |
| 496 |
system_wide = False |
| 497 |
mounts = '/proc/mounts' |
| 498 |
cgroup2_separator = ' cgroup2 rw,' |
| 499 |
cgroup2_mountpoint = cgroup2_root() |
| 500 |
|
| 501 |
if cgroup2_mountpoint is None: |
| 502 |
log('ERROR: unified cgroup hierarchy is not mounted, exit') |
| 503 |
exit(1) |
| 504 |
|
| 505 |
source_dir = cgroup2_mountpoint + target |
| 506 |
cpu_file = source_dir + '/cpu.pressure' |
| 507 |
io_file = source_dir + '/io.pressure' |
| 508 |
memory_file = source_dir + '/memory.pressure' |
| 509 |
log_head('PSI source dir: {}{}/, source files: cpu.pressure, io.pressure,' |
| 510 |
' memory.pressure'.format(cgroup2_mountpoint, target)) |
| 511 |
|
| 512 |
|
| 513 |
abnormal_interval = 1.01 * interval |
| 514 |
abnormal_inaccuracy = 0.05 |
| 515 |
|
| 516 |
|
| 517 |
if target == 'SYSTEM_WIDE': |
| 518 |
total_cs_0 = psi_file_cpu_to_total(cpu_file) |
| 519 |
total_is_0, total_if_0 = psi_file_mem_to_total(io_file) |
| 520 |
total_ms_0, total_mf_0 = psi_file_mem_to_total(memory_file) |
| 521 |
t_0 = monotonic() |
| 522 |
|
| 523 |
|
| 524 |
peaks_dict = dict() |
| 525 |
|
| 526 |
|
| 527 |
sig_list = [SIGTERM, SIGINT, SIGQUIT, SIGHUP] |
| 528 |
|
| 529 |
for i in sig_list: |
| 530 |
signal(i, signal_handler) |
| 531 |
|
| 532 |
|
| 533 |
mlockall() |
| 534 |
|
| 535 |
|
| 536 |
if mode == '0': |
| 537 |
|
| 538 |
print_head_0() |
| 539 |
|
| 540 |
while True: |
| 541 |
|
| 542 |
try: |
| 543 |
|
| 544 |
(c_some_avg10, c_some_avg60, c_some_avg300 |
| 545 |
) = psi_file_cpu_to_metrics(cpu_file) |
| 546 |
|
| 547 |
(i_some_avg10, i_some_avg60, i_some_avg300, |
| 548 |
i_full_avg10, i_full_avg60, i_full_avg300 |
| 549 |
) = psi_file_mem_to_metrics(io_file) |
| 550 |
|
| 551 |
(m_some_avg10, m_some_avg60, m_some_avg300, |
| 552 |
m_full_avg10, m_full_avg60, m_full_avg300 |
| 553 |
) = psi_file_mem_to_metrics(memory_file) |
| 554 |
|
| 555 |
except TypeError: |
| 556 |
stdout.flush() |
| 557 |
sleep(interval) |
| 558 |
continue |
| 559 |
|
| 560 |
log('{:>6} {:>6} || {:>6} {:>6} | {:>6} {:>6} || {:>6} {:>6} | {:>6} ' |
| 561 |
'{:>6}'.format( |
| 562 |
|
| 563 |
c_some_avg10, c_some_avg60, |
| 564 |
|
| 565 |
i_some_avg10, i_some_avg60, |
| 566 |
i_full_avg10, i_full_avg60, |
| 567 |
|
| 568 |
m_some_avg10, m_some_avg60, |
| 569 |
m_full_avg10, m_full_avg60 |
| 570 |
|
| 571 |
)) |
| 572 |
|
| 573 |
c_some_avg10 = float(c_some_avg10) |
| 574 |
if ('c_some_avg10' not in peaks_dict or |
| 575 |
peaks_dict['c_some_avg10'] < c_some_avg10): |
| 576 |
peaks_dict['c_some_avg10'] = c_some_avg10 |
| 577 |
|
| 578 |
c_some_avg60 = float(c_some_avg60) |
| 579 |
if ('c_some_avg60' not in peaks_dict or |
| 580 |
peaks_dict['c_some_avg60'] < c_some_avg60): |
| 581 |
peaks_dict['c_some_avg60'] = c_some_avg60 |
| 582 |
|
| 583 |
c_some_avg300 = float(c_some_avg300) |
| 584 |
if ('c_some_avg300' not in peaks_dict or |
| 585 |
peaks_dict['c_some_avg300'] < c_some_avg300): |
| 586 |
peaks_dict['c_some_avg300'] = c_some_avg300 |
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
i_some_avg10 = float(i_some_avg10) |
| 591 |
if ('i_some_avg10' not in peaks_dict or |
| 592 |
peaks_dict['i_some_avg10'] < i_some_avg10): |
| 593 |
peaks_dict['i_some_avg10'] = i_some_avg10 |
| 594 |
|
| 595 |
i_some_avg60 = float(i_some_avg60) |
| 596 |
if ('i_some_avg60' not in peaks_dict or |
| 597 |
peaks_dict['i_some_avg60'] < i_some_avg60): |
| 598 |
peaks_dict['i_some_avg60'] = i_some_avg60 |
| 599 |
|
| 600 |
i_some_avg300 = float(i_some_avg300) |
| 601 |
if ('i_some_avg300' not in peaks_dict or |
| 602 |
peaks_dict['i_some_avg300'] < i_some_avg300): |
| 603 |
peaks_dict['i_some_avg300'] = i_some_avg300 |
| 604 |
|
| 605 |
i_full_avg10 = float(i_full_avg10) |
| 606 |
if ('i_full_avg10' not in peaks_dict or |
| 607 |
peaks_dict['i_full_avg10'] < i_full_avg10): |
| 608 |
peaks_dict['i_full_avg10'] = i_full_avg10 |
| 609 |
|
| 610 |
i_full_avg60 = float(i_full_avg60) |
| 611 |
if ('i_full_avg60' not in peaks_dict or |
| 612 |
peaks_dict['i_full_avg60'] < i_full_avg60): |
| 613 |
peaks_dict['i_full_avg60'] = i_full_avg60 |
| 614 |
|
| 615 |
i_full_avg300 = float(i_full_avg300) |
| 616 |
if ('i_full_avg300' not in peaks_dict or |
| 617 |
peaks_dict['i_full_avg300'] < i_full_avg300): |
| 618 |
peaks_dict['i_full_avg300'] = i_full_avg300 |
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
m_some_avg10 = float(m_some_avg10) |
| 623 |
if ('m_some_avg10' not in peaks_dict or |
| 624 |
peaks_dict['m_some_avg10'] < m_some_avg10): |
| 625 |
peaks_dict['m_some_avg10'] = m_some_avg10 |
| 626 |
|
| 627 |
m_some_avg60 = float(m_some_avg60) |
| 628 |
if ('m_some_avg60' not in peaks_dict or |
| 629 |
peaks_dict['m_some_avg60'] < m_some_avg60): |
| 630 |
peaks_dict['m_some_avg60'] = m_some_avg60 |
| 631 |
|
| 632 |
m_some_avg300 = float(m_some_avg300) |
| 633 |
if ('m_some_avg300' not in peaks_dict or |
| 634 |
peaks_dict['m_some_avg300'] < m_some_avg300): |
| 635 |
peaks_dict['m_some_avg300'] = m_some_avg300 |
| 636 |
|
| 637 |
m_full_avg10 = float(m_full_avg10) |
| 638 |
if ('m_full_avg10' not in peaks_dict or |
| 639 |
peaks_dict['m_full_avg10'] < m_full_avg10): |
| 640 |
peaks_dict['m_full_avg10'] = m_full_avg10 |
| 641 |
|
| 642 |
m_full_avg60 = float(m_full_avg60) |
| 643 |
if ('m_full_avg60' not in peaks_dict or |
| 644 |
peaks_dict['m_full_avg60'] < m_full_avg60): |
| 645 |
peaks_dict['m_full_avg60'] = m_full_avg60 |
| 646 |
|
| 647 |
m_full_avg300 = float(m_full_avg300) |
| 648 |
if ('m_full_avg300' not in peaks_dict or |
| 649 |
peaks_dict['m_full_avg300'] < m_full_avg300): |
| 650 |
peaks_dict['m_full_avg300'] = m_full_avg300 |
| 651 |
|
| 652 |
stdout.flush() |
| 653 |
sleep(interval) |
| 654 |
|
| 655 |
|
| 656 |
if mode == '1': |
| 657 |
|
| 658 |
print_head_1() |
| 659 |
|
| 660 |
while True: |
| 661 |
|
| 662 |
try: |
| 663 |
|
| 664 |
(c_some_avg10, c_some_avg60, c_some_avg300 |
| 665 |
) = psi_file_cpu_to_metrics(cpu_file) |
| 666 |
|
| 667 |
(i_some_avg10, i_some_avg60, i_some_avg300, |
| 668 |
i_full_avg10, i_full_avg60, i_full_avg300 |
| 669 |
) = psi_file_mem_to_metrics(io_file) |
| 670 |
|
| 671 |
(m_some_avg10, m_some_avg60, m_some_avg300, |
| 672 |
m_full_avg10, m_full_avg60, m_full_avg300 |
| 673 |
) = psi_file_mem_to_metrics(memory_file) |
| 674 |
|
| 675 |
except TypeError: |
| 676 |
stdout.flush() |
| 677 |
sleep(interval) |
| 678 |
continue |
| 679 |
|
| 680 |
log('{:>6} {:>6} {:>6} || {:>6} {:>6} {:>6} | {:>6} {:>6} {:>6} || ' |
| 681 |
'{:>6} {:>6} {:>6} | {:>6} {:>6} {:>6}'.format( |
| 682 |
|
| 683 |
c_some_avg10, c_some_avg60, c_some_avg300, |
| 684 |
|
| 685 |
i_some_avg10, i_some_avg60, i_some_avg300, |
| 686 |
i_full_avg10, i_full_avg60, i_full_avg300, |
| 687 |
|
| 688 |
m_some_avg10, m_some_avg60, m_some_avg300, |
| 689 |
m_full_avg10, m_full_avg60, m_full_avg300 |
| 690 |
|
| 691 |
)) |
| 692 |
|
| 693 |
c_some_avg10 = float(c_some_avg10) |
| 694 |
if ('c_some_avg10' not in peaks_dict or |
| 695 |
peaks_dict['c_some_avg10'] < c_some_avg10): |
| 696 |
peaks_dict['c_some_avg10'] = c_some_avg10 |
| 697 |
|
| 698 |
c_some_avg60 = float(c_some_avg60) |
| 699 |
if ('c_some_avg60' not in peaks_dict or |
| 700 |
peaks_dict['c_some_avg60'] < c_some_avg60): |
| 701 |
peaks_dict['c_some_avg60'] = c_some_avg60 |
| 702 |
|
| 703 |
c_some_avg300 = float(c_some_avg300) |
| 704 |
if ('c_some_avg300' not in peaks_dict or |
| 705 |
peaks_dict['c_some_avg300'] < c_some_avg300): |
| 706 |
peaks_dict['c_some_avg300'] = c_some_avg300 |
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
i_some_avg10 = float(i_some_avg10) |
| 711 |
if ('i_some_avg10' not in peaks_dict or |
| 712 |
peaks_dict['i_some_avg10'] < i_some_avg10): |
| 713 |
peaks_dict['i_some_avg10'] = i_some_avg10 |
| 714 |
|
| 715 |
i_some_avg60 = float(i_some_avg60) |
| 716 |
if ('i_some_avg60' not in peaks_dict or |
| 717 |
peaks_dict['i_some_avg60'] < i_some_avg60): |
| 718 |
peaks_dict['i_some_avg60'] = i_some_avg60 |
| 719 |
|
| 720 |
i_some_avg300 = float(i_some_avg300) |
| 721 |
if ('i_some_avg300' not in peaks_dict or |
| 722 |
peaks_dict['i_some_avg300'] < i_some_avg300): |
| 723 |
peaks_dict['i_some_avg300'] = i_some_avg300 |
| 724 |
|
| 725 |
i_full_avg10 = float(i_full_avg10) |
| 726 |
if ('i_full_avg10' not in peaks_dict or |
| 727 |
peaks_dict['i_full_avg10'] < i_full_avg10): |
| 728 |
peaks_dict['i_full_avg10'] = i_full_avg10 |
| 729 |
|
| 730 |
i_full_avg60 = float(i_full_avg60) |
| 731 |
if ('i_full_avg60' not in peaks_dict or |
| 732 |
peaks_dict['i_full_avg60'] < i_full_avg60): |
| 733 |
peaks_dict['i_full_avg60'] = i_full_avg60 |
| 734 |
|
| 735 |
i_full_avg300 = float(i_full_avg300) |
| 736 |
if ('i_full_avg300' not in peaks_dict or |
| 737 |
peaks_dict['i_full_avg300'] < i_full_avg300): |
| 738 |
peaks_dict['i_full_avg300'] = i_full_avg300 |
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
m_some_avg10 = float(m_some_avg10) |
| 743 |
if ('m_some_avg10' not in peaks_dict or |
| 744 |
peaks_dict['m_some_avg10'] < m_some_avg10): |
| 745 |
peaks_dict['m_some_avg10'] = m_some_avg10 |
| 746 |
|
| 747 |
m_some_avg60 = float(m_some_avg60) |
| 748 |
if ('m_some_avg60' not in peaks_dict or |
| 749 |
peaks_dict['m_some_avg60'] < m_some_avg60): |
| 750 |
peaks_dict['m_some_avg60'] = m_some_avg60 |
| 751 |
|
| 752 |
m_some_avg300 = float(m_some_avg300) |
| 753 |
if ('m_some_avg300' not in peaks_dict or |
| 754 |
peaks_dict['m_some_avg300'] < m_some_avg300): |
| 755 |
peaks_dict['m_some_avg300'] = m_some_avg300 |
| 756 |
|
| 757 |
m_full_avg10 = float(m_full_avg10) |
| 758 |
if ('m_full_avg10' not in peaks_dict or |
| 759 |
peaks_dict['m_full_avg10'] < m_full_avg10): |
| 760 |
peaks_dict['m_full_avg10'] = m_full_avg10 |
| 761 |
|
| 762 |
m_full_avg60 = float(m_full_avg60) |
| 763 |
if ('m_full_avg60' not in peaks_dict or |
| 764 |
peaks_dict['m_full_avg60'] < m_full_avg60): |
| 765 |
peaks_dict['m_full_avg60'] = m_full_avg60 |
| 766 |
|
| 767 |
m_full_avg300 = float(m_full_avg300) |
| 768 |
if ('m_full_avg300' not in peaks_dict or |
| 769 |
peaks_dict['m_full_avg300'] < m_full_avg300): |
| 770 |
peaks_dict['m_full_avg300'] = m_full_avg300 |
| 771 |
|
| 772 |
stdout.flush() |
| 773 |
sleep(interval) |
| 774 |
|
| 775 |
|
| 776 |
print_head_2() |
| 777 |
|
| 778 |
try: |
| 779 |
|
| 780 |
total_cs0 = psi_file_cpu_to_total(cpu_file) |
| 781 |
total_is0, total_if0 = psi_file_mem_to_total(io_file) |
| 782 |
total_ms0, total_mf0 = psi_file_mem_to_total(memory_file) |
| 783 |
monotonic0 = monotonic() |
| 784 |
stdout.flush() |
| 785 |
sleep(interval) |
| 786 |
|
| 787 |
except TypeError: |
| 788 |
stdout.flush() |
| 789 |
sleep(interval) |
| 790 |
|
| 791 |
TT = 10000 |
| 792 |
|
| 793 |
while True: |
| 794 |
|
| 795 |
try: |
| 796 |
|
| 797 |
total_cs1 = psi_file_cpu_to_total(cpu_file) |
| 798 |
total_is1, total_if1 = psi_file_mem_to_total(io_file) |
| 799 |
total_ms1, total_mf1 = psi_file_mem_to_total(memory_file) |
| 800 |
monotonic1 = monotonic() |
| 801 |
dm = monotonic1 - monotonic0 |
| 802 |
|
| 803 |
if dm > abnormal_interval and dm - interval > abnormal_inaccuracy: |
| 804 |
log('WARNING: abnormal interval ({} sec), metrics may be prov' |
| 805 |
'ided incorrect'.format(round(dm, 3))) |
| 806 |
|
| 807 |
monotonic0 = monotonic1 |
| 808 |
|
| 809 |
except TypeError: |
| 810 |
stdout.flush() |
| 811 |
sleep(interval) |
| 812 |
continue |
| 813 |
|
| 814 |
dtotal_cs = total_cs1 - total_cs0 |
| 815 |
avg_cs = dtotal_cs / dm / TT |
| 816 |
if 'avg_cs' not in peaks_dict or peaks_dict['avg_cs'] < avg_cs: |
| 817 |
peaks_dict['avg_cs'] = avg_cs |
| 818 |
total_cs0 = total_cs1 |
| 819 |
|
| 820 |
dtotal_is = total_is1 - total_is0 |
| 821 |
avg_is = dtotal_is / dm / TT |
| 822 |
if 'avg_is' not in peaks_dict or peaks_dict['avg_is'] < avg_is: |
| 823 |
peaks_dict['avg_is'] = avg_is |
| 824 |
total_is0 = total_is1 |
| 825 |
|
| 826 |
dtotal_if = total_if1 - total_if0 |
| 827 |
avg_if = dtotal_if / dm / TT |
| 828 |
if 'avg_if' not in peaks_dict or peaks_dict['avg_if'] < avg_if: |
| 829 |
peaks_dict['avg_if'] = avg_if |
| 830 |
total_if0 = total_if1 |
| 831 |
|
| 832 |
dtotal_ms = total_ms1 - total_ms0 |
| 833 |
avg_ms = dtotal_ms / dm / TT |
| 834 |
if 'avg_ms' not in peaks_dict or peaks_dict['avg_ms'] < avg_ms: |
| 835 |
peaks_dict['avg_ms'] = avg_ms |
| 836 |
total_ms0 = total_ms1 |
| 837 |
|
| 838 |
dtotal_mf = total_mf1 - total_mf0 |
| 839 |
avg_mf = dtotal_mf / dm / TT |
| 840 |
if 'avg_mf' not in peaks_dict or peaks_dict['avg_mf'] < avg_mf: |
| 841 |
peaks_dict['avg_mf'] = avg_mf |
| 842 |
total_mf0 = total_mf1 |
| 843 |
|
| 844 |
log('{:>5} | {:>5} {:>5} | {:>5} {:>5} | {}'.format( |
| 845 |
|
| 846 |
round(avg_cs, 1), |
| 847 |
|
| 848 |
round(avg_is, 1), |
| 849 |
round(avg_if, 1), |
| 850 |
|
| 851 |
round(avg_ms, 1), |
| 852 |
round(avg_mf, 1), |
| 853 |
|
| 854 |
round(dm, 2) |
| 855 |
)) |
| 856 |
|
| 857 |
stdout.flush() |
| 858 |
sleep(interval) |