Skip to content

Commit

Permalink
Add interface support for stack traces. Remove shared memory interface
Browse files Browse the repository at this point in the history
except for a template for future work.


git-svn-id: svn+ssh://truffle/home/andrew/repo/memview@124 6d4121a9-3ab8-48c6-8db9-97dca464e206
  • Loading branch information
ajclinto committed Jan 9, 2013
1 parent c54f3bf commit ebc9cdd
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 55 deletions.
46 changes: 20 additions & 26 deletions Loader.C
Expand Up @@ -93,8 +93,6 @@ Loader::openPipe(int argc, char *argv[])
}

memset(mySharedData, 0, sizeof(SharedData));
for (int i = 0; i < theBlockCount; i++)
mySharedData->myBlocks[i].myWSem = 1;
}

int fd[2];
Expand Down Expand Up @@ -332,10 +330,26 @@ Loader::loadFromPipe()
if (retval == 0)
return true;

if (read(myPipeFD, block.get(), sizeof(TraceBlock)))
Header header;
if (!read(myPipeFD, &header, sizeof(Header)))
return false;

if (header.myType == MV_BLOCK)
{
if (block->myEntries && loadBlock(block))
if (read(myPipeFD, block.get(), sizeof(TraceBlock)))
{
if (block->myEntries && loadBlock(block))
return true;
}
}
else if (header.myType == MV_STACKTRACE)
{
char buf[MV_STACKTRACE_BUFSIZE];
if (read(myPipeFD, buf, header.mySize))
{
fprintf(stderr, "%s\n", buf);
return true;
}
}

return false;
Expand All @@ -344,28 +358,8 @@ Loader::loadFromPipe()
bool
Loader::loadFromSharedMemory()
{
if (!mySharedData)
return false;

TraceBlock &block = mySharedData->myBlocks[myIdx];
while (!__sync_bool_compare_and_swap(&block.myRSem, 1, 0))
;

if (block.myEntries)
{
TraceBlockHandle handle(new TraceBlock(block));
if (!loadBlock(handle))
return false;
}

__sync_bool_compare_and_swap(&block.myWSem, 0, 1);

myIdx++;
if (myIdx == theBlockCount)
myIdx = 0;

// If it was empty, we're at the end of the stream.
return block.myEntries;
fprintf(stderr, "Shared memory currently unsupported\n");
return false;
}

