Skip to content

Commit

Permalink
i#2006 generalize drcachesim: add the histogram tool
Browse files Browse the repository at this point in the history
Adds the histogram tool to track each cacheline usage by the trace.

Review-URL: https://codereview.appspot.com/304700043
  • Loading branch information
zhaoqin committed Oct 12, 2016
1 parent facbafc commit 141824d
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 2 deletions.
1 change: 1 addition & 0 deletions clients/drcachesim/CMakeLists.txt
Expand Up @@ -62,6 +62,7 @@ add_executable(drcachesim
simulator/cache_simulator.cpp
simulator/tlb.cpp
simulator/tlb_simulator.cpp
tools/histogram.cpp
# We embed the raw2trace conversion for convenience:
tracer/raw2trace.cpp
tracer/instru.cpp
Expand Down
5 changes: 4 additions & 1 deletion clients/drcachesim/analyzer.cpp
Expand Up @@ -38,6 +38,7 @@
#include "reader/ipc_reader.h"
#include "simulator/cache_simulator.h"
#include "simulator/tlb_simulator.h"
#include "tools/histogram.h"
#include "tracer/raw2trace.h"
#include <fstream>

Expand Down Expand Up @@ -138,9 +139,11 @@ analyzer_t::create_analysis_tools()
tools[0] = new cache_simulator_t;
else if (op_simulator_type.get_value() == TLB)
tools[0] = new tlb_simulator_t;
else if (op_simulator_type.get_value() == HISTOGRAM)
tools[0] = new histogram_t;
else {
ERRMSG("Usage error: unsupported analyzer type. "
"Please choose " CPU_CACHE" or " TLB".\n");
"Please choose " CPU_CACHE", " TLB", or "HISTOGRAM".\n");
return false;
}
if (!tools[0])
Expand Down
5 changes: 5 additions & 0 deletions clients/drcachesim/common/options.cpp
Expand Up @@ -201,3 +201,8 @@ droption_t<bytesize_t> op_sim_refs
"Specifies the number of memory references simulated. "
"The simulated references come after the skipped and warmup references, "
"and the references following the simulated ones are dropped.");

droption_t<unsigned int> op_report_top
(DROPTION_SCOPE_FRONTEND, "report_top", 10,
"Number of top results to be reported",
"Specifies the number of top results to be reported.");
3 changes: 2 additions & 1 deletion clients/drcachesim/common/options.h
Expand Up @@ -41,6 +41,7 @@
#define REPLACE_POLICY_FIFO "FIFO"
#define CPU_CACHE "cache"
#define TLB "TLB"
#define HISTOGRAM "histogram"

#include <string>
#include "droption.h"
Expand Down Expand Up @@ -79,5 +80,5 @@ extern droption_t<std::string> op_tracer_ops;
extern droption_t<bytesize_t> op_skip_refs;
extern droption_t<bytesize_t> op_warmup_refs;
extern droption_t<bytesize_t> op_sim_refs;

extern droption_t<unsigned int> op_report_top;
#endif /* _OPTIONS_H_ */
9 changes: 9 additions & 0 deletions clients/drcachesim/tests/histogram.templatex
@@ -0,0 +1,9 @@
Hello, world!
---- <application exited with code 0> ----
Cache Histogram result:
Cache Histogram: icache = [0-9]+ unique cache lines
Cache Histogram: dcache = [0-9]+ unique cache lines
Cache Histogram: icache top 10
.*
Cache Histogram: dcache top 10
.*
102 changes: 102 additions & 0 deletions clients/drcachesim/tools/histogram.cpp
@@ -0,0 +1,102 @@
/* **********************************************************
* Copyright (c) 2016 Google, Inc. All rights reserved.
* **********************************************************/

