111803Srjthakur@google.com/*
211803Srjthakur@google.com * Copyright (c) 2016 Google Inc.
311803Srjthakur@google.com * All rights reserved.
411803Srjthakur@google.com *
511803Srjthakur@google.com * The license below extends only to copyright in the software and
611803Srjthakur@google.com * shall not be construed as granting a license to any other
711803Srjthakur@google.com * intellectual property including but not limited to intellectual
811803Srjthakur@google.com * property relating to a hardware implementation of the
911803Srjthakur@google.com * functionality of the software licensed hereunder.  You may use the
1011803Srjthakur@google.com * software subject to the license terms below provided that you
1111803Srjthakur@google.com * ensure that this notice is replicated unmodified and in its
1211803Srjthakur@google.com * entirety in all distributions of the software, modified or
1311803Srjthakur@google.com * unmodified, in source code or in binary form.
1411803Srjthakur@google.com *
1511803Srjthakur@google.com * Redistribution and use in source and binary forms, with or without
1611803Srjthakur@google.com * modification, are permitted provided that the following conditions are
1711803Srjthakur@google.com * met: redistributions of source code must retain the above copyright
1811803Srjthakur@google.com * notice, this list of conditions and the following disclaimer;
1911803Srjthakur@google.com * redistributions in binary form must reproduce the above copyright
2011803Srjthakur@google.com * notice, this list of conditions and the following disclaimer in the
2111803Srjthakur@google.com * documentation and/or other materials provided with the distribution;
2211803Srjthakur@google.com * neither the name of the copyright holders nor the names of its
2311803Srjthakur@google.com * contributors may be used to endorse or promote products derived from
2411803Srjthakur@google.com * this software without specific prior written permission.
2511803Srjthakur@google.com *
2611803Srjthakur@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2711803Srjthakur@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2811803Srjthakur@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2911803Srjthakur@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3011803Srjthakur@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3111803Srjthakur@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3211803Srjthakur@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3311803Srjthakur@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3411803Srjthakur@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3511803Srjthakur@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3611803Srjthakur@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3711803Srjthakur@google.com *
3811803Srjthakur@google.com * Authors: Rahul Thakur
3911803Srjthakur@google.com */
4011803Srjthakur@google.com
4111803Srjthakur@google.com#include "mem/probes/mem_footprint.hh"
4211803Srjthakur@google.com
4311803Srjthakur@google.com#include "base/intmath.hh"
4411803Srjthakur@google.com#include "params/MemFootprintProbe.hh"
4511803Srjthakur@google.com
4611803Srjthakur@google.comMemFootprintProbe::MemFootprintProbe(MemFootprintProbeParams *p)
4711803Srjthakur@google.com    : BaseMemProbe(p),
4811803Srjthakur@google.com      cacheLineSizeLg2(floorLog2(p->system->cacheLineSize())),
4911803Srjthakur@google.com      pageSizeLg2(floorLog2(p->page_size)),
5011803Srjthakur@google.com      totalCacheLinesInMem(p->system->memSize() / p->system->cacheLineSize()),
5111803Srjthakur@google.com      totalPagesInMem(p->system->memSize() / p->page_size),
5211803Srjthakur@google.com      cacheLines(),
5311803Srjthakur@google.com      cacheLinesAll(),
5411803Srjthakur@google.com      pages(),
5511803Srjthakur@google.com      pagesAll(),
5611803Srjthakur@google.com      system(p->system)
5711803Srjthakur@google.com{
5811803Srjthakur@google.com    fatal_if(!isPowerOf2(system->cacheLineSize()),
5911803Srjthakur@google.com             "MemFootprintProbe expects cache line size is power of 2.");
6011803Srjthakur@google.com    fatal_if(!isPowerOf2(p->page_size),
6111803Srjthakur@google.com             "MemFootprintProbe expects page size parameter is power of 2");
6211803Srjthakur@google.com}
6311803Srjthakur@google.com
6411803Srjthakur@google.comvoid
6511803Srjthakur@google.comMemFootprintProbe::regStats()
6611803Srjthakur@google.com{
6711803Srjthakur@google.com    BaseMemProbe::regStats();
6811803Srjthakur@google.com
6911803Srjthakur@google.com    using namespace Stats;
7011803Srjthakur@google.com    // clang-format off
7111803Srjthakur@google.com    fpCacheLine.name(name() + ".cacheline")
7211803Srjthakur@google.com        .desc("Memory footprint at cache line granularity")
7311803Srjthakur@google.com        .flags(nozero | nonan);
7411803Srjthakur@google.com    fpCacheLineTotal.name(name() + ".cacheline_total")
7511803Srjthakur@google.com        .desc("Total memory footprint at cache line granularity since "
7611803Srjthakur@google.com              "simulation begin")
7711803Srjthakur@google.com        .flags(nozero | nonan);
7811803Srjthakur@google.com    fpPage.name(name() + ".page")
7911803Srjthakur@google.com        .desc("Memory footprint at page granularity")
8011803Srjthakur@google.com        .flags(nozero | nonan);
8111803Srjthakur@google.com    fpPageTotal.name(name() + ".page_total")
8211803Srjthakur@google.com        .desc("Total memory footprint at page granularity since simulation "
8311803Srjthakur@google.com              "begin")
8411803Srjthakur@google.com        .flags(nozero | nonan);
8511803Srjthakur@google.com    // clang-format on
8611803Srjthakur@google.com
8711803Srjthakur@google.com    registerResetCallback(
8811803Srjthakur@google.com        new MakeCallback<MemFootprintProbe, &MemFootprintProbe::statReset>(
8911803Srjthakur@google.com            this));
9011803Srjthakur@google.com}
9111803Srjthakur@google.com
9211803Srjthakur@google.comvoid
9311803Srjthakur@google.comMemFootprintProbe::insertAddr(Addr addr, AddrSet *set, uint64_t limit)
9411803Srjthakur@google.com{
9511803Srjthakur@google.com    set->insert(addr);
9611803Srjthakur@google.com    assert(set->size() <= limit);
9711803Srjthakur@google.com}
9811803Srjthakur@google.com
9911803Srjthakur@google.comvoid
10011803Srjthakur@google.comMemFootprintProbe::handleRequest(const ProbePoints::PacketInfo &pi)
10111803Srjthakur@google.com{
10211803Srjthakur@google.com    if (!pi.cmd.isRequest() || !system->isMemAddr(pi.addr))
10311803Srjthakur@google.com        return;
10411803Srjthakur@google.com
10511803Srjthakur@google.com    const Addr cl_addr = (pi.addr >> cacheLineSizeLg2) << cacheLineSizeLg2;
10611803Srjthakur@google.com    const Addr page_addr = (pi.addr >> pageSizeLg2) << pageSizeLg2;
10711803Srjthakur@google.com    insertAddr(cl_addr, &cacheLines, totalCacheLinesInMem);
10811803Srjthakur@google.com    insertAddr(cl_addr, &cacheLinesAll, totalCacheLinesInMem);
10911803Srjthakur@google.com    insertAddr(page_addr, &pages, totalPagesInMem);
11011803Srjthakur@google.com    insertAddr(page_addr, &pagesAll, totalPagesInMem);
11111803Srjthakur@google.com
11211803Srjthakur@google.com    assert(cacheLines.size() <= cacheLinesAll.size());
11311803Srjthakur@google.com    assert(pages.size() <= pagesAll.size());
11411803Srjthakur@google.com
11511803Srjthakur@google.com    fpCacheLine = cacheLines.size() << cacheLineSizeLg2;
11611803Srjthakur@google.com    fpCacheLineTotal = cacheLinesAll.size() << cacheLineSizeLg2;
11711803Srjthakur@google.com    fpPage = pages.size() << pageSizeLg2;
11811803Srjthakur@google.com    fpPageTotal = pagesAll.size() << pageSizeLg2;
11911803Srjthakur@google.com}
12011803Srjthakur@google.com
12111803Srjthakur@google.comvoid
12211803Srjthakur@google.comMemFootprintProbe::statReset()
12311803Srjthakur@google.com{
12411803Srjthakur@google.com    cacheLines.clear();
12511803Srjthakur@google.com    pages.clear();
12611803Srjthakur@google.com}
12711803Srjthakur@google.com
12811803Srjthakur@google.comMemFootprintProbe *
12911803Srjthakur@google.comMemFootprintProbeParams::create()
13011803Srjthakur@google.com{
13111803Srjthakur@google.com    return new MemFootprintProbe(this);
13211803Srjthakur@google.com}
133