2012-06-13 08:35:31 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2012-06-12 10:06:21 -07:00
|
|
|
|
|
|
|
#include "mozilla/VisualEventTracer.h"
|
|
|
|
#include "mozilla/Monitor.h"
|
|
|
|
#include "mozilla/TimeStamp.h"
|
|
|
|
#include "nscore.h"
|
|
|
|
#include "prthread.h"
|
|
|
|
#include "prprf.h"
|
|
|
|
#include "prenv.h"
|
2012-07-11 11:20:17 -07:00
|
|
|
#include "plstr.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2012-06-12 10:06:21 -07:00
|
|
|
|
|
|
|
namespace mozilla { namespace eventtracer {
|
|
|
|
|
|
|
|
#ifdef MOZ_VISUAL_EVENT_TRACER
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2013-03-28 10:38:05 -07:00
|
|
|
const uint32_t kBatchSize = 256;
|
2012-06-12 10:06:21 -07:00
|
|
|
const char kTypeChars[eventtracer::eLast] = {' ','N','S','W','E','D'};
|
|
|
|
|
|
|
|
// Flushing thread and records queue monitor
|
2012-07-30 07:20:58 -07:00
|
|
|
mozilla::Monitor * gMonitor = nullptr;
|
2012-06-12 10:06:21 -07:00
|
|
|
|
2013-07-09 07:43:42 -07:00
|
|
|
// gInitialized and gCapture can be accessed from multiple threads
|
|
|
|
// simultaneously without any locking. However, since they are only ever
|
|
|
|
// *set* from the main thread, the chance of races manifesting is small
|
|
|
|
// and unlikely to be a problem in practice.
|
|
|
|
bool gInitialized;
|
2013-03-28 10:38:05 -07:00
|
|
|
|
|
|
|
// Flag to allow capturing
|
2013-07-09 07:43:42 -07:00
|
|
|
bool gCapture;
|
2013-03-28 10:38:05 -07:00
|
|
|
|
|
|
|
// Time stamp of the epoch we have started to capture
|
|
|
|
mozilla::TimeStamp * gProfilerStart;
|
|
|
|
|
|
|
|
// Duration of the log to keep up to
|
|
|
|
mozilla::TimeDuration * gMaxBacklogTime;
|
|
|
|
|
2012-06-12 10:06:21 -07:00
|
|
|
|
|
|
|
// Record of a single event
|
|
|
|
class Record {
|
|
|
|
public:
|
2014-05-05 10:30:39 -07:00
|
|
|
Record()
|
2012-06-12 10:06:21 -07:00
|
|
|
: mType(::mozilla::eventtracer::eNone)
|
2012-07-30 07:20:58 -07:00
|
|
|
, mItem(nullptr)
|
|
|
|
, mText(nullptr)
|
2014-05-05 10:30:39 -07:00
|
|
|
, mText2(nullptr)
|
2012-06-12 10:06:21 -07:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(Record);
|
2014-05-05 10:30:39 -07:00
|
|
|
}
|
2013-03-28 10:38:05 -07:00
|
|
|
|
|
|
|
Record& operator=(const Record & aOther)
|
|
|
|
{
|
|
|
|
mType = aOther.mType;
|
|
|
|
mTime = aOther.mTime;
|
|
|
|
mItem = aOther.mItem;
|
|
|
|
mText = PL_strdup(aOther.mText);
|
|
|
|
mText2 = aOther.mText2 ? PL_strdup(aOther.mText2) : nullptr;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-05-05 10:30:39 -07:00
|
|
|
~Record()
|
2012-06-12 10:06:21 -07:00
|
|
|
{
|
|
|
|
PL_strfree(mText2);
|
2014-05-05 10:30:39 -07:00
|
|
|
PL_strfree(mText);
|
2012-06-12 10:06:21 -07:00
|
|
|
MOZ_COUNT_DTOR(Record);
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mType;
|
2013-03-28 10:38:05 -07:00
|
|
|
TimeStamp mTime;
|
2012-06-12 10:06:21 -07:00
|
|
|
void * mItem;
|
|
|
|
char * mText;
|
|
|
|
char * mText2;
|
|
|
|
};
|
|
|
|
|
2012-07-11 11:20:17 -07:00
|
|
|
char * DupCurrentThreadName()
|
|
|
|
{
|
|
|
|
if (NS_IsMainThread())
|
|
|
|
return PL_strdup("Main Thread");
|
|
|
|
|
|
|
|
PRThread * currentThread = PR_GetCurrentThread();
|
|
|
|
const char * name = PR_GetThreadName(currentThread);
|
|
|
|
if (name)
|
|
|
|
return PL_strdup(name);
|
|
|
|
|
|
|
|
char buffer[128];
|
|
|
|
PR_snprintf(buffer, 127, "Nameless %p", currentThread);
|
|
|
|
|
|
|
|
return PL_strdup(buffer);
|
|
|
|
}
|
|
|
|
|
2012-06-12 10:06:21 -07:00
|
|
|
// An array of events, each thread keeps its own private instance
|
|
|
|
class RecordBatch {
|
|
|
|
public:
|
2013-03-28 10:38:05 -07:00
|
|
|
RecordBatch(size_t aLength = kBatchSize,
|
|
|
|
char * aThreadName = DupCurrentThreadName())
|
|
|
|
: mRecordsHead(new Record[aLength])
|
|
|
|
, mRecordsTail(mRecordsHead + aLength)
|
2012-06-12 10:06:21 -07:00
|
|
|
, mNextRecord(mRecordsHead)
|
2012-07-30 07:20:58 -07:00
|
|
|
, mNextBatch(nullptr)
|
2013-03-28 10:38:05 -07:00
|
|
|
, mThreadNameCopy(aThreadName)
|
|
|
|
, mClosed(false)
|
2012-06-12 10:06:21 -07:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(RecordBatch);
|
|
|
|
}
|
|
|
|
|
|
|
|
~RecordBatch()
|
|
|
|
{
|
|
|
|
delete [] mRecordsHead;
|
|
|
|
PL_strfree(mThreadNameCopy);
|
|
|
|
MOZ_COUNT_DTOR(RecordBatch);
|
|
|
|
}
|
|
|
|
|
2013-03-28 10:38:05 -07:00
|
|
|
void Close() { mClosed = true; }
|
|
|
|
|
|
|
|
size_t Length() const { return mNextRecord - mRecordsHead; }
|
|
|
|
bool CanBeDeleted(const TimeStamp& aUntil) const;
|
|
|
|
|
|
|
|
static RecordBatch * Register();
|
|
|
|
static void Close(void * data); // Registered on freeing thread data
|
|
|
|
static RecordBatch * Clone(RecordBatch * aLog, const TimeStamp& aSince);
|
|
|
|
static void Delete(RecordBatch * aLog);
|
|
|
|
|
|
|
|
static RecordBatch * CloneLog();
|
|
|
|
static void GCLog(const TimeStamp& aUntil);
|
|
|
|
static void DeleteLog();
|
2012-06-12 10:06:21 -07:00
|
|
|
|
|
|
|
Record * mRecordsHead;
|
|
|
|
Record * mRecordsTail;
|
2013-07-09 07:43:42 -07:00
|
|
|
Record * mNextRecord;
|
2012-06-12 10:06:21 -07:00
|
|
|
|
|
|
|
RecordBatch * mNextBatch;
|
|
|
|
char * mThreadNameCopy;
|
2013-03-28 10:38:05 -07:00
|
|
|
bool mClosed;
|
2012-06-12 10:06:21 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// Protected by gMonitor, accessed concurently
|
|
|
|
// Linked list of batches threads want to flush on disk
|
2013-07-09 07:43:42 -07:00
|
|
|
RecordBatch * gLogHead = nullptr;
|
|
|
|
RecordBatch * gLogTail = nullptr;
|
2013-03-28 10:38:05 -07:00
|
|
|
|
|
|
|
// Registers the batch in the linked list
|
|
|
|
// static
|
|
|
|
RecordBatch *
|
|
|
|
RecordBatch::Register()
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(*gMonitor);
|
2012-06-12 10:06:21 -07:00
|
|
|
|
2013-03-28 10:38:05 -07:00
|
|
|
if (!gInitialized)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if (gLogHead)
|
|
|
|
RecordBatch::GCLog(TimeStamp::Now() - *gMaxBacklogTime);
|
|
|
|
|
|
|
|
RecordBatch * batch = new RecordBatch();
|
|
|
|
if (!gLogHead)
|
|
|
|
gLogHead = batch;
|
|
|
|
else // gLogTail is non-null
|
|
|
|
gLogTail->mNextBatch = batch;
|
|
|
|
gLogTail = batch;
|
|
|
|
|
|
|
|
mon.Notify();
|
|
|
|
return batch;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RecordBatch::Close(void * data)
|
|
|
|
{
|
|
|
|
RecordBatch * batch = static_cast<RecordBatch*>(data);
|
|
|
|
batch->Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
RecordBatch *
|
|
|
|
RecordBatch::Clone(RecordBatch * aOther, const TimeStamp& aSince)
|
|
|
|
{
|
|
|
|
if (!aOther)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
size_t length = aOther->Length();
|
|
|
|
size_t min = 0;
|
|
|
|
size_t max = length;
|
|
|
|
Record * record = nullptr;
|
|
|
|
|
|
|
|
// Binary search for record with time >= aSince
|
|
|
|
size_t i;
|
|
|
|
while (min < max) {
|
|
|
|
i = (max + min) / 2;
|
|
|
|
|
|
|
|
record = aOther->mRecordsHead + i;
|
|
|
|
if (record->mTime >= aSince)
|
|
|
|
max = i;
|
|
|
|
else
|
|
|
|
min = i+1;
|
|
|
|
}
|
|
|
|
i = (max + min) / 2;
|
|
|
|
|
|
|
|
// How many Record's to copy?
|
|
|
|
size_t toCopy = length - i;
|
|
|
|
if (!toCopy)
|
|
|
|
return RecordBatch::Clone(aOther->mNextBatch, aSince);
|
|
|
|
|
|
|
|
// Clone
|
|
|
|
RecordBatch * clone = new RecordBatch(toCopy, PL_strdup(aOther->mThreadNameCopy));
|
|
|
|
for (; i < length; ++i) {
|
|
|
|
record = aOther->mRecordsHead + i;
|
|
|
|
*clone->mNextRecord = *record;
|
|
|
|
++clone->mNextRecord;
|
|
|
|
}
|
|
|
|
clone->mNextBatch = RecordBatch::Clone(aOther->mNextBatch, aSince);
|
|
|
|
|
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2012-06-12 10:06:21 -07:00
|
|
|
void
|
2013-03-28 10:38:05 -07:00
|
|
|
RecordBatch::Delete(RecordBatch * aLog)
|
|
|
|
{
|
|
|
|
while (aLog) {
|
|
|
|
RecordBatch * batch = aLog;
|
|
|
|
aLog = aLog->mNextBatch;
|
|
|
|
delete batch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
RecordBatch *
|
|
|
|
RecordBatch::CloneLog()
|
2012-06-12 10:06:21 -07:00
|
|
|
{
|
2013-03-28 10:38:05 -07:00
|
|
|
TimeStamp startEpoch = *gProfilerStart;
|
|
|
|
TimeStamp backlogEpoch = TimeStamp::Now() - *gMaxBacklogTime;
|
|
|
|
|
|
|
|
TimeStamp since = (startEpoch > backlogEpoch) ? startEpoch : backlogEpoch;
|
2012-06-12 10:06:21 -07:00
|
|
|
|
|
|
|
MonitorAutoLock mon(*gMonitor);
|
|
|
|
|
2013-03-28 10:38:05 -07:00
|
|
|
return RecordBatch::Clone(gLogHead, since);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void
|
|
|
|
RecordBatch::GCLog(const TimeStamp& aUntil)
|
|
|
|
{
|
|
|
|
// Garbage collect all unreferenced and old batches
|
|
|
|
gMonitor->AssertCurrentThreadOwns();
|
|
|
|
|
|
|
|
RecordBatch *volatile * referer = &gLogHead;
|
|
|
|
gLogTail = nullptr;
|
|
|
|
|
|
|
|
RecordBatch * batch = *referer;
|
|
|
|
while (batch) {
|
|
|
|
if (batch->CanBeDeleted(aUntil)) {
|
|
|
|
// The batch is completed and thus unreferenced by the thread
|
|
|
|
// and the most recent record has time older then the time
|
|
|
|
// we want to save records for, hence delete it.
|
|
|
|
*referer = batch->mNextBatch;
|
|
|
|
delete batch;
|
|
|
|
batch = *referer;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// We walk the whole list, so this will end up filled with
|
|
|
|
// the very last valid element of it.
|
|
|
|
gLogTail = batch;
|
|
|
|
// The current batch is active, examine the next in the list.
|
|
|
|
batch = batch->mNextBatch;
|
|
|
|
// When the next batch is found expired, we must extract it
|
|
|
|
// from the list, shift the referer.
|
|
|
|
referer = &((*referer)->mNextBatch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void
|
|
|
|
RecordBatch::DeleteLog()
|
|
|
|
{
|
|
|
|
RecordBatch * batch;
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(*gMonitor);
|
|
|
|
batch = gLogHead;
|
|
|
|
gLogHead = nullptr;
|
|
|
|
gLogTail = nullptr;
|
2012-06-12 10:06:21 -07:00
|
|
|
}
|
|
|
|
|
2013-03-28 10:38:05 -07:00
|
|
|
RecordBatch::Delete(batch);
|
|
|
|
}
|
2012-06-12 10:06:21 -07:00
|
|
|
|
2013-03-28 10:38:05 -07:00
|
|
|
bool
|
|
|
|
RecordBatch::CanBeDeleted(const TimeStamp& aUntil) const
|
|
|
|
{
|
|
|
|
if (mClosed) {
|
|
|
|
// This flag is set when a thread releases this batch as
|
|
|
|
// its private data. It happens when the list is full or
|
|
|
|
// when the thread ends its job. We must not delete this
|
|
|
|
// batch from memory while it's held by a thread.
|
|
|
|
|
|
|
|
if (!Length()) {
|
|
|
|
// There are no records, just get rid of this empty batch.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((mNextRecord-1)->mTime <= aUntil) {
|
|
|
|
// Is the last record older then the time we demand records
|
|
|
|
// for? If not, this batch has expired.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not all conditions to close the batch met, keep it.
|
|
|
|
return false;
|
2012-06-12 10:06:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Helper class for filtering events by MOZ_PROFILING_EVENTS
|
|
|
|
class EventFilter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static EventFilter * Build(const char * filterVar);
|
|
|
|
bool EventPasses(const char * eventName);
|
|
|
|
|
|
|
|
~EventFilter()
|
|
|
|
{
|
|
|
|
delete mNext;
|
|
|
|
PL_strfree(mFilter);
|
|
|
|
MOZ_COUNT_DTOR(EventFilter);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
EventFilter(const char * eventName, EventFilter * next)
|
|
|
|
: mFilter(PL_strdup(eventName))
|
|
|
|
, mNext(next)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(EventFilter);
|
|
|
|
}
|
|
|
|
|
|
|
|
char * mFilter;
|
|
|
|
EventFilter * mNext;
|
|
|
|
};
|
|
|
|
|
|
|
|
// static
|
|
|
|
EventFilter *
|
|
|
|
EventFilter::Build(const char * filterVar)
|
|
|
|
{
|
|
|
|
if (!filterVar || !*filterVar)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2012-06-12 10:06:21 -07:00
|
|
|
|
|
|
|
// Reads a comma serpatated list of events.
|
|
|
|
|
|
|
|
// Copied from nspr logging code (read of NSPR_LOG_MODULES)
|
|
|
|
char eventName[64];
|
2013-07-25 09:30:07 -07:00
|
|
|
int pos = 0, count, delta = 0;
|
2012-06-12 10:06:21 -07:00
|
|
|
|
|
|
|
// Read up to a comma or EOF -> get name of an event first in the list
|
|
|
|
count = sscanf(filterVar, "%63[^,]%n", eventName, &delta);
|
2014-05-05 10:30:39 -07:00
|
|
|
if (count == 0)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2012-06-12 10:06:21 -07:00
|
|
|
|
|
|
|
pos = delta;
|
|
|
|
|
|
|
|
// Skip a comma, if present, accept spaces around it
|
|
|
|
count = sscanf(filterVar + pos, " , %n", &delta);
|
|
|
|
if (count != EOF)
|
|
|
|
pos += delta;
|
|
|
|
|
|
|
|
// eventName contains name of the first event in the list
|
|
|
|
// second argument recursively parses the rest of the list string and
|
|
|
|
// fills mNext of the just created EventFilter object chaining the objects
|
|
|
|
return new EventFilter(eventName, Build(filterVar + pos));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
EventFilter::EventPasses(const char * eventName)
|
|
|
|
{
|
|
|
|
if (!strcmp(eventName, mFilter))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (mNext)
|
|
|
|
return mNext->EventPasses(eventName);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-05 10:30:39 -07:00
|
|
|
// State and control variables, initialized in Init() method, after it
|
2012-06-12 10:06:21 -07:00
|
|
|
// immutable and read concurently.
|
2012-07-30 07:20:58 -07:00
|
|
|
EventFilter * gEventFilter = nullptr;
|
2012-08-22 08:56:38 -07:00
|
|
|
unsigned gThreadPrivateIndex;
|
2012-06-12 10:06:21 -07:00
|
|
|
|
|
|
|
// static
|
2012-08-22 08:56:38 -07:00
|
|
|
bool CheckEventFilters(uint32_t aType, void * aItem, const char * aText)
|
2012-06-12 10:06:21 -07:00
|
|
|
{
|
|
|
|
if (!gEventFilter)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (aType == eName)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return gEventFilter->EventPasses(aText);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anon namespace
|
|
|
|
|
|
|
|
#endif //MOZ_VISUAL_EVENT_TRACER
|
|
|
|
|
2014-05-05 10:30:39 -07:00
|
|
|
// static
|
2012-06-12 10:06:21 -07:00
|
|
|
void Init()
|
|
|
|
{
|
|
|
|
#ifdef MOZ_VISUAL_EVENT_TRACER
|
|
|
|
const char * logEvents = PR_GetEnv("MOZ_PROFILING_EVENTS");
|
|
|
|
if (logEvents && *logEvents)
|
|
|
|
gEventFilter = EventFilter::Build(logEvents);
|
|
|
|
|
2013-03-28 10:38:05 -07:00
|
|
|
PRStatus status = PR_NewThreadPrivateIndex(&gThreadPrivateIndex, &RecordBatch::Close);
|
2012-06-12 10:06:21 -07:00
|
|
|
if (status != PR_SUCCESS)
|
|
|
|
return;
|
|
|
|
|
|
|
|
gMonitor = new mozilla::Monitor("Profiler");
|
|
|
|
if (!gMonitor)
|
|
|
|
return;
|
|
|
|
|
2013-03-28 10:38:05 -07:00
|
|
|
gProfilerStart = new mozilla::TimeStamp();
|
|
|
|
gMaxBacklogTime = new mozilla::TimeDuration();
|
2012-06-12 10:06:21 -07:00
|
|
|
|
2013-03-28 10:38:05 -07:00
|
|
|
gInitialized = true;
|
2012-06-12 10:06:21 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-05-05 10:30:39 -07:00
|
|
|
// static
|
2012-06-12 10:06:21 -07:00
|
|
|
void Shutdown()
|
|
|
|
{
|
|
|
|
#ifdef MOZ_VISUAL_EVENT_TRACER
|
2013-03-28 10:38:05 -07:00
|
|
|
gCapture = false;
|
|
|
|
gInitialized = false;
|
2012-06-12 10:06:21 -07:00
|
|
|
|
2013-03-28 10:38:05 -07:00
|
|
|
RecordBatch::DeleteLog();
|
2012-06-12 10:06:21 -07:00
|
|
|
|
|
|
|
if (gMonitor) {
|
|
|
|
delete gMonitor;
|
2012-07-30 07:20:58 -07:00
|
|
|
gMonitor = nullptr;
|
2012-06-12 10:06:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (gEventFilter) {
|
|
|
|
delete gEventFilter;
|
2012-07-30 07:20:58 -07:00
|
|
|
gEventFilter = nullptr;
|
2012-06-12 10:06:21 -07:00
|
|
|
}
|
2013-03-28 10:38:05 -07:00
|
|
|
|
|
|
|
if (gProfilerStart) {
|
|
|
|
delete gProfilerStart;
|
|
|
|
gProfilerStart = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gMaxBacklogTime) {
|
|
|
|
delete gMaxBacklogTime;
|
|
|
|
gMaxBacklogTime = nullptr;
|
|
|
|
}
|
2012-06-12 10:06:21 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-05-05 10:30:39 -07:00
|
|
|
// static
|
2012-08-22 08:56:38 -07:00
|
|
|
void Mark(uint32_t aType, void * aItem, const char * aText, const char * aText2)
|
2012-06-12 10:06:21 -07:00
|
|
|
{
|
|
|
|
#ifdef MOZ_VISUAL_EVENT_TRACER
|
2013-03-28 10:38:05 -07:00
|
|
|
if (!gInitialized || !gCapture)
|
2012-06-12 10:06:21 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (aType == eNone)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!CheckEventFilters(aType, aItem, aText)) // Events use just aText
|
|
|
|
return;
|
|
|
|
|
|
|
|
RecordBatch * threadLogPrivate = static_cast<RecordBatch *>(
|
2014-05-05 10:30:39 -07:00
|
|
|
PR_GetThreadPrivate(gThreadPrivateIndex));
|
2012-06-12 10:06:21 -07:00
|
|
|
if (!threadLogPrivate) {
|
2013-03-28 10:38:05 -07:00
|
|
|
threadLogPrivate = RecordBatch::Register();
|
|
|
|
if (!threadLogPrivate)
|
|
|
|
return;
|
|
|
|
|
2012-06-12 10:06:21 -07:00
|
|
|
PR_SetThreadPrivate(gThreadPrivateIndex, threadLogPrivate);
|
|
|
|
}
|
|
|
|
|
|
|
|
Record * record = threadLogPrivate->mNextRecord;
|
|
|
|
record->mType = aType;
|
2013-03-28 10:38:05 -07:00
|
|
|
record->mTime = mozilla::TimeStamp::Now();
|
2012-06-12 10:06:21 -07:00
|
|
|
record->mItem = aItem;
|
|
|
|
record->mText = PL_strdup(aText);
|
2012-07-30 07:20:58 -07:00
|
|
|
record->mText2 = aText2 ? PL_strdup(aText2) : nullptr;
|
2012-06-12 10:06:21 -07:00
|
|
|
|
|
|
|
++threadLogPrivate->mNextRecord;
|
|
|
|
if (threadLogPrivate->mNextRecord == threadLogPrivate->mRecordsTail) {
|
2013-03-28 10:38:05 -07:00
|
|
|
// Calls RecordBatch::Close(threadLogPrivate) that marks
|
|
|
|
// the batch as OK to be deleted from memory when no longer needed.
|
2012-07-30 07:20:58 -07:00
|
|
|
PR_SetThreadPrivate(gThreadPrivateIndex, nullptr);
|
2012-06-12 10:06:21 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-03-28 10:38:05 -07:00
|
|
|
|
|
|
|
#ifdef MOZ_VISUAL_EVENT_TRACER
|
|
|
|
|
|
|
|
// The scriptable classes
|
|
|
|
|
|
|
|
class VisualEventTracerLog : public nsIVisualEventTracerLog
|
|
|
|
{
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIVISUALEVENTTRACERLOG
|
|
|
|
|
|
|
|
VisualEventTracerLog(RecordBatch* aBatch)
|
|
|
|
: mBatch(aBatch)
|
|
|
|
, mProfilerStart(*gProfilerStart)
|
|
|
|
{}
|
|
|
|
|
|
|
|
virtual ~VisualEventTracerLog();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
RecordBatch * mBatch;
|
|
|
|
TimeStamp mProfilerStart;
|
|
|
|
};
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(VisualEventTracerLog, nsIVisualEventTracerLog)
|
2013-03-28 10:38:05 -07:00
|
|
|
|
|
|
|
VisualEventTracerLog::~VisualEventTracerLog()
|
|
|
|
{
|
|
|
|
RecordBatch::Delete(mBatch);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
VisualEventTracerLog::GetJSONString(nsACString & _retval)
|
|
|
|
{
|
|
|
|
nsCString buffer;
|
|
|
|
|
|
|
|
buffer.Assign(NS_LITERAL_CSTRING("{\n\"version\": 1,\n\"records\":[\n"));
|
|
|
|
|
|
|
|
RecordBatch * batch = mBatch;
|
|
|
|
while (batch) {
|
|
|
|
if (batch != mBatch) {
|
|
|
|
// This is not the first batch we are writting, add comma
|
|
|
|
buffer.Append(NS_LITERAL_CSTRING(",\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer.Append(NS_LITERAL_CSTRING("{\"thread\":\""));
|
|
|
|
buffer.Append(batch->mThreadNameCopy);
|
|
|
|
buffer.Append(NS_LITERAL_CSTRING("\",\"log\":[\n"));
|
|
|
|
|
|
|
|
static const int kBufferSize = 2048;
|
|
|
|
char buf[kBufferSize];
|
|
|
|
|
|
|
|
for (Record * record = batch->mRecordsHead;
|
|
|
|
record < batch->mNextRecord;
|
|
|
|
++record) {
|
|
|
|
|
|
|
|
// mType carries both type and flags, separate type
|
|
|
|
// as lower 16 bits and flags as higher 16 bits.
|
|
|
|
// The json format expects this separated.
|
|
|
|
uint32_t type = record->mType & 0xffffUL;
|
|
|
|
uint32_t flags = record->mType >> 16;
|
|
|
|
PR_snprintf(buf, kBufferSize,
|
2013-05-01 09:57:57 -07:00
|
|
|
"{\"e\":\"%c\",\"t\":%llu,\"f\":%d,\"i\":\"%p\",\"n\":\"%s%s\"}%s\n",
|
2013-03-28 10:38:05 -07:00
|
|
|
kTypeChars[type],
|
2013-05-01 09:57:57 -07:00
|
|
|
static_cast<uint64_t>((record->mTime - mProfilerStart).ToMilliseconds()),
|
2013-03-28 10:38:05 -07:00
|
|
|
flags,
|
|
|
|
record->mItem,
|
|
|
|
record->mText,
|
|
|
|
record->mText2 ? record->mText2 : "",
|
|
|
|
(record == batch->mNextRecord - 1) ? "" : ",");
|
|
|
|
|
|
|
|
buffer.Append(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer.Append(NS_LITERAL_CSTRING("]}\n"));
|
|
|
|
|
|
|
|
RecordBatch * next = batch->mNextBatch;
|
|
|
|
batch = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer.Append(NS_LITERAL_CSTRING("]}\n"));
|
|
|
|
_retval.Assign(buffer);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-07-25 09:21:58 -07:00
|
|
|
nsresult
|
|
|
|
VisualEventTracerLog::WriteToProfilingFile()
|
|
|
|
{
|
|
|
|
const char* filename = PR_GetEnv("MOZ_TRACE_FILE");
|
|
|
|
if (!filename) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRFileDesc* fd = PR_Open(filename, PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE,
|
2014-05-05 10:30:39 -07:00
|
|
|
0644);
|
2013-07-25 09:21:58 -07:00
|
|
|
if (!fd) {
|
|
|
|
return NS_ERROR_FILE_ACCESS_DENIED;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString json;
|
|
|
|
GetJSONString(json);
|
|
|
|
|
|
|
|
int32_t bytesWritten = PR_Write(fd, json.get(), json.Length());
|
|
|
|
PR_Close(fd);
|
|
|
|
|
|
|
|
if (bytesWritten < json.Length()) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(VisualEventTracer, nsIVisualEventTracer)
|
2013-03-28 10:38:05 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
VisualEventTracer::Start(const uint32_t aMaxBacklogSeconds)
|
|
|
|
{
|
|
|
|
if (!gInitialized)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
if (gCapture) {
|
|
|
|
NS_WARNING("VisualEventTracer has already been started");
|
|
|
|
return NS_ERROR_ALREADY_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
*gMaxBacklogTime = TimeDuration::FromMilliseconds(aMaxBacklogSeconds * 1000);
|
|
|
|
|
|
|
|
*gProfilerStart = mozilla::TimeStamp::Now();
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(*gMonitor);
|
|
|
|
RecordBatch::GCLog(*gProfilerStart);
|
|
|
|
}
|
|
|
|
gCapture = true;
|
|
|
|
|
|
|
|
MOZ_EVENT_TRACER_MARK(this, "trace::start");
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
VisualEventTracer::Stop()
|
|
|
|
{
|
|
|
|
if (!gInitialized)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
if (!gCapture) {
|
|
|
|
NS_WARNING("VisualEventTracer is not runing");
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_EVENT_TRACER_MARK(this, "trace::stop");
|
|
|
|
|
|
|
|
gCapture = false;
|
|
|
|
|
2013-07-25 09:21:58 -07:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (PR_GetEnv("MOZ_TRACE_FILE")) {
|
|
|
|
nsCOMPtr<nsIVisualEventTracerLog> tracelog;
|
|
|
|
rv = Snapshot(getter_AddRefs(tracelog));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = tracelog->WriteToProfilingFile();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2013-03-28 10:38:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
VisualEventTracer::Snapshot(nsIVisualEventTracerLog ** _result)
|
|
|
|
{
|
|
|
|
if (!gInitialized)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
RecordBatch * batch = RecordBatch::CloneLog();
|
|
|
|
|
|
|
|
nsRefPtr<VisualEventTracerLog> log = new VisualEventTracerLog(batch);
|
|
|
|
log.forget(_result);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2012-06-12 10:06:21 -07:00
|
|
|
} // eventtracer
|
|
|
|
} // mozilla
|