Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tools/power/x86/intel_pstate_tracer: Add optional setting of trace buffer memory allocation

Allow the user to override the default trace buffer memory allocation
by adding a command line option to override the default.

The patch also:

Adds a SIGINT (i.e. CTRL C exit) handler,
so that things can be cleaned up before exit.

Moves the postion of some other cleanup from after to
before the potential "No valid data to plot" exit.

Replaces all quit() calls with sys.exit, because
quit() is not supposed to be used in scripts.

Signed-off-by: Doug Smythies <dsmythies@telus.net>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>

authored by

Doug Smythies and committed by
Rafael J. Wysocki
35459105 67b8d5c7

+35 -19
+35 -19
tools/power/x86/intel_pstate_tracer/intel_pstate_tracer.py
··· 28 28 import os 29 29 import time 30 30 import re 31 + import signal 31 32 import sys 32 33 import getopt 33 34 import Gnuplot ··· 79 78 print(' Or') 80 79 print(' ./intel_pstate_tracer.py [--cpu cpus] ---trace_file <trace_file> --name <test_name>') 81 80 print(' To generate trace file, parse and plot, use (sudo required):') 82 - print(' sudo ./intel_pstate_tracer.py [-c cpus] -i <interval> -n <test_name>') 81 + print(' sudo ./intel_pstate_tracer.py [-c cpus] -i <interval> -n <test_name> -m <kbytes>') 83 82 print(' Or') 84 - print(' sudo ./intel_pstate_tracer.py [--cpu cpus] --interval <interval> --name <test_name>') 83 + print(' sudo ./intel_pstate_tracer.py [--cpu cpus] --interval <interval> --name <test_name> --memory <kbytes>') 85 84 print(' Optional argument:') 86 - print(' cpus: comma separated list of CPUs') 85 + print(' cpus: comma separated list of CPUs') 86 + print(' kbytes: Kilo bytes of memory per CPU to allocate to the trace buffer. Default: 10240') 87 87 print(' Output:') 88 88 print(' If not already present, creates a "results/test_name" folder in the current working directory with:') 89 89 print(' cpu.csv - comma seperated values file with trace contents and some additional calculations.') ··· 381 379 f_handle.close() 382 380 except: 383 381 print('IO error clearing trace file ') 384 - quit() 382 + sys.exit(2) 385 383 386 384 def enable_trace(): 387 385 """ Enable trace """ ··· 391 389 , 'w').write("1") 392 390 except: 393 391 print('IO error enabling trace ') 394 - quit() 392 + sys.exit(2) 395 393 396 394 def disable_trace(): 397 395 """ Disable trace """ ··· 401 399 , 'w').write("0") 402 400 except: 403 401 print('IO error disabling trace ') 404 - quit() 402 + sys.exit(2) 405 403 406 404 def set_trace_buffer_size(): 407 405 """ Set trace buffer size """ 408 406 409 407 try: 410 - open('/sys/kernel/debug/tracing/buffer_size_kb' 411 - , 'w').write("10240") 408 + with open('/sys/kernel/debug/tracing/buffer_size_kb', 'w') as fp: 409 + fp.write(memory) 412 410 except: 413 - print('IO error setting trace buffer size ') 414 - quit() 411 + print('IO error setting trace buffer size ') 412 + sys.exit(2) 415 413 416 414 def free_trace_buffer(): 417 415 """ Free the trace buffer memory """ ··· 420 418 open('/sys/kernel/debug/tracing/buffer_size_kb' 421 419 , 'w').write("1") 422 420 except: 423 - print('IO error setting trace buffer size ') 424 - quit() 421 + print('IO error freeing trace buffer ') 422 + sys.exit(2) 425 423 426 424 def read_trace_data(filename): 427 425 """ Read and parse trace data """ ··· 433 431 data = open(filename, 'r').read() 434 432 except: 435 433 print('Error opening ', filename) 436 - quit() 434 + sys.exit(2) 437 435 438 436 for line in data.splitlines(): 439 437 search_obj = \ ··· 491 489 # Now seperate the main overall csv file into per CPU csv files. 492 490 split_csv() 493 491 492 + def signal_handler(signal, frame): 493 + print(' SIGINT: Forcing cleanup before exit.') 494 + if interval: 495 + disable_trace() 496 + clear_trace_file() 497 + # Free the memory 498 + free_trace_buffer() 499 + sys.exit(0) 500 + 501 + signal.signal(signal.SIGINT, signal_handler) 502 + 494 503 interval = "" 495 504 filename = "" 496 505 cpu_list = "" 497 506 testname = "" 507 + memory = "10240" 498 508 graph_data_present = False; 499 509 500 510 valid1 = False ··· 515 501 cpu_mask = zeros((MAX_CPUS,), dtype=int) 516 502 517 503 try: 518 - opts, args = getopt.getopt(sys.argv[1:],"ht:i:c:n:",["help","trace_file=","interval=","cpu=","name="]) 504 + opts, args = getopt.getopt(sys.argv[1:],"ht:i:c:n:m:",["help","trace_file=","interval=","cpu=","name=","memory="]) 519 505 except getopt.GetoptError: 520 506 print_help() 521 507 sys.exit(2) ··· 535 521 elif opt in ("-n", "--name"): 536 522 valid2 = True 537 523 testname = arg 524 + elif opt in ("-m", "--memory"): 525 + memory = arg 538 526 539 527 if not (valid1 and valid2): 540 528 print_help() ··· 585 569 586 570 read_trace_data(filename) 587 571 572 + clear_trace_file() 573 + # Free the memory 574 + if interval: 575 + free_trace_buffer() 576 + 588 577 if graph_data_present == False: 589 578 print('No valid data to plot') 590 579 sys.exit(2) ··· 613 592 for root, dirs, files in os.walk('.'): 614 593 for f in files: 615 594 fix_ownership(f) 616 - 617 - clear_trace_file() 618 - # Free the memory 619 - if interval: 620 - free_trace_buffer() 621 595 622 596 os.chdir('../../')