Skip to content

Commit ab3ff9f

Browse files
JinzhouSurafaeljw
authored andcommitted
tools/power/x86/intel_pstate_tracer: make tracer as a module
Make intel_pstate_tracer as a module. Other trace event can import this module to analyze their trace data. Signed-off-by: Jinzhou Su <Jinzhou.Su@amd.com> Acked-by: Doug Smythies <dsmythies@telus.net> Reviewed-by: Huang Rui <ray.huang@amd.com> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
1 parent 23c296f commit ab3ff9f

1 file changed

Lines changed: 129 additions & 131 deletions

File tree

tools/power/x86/intel_pstate_tracer/intel_pstate_tracer.py

Lines changed: 129 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
C_SEC = 2
6464
C_CPU = 1
6565

66-
global sample_num, last_sec_cpu, last_usec_cpu, start_time, testname
66+
global sample_num, last_sec_cpu, last_usec_cpu, start_time, testname, trace_file
6767

6868
# 11 digits covers uptime to 115 days
6969
getcontext().prec = 11
@@ -72,17 +72,17 @@
7272
last_sec_cpu = [0] * MAX_CPUS
7373
last_usec_cpu = [0] * MAX_CPUS
7474

75-
def print_help():
76-
print('intel_pstate_tracer.py:')
75+
def print_help(driver_name):
76+
print('%s_tracer.py:'%driver_name)
7777
print(' Usage:')
7878
print(' If the trace file is available, then to simply parse and plot, use (sudo not required):')
79-
print(' ./intel_pstate_tracer.py [-c cpus] -t <trace_file> -n <test_name>')
79+
print(' ./%s_tracer.py [-c cpus] -t <trace_file> -n <test_name>'%driver_name)
8080
print(' Or')
81-
print(' ./intel_pstate_tracer.py [--cpu cpus] ---trace_file <trace_file> --name <test_name>')
81+
print(' ./%s_tracer.py [--cpu cpus] ---trace_file <trace_file> --name <test_name>'%driver_name)
8282
print(' To generate trace file, parse and plot, use (sudo required):')
83-
print(' sudo ./intel_pstate_tracer.py [-c cpus] -i <interval> -n <test_name> -m <kbytes>')
83+
print(' sudo ./%s_tracer.py [-c cpus] -i <interval> -n <test_name> -m <kbytes>'%driver_name)
8484
print(' Or')
85-
print(' sudo ./intel_pstate_tracer.py [--cpu cpus] --interval <interval> --name <test_name> --memory <kbytes>')
85+
print(' sudo ./%s_tracer.py [--cpu cpus] --interval <interval> --name <test_name> --memory <kbytes>'%driver_name)
8686
print(' Optional argument:')
8787
print(' cpus: comma separated list of CPUs')
8888
print(' kbytes: Kilo bytes of memory per CPU to allocate to the trace buffer. Default: 10240')
@@ -323,7 +323,7 @@ def set_4_plot_linestyles(g_plot):
323323
g_plot('set style line 3 linetype 1 linecolor rgb "purple" pointtype -1')
324324
g_plot('set style line 4 linetype 1 linecolor rgb "blue" pointtype -1')
325325

326-
def store_csv(cpu_int, time_pre_dec, time_post_dec, core_busy, scaled, _from, _to, mperf, aperf, tsc, freq_ghz, io_boost, common_comm, load, duration_ms, sample_num, elapsed_time, tsc_ghz):
326+
def store_csv(cpu_int, time_pre_dec, time_post_dec, core_busy, scaled, _from, _to, mperf, aperf, tsc, freq_ghz, io_boost, common_comm, load, duration_ms, sample_num, elapsed_time, tsc_ghz, cpu_mask):
327327
""" Store master csv file information """
328328

