1#!/usr/bin/env python2.7
2
3# Copyright (c) 2015 ARM Limited
4# All rights reserved
5#
6# The license below extends only to copyright in the software and shall
7# not be construed as granting a license to any other intellectual
8# property including but not limited to intellectual property relating
9# to a hardware implementation of the functionality of the software
10# licensed hereunder.  You may use the software subject to the license
11# terms below provided that you ensure that this notice is replicated
12# unmodified and in its entirety in all distributions of the software,
13# modified or unmodified, in source code or in binary form.
14#
15# Redistribution and use in source and binary forms, with or without
16# modification, are permitted provided that the following conditions are
17# met: redistributions of source code must retain the above copyright
18# notice, this list of conditions and the following disclaimer;
19# redistributions in binary form must reproduce the above copyright
20# notice, this list of conditions and the following disclaimer in the
21# documentation and/or other materials provided with the distribution;
22# neither the name of the copyright holders nor the names of its
23# contributors may be used to endorse or promote products derived from
24# this software without specific prior written permission.
25#
26# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37#
38# Authors: Andreas Hansson
39
40try:
41    import matplotlib.pyplot as plt
42    import matplotlib as mpl
43    import numpy as np
44except ImportError:
45    print "Failed to import matplotlib and numpy"
46    exit(-1)
47
48import sys
49import re
50
51# This script is intended to post process and plot the output from
52# running configs/dram/lat_mem_rd.py, as such it parses the simout and
53# stats.txt to get the relevant data points.
54def main():
55
56    if len(sys.argv) != 2:
57        print "Usage: ", sys.argv[0], "<simout directory>"
58        exit(-1)
59
60    try:
61        stats = open(sys.argv[1] + '/stats.txt', 'r')
62    except IOError:
63        print "Failed to open ", sys.argv[1] + '/stats.txt', " for reading"
64        exit(-1)
65
66    try:
67        simout = open(sys.argv[1] + '/simout', 'r')
68    except IOError:
69        print "Failed to open ", sys.argv[1] + '/simout', " for reading"
70        exit(-1)
71
72    # Get the address ranges
73    got_ranges = False
74    ranges = []
75
76    iterations = 1
77
78    for line in simout:
79        if got_ranges:
80            ranges.append(int(line) / 1024)
81
82        match = re.match("lat_mem_rd with (\d+) iterations, ranges:.*", line)
83        if match:
84            got_ranges = True
85            iterations = int(match.groups(0)[0])
86
87    simout.close()
88
89    if not got_ranges:
90        print "Failed to get address ranges, ensure simout is up-to-date"
91        exit(-1)
92
93    # Now parse the stats
94    raw_rd_lat = []
95
96    for line in stats:
97        match = re.match(".*readLatencyHist::mean\s+(.+)\s+#.*", line)
98        if match:
99            raw_rd_lat.append(float(match.groups(0)[0]) / 1000)
100    stats.close()
101
102    # The stats also contain the warming, so filter the latency stats
103    i = 0
104    filtered_rd_lat = []
105    for l in raw_rd_lat:
106        if i % (iterations + 1) == 0:
107            pass
108        else:
109            filtered_rd_lat.append(l)
110        i = i + 1
111
112    # Next we need to take care of the iterations
113    rd_lat = []
114    for i in range(iterations):
115        rd_lat.append(filtered_rd_lat[i::iterations])
116
117    final_rd_lat = map(lambda p: min(p), zip(*rd_lat))
118
119    # Sanity check
120    if not (len(ranges) == len(final_rd_lat)):
121        print "Address ranges (%d) and read latency (%d) do not match" % \
122            (len(ranges), len(final_rd_lat))
123        exit(-1)
124
125    for (r, l) in zip(ranges, final_rd_lat):
126        print r, round(l, 2)
127
128    # lazy version to check if an integer is a power of two
129    def is_pow2(num):
130        return num != 0 and ((num & (num - 1)) == 0)
131
132    plt.semilogx(ranges, final_rd_lat)
133
134    # create human readable labels
135    xticks_locations = [r for r in ranges if is_pow2(r)]
136    xticks_labels = []
137    for x in xticks_locations:
138        if x < 1024:
139            xticks_labels.append('%d kB' % x)
140        else:
141            xticks_labels.append('%d MB' % (x / 1024))
142    plt.xticks(xticks_locations, xticks_labels, rotation=-45)
143
144    plt.minorticks_off()
145    plt.xlim((xticks_locations[0], xticks_locations[-1]))
146    plt.ylabel("Latency (ns)")
147    plt.grid(True)
148    plt.show()
149
150if __name__ == "__main__":
151    main()
152