1f08c3bdfSopenharmony_ci#!/usr/bin/env python3
2f08c3bdfSopenharmony_ci''' This Python script interprets various sched stats values.
3f08c3bdfSopenharmony_ci    Validates cpu consolidation for given sched_mc_power_saving value
4f08c3bdfSopenharmony_ci'''
5f08c3bdfSopenharmony_ci
6f08c3bdfSopenharmony_ciimport os
7f08c3bdfSopenharmony_ciimport sys
8f08c3bdfSopenharmony_ciimport time
9f08c3bdfSopenharmony_cifrom optparse import OptionParser
10f08c3bdfSopenharmony_cifrom pm_sched_mc import *
11f08c3bdfSopenharmony_ci
12f08c3bdfSopenharmony_ci__author__ = "Poornima Nayak <mpnayak@linux.vnet.ibm.com>"
13f08c3bdfSopenharmony_ci
14f08c3bdfSopenharmony_ciclass Usage(Exception):
15f08c3bdfSopenharmony_ci    def __init__(self, msg):
16f08c3bdfSopenharmony_ci        self.msg = msg
17f08c3bdfSopenharmony_ci
18f08c3bdfSopenharmony_cidef main(argv=None):
19f08c3bdfSopenharmony_ci    if argv is None:
20f08c3bdfSopenharmony_ci        argv = sys.argv
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_ci    usage = "-w"
23f08c3bdfSopenharmony_ci    parser = OptionParser(usage)
24f08c3bdfSopenharmony_ci    parser.add_option("-v", "--variation_test", dest="vary_mc_smt",
25f08c3bdfSopenharmony_ci        default=False, action="store_true", help="Vary sched_mc & sched_smt. \
26f08c3bdfSopenharmony_ci            -c and -t inputs are initial value of sched_mc & sched_smt")
27f08c3bdfSopenharmony_ci    parser.add_option("-c", "--mc_value", dest="mc_value",
28f08c3bdfSopenharmony_ci        default=0, help="Sched mc power saving value 0/1/2")
29f08c3bdfSopenharmony_ci    parser.add_option("-t", "--smt_value", dest="smt_value",
30f08c3bdfSopenharmony_ci        default=0, help="Sched smt power saving value 0/1/2")
31f08c3bdfSopenharmony_ci    parser.add_option("-w", "--workload", dest="work_ld",
32f08c3bdfSopenharmony_ci        default="ebizzy", help="Workload can be ebizzy/kernbench")
33f08c3bdfSopenharmony_ci    parser.add_option("-s", "--stress", dest="stress",
34f08c3bdfSopenharmony_ci        default="partial", help="Load on system is full/partial [i.e 50%]/thread")
35f08c3bdfSopenharmony_ci    parser.add_option("-p", "--performance", dest="perf_test",
36f08c3bdfSopenharmony_ci        default=False, action="store_true", help="Enable performance test")
37f08c3bdfSopenharmony_ci    (options, args) = parser.parse_args()
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_ci    try:
40f08c3bdfSopenharmony_ci        count_num_cpu()
41f08c3bdfSopenharmony_ci        count_num_sockets()
42f08c3bdfSopenharmony_ci        if is_hyper_threaded():
43f08c3bdfSopenharmony_ci            generate_sibling_list()
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_ci        # User should set option -v to test cpu consolidation
46f08c3bdfSopenharmony_ci        # resets when sched_mc &(/) sched_smt is disabled when
47f08c3bdfSopenharmony_ci        # workload is already running in the system
48f08c3bdfSopenharmony_ci
49f08c3bdfSopenharmony_ci        if options.vary_mc_smt:
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_ci            # Since same code is used for testing package consolidation and core
52f08c3bdfSopenharmony_ci            # consolidation is_multi_socket & is_hyper_threaded check is done
53f08c3bdfSopenharmony_ci            if is_multi_socket() and is_multi_core() and options.mc_value:
54f08c3bdfSopenharmony_ci                set_sched_mc_power(options.mc_value)
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_ci            if is_hyper_threaded() and options.smt_value:
57f08c3bdfSopenharmony_ci                set_sched_smt_power(options.smt_value)
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_ci            #Generate arguments for trigger workload, run workload in background
60f08c3bdfSopenharmony_ci            map_cpuid_pkgid()
61f08c3bdfSopenharmony_ci            background="yes"
62f08c3bdfSopenharmony_ci            duration=360
63f08c3bdfSopenharmony_ci            pinned="no"
64f08c3bdfSopenharmony_ci            if int(options.mc_value) < 2 and int(options.smt_value) < 2:
65f08c3bdfSopenharmony_ci                trigger_ebizzy (options.smt_value, "partial", duration, background, pinned)
66f08c3bdfSopenharmony_ci                work_ld="ebizzy"
67f08c3bdfSopenharmony_ci                #Wait for 120 seconds and then validate cpu consolidation works
68f08c3bdfSopenharmony_ci                #When sched_mc & sched_smt is set
69f08c3bdfSopenharmony_ci                import time
70f08c3bdfSopenharmony_ci                time.sleep(120)
71f08c3bdfSopenharmony_ci            else:
72f08c3bdfSopenharmony_ci                #Wait for 120 seconds and then validate cpu consolidation works
73f08c3bdfSopenharmony_ci                #When sched_mc & sched_smt is set
74f08c3bdfSopenharmony_ci                trigger_kernbench (options.smt_value, "partial", background, pinned, "no")
75f08c3bdfSopenharmony_ci                work_ld="kernbench"
76f08c3bdfSopenharmony_ci                import time
77f08c3bdfSopenharmony_ci                time.sleep(300)
78f08c3bdfSopenharmony_ci
79f08c3bdfSopenharmony_ci            generate_report()
80f08c3bdfSopenharmony_ci            status = validate_cpu_consolidation("partial", work_ld, options.mc_value, options.smt_value)
81f08c3bdfSopenharmony_ci            if status == 0:
82f08c3bdfSopenharmony_ci                print("INFO: Consolidation worked sched_smt &(/) sched_mc is set")
83f08c3bdfSopenharmony_ci                #Disable sched_smt & sched_mc interface values
84f08c3bdfSopenharmony_ci                if options.vary_mc_smt and options.mc_value > 0:
85f08c3bdfSopenharmony_ci                    set_sched_mc_power(0)
86f08c3bdfSopenharmony_ci                    mc_value = options.mc_value
87f08c3bdfSopenharmony_ci                else:
88f08c3bdfSopenharmony_ci                    mc_value = 0
89f08c3bdfSopenharmony_ci                if options.vary_mc_smt and options.smt_value > 0 and is_hyper_threaded():
90f08c3bdfSopenharmony_ci                    set_sched_smt_power(0)
91f08c3bdfSopenharmony_ci                    smt_value = options.smt_value
92f08c3bdfSopenharmony_ci                else:
93f08c3bdfSopenharmony_ci                    smt_value = 0
94f08c3bdfSopenharmony_ci
95f08c3bdfSopenharmony_ci                if work_ld == "kernbench":
96f08c3bdfSopenharmony_ci                    time.sleep(240)
97f08c3bdfSopenharmony_ci                else:
98f08c3bdfSopenharmony_ci                    time.sleep(120)
99f08c3bdfSopenharmony_ci
100f08c3bdfSopenharmony_ci                generate_report()
101f08c3bdfSopenharmony_ci                status = validate_cpu_consolidation("partial", work_ld, mc_value, smt_value)
102f08c3bdfSopenharmony_ci                if background == "yes":
103f08c3bdfSopenharmony_ci                    stop_wkld(work_ld)
104f08c3bdfSopenharmony_ci                #CPU consolidation should fail as sched_mc &(/) sched_smt is disabled
105f08c3bdfSopenharmony_ci                if status == 1:
106f08c3bdfSopenharmony_ci                    return(0)
107f08c3bdfSopenharmony_ci                else:
108f08c3bdfSopenharmony_ci                    return(1)
109f08c3bdfSopenharmony_ci            else:
110f08c3bdfSopenharmony_ci                print("INFO: CPU consolidation failed when sched_mc &(/) \
111f08c3bdfSopenharmony_cisched_smt was enabled. This is pre-requisite to proceed")
112f08c3bdfSopenharmony_ci                return(status)
113f08c3bdfSopenharmony_ci        else:
114f08c3bdfSopenharmony_ci            #The else part of the code validates behaviour of sched_mc
115f08c3bdfSopenharmony_ci            # and sched_smt set to 0, 1 & 2
116f08c3bdfSopenharmony_ci            if is_multi_socket():
117f08c3bdfSopenharmony_ci                set_sched_mc_power(options.mc_value)
118f08c3bdfSopenharmony_ci            if is_hyper_threaded():
119f08c3bdfSopenharmony_ci                set_sched_smt_power(options.smt_value)
120f08c3bdfSopenharmony_ci            map_cpuid_pkgid()
121f08c3bdfSopenharmony_ci            print("INFO: Created table mapping cpu to package")
122f08c3bdfSopenharmony_ci            background="no"
123f08c3bdfSopenharmony_ci            duration=60
124f08c3bdfSopenharmony_ci            pinned ="no"
125f08c3bdfSopenharmony_ci
126f08c3bdfSopenharmony_ci            if options.perf_test:
127f08c3bdfSopenharmony_ci                perf_test="yes"
128f08c3bdfSopenharmony_ci            else:
129f08c3bdfSopenharmony_ci                perf_test="no"
130f08c3bdfSopenharmony_ci
131f08c3bdfSopenharmony_ci            trigger_workld( options.smt_value, options.work_ld, options.stress, duration, background, pinned, perf_test)
132f08c3bdfSopenharmony_ci            generate_report()
133f08c3bdfSopenharmony_ci            status = validate_cpu_consolidation(options.stress, options.work_ld,options.mc_value, options.smt_value)
134f08c3bdfSopenharmony_ci            reset_schedmc()
135f08c3bdfSopenharmony_ci            if is_hyper_threaded():
136f08c3bdfSopenharmony_ci                reset_schedsmt()
137f08c3bdfSopenharmony_ci            return(status)
138f08c3bdfSopenharmony_ci    except Exception as details:
139f08c3bdfSopenharmony_ci        print("INFO: CPU consolidation failed", details)
140f08c3bdfSopenharmony_ci        return(1)
141f08c3bdfSopenharmony_ci
142f08c3bdfSopenharmony_ciif __name__ == "__main__":
143f08c3bdfSopenharmony_ci    sys.exit(main())
144