Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: PMU data collection #4

Open
wants to merge 9 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ find_package(MPI REQUIRED)
include_directories(${MPI_INCLUDE_PATH})

include_directories(include)
include_directories(3rd-party/dbg-macro)
# Required packages: libunwind, papi, jsoncpp

SET(CMAKE_C_COMPILER mpicc)
Expand Down
19 changes: 19 additions & 0 deletions include/collector_papi.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
#pragma once
#include <collector.h>

#define checkPapi(call) \
{ \
auto err = call; \
if (PAPI_OK != err) { \
fprintf(stderr, "PAPI error in %s:%i : %s.\n", __FILE__, __LINE__, \
PAPI_strerror(retval)); \
exit(EXIT_FAILURE); \
} \
}

static __inline__ unsigned long long rdtsc()
{
unsigned hi, lo;
__asm__ __volatile__("rdtsc"
: "=a"(lo), "=d"(hi));
return ((unsigned long long)lo) | (((unsigned long long)hi) << 32);
}

namespace vapro {

const string metricTotalInst("TOT_INST"), metricTSC("TSC");
class CollectorPapi : public Collector {

MetricVec allMetrics = {metricTotalInst, metricTSC};
int EventSet;

public:
CollectorPapi();
Expand Down
2 changes: 1 addition & 1 deletion include/controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Controller {
public:
Controller();

void readData();
DataVec readData();
};

} // namespace vapro
33 changes: 30 additions & 3 deletions src/CollectorPapi.cc
Original file line number Diff line number Diff line change
@@ -1,16 +1,43 @@
#include "collector_papi.h"
#include <papi.h>

namespace vapro {
#define checkPapi(call,stdpapi) \
{ \
auto err = call; \
if (stdpapi != err) { \
fprintf(stderr, "PAPI error in %s:%i : %s.\n", __FILE__, __LINE__, \
PAPI_strerror(retval)); \
exit(EXIT_FAILURE); \
} \
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This macro is already defined in include/collector_papi.h. This overload with two paramters seems to be seldom used as most of the return values are PAPI_OK. I suggest using the original papiCheck without a custom return value.


namespace vapro {
CollectorPapi::CollectorPapi() {
// TODO: init the PAPI library. Refer to PAPI-reated functions in papi_init()
// and papi_start_counters() in deprecated/pmc.cpp.
int retval = 0;
EventSet = PAPI_NULL;
checkPapi(PAPI_library_init(PAPI_VER_CURRENT),PAPI_VER_CURRENT);
checkPapi(PAPI_create_eventset(&EventSet),PAPI_OK);
checkPapi(PAPI_add_event(EventSet, PAPI_TOT_INS),PAPI_OK);
checkPapi(PAPI_add_event(EventSet, PAPI_L1_DCM),PAPI_OK);
checkPapi(PAPI_start(EventSet),PAPI_OK);
}

DataVec CollectorPapi::readData() {
dbg("Invoked");
// TODO: read data by the PAPI library. Refer to papi_get_data() in
// deprecated/pmc.cpp.
DataVec dtvc;
long long int values[2];
int retval = 0;
if (retval = PAPI_read(EventSet, values) != PAPI_OK) {
fprintf(stderr, "4,PAPI error %d: %s\n", retval, PAPI_strerror(retval));
exit(-1);
}

dtvc.emplace_back(values[0]);
dtvc.emplace_back(values[1]);
dtvc.emplace_back(rdtsc());
return dtvc;
}

} // namespace vapro
11 changes: 9 additions & 2 deletions src/controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@ Controller::Controller() {
collectors.emplace_back(std::make_unique<CollectorPapi>());
}

void Controller::readData() {
DataVec Controller::readData() {
DataVec dtvc;
for (auto &c : collectors) {
auto data = c->readData();
dbg(data);
dbg(data[0]);
dbg(data[1]);
dbg(data[2]);
dtvc.push_back(data[0]);
dtvc.push_back(data[1]);
dtvc.push_back(data[2]);
}
return dtvc;
}

} // namespace vapro
72 changes: 70 additions & 2 deletions test/test_collector.cc
Original file line number Diff line number Diff line change
@@ -1,13 +1,81 @@
#include "controller.h"
#include "papi.h"
#include "stdio.h"
zhengly123 marked this conversation as resolved.
Show resolved Hide resolved
using namespace vapro;

void workload() {
int load = 0;
for (int i = 0; i < 10000; i++)
for (int j = 0; j < i; j++)
load += (i + j);
}
void workload_1() {
int N = 1000;
int x[N];
for (int j = 0; j < N; j++) {
x[j] = j;
}
for (int loop = 0; loop < 10; loop++) {
for (int i = 0; i < N; i++) {
x[i] = x[i] * x[i];
}
}
}
void workload_2() {
int N = 1000;
int x[N];
for (int j = 0; j < N; j++) {
x[j] = j;
}
for (int i = 0; i < N; i++) {
for (int loop = 0; loop < 10; loop++) {
x[i] = x[i] * x[i];
}
}
}

void result(DataVec d1,DataVec d2)
{
printf("TOT_INS:%ld\n",d2[0]-d1[0]);
printf("TOT_CM:%ld\n",d2[1]-d1[1]);
printf("TOT_TIME:%ld\n",d2[2]-d1[2]);
}

int main() {

printf("begin test\n");
// test0();
Controller controller;

DataVec d1,d2,d3;
// 完成CollectorPapi.cc中的TODO。利用PAPI采集PAPI_TOT_INS,即程序片段的总指令数。如果遇到采集出来为0,或者出错,可能和系统的权限相关,可以来问下我。

// This readData function will print the performance data
controller.readData();
d1=controller.readData();
// TODO: add some workload here for profiling
controller.readData();

workload_1();

d2=controller.readData();

result(d1,d2);
// TOOD: compare the whether the printed data matches your expectation

workload_2();

d3=controller.readData();

result(d2,d3);

printf("end test\n");
return 0;
}
// 在test终端打开
// source /opt/spack/share/spack/setup-env.sh
// spack load openmpi@4.1.5
// spack load papi
// cmake ..
// make
//./test_vapro

// source /opt/spack/share/spack/setup-env.sh;spack load openmpi@4.1.5;spack load papi;cmake ..;make;./test_vapro