1#    Copyright 2015-2017 ARM Limited
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14#
15
16
17import os
18import pandas as pd
19import re
20
21class SysfsExtractor(object):
22    """Operate on the parameters of a dump of Workload Automation's
23       sysfs extractor instrumentation.
24
25    :param path: The path to the workload in a output directory created by
26        WA.
27    :type path: str
28    """
29
30    def __init__(self, path):
31        self.thermal_path = os.path.join(path, "after", "sys", "devices",
32                                         "virtual", "thermal", "thermal_zone0")
33        self.properties = ["integral_cutoff", "k_d", "k_i", "k_po", "k_pu",
34                           "policy", "sustainable_power"]
35
36        try:
37            sysfs_files = os.listdir(self.thermal_path)
38        except OSError:
39            sysfs_files = []
40
41        for fname in sysfs_files:
42            if re.search(r"cdev\d+_weight", fname):
43                self.properties.append(fname)
44            elif re.search(r"trip_point_\d+_temp", fname):
45                self.properties.append(fname)
46
47    def get_parameters(self):
48        """Get the parameters from a sysfs extractor dump
49
50        WorkloadAutomation (WA) can dump sysfs values using its
51        sysfs_extractor instrumentation.  Parse the tree and return the
52        thermal parameters as a dict of key and values where the keys are
53        the names of the files and values its corresponding values.
54
55        """
56
57        ret = {}
58
59        for property_name in self.properties:
60            property_path = os.path.join(self.thermal_path, property_name)
61
62            if not os.path.isfile(property_path):
63                continue
64
65            with open(property_path) as fin:
66                contents = fin.read()
67                # Trim trailing newline
68                contents = contents[:-1]
69
70                try:
71                    ret[property_name] = int(contents)
72                except ValueError:
73                    ret[property_name] = contents
74
75        return ret
76
77    def pretty_print_in_ipython(self):
78        """Print parameters extracted from sysfs from a WA run in a pretty HTML table.
79
80        This won't work if the code is not running in an ipython notebook."""
81
82        from IPython.display import display, HTML
83
84        params = self.get_parameters()
85
86        # Don't print anything if we couldn't find any parameters
87        if len(params) == 0:
88            return
89
90        params_items = [(key, [value]) for key, value in sorted(params.items())]
91        dfr = pd.DataFrame.from_items(params_items, orient="index",
92                                      columns=["Value"])
93        display(HTML(dfr.to_html(header=False)))
94