329329
global graph_data_present
@@ -342,11 +342,9 @@ def store_csv(cpu_int, time_pre_dec, time_post_dec, core_busy, scaled, _from, _t
342342

343343
graph_data_present = True;
344344

345-
def split_csv():
345+
def split_csv(current_max_cpu, cpu_mask):
346346
""" seperate the all csv file into per CPU csv files. """
347347

348-
global current_max_cpu
349-
350348
if os.path.exists('cpu.csv'):
351349
for index in range(0, current_max_cpu + 1):
352350
if cpu_mask[int(index)] != 0:
@@ -381,27 +379,25 @@ def clear_trace_file():
381379
print('IO error clearing trace file ')
382380
sys.exit(2)
383381

384-
def enable_trace():
382+
def enable_trace(trace_file):
385383
""" Enable trace """
386384

387385
try:
388-
open('/sys/kernel/debug/tracing/events/power/pstate_sample/enable'
389-
, 'w').write("1")
386+
open(trace_file,'w').write("1")
390387
except:
391388
print('IO error enabling trace ')
392389
sys.exit(2)
393390

394-
def disable_trace():
391+
def disable_trace(trace_file):
395392
""" Disable trace """
396393

397394
try:
398-
open('/sys/kernel/debug/tracing/events/power/pstate_sample/enable'
399-
, 'w').write("0")
395+
open(trace_file, 'w').write("0")
400396
except:
401397
print('IO error disabling trace ')
402398
sys.exit(2)
403399

404-
def set_trace_buffer_size():
400+
def set_trace_buffer_size(memory):
405401
""" Set trace buffer size """
406402

407403
try:
@@ -421,7 +417,7 @@ def free_trace_buffer():
421417
print('IO error freeing trace buffer ')
422418
sys.exit(2)
423419

424-
def read_trace_data(filename):
420+
def read_trace_data(filename, cpu_mask):
425421
""" Read and parse trace data """
426422

427423
global current_max_cpu
@@ -481,135 +477,137 @@ def read_trace_data(filename):
481477
tsc_ghz = Decimal(0)
482478
if duration_ms != Decimal(0) :
483479
tsc_ghz = Decimal(tsc)/duration_ms/Decimal(1000000)
484-
store_csv(cpu_int, time_pre_dec, time_post_dec, core_busy, scaled, _from, _to, mperf, aperf, tsc, freq_ghz, io_boost, common_comm, load, duration_ms, sample_num, elapsed_time, tsc_ghz)
480+
store_csv(cpu_int, time_pre_dec, time_post_dec, core_busy, scaled, _from, _to, mperf, aperf, tsc, freq_ghz, io_boost, common_comm, load, duration_ms, sample_num, elapsed_time, tsc_ghz, cpu_mask)
485481

486482
if cpu_int > current_max_cpu:
487483
current_max_cpu = cpu_int
488484
# End of for each trace line loop
489485
# Now seperate the main overall csv file into per CPU csv files.
490-
split_csv()
486+
split_csv(current_max_cpu, cpu_mask)
491487

492488
def signal_handler(signal, frame):
493489
print(' SIGINT: Forcing cleanup before exit.')
494490
if interval:
495-
disable_trace()
491+
disable_trace(trace_file)
496492
clear_trace_file()
497493
# Free the memory
498494
free_trace_buffer()
499495
sys.exit(0)
500496

501-
signal.signal(signal.SIGINT, signal_handler)
497+
if __name__ == "__main__":
498+
trace_file = "/sys/kernel/debug/tracing/events/power/pstate_sample/enable"
499+
signal.signal(signal.SIGINT, signal_handler)
502500

503-
interval = ""
504-
filename = ""
505-
cpu_list = ""
506-
testname = ""
507-
memory = "10240"
508-
graph_data_present = False;
501+
interval = ""
502+
filename = ""
503+
cpu_list = ""
504+
testname = ""
505+
memory = "10240"
506+
graph_data_present = False;
509507

510-
valid1 = False
511-
valid2 = False
508+
valid1 = False
509+
valid2 = False
512510

513-
cpu_mask = zeros((MAX_CPUS,), dtype=int)
511+
cpu_mask = zeros((MAX_CPUS,), dtype=int)
514512

515-
try:
516-
opts, args = getopt.getopt(sys.argv[1:],"ht:i:c:n:m:",["help","trace_file=","interval=","cpu=","name=","memory="])
517-
except getopt.GetoptError:
518-
print_help()
519-
sys.exit(2)
520-
for opt, arg in opts:
521-
if opt == '-h':
522-
print()
513+
try:
514+
opts, args = getopt.getopt(sys.argv[1:],"ht:i:c:n:m:",["help","trace_file=","interval=","cpu=","name=","memory="])
515+
except getopt.GetoptError:
516+
print_help('intel_pstate')
517+
sys.exit(2)
518+
for opt, arg in opts:
519+
if opt == '-h':
520+
print_help('intel_pstate')
521+
sys.exit()
522+
elif opt in ("-t", "--trace_file"):
523+
valid1 = True
524+
location = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))
525+
filename = os.path.join(location, arg)
526+
elif opt in ("-i", "--interval"):
527+
valid1 = True
528+
interval = arg
529+
elif opt in ("-c", "--cpu"):
530+
cpu_list = arg
531+
elif opt in ("-n", "--name"):
532+
valid2 = True
533+
testname = arg
534+
elif opt in ("-m", "--memory"):
535+
memory = arg
536+
537+
if not (valid1 and valid2):
538+
print_help('intel_pstate')
523539
sys.exit()
524-
elif opt in ("-t", "--trace_file"):
525-
valid1 = True
526-
location = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))
527-
filename = os.path.join(location, arg)
528-
elif opt in ("-i", "--interval"):
529-
valid1 = True
530-
interval = arg
531-
elif opt in ("-c", "--cpu"):
532-
cpu_list = arg
533-
elif opt in ("-n", "--name"):
534-
valid2 = True
535-
testname = arg
536-
elif opt in ("-m", "--memory"):
537-
memory = arg
538-
539-
if not (valid1 and valid2):
540-
print_help()
541-
sys.exit()
542-
543-
if cpu_list:
544-
for p in re.split("[,]", cpu_list):
545-
if int(p) < MAX_CPUS :
546-
cpu_mask[int(p)] = 1
547-
else:
548-
for i in range (0, MAX_CPUS):
549-
cpu_mask[i] = 1
550-
551-
if not os.path.exists('results'):
552-
os.mkdir('results')
540+
541+
if cpu_list:
542+
for p in re.split("[,]", cpu_list):
543+
if int(p) < MAX_CPUS :
544+
cpu_mask[int(p)] = 1
545+
else:
546+
for i in range (0, MAX_CPUS):
547+
cpu_mask[i] = 1
548+
549+
if not os.path.exists('results'):
550+
os.mkdir('results')
551+
# The regular user needs to own the directory, not root.
552+
fix_ownership('results')
553+
554+
os.chdir('results')
555+
if os.path.exists(testname):
556+
print('The test name directory already exists. Please provide a unique test name. Test re-run not supported, yet.')
557+
sys.exit()
558+
os.mkdir(testname)
553559
# The regular user needs to own the directory, not root.
554-
fix_ownership('results')
555-
556-
os.chdir('results')
557-
if os.path.exists(testname):
558-
print('The test name directory already exists. Please provide a unique test name. Test re-run not supported, yet.')
559-
sys.exit()
560-
os.mkdir(testname)
561-
# The regular user needs to own the directory, not root.
562-
fix_ownership(testname)
563-
os.chdir(testname)
564-
565-
# Temporary (or perhaps not)
566-
cur_version = sys.version_info
567-
print('python version (should be >= 2.7):')
568-
print(cur_version)
569-
570-
# Left as "cleanup" for potential future re-run ability.
571-
cleanup_data_files()
572-
573-
if interval:
574-
filename = "/sys/kernel/debug/tracing/trace"
575-
clear_trace_file()
576-
set_trace_buffer_size()
577-
enable_trace()
578-
print('Sleeping for ', interval, 'seconds')
579-
time.sleep(int(interval))
580-
disable_trace()
581-
582-
current_max_cpu = 0
583-
584-
read_trace_data(filename)
585-
586-
if interval:
587-
clear_trace_file()
588-
# Free the memory
589-
free_trace_buffer()
590-
591-
if graph_data_present == False:
592-
print('No valid data to plot')
593-
sys.exit(2)
594-
595-
for cpu_no in range(0, current_max_cpu + 1):
596-
plot_perf_busy_with_sample(cpu_no)
597-
plot_perf_busy(cpu_no)
598-
plot_durations(cpu_no)
599-
plot_loads(cpu_no)
600-
601-
plot_pstate_cpu_with_sample()
602-
plot_pstate_cpu()
603-
plot_load_cpu()
604-
plot_frequency_cpu()
605-
plot_duration_cpu()
606-
plot_scaled_cpu()
607-
plot_boost_cpu()
608-
plot_ghz_cpu()
609-
610-
# It is preferrable, but not necessary, that the regular user owns the files, not root.
611-
for root, dirs, files in os.walk('.'):
612-
for f in files:
613-
fix_ownership(f)
614-
615-
os.chdir('../../')
560+
fix_ownership(testname)
561+
os.chdir(testname)
562+
563+
# Temporary (or perhaps not)
564+
cur_version = sys.version_info
565+
print('python version (should be >= 2.7):')
566+
print(cur_version)
567+
568+
# Left as "cleanup" for potential future re-run ability.
569+
cleanup_data_files()
570+
571+
if interval:
572+
filename = "/sys/kernel/debug/tracing/trace"
573+
clear_trace_file()
574+
set_trace_buffer_size(memory)
575+
enable_trace(trace_file)
576+
print('Sleeping for ', interval, 'seconds')
577+
time.sleep(int(interval))
578+
disable_trace(trace_file)
579+
580+
current_max_cpu = 0
581+
582+
read_trace_data(filename, cpu_mask)
583+
584+
if interval:
585+
clear_trace_file()
586+
# Free the memory
587+
free_trace_buffer()
588+
589+
if graph_data_present == False:
590+
print('No valid data to plot')
591+
sys.exit(2)
592+
593+
for cpu_no in range(0, current_max_cpu + 1):
594+
plot_perf_busy_with_sample(cpu_no)
595+
plot_perf_busy(cpu_no)
596+
plot_durations(cpu_no)
597+
plot_loads(cpu_no)
598+
599+
plot_pstate_cpu_with_sample()
600+
plot_pstate_cpu()
601+
plot_load_cpu()
602+
plot_frequency_cpu()
603+
plot_duration_cpu()
604+
plot_scaled_cpu()
605+
plot_boost_cpu()
606+
plot_ghz_cpu()
607+
608+
# It is preferrable, but not necessary, that the regular user owns the files, not root.
609+
for root, dirs, files in os.walk('.'):
610+
for f in files:
611+
fix_ownership(f)
612+
613+
os.chdir('../../')

0 commit comments

Comments
 (0)