bool
Expand Down
3 changes: 2 additions & 1 deletion Loader.h
Expand Up @@ -57,7 +57,8 @@ class Loader : public QThread {
int myPipeFD;
FILE *myPipe;

// Shared memory
// Shared memory. This interface is only available as a template for
// future work.
SharedData *mySharedData;
int myIdx;

Expand Down
29 changes: 22 additions & 7 deletions tool/mv_ipc.h
@@ -1,8 +1,26 @@
#ifndef MV_IPC_H
#define MV_IPC_H

//
// The unidirectional (tool to memview) pipe communication protocol is
// simple. First a message header is sent, followed by the data. The size
// of the data is specified in the header.
//

// Message types
typedef enum {
MV_BLOCK,
MV_STACKTRACE
} MessageType;

typedef struct {
int mySize;
int myType;
} Header;

#define MV_STACKTRACE_BUFSIZE 4096

#define theBlockSize (1024*32)
#define theBlockCount 4

#define theAddrMask 0x001FFFFFFFFFFFFFul

Expand All @@ -15,7 +33,7 @@
// Order is important here - we use a max() for downsampling, which will
// cause reads to be preferred over writes when the event time matches. If
// you update these values, you will also need to update the shader.frag
// code to handle it.
// code.
#define theTypeAlloc 0
#define theTypeInstr 1
#define theTypeWrite 2
Expand All @@ -31,13 +49,10 @@
typedef struct {
unsigned long long myAddr[theBlockSize];
unsigned int myEntries;
volatile int myWSem;
volatile int myRSem;
} TraceBlock;

typedef struct {
TraceBlock myBlocks[theBlockCount];
} SharedData;
// Template for future work
typedef struct {} SharedData;

#endif

80 changes: 59 additions & 21 deletions tool/mv_main.c
Expand Up @@ -68,33 +68,69 @@ static TraceBlock *theBlock = 0;
static TraceBlock theBlockData;
// Data for shm
static SharedData *theSharedData = 0;
static int theIdx = 0;

typedef unsigned long long uint64;
static uint64 theTotalEvents = 0;

static void flush_data(void)
typedef struct {
HChar buf[MV_STACKTRACE_BUFSIZE];
int size;
} SizedBuffer;

static SizedBuffer theStackTrace;

static void appendIpDesc(UInt n, Addr ip, void* uu_opaque)
{
//VG_(get_and_pp_StackTrace)(VG_(get_running_tid)(), 8);
if (clo_shared_mem)
{
// Post the full block
__sync_bool_compare_and_swap(&theBlock->myRSem, 0, 1);
SizedBuffer *sbuf = (SizedBuffer *)uu_opaque;
HChar tmp[MV_STACKTRACE_BUFSIZE];

VG_(describe_IP)(ip, tmp, MV_STACKTRACE_BUFSIZE);

theIdx++;
if (theIdx == theBlockCount)
theIdx = 0;
int available = MV_STACKTRACE_BUFSIZE - sbuf->size;
int len =
VG_(snprintf)(
&sbuf->buf[sbuf->size],
available,
"%s %s\n",
( n == 0 ? "at" : "by" ), tmp);

theBlock = &theSharedData->myBlocks[theIdx];
if (len >= available)
sbuf->size += available-1;
else
sbuf->size += len;
}

// Wait until we can write to the new theBlock
while (!__sync_bool_compare_and_swap(&theBlock->myWSem, 1, 0))
;
static void flush_data(void)
{
if (clo_shared_mem)
{
}
else if (clo_pipe)
{
Header header;
header.myType = MV_BLOCK;

VG_(write)(clo_pipe, &header, sizeof(Header));
VG_(write)(clo_pipe, &theBlockData, sizeof(theBlockData));

Addr ips[8];
UInt n_ips = VG_(get_StackTrace)(
VG_(get_running_tid)(),
ips, 8,
NULL/*array to dump SP values in*/,
NULL/*array to dump FP values in*/,
0/*first_ip_delta*/);

theStackTrace.size = 0;
VG_(apply_StackTrace)(appendIpDesc, &theStackTrace, ips, n_ips);

header.myType = MV_STACKTRACE;
header.mySize = theStackTrace.size+1; // Include terminating '\0'

VG_(write)(clo_pipe, &header, sizeof(Header));
VG_(write)(clo_pipe, theStackTrace.buf, header.mySize);
}

#if 0
VG_(printf)("flush_data: %d\n", theBlock->myEntries);
int i;
Expand All @@ -103,6 +139,7 @@ static void flush_data(void)
VG_(printf)("addr: %llx\n", theBlock->myAddr[i]);
}
#endif

theTotalEvents += theBlock->myEntries;
theBlock->myEntries = 0;
}
Expand Down Expand Up @@ -289,12 +326,6 @@ static VG_REGPARM(3) void trace_storeload(Addr addr, Addr addr2, SizeT size)
put_data(addr2, theShiftedRead, (uint64)size);
}

/* assign value to tmp */
static inline
void assign ( IRSB* sb, IRTemp tmp, IRExpr* expr ) {
addStmtToIRSB( sb, IRStmt_WrTmp(tmp, expr) );
}

/* build various kinds of expressions */
#define triop(_op, _arg1, _arg2, _arg3) \
assignNew(sb, IRExpr_Triop((_op),(_arg1),(_arg2),(_arg3)))
Expand All @@ -308,6 +339,12 @@ void assign ( IRSB* sb, IRTemp tmp, IRExpr* expr ) {
#define mkV128(_n) IRExpr_Const(IRConst_V128(_n))
#define mkexpr(_tmp) IRExpr_RdTmp((_tmp))

/* assign value to tmp */
static inline
void assign ( IRSB* sb, IRTemp tmp, IRExpr* expr ) {
addStmtToIRSB( sb, IRStmt_WrTmp(tmp, expr) );
}

static IRAtom* assignNew ( IRSB* sb, IRExpr* e )
{
IRTemp t;
Expand Down Expand Up @@ -742,7 +779,8 @@ static void mv_post_clo_init(void)
theSharedData = (SharedData *)(Addr)sr_Res(res);
VG_(dmsg)("got memory %p\n", theSharedData);

theBlock = &theSharedData->myBlocks[0];
VG_(umsg)("shared memory interface not implemented\n");
VG_(exit)(1);
}
else
{
Expand Down

0 comments on commit ebc9cdd

Please sign in to comment.