Skip to content

Commit

Permalink
[LNT] Added reading nm and objdump paths from env variables
Browse files Browse the repository at this point in the history
  - Added reading nm and objdump paths from env variables
  - Added binaryCacheRoot

Reviewed By: thopre

Differential Revision: https://reviews.llvm.org/D110839

OS Laboratory. Huawei Russian Research Institute. Saint-Petersburg
  • Loading branch information
Pavel Kosov committed Oct 21, 2021
1 parent efe3935 commit 9290733
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
36 changes: 21 additions & 15 deletions lnt/testing/profile/cPerf.cpp
Expand Up @@ -259,16 +259,18 @@ struct Symbol {

class NmOutput : public std::vector<Symbol> {
public:
std::string Nm;
std::string Nm, BinaryCacheRoot;

NmOutput(std::string Nm) : Nm(Nm) {}
NmOutput(std::string Nm, std::string BinaryCacheRoot)
: Nm(Nm), BinaryCacheRoot(BinaryCacheRoot) {}

void fetchSymbols(Map *M, bool Dynamic) {
std::string D = "-D";
if (!Dynamic)
// Don't fetch the dynamic symbols - instead fetch static ones.
D = "";
std::string Cmd = Nm + " " + D + " -S --defined-only " + std::string(M->Filename) +
std::string Cmd = Nm + " " + D + " -S --defined-only " +
BinaryCacheRoot + std::string(M->Filename) +
" 2>/dev/null";
auto Stream = ForkAndExec(Cmd);

Expand Down Expand Up @@ -343,16 +345,17 @@ class NmOutput : public std::vector<Symbol> {

class ObjdumpOutput {
public:
std::string Objdump;
std::string Objdump, BinaryCacheRoot;
FILE *Stream;
char *ThisText;
uint64_t ThisAddress;
uint64_t EndAddress;
char *Line;
size_t LineLen;

ObjdumpOutput(std::string Objdump)
: Objdump(Objdump), Stream(nullptr), Line(NULL), LineLen(0) {}
ObjdumpOutput(std::string Objdump, std::string BinaryCacheRoot)
: Objdump(Objdump), BinaryCacheRoot(BinaryCacheRoot), Stream(nullptr),
Line(NULL), LineLen(0) {}
~ObjdumpOutput() {
if (Stream) {
fclose(Stream);
Expand All @@ -375,7 +378,8 @@ class ObjdumpOutput {

std::string Cmd = Objdump + " -d --no-show-raw-insn --start-address=" +
std::string(buf1) + " --stop-address=" +
std::string(buf2) + " " + std::string(M->Filename) +
std::string(buf2) + " " +
BinaryCacheRoot + std::string(M->Filename) +
" 2>/dev/null";
Stream = ForkAndExec(Cmd);

Expand Down Expand Up @@ -420,7 +424,7 @@ class ObjdumpOutput {
class PerfReader {
public:
PerfReader(const std::string &Filename, std::string Nm,
std::string Objdump);
std::string Objdump, std::string BinaryCacheRoot);
~PerfReader();

void readHeader();
Expand Down Expand Up @@ -458,12 +462,13 @@ class PerfReader {
PyObject *Functions, *TopLevelCounters;
std::vector<PyObject*> Lines;

std::string Nm, Objdump;
std::string Nm, Objdump, BinaryCacheRoot;
};

PerfReader::PerfReader(const std::string &Filename,
std::string Nm, std::string Objdump)
: Nm(Nm), Objdump(Objdump) {
std::string Nm, std::string Objdump,
std::string BinaryCacheRoot)
: Nm(Nm), Objdump(Objdump), BinaryCacheRoot(BinaryCacheRoot) {
int fd = open(Filename.c_str(), O_RDONLY);
assert(fd > 0);

Expand Down Expand Up @@ -707,7 +712,7 @@ void PerfReader::emitMaps() {
bool IsSO = IsSharedObject(Maps[MapID].Filename);
uint64_t Adjust = IsSO ? Maps[MapID].Start : 0;

NmOutput Syms(Nm);
NmOutput Syms(Nm, BinaryCacheRoot);
Syms.reset(&Maps[MapID]);

// Accumulate the event totals for each symbol
Expand Down Expand Up @@ -753,7 +758,7 @@ void PerfReader::emitSymbol(
std::map<uint64_t, std::map<const char *, uint64_t>>::iterator Event,
std::map<const char *, uint64_t> &SymEvents,
uint64_t Adjust) {
ObjdumpOutput Dump(Objdump);
ObjdumpOutput Dump(Objdump, BinaryCacheRoot);
Dump.reset(&M, Sym.Start, Sym.End);
Dump.next();

Expand Down Expand Up @@ -784,11 +789,12 @@ static PyObject *cPerf_importPerf(PyObject *self, PyObject *args) {
const char *Fname;
const char *Nm = "nm";
const char *Objdump = "objdump";
if (!PyArg_ParseTuple(args, "s|ss", &Fname, &Nm, &Objdump))
const char *BinaryCacheRoot = "";
if (!PyArg_ParseTuple(args, "s|sss", &Fname, &Nm, &Objdump, &BinaryCacheRoot))
return NULL;

try {
PerfReader P(Fname, Nm, Objdump);
PerfReader P(Fname, Nm, Objdump, BinaryCacheRoot);
P.readHeader();
P.readAttrs();
P.readDataStream();
Expand Down
3 changes: 2 additions & 1 deletion lnt/testing/profile/perf.py
Expand Up @@ -22,7 +22,8 @@ def checkFile(fn):
return f.read(8) == b'PERFILE2'

@staticmethod
def deserialize(f, nm='nm', objdump='objdump', propagateExceptions=False):
def deserialize(f, nm='nm', objdump='objdump', propagateExceptions=False,
binaryCacheRoot=''):
f = f.name

if os.path.getsize(f) == 0:
Expand Down
10 changes: 9 additions & 1 deletion lnt/testing/profile/profile.py
Expand Up @@ -27,7 +27,15 @@ def fromFile(f):
"""
for impl in lnt.testing.profile.IMPLEMENTATIONS.values():
if impl.checkFile(f):
ret = impl.deserialize(open(f, 'rb'))
ret = None
with open(f, 'rb') as fd:
if impl is lnt.testing.profile.perf.LinuxPerfProfile:
ret = impl.deserialize(fd,
nm = os.getenv('CMAKE_NM', 'nm'),
objdump = os.getenv('CMAKE_OBJDUMP', 'objdump'),
binaryCacheRoot = os.getenv('LNT_BINARY_CACHE_ROOT', ''))
else:
ret = impl.deserialize(fd)
if ret:
return Profile(ret)
else:
Expand Down

0 comments on commit 9290733

Please sign in to comment.