1# SPDX-License-Identifier: Apache-2.0
2#
3# Copyright (C) 2015, ARM Limited and contributors.
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may
6# not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18""" Functions Analysis Module """
19
20from trappy.utils import listify
21
22from analysis_module import AnalysisModule
23
24
25class FunctionsAnalysis(AnalysisModule):
26    """
27    Support for kernel functions profiling and analysis
28
29    :param trace: input Trace object
30    :type trace: :mod:`libs.utils.Trace`
31    """
32
33    def __init__(self, trace):
34        super(FunctionsAnalysis, self).__init__(trace)
35
36    def plotProfilingStats(self, functions=None, metrics='avg'):
37        """
38        Plot functions profiling metrics for the specified kernel functions.
39
40        For each speficied metric a barplot is generated which report the value
41        of the metric when the kernel function has been executed on each CPU.
42        By default all the kernel functions are plotted.
43
44        :param functions: the name of list of name of kernel functions to plot
45        :type functions: str or list(str)
46
47        :param metrics: the metrics to plot
48                        avg   - average execution time
49                        time  - total execution time
50        :type metrics: srt or list(str)
51        """
52        if not hasattr(self._trace, '_functions_stats_df'):
53            self._log.warning('Functions stats data not available')
54            return
55
56        metrics = listify(metrics)
57        df = self._trace.data_frame.functions_stats(functions)
58
59        # Check that all the required metrics are acutally availabe
60        available_metrics = df.columns.tolist()
61        if not set(metrics).issubset(set(available_metrics)):
62            msg = 'Metrics {} not supported, available metrics are {}'\
63                    .format(set(metrics) - set(available_metrics),
64                            available_metrics)
65            raise ValueError(msg)
66
67        for metric in metrics:
68            if metric.upper() == 'AVG':
69                title = 'Average Completion Time per CPUs'
70                ylabel = 'Completion Time [us]'
71            if metric.upper() == 'TIME':
72                title = 'Total Execution Time per CPUs'
73                ylabel = 'Execution Time [us]'
74            data = df[metric.lower()].unstack()
75            axes = data.plot(kind='bar',
76                             figsize=(16, 8), legend=True,
77                             title=title, table=True)
78            axes.set_ylabel(ylabel)
79            axes.get_xaxis().set_visible(False)
80
81# vim :set tabstop=4 shiftwidth=4 expandtab
82