/*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of Google, Inc. nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL VMWARE, INC. OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/

#include <algorithm>
#include <iostream>
#include "droption.h"
#include "histogram.h"
#include "../common/options.h"
#include "../common/utils.h"

const std::string histogram_t::TOOL_NAME = "Cache Histogram";

histogram_t::histogram_t()
{
line_size = op_line_size.get_value();
line_size_bits = compute_log2((int)line_size);
report_top = op_report_top.get_value();
}

histogram_t::~histogram_t()
{
}

bool
histogram_t::process_memref(const memref_t &memref)
{
if (memref.type == TRACE_TYPE_INSTR ||
memref.type == TRACE_TYPE_PREFETCH_INSTR)
++icache_map[memref.addr >> line_size_bits];
else if (memref.type == TRACE_TYPE_READ ||
memref.type == TRACE_TYPE_WRITE ||
// We may potentially handle prefetches differently.
// TRACE_TYPE_PREFETCH_INSTR is handled above.
type_is_prefetch(memref.type))
++dcache_map[memref.addr >> line_size_bits];
return true;
}

bool cmp(const std::pair<addr_t, uint64_t> &l,
const std::pair<addr_t, uint64_t> &r)
{
return l.second > r.second;
}

bool
histogram_t::print_results()
{
std::cerr << TOOL_NAME << " result:\n";
std::cerr << TOOL_NAME << ": icache = " << icache_map.size()
<< " unique cache lines\n";
std::cerr << TOOL_NAME << ": dcache = " << dcache_map.size()
<< " unique cache lines\n";
std::vector<std::pair<addr_t, uint64_t> > top(report_top);
std::partial_sort_copy(icache_map.begin(), icache_map.end(),
top.begin(), top.end(), cmp);
std::cerr << TOOL_NAME << ": icache top " << top.size() << "\n";
for (std::vector<std::pair<addr_t, uint64_t> >::iterator it = top.begin();
it != top.end(); ++it) {
std::cerr << std::setw(18) << std::hex << std::showbase << (it->first << 6)
<< ": " << std::dec << it->second << "\n";
}
top.clear();
top.resize(report_top);
std::partial_sort_copy(dcache_map.begin(), dcache_map.end(),
top.begin(), top.end(), cmp);
std::cerr << TOOL_NAME << ": dcache top " << top.size() << "\n";
for (std::vector<std::pair<addr_t, uint64_t> >::iterator it = top.begin();
it != top.end(); ++it) {
std::cerr << std::setw(18) << std::hex << std::showbase << (it->first << 6)
<< ": " << std::dec << it->second << "\n";
}
return true;
}
63 changes: 63 additions & 0 deletions clients/drcachesim/tools/histogram.h
@@ -0,0 +1,63 @@
/* **********************************************************
* Copyright (c) 2016 Google, Inc. All rights reserved.
* **********************************************************/

/*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of Google, Inc. nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL VMWARE, INC. OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/

/* histogram: a memory trace histogram analysis tool.
*/

#ifndef _HISTOGRAM_H_
#define _HISTOGRAM_H_ 1

#include <map>
#include <string>
#include "../analysis_tool.h"
#include "../common/memref.h"

class histogram_t : public analysis_tool_t
{
public:
histogram_t();
virtual ~histogram_t();
virtual bool process_memref(const memref_t &memref);
virtual bool print_results();

protected:
/* FIXME i#2020: use unsorted_map (C++11) for faster lookup */
std::map<addr_t, uint64_t> icache_map;
std::map<addr_t, uint64_t> dcache_map;

size_t line_size;
size_t line_size_bits;
size_t report_top; /* most accessed lines */
static const std::string TOOL_NAME;
};

#endif /* _HISTOGRAM_H_ */
8 changes: 8 additions & 0 deletions suite/tests/CMakeLists.txt
Expand Up @@ -2420,6 +2420,14 @@ if (CLIENT_INTERFACE)
set(tool.drcachesim.multiproc_rawtemp ON) # no preprocessor
endif ()

# Test other analysis tools
torunonly_ci(tool.histogram ${ci_shared_app} drcachesim
"histogram.c" # for templatex basename
"-ipc_name drtestpipe5 -simulator_type histogram" "" "")
set(tool.histogram_toolname "drcachesim")
set(tool.histogram_basedir
"${PROJECT_SOURCE_DIR}/clients/drcachesim/tests")

# Test offline traces.
# XXX: we could exclude the pipe files and build the offline trace
# support by itself for Android.
Expand Down

0 comments on commit 141824d

Please sign in to comment.