<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Test environment setup
Step1:  Attributes
The initialization of the test environment pre-initialize some useful<br>
environment variables which are available to write test cases.
These are some of the information available via the TestEnv object.
Step2:  Functions
Some methods are also exposed to test developers which could be used to easy
the creation of tests.
These are some of the methods available
Step3:  A special TestEnv attribute is <b>target</b>, which represent a <b>devlib instance</b>.<br>
Using the target attribute we can access to the full set of devlib provided<br>
functionalities. Which are summarized in the following sections.
Access to the devlib API
Step4:  Sample energy from the target
Step5:  Configure FTrace for a sepcific experiment
<ASSISTANT_TASK:>
Python Code:
# Setup a target configuration
conf = {
    # Platform and board to target
    "platform"    : "linux",
    "board"       : "juno",
    # Login credentials
    "host"        : "192.168.0.1",
    "username"    : "root",
    "password"    : "",
    # Local installation path
    "tftp"  : {
        "folder"    : "/var/lib/tftpboot",
        "kernel"    : "kern.bin",
        "dtb"       : "dtb.bin",
    },
    # Tools to deploy
    "tools" : [ "rt-app", "taskset" ],
    # RTApp calibration values (comment to let LISA do a calibration run)
    "rtapp-calib" :  {
        "0": 358, "1": 138, "2": 138, "3": 357, "4": 359, "5": 355
    },
    # FTrace configuration
    "ftrace" : {
         "events" : [
             "cpu_idle",
             "sched_switch",
         ],
         "buffsize" : 10240,
    },
    
    # Where results are collected
    "results_dir" : "TestEnvExample",
    
    # Tune which devlib module are required
    #"exclude_modules" : [ "hwmon" ],
}
from env import TestEnv
# Initialize a test environment using the provided configuration
te = TestEnv(conf)
Explanation: Test environment setup
End of explanation
# The complete configuration of the target we have configured
print json.dumps(te.conf, indent=4)
# Last configured kernel and DTB image
print te.kernel
print te.dtb
# The IP and MAC address of the target
print te.ip
print te.mac
# A full platform descriptor
print json.dumps(te.platform, indent=4)
# A pre-created folder to host the tests results generated using this
# test environment, notice that the suite could add additional information
# in this folder, like for example a copy of the target configuration
# and other target specific collected information
te.res_dir
# The working directory on the target
te.workdir
# The target topology, which can be used to build BART assertions
te.topology
Explanation: Attributes
The initialization of the test environment pre-initialize some useful<br>
environment variables which are available to write test cases.
These are some of the information available via the TestEnv object.
End of explanation
# Calibrate RT-App (if required) and get the most updated calibration value
te.calibration()
# Generate a JSON file with the complete platform description
te.platform_dump(dest_dir='/tmp')
# Force a reboot of the target (and wait specified [s] before reconnect)
te.reboot(reboot_time=60, ping_time=15)
# Resolve a MAC address into an IP address
te.resolv_host(host='00:02:F7:00:5A:5B')
# Copy the specified file into the TFTP server folder defined by configuration
te.tftp_deploy('/etc/group')
Explanation: Functions
Some methods are also exposed to test developers which could be used to easy
the creation of tests.
These are some of the methods available:
End of explanation
# Run a command on the target
te.target.execute("echo -n 'Hello Test Environment'", as_root=False)
# Spawn a command in background on the target
te.target.kick_off("sleep 10", as_root=True)
# Acces to many target specific information
print "ABI                 : ", te.target.abi
print "big Core Family     : ", te.target.big_core
print "LITTLE Core Family  : ", te.target.little_core
print "CPU's Clusters IDs  : ", te.target.core_clusters
print "CPUs type           : ", te.target.core_names
# Access to big.LITTLE specific information
print "big CPUs IDs        : ", te.target.bl.bigs
print "LITTLE CPUs IDs     : ", te.target.bl.littles
print "big CPUs freqs      : {}".format(te.target.bl.get_bigs_frequency())
print "big CPUs governor   : {}".format(te.target.bl.get_bigs_governor())
Explanation: A special TestEnv attribute is <b>target</b>, which represent a <b>devlib instance</b>.<br>
Using the target attribute we can access to the full set of devlib provided<br>
functionalities. Which are summarized in the following sections.
Access to the devlib API
End of explanation
# Reset and sample energy counters
te.emeter.reset()
nrg = te.emeter.sample()
nrg = json.dumps(te.emeter.sample(), indent=4)
print "First read: ", nrg
time.sleep(2)
nrg = te.emeter.sample()
nrg = json.dumps(te.emeter.sample(), indent=4)
print "Second read: ", nrg
Explanation: Sample energy from the target
End of explanation
# Configure a specific set of events to trace
te.ftrace_conf(
    {                                                                                                                                             
         "events" : [                                                                                                                                            
             "cpu_idle",                                                                                                                                         
             "cpu_capacity",
             "cpu_frequency",
             "sched_switch",
         ],                                                                                                                                                      
         "buffsize" : 10240                                                                                                                                      
    }
)
# Start/Stop a FTrace session
te.ftrace.start()
te.target.execute("uname -a")
te.ftrace.stop()
# Collect and visualize the trace
trace_file = os.path.join(te.res_dir, 'trace.dat')
te.ftrace.get_trace(trace_file)
output = os.popen("DISPLAY=:0.0 kernelshark {}".format(trace_file))
Explanation: Configure FTrace for a sepcific experiment
End of explanation
<END_TASK>