1/*
2 * Copyright (c) 2016 Google Inc.
3 * All rights reserved.
4 *
5 * The license below extends only to copyright in the software and
6 * shall not be construed as granting a license to any other
7 * intellectual property including but not limited to intellectual
8 * property relating to a hardware implementation of the
9 * functionality of the software licensed hereunder.  You may use the
10 * software subject to the license terms below provided that you
11 * ensure that this notice is replicated unmodified and in its
12 * entirety in all distributions of the software, modified or
13 * 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: Rahul Thakur
39 */
40
41#include "mem/probes/mem_footprint.hh"
42
43#include "base/intmath.hh"
44#include "params/MemFootprintProbe.hh"
45
46MemFootprintProbe::MemFootprintProbe(MemFootprintProbeParams *p)
47    : BaseMemProbe(p),
48      cacheLineSizeLg2(floorLog2(p->system->cacheLineSize())),
49      pageSizeLg2(floorLog2(p->page_size)),
50      totalCacheLinesInMem(p->system->memSize() / p->system->cacheLineSize()),
51      totalPagesInMem(p->system->memSize() / p->page_size),
52      cacheLines(),
53      cacheLinesAll(),
54      pages(),
55      pagesAll(),
56      system(p->system)
57{
58    fatal_if(!isPowerOf2(system->cacheLineSize()),
59             "MemFootprintProbe expects cache line size is power of 2.");
60    fatal_if(!isPowerOf2(p->page_size),
61             "MemFootprintProbe expects page size parameter is power of 2");
62}
63
64void
65MemFootprintProbe::regStats()
66{
67    BaseMemProbe::regStats();
68
69    using namespace Stats;
70    // clang-format off
71    fpCacheLine.name(name() + ".cacheline")
72        .desc("Memory footprint at cache line granularity")
73        .flags(nozero | nonan);
74    fpCacheLineTotal.name(name() + ".cacheline_total")
75        .desc("Total memory footprint at cache line granularity since "
76              "simulation begin")
77        .flags(nozero | nonan);
78    fpPage.name(name() + ".page")
79        .desc("Memory footprint at page granularity")
80        .flags(nozero | nonan);
81    fpPageTotal.name(name() + ".page_total")
82        .desc("Total memory footprint at page granularity since simulation "
83              "begin")
84        .flags(nozero | nonan);
85    // clang-format on
86
87    registerResetCallback(
88        new MakeCallback<MemFootprintProbe, &MemFootprintProbe::statReset>(
89            this));
90}
91
92void
93MemFootprintProbe::insertAddr(Addr addr, AddrSet *set, uint64_t limit)
94{
95    set->insert(addr);
96    assert(set->size() <= limit);
97}
98
99void
100MemFootprintProbe::handleRequest(const ProbePoints::PacketInfo &pi)
101{
102    if (!pi.cmd.isRequest() || !system->isMemAddr(pi.addr))
103        return;
104
105    const Addr cl_addr = (pi.addr >> cacheLineSizeLg2) << cacheLineSizeLg2;
106    const Addr page_addr = (pi.addr >> pageSizeLg2) << pageSizeLg2;
107    insertAddr(cl_addr, &cacheLines, totalCacheLinesInMem);
108    insertAddr(cl_addr, &cacheLinesAll, totalCacheLinesInMem);
109    insertAddr(page_addr, &pages, totalPagesInMem);
110    insertAddr(page_addr, &pagesAll, totalPagesInMem);
111
112    assert(cacheLines.size() <= cacheLinesAll.size());
113    assert(pages.size() <= pagesAll.size());
114
115    fpCacheLine = cacheLines.size() << cacheLineSizeLg2;
116    fpCacheLineTotal = cacheLinesAll.size() << cacheLineSizeLg2;
117    fpPage = pages.size() << pageSizeLg2;
118    fpPageTotal = pagesAll.size() << pageSizeLg2;
119}
120
121void
122MemFootprintProbe::statReset()
123{
124    cacheLines.clear();
125    pages.clear();
126}
127
128MemFootprintProbe *
129MemFootprintProbeParams::create()
130{
131    return new MemFootprintProbe(this);
132}
133