28 #include <gtest/gtest.h>
32 std::string
env(
const char* name) {
const char* v = getenv(name);
return v ? v :
""; }
33 std::string
cctlib_root() { std::string r =
env(
"CCTLIB_ROOT");
return r.empty() ?
"../.." : r; }
36 int run_pin(
const std::string& tool,
const std::vector<std::string>& args) {
37 std::string pin =
pin_root() +
"/pin";
38 std::vector<char*> argv;
39 argv.push_back(
const_cast<char*
>(pin.c_str()));
40 argv.push_back(
const_cast<char*
>(
"-t"));
41 argv.push_back(
const_cast<char*
>(tool.c_str()));
42 argv.push_back(
const_cast<char*
>(
"--"));
43 for (
auto&
a : args) argv.push_back(
const_cast<char*
>(
a.c_str()));
44 argv.push_back(
nullptr);
47 if (pid < 0)
return -1;
49 if (!
env(
"CCTLIB_TEST_VERBOSE").size()) {
50 int devnull = open(
"/dev/null", O_WRONLY);
51 if (devnull >= 0) { dup2(devnull, 1); dup2(devnull, 2); close(devnull); }
53 execv(pin.c_str(), argv.data());
57 if (waitpid(pid, &
status, 0) < 0)
return -1;
62 std::string
find_newest(
const std::string& dir,
const std::string& prefix) {
63 std::string cmd =
"ls -t " + dir +
"/" + prefix +
"* 2>/dev/null | head -1";
64 FILE* p = popen(cmd.c_str(),
"r");
66 char buf[4096]; std::string out;
67 if (fgets(
buf,
sizeof(
buf), p)) out =
buf;
69 if (!out.empty() && out.back() ==
'\n') out.pop_back();
73 std::ifstream in(path); std::stringstream ss; ss << in.rdbuf();
return ss.str();
75 void cleanup(
const std::string& dir,
const std::string& prefix) {
76 std::string cmd =
"rm -f " + dir +
"/" + prefix +
"*"; (void)system(cmd.c_str());
82 std::regex re(R
"(Total redundant bytes = ([\d.]+)\s*%)");
84 if (!std::regex_search(content, m, re))
return -1;
85 return std::stod(m[1]);
94 ASSERT_FALSE(
pin_root().empty()) <<
"PIN_ROOT required";
95 tool_ = root_ +
"/clients/obj-intel64/redspy_client.so";
96 ASSERT_EQ(0, access(tool_.c_str(), F_OK)) << tool_ <<
" missing";
97 ASSERT_EQ(0, chdir(root_.c_str())) <<
"chdir failed";
102 int rc =
run_pin(tool_, {victim});
103 EXPECT_EQ(0, rc) <<
"redspy on " << victim <<
" returned " << rc;
104 std::string out =
find_newest(root_,
"redspy.out.");
105 EXPECT_FALSE(out.empty()) <<
"no redspy.out.* file";
112 double pct = run_and_parse_pct(
"/bin/ls");
114 EXPECT_LT(pct, 100.0);
123 double tp = run_and_parse_pct(root_ +
"/tests/gtest/obj/apps/redspy_tp_simple");
124 double tn = run_and_parse_pct(root_ +
"/tests/gtest/obj/apps/redspy_tn_simple");
131 EXPECT_GT(tp, tn + 2.0)
132 <<
"TP=" << tp <<
"% TN=" << tn <<
"% -- expected TP > TN + 2%";
137 double tp = run_and_parse_pct(root_ +
"/tests/gtest/obj/apps/redspy_tp_sizes");
138 double tn = run_and_parse_pct(root_ +
"/tests/gtest/obj/apps/redspy_tn_simple");
141 EXPECT_GT(tp, tn + 2.0) <<
"TP-sizes=" << tp <<
"% TN=" << tn <<
"%";
148 int rc =
run_pin(tool_, {root_ +
"/tests/gtest/obj/apps/redspy_tp_simple"});
150 std::string out =
find_newest(root_,
"redspy.out.");
151 ASSERT_FALSE(out.empty());
152 std::string cmd =
"grep -c 'store8:.*redspy_tp_simple.c' " + out;
153 FILE* p = popen(cmd.c_str(),
"r");
154 ASSERT_NE(p,
nullptr);
155 char buf[64]; std::string s;
158 long hits = s.empty() ? 0 : std::stol(s);
160 <<
"redspy report has no context mentioning store8 in redspy_tp_simple.c";
173 public ::testing::WithParamInterface<RedspyIsaVictim> {};
176 double tn = run_and_parse_pct(root_ +
"/tests/gtest/obj/apps/redspy_tn_simple");
177 double isa = run_and_parse_pct(root_ +
"/tests/gtest/obj/apps/isa/" + GetParam().name);
180 EXPECT_GT(isa - tn, GetParam().min_extra_pct)
181 <<
"victim=" << GetParam().name
182 <<
" isa-pct=" << isa
184 <<
"% threshold=" << GetParam().min_extra_pct;
193 RedspyIsaVictim{
"redspy_avx32_tp", 2.0},
195 RedspyIsaVictim{
"redspy_immediate_tp", 1.0},
197 RedspyIsaVictim{
"redspy_partial_qword_then_byte_tp", 0.3},
200 RedspyIsaVictim{
"redspy_zero_fill_tp", 2.0},
202 RedspyIsaVictim{
"redspy_scalar_sse_tp", 1.0},
205 RedspyIsaVictim{
"redspy_cross_page_tp", 0.5}),
206 [](
const testing::TestParamInfo<RedspyIsaVictim>& info) {
207 return info.param.name;
double run_and_parse_pct(const std::string &victim)
static uint64_t buf[ITERS]
std::string find_newest(const std::string &dir, const std::string &prefix)
std::string env(const char *name)
TEST_P(RedspyIsa, ExceedsBaseline)
void cleanup(const std::string &dir, const std::string &prefix)
std::string read_file(const std::string &path)
double parse_first_redundant_pct(const std::string &content)
TEST_F(RedspyIntegration, TPReportAttributesToStore8)
INSTANTIATE_TEST_SUITE_P(IsaBreadth, RedspyIsa, ::testing::Values(RedspyIsaVictim{"redspy_sse16_tp", 2.0}, RedspyIsaVictim{"redspy_avx32_tp", 2.0}, RedspyIsaVictim{"redspy_immediate_tp", 1.0}, RedspyIsaVictim{"redspy_partial_qword_then_byte_tp", 0.3}, RedspyIsaVictim{"redspy_zero_fill_tp", 2.0}, RedspyIsaVictim{"redspy_scalar_sse_tp", 1.0}, RedspyIsaVictim{"redspy_cross_page_tp", 0.5}), [](const testing::TestParamInfo< RedspyIsaVictim > &info) { return info.param.name;})
int run_pin(const std::string &tool, const std::vector< std::string > &args)
std::string cctlib_root()