2014-06-30 19:39:45 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-06-13 19:35:31 +04: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 21:06:21 +04: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 22:20:17 +04:00
|
|
|
#include "plstr.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2012-06-12 21:06:21 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace eventtracer {
|
2012-06-12 21:06:21 +04:00
|
|
|
|
|
|
|
#ifdef MOZ_VISUAL_EVENT_TRACER
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2013-03-28 21:38:05 +04:00
|
|
|
const uint32_t kBatchSize = 256;
|
2014-05-13 21:41:38 +04:00
|
|
|
const char kTypeChars[eventtracer::eLast] = {' ', 'N', 'S', 'W', 'E', 'D'};
|
2012-06-12 21:06:21 +04:00
|
|
|
|
|
|
|
// Flushing thread and records queue monitor
|
2014-05-13 21:41:38 +04:00
|
|
|
mozilla::Monitor* gMonitor = nullptr;
|
2012-06-12 21:06:21 +04:00
|
|
|
|
2013-07-09 18:43:42 +04: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 21:38:05 +04:00
|
|
|
|
|
|
|
// Flag to allow capturing
|
2013-07-09 18:43:42 +04:00
|
|
|
bool gCapture;
|
2013-03-28 21:38:05 +04:00
|
|
|
|
|
|
|
// Time stamp of the epoch we have started to capture
|
2014-05-13 21:41:38 +04:00
|
|
|
mozilla::TimeStamp* gProfilerStart;
|
2013-03-28 21:38:05 +04:00
|
|
|
|
|
|
|
// Duration of the log to keep up to
|
2014-05-13 21:41:38 +04:00
|
|
|
mozilla::TimeDuration* gMaxBacklogTime;
|
2013-03-28 21:38:05 +04:00
|
|
|
|
2012-06-12 21:06:21 +04:00
|
|
|
|
|
|
|
// Record of a single event
|
2014-05-13 21:41:38 +04:00
|
|
|
class Record
|
|
|
|
{
|
2012-06-12 21:06:21 +04:00
|
|
|
public:
|
2014-05-05 21:30:39 +04:00
|
|
|
Record()
|
2012-06-12 21:06:21 +04:00
|
|
|
: mType(::mozilla::eventtracer::eNone)
|
2012-07-30 18:20:58 +04:00
|
|
|
, mItem(nullptr)
|
|
|
|
, mText(nullptr)
|
2014-05-05 21:30:39 +04:00
|
|
|
, mText2(nullptr)
|
2012-06-12 21:06:21 +04:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(Record);
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2013-03-28 21:38:05 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
Record& operator=(const Record& aOther)
|
2013-03-28 21:38:05 +04:00
|
|
|
{
|
|
|
|
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 21:30:39 +04:00
|
|
|
~Record()
|
2012-06-12 21:06:21 +04:00
|
|
|
{
|
|
|
|
PL_strfree(mText2);
|
2014-05-05 21:30:39 +04:00
|
|
|
PL_strfree(mText);
|
2012-06-12 21:06:21 +04:00
|
|
|
MOZ_COUNT_DTOR(Record);
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mType;
|
2013-03-28 21:38:05 +04:00
|
|
|
TimeStamp mTime;
|
2014-05-13 21:41:38 +04:00
|
|
|
void* mItem;
|
|
|
|
char* mText;
|
|
|
|
char* mText2;
|
2012-06-12 21:06:21 +04:00
|
|
|
};
|
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
char* DupCurrentThreadName()
|
2012-07-11 22:20:17 +04:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
if (NS_IsMainThread()) {
|
2012-07-11 22:20:17 +04:00
|
|
|
return PL_strdup("Main Thread");
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2012-07-11 22:20:17 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
PRThread* currentThread = PR_GetCurrentThread();
|
|
|
|
const char* name = PR_GetThreadName(currentThread);
|
|
|
|
if (name) {
|
2012-07-11 22:20:17 +04:00
|
|
|
return PL_strdup(name);
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2012-07-11 22:20:17 +04:00
|
|
|
|
|
|
|
char buffer[128];
|
|
|
|
PR_snprintf(buffer, 127, "Nameless %p", currentThread);
|
|
|
|
|
|
|
|
return PL_strdup(buffer);
|
|
|
|
}
|
|
|
|
|
2012-06-12 21:06:21 +04:00
|
|
|
// An array of events, each thread keeps its own private instance
|
2014-05-13 21:41:38 +04:00
|
|
|
class RecordBatch
|
|
|
|
{
|
2012-06-12 21:06:21 +04:00
|
|
|
public:
|
2013-03-28 21:38:05 +04:00
|
|
|
RecordBatch(size_t aLength = kBatchSize,
|
2014-05-13 21:41:38 +04:00
|
|
|
char* aThreadName = DupCurrentThreadName())
|
2013-03-28 21:38:05 +04:00
|
|
|
: mRecordsHead(new Record[aLength])
|
|
|
|
, mRecordsTail(mRecordsHead + aLength)
|
2012-06-12 21:06:21 +04:00
|
|
|
, mNextRecord(mRecordsHead)
|
2012-07-30 18:20:58 +04:00
|
|
|
, mNextBatch(nullptr)
|
2013-03-28 21:38:05 +04:00
|
|
|
, mThreadNameCopy(aThreadName)
|
|
|
|
, mClosed(false)
|
2012-06-12 21:06:21 +04:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(RecordBatch);
|
|
|
|
}
|
|
|
|
|
|
|
|
~RecordBatch()
|
|
|
|
{
|
|
|
|
delete [] mRecordsHead;
|
|
|
|
PL_strfree(mThreadNameCopy);
|
|
|
|
MOZ_COUNT_DTOR(RecordBatch);
|
|
|
|
}
|
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
void Close()
|
|
|
|
{
|
|
|
|
mClosed = true;
|
|
|
|
}
|
2013-03-28 21:38:05 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
size_t Length() const
|
|
|
|
{
|
|
|
|
return mNextRecord - mRecordsHead;
|
|
|
|
}
|
2013-03-28 21:38:05 +04:00
|
|
|
bool CanBeDeleted(const TimeStamp& aUntil) const;
|
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
static RecordBatch* Register();
|
2014-05-15 00:14:58 +04:00
|
|
|
static void Close(void* aData); // Registered on freeing thread data
|
2014-05-13 21:41:38 +04:00
|
|
|
static RecordBatch* Clone(RecordBatch* aLog, const TimeStamp& aSince);
|
|
|
|
static void Delete(RecordBatch* aLog);
|
2013-03-28 21:38:05 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
static RecordBatch* CloneLog();
|
2013-03-28 21:38:05 +04:00
|
|
|
static void GCLog(const TimeStamp& aUntil);
|
|
|
|
static void DeleteLog();
|
2012-06-12 21:06:21 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
Record* mRecordsHead;
|
|
|
|
Record* mRecordsTail;
|
|
|
|
Record* mNextRecord;
|
2012-06-12 21:06:21 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
RecordBatch* mNextBatch;
|
|
|
|
char* mThreadNameCopy;
|
2013-03-28 21:38:05 +04:00
|
|
|
bool mClosed;
|
2012-06-12 21:06:21 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Protected by gMonitor, accessed concurently
|
|
|
|
// Linked list of batches threads want to flush on disk
|
2014-05-13 21:41:38 +04:00
|
|
|
RecordBatch* gLogHead = nullptr;
|
|
|
|
RecordBatch* gLogTail = nullptr;
|
2013-03-28 21:38:05 +04:00
|
|
|
|
|
|
|
// Registers the batch in the linked list
|
|
|
|
// static
|
2014-05-13 21:41:38 +04:00
|
|
|
RecordBatch*
|
2013-03-28 21:38:05 +04:00
|
|
|
RecordBatch::Register()
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(*gMonitor);
|
2012-06-12 21:06:21 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
if (!gInitialized) {
|
2013-03-28 21:38:05 +04:00
|
|
|
return nullptr;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2013-03-28 21:38:05 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
if (gLogHead) {
|
2013-03-28 21:38:05 +04:00
|
|
|
RecordBatch::GCLog(TimeStamp::Now() - *gMaxBacklogTime);
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2013-03-28 21:38:05 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
RecordBatch* batch = new RecordBatch();
|
|
|
|
if (!gLogHead) {
|
2013-03-28 21:38:05 +04:00
|
|
|
gLogHead = batch;
|
2014-05-13 21:41:38 +04:00
|
|
|
} else { // gLogTail is non-null
|
2013-03-28 21:38:05 +04:00
|
|
|
gLogTail->mNextBatch = batch;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2013-03-28 21:38:05 +04:00
|
|
|
gLogTail = batch;
|
|
|
|
|
|
|
|
mon.Notify();
|
|
|
|
return batch;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-05-15 00:14:58 +04:00
|
|
|
RecordBatch::Close(void* aData)
|
2013-03-28 21:38:05 +04:00
|
|
|
{
|
2014-05-15 00:14:58 +04:00
|
|
|
RecordBatch* batch = static_cast<RecordBatch*>(aData);
|
2013-03-28 21:38:05 +04:00
|
|
|
batch->Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2014-05-13 21:41:38 +04:00
|
|
|
RecordBatch*
|
|
|
|
RecordBatch::Clone(RecordBatch* aOther, const TimeStamp& aSince)
|
2013-03-28 21:38:05 +04:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
if (!aOther) {
|
2013-03-28 21:38:05 +04:00
|
|
|
return nullptr;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2013-03-28 21:38:05 +04:00
|
|
|
|
|
|
|
size_t length = aOther->Length();
|
|
|
|
size_t min = 0;
|
|
|
|
size_t max = length;
|
2014-05-13 21:41:38 +04:00
|
|
|
Record* record = nullptr;
|
2013-03-28 21:38:05 +04:00
|
|
|
|
|
|
|
// Binary search for record with time >= aSince
|
|
|
|
size_t i;
|
|
|
|
while (min < max) {
|
|
|
|
i = (max + min) / 2;
|
|
|
|
|
|
|
|
record = aOther->mRecordsHead + i;
|
2014-05-13 21:41:38 +04:00
|
|
|
if (record->mTime >= aSince) {
|
2013-03-28 21:38:05 +04:00
|
|
|
max = i;
|
2014-05-13 21:41:38 +04:00
|
|
|
} else {
|
|
|
|
min = i + 1;
|
|
|
|
}
|
2013-03-28 21:38:05 +04:00
|
|
|
}
|
|
|
|
i = (max + min) / 2;
|
|
|
|
|
|
|
|
// How many Record's to copy?
|
|
|
|
size_t toCopy = length - i;
|
2014-05-13 21:41:38 +04:00
|
|
|
if (!toCopy) {
|
2013-03-28 21:38:05 +04:00
|
|
|
return RecordBatch::Clone(aOther->mNextBatch, aSince);
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2013-03-28 21:38:05 +04:00
|
|
|
|
|
|
|
// Clone
|
2014-05-13 21:41:38 +04:00
|
|
|
RecordBatch* clone = new RecordBatch(toCopy, PL_strdup(aOther->mThreadNameCopy));
|
2013-03-28 21:38:05 +04:00
|
|
|
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 21:06:21 +04:00
|
|
|
void
|
2014-05-13 21:41:38 +04:00
|
|
|
RecordBatch::Delete(RecordBatch* aLog)
|
2013-03-28 21:38:05 +04:00
|
|
|
{
|
|
|
|
while (aLog) {
|
2014-05-13 21:41:38 +04:00
|
|
|
RecordBatch* batch = aLog;
|
2013-03-28 21:38:05 +04:00
|
|
|
aLog = aLog->mNextBatch;
|
|
|
|
delete batch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2014-05-13 21:41:38 +04:00
|
|
|
RecordBatch*
|
2013-03-28 21:38:05 +04:00
|
|
|
RecordBatch::CloneLog()
|
2012-06-12 21:06:21 +04:00
|
|
|
{
|
2013-03-28 21:38:05 +04:00
|
|
|
TimeStamp startEpoch = *gProfilerStart;
|
|
|
|
TimeStamp backlogEpoch = TimeStamp::Now() - *gMaxBacklogTime;
|
|
|
|
|
|
|
|
TimeStamp since = (startEpoch > backlogEpoch) ? startEpoch : backlogEpoch;
|
2012-06-12 21:06:21 +04:00
|
|
|
|
|
|
|
MonitorAutoLock mon(*gMonitor);
|
|
|
|
|
2013-03-28 21:38:05 +04:00
|
|
|
return RecordBatch::Clone(gLogHead, since);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void
|
|
|
|
RecordBatch::GCLog(const TimeStamp& aUntil)
|
|
|
|
{
|
|
|
|
// Garbage collect all unreferenced and old batches
|
|
|
|
gMonitor->AssertCurrentThreadOwns();
|
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
RecordBatch* volatile* referer = &gLogHead;
|
2013-03-28 21:38:05 +04:00
|
|
|
gLogTail = nullptr;
|
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
RecordBatch* batch = *referer;
|
2013-03-28 21:38:05 +04:00
|
|
|
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;
|
2014-05-13 21:41:38 +04:00
|
|
|
} else {
|
2013-03-28 21:38:05 +04:00
|
|
|
// 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()
|
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
RecordBatch* batch;
|
2013-03-28 21:38:05 +04:00
|
|
|
{
|
|
|
|
MonitorAutoLock mon(*gMonitor);
|
|
|
|
batch = gLogHead;
|
|
|
|
gLogHead = nullptr;
|
|
|
|
gLogTail = nullptr;
|
2012-06-12 21:06:21 +04:00
|
|
|
}
|
|
|
|
|
2013-03-28 21:38:05 +04:00
|
|
|
RecordBatch::Delete(batch);
|
|
|
|
}
|
2012-06-12 21:06:21 +04:00
|
|
|
|
2013-03-28 21:38:05 +04: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;
|
|
|
|
}
|
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
if ((mNextRecord - 1)->mTime <= aUntil) {
|
2013-03-28 21:38:05 +04:00
|
|
|
// 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 21:06:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Helper class for filtering events by MOZ_PROFILING_EVENTS
|
|
|
|
class EventFilter
|
|
|
|
{
|
|
|
|
public:
|
2014-05-15 00:14:58 +04:00
|
|
|
static EventFilter* Build(const char* aFilterVar);
|
|
|
|
bool EventPasses(const char* aFilterVar);
|
2012-06-12 21:06:21 +04:00
|
|
|
|
|
|
|
~EventFilter()
|
|
|
|
{
|
|
|
|
delete mNext;
|
|
|
|
PL_strfree(mFilter);
|
|
|
|
MOZ_COUNT_DTOR(EventFilter);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-05-15 00:14:58 +04:00
|
|
|
EventFilter(const char* aEventName, EventFilter* aNext)
|
|
|
|
: mFilter(PL_strdup(aEventName))
|
|
|
|
, mNext(aNext)
|
2012-06-12 21:06:21 +04:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(EventFilter);
|
|
|
|
}
|
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
char* mFilter;
|
|
|
|
EventFilter* mNext;
|
2012-06-12 21:06:21 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// static
|
2014-05-13 21:41:38 +04:00
|
|
|
EventFilter*
|
2014-05-15 00:14:58 +04:00
|
|
|
EventFilter::Build(const char* aFilterVar)
|
2012-06-12 21:06:21 +04:00
|
|
|
{
|
2014-05-15 00:14:58 +04:00
|
|
|
if (!aFilterVar || !*aFilterVar) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2012-06-12 21:06:21 +04:00
|
|
|
|
|
|
|
// Reads a comma serpatated list of events.
|
|
|
|
|
|
|
|
// Copied from nspr logging code (read of NSPR_LOG_MODULES)
|
|
|
|
char eventName[64];
|
2013-07-25 20:30:07 +04:00
|
|
|
int pos = 0, count, delta = 0;
|
2012-06-12 21:06:21 +04:00
|
|
|
|
|
|
|
// Read up to a comma or EOF -> get name of an event first in the list
|
2014-05-15 00:14:58 +04:00
|
|
|
count = sscanf(aFilterVar, "%63[^,]%n", eventName, &delta);
|
2014-05-13 21:41:38 +04:00
|
|
|
if (count == 0) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2012-06-12 21:06:21 +04:00
|
|
|
|
|
|
|
pos = delta;
|
|
|
|
|
|
|
|
// Skip a comma, if present, accept spaces around it
|
2014-05-15 00:14:58 +04:00
|
|
|
count = sscanf(aFilterVar + pos, " , %n", &delta);
|
2014-05-13 21:41:38 +04:00
|
|
|
if (count != EOF) {
|
2012-06-12 21:06:21 +04:00
|
|
|
pos += delta;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2012-06-12 21:06:21 +04:00
|
|
|
|
|
|
|
// 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
|
2014-05-15 00:14:58 +04:00
|
|
|
return new EventFilter(eventName, Build(aFilterVar + pos));
|
2012-06-12 21:06:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-05-15 00:14:58 +04:00
|
|
|
EventFilter::EventPasses(const char* aEventName)
|
2012-06-12 21:06:21 +04:00
|
|
|
{
|
2014-05-15 00:14:58 +04:00
|
|
|
if (!strcmp(aEventName, mFilter)) {
|
2012-06-12 21:06:21 +04:00
|
|
|
return true;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2012-06-12 21:06:21 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
if (mNext) {
|
2014-05-15 00:14:58 +04:00
|
|
|
return mNext->EventPasses(aEventName);
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2012-06-12 21:06:21 +04:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
// State and control variables, initialized in Init() method, after it
|
2012-06-12 21:06:21 +04:00
|
|
|
// immutable and read concurently.
|
2014-05-13 21:41:38 +04:00
|
|
|
EventFilter* gEventFilter = nullptr;
|
2012-08-22 19:56:38 +04:00
|
|
|
unsigned gThreadPrivateIndex;
|
2012-06-12 21:06:21 +04:00
|
|
|
|
|
|
|
// static
|
2014-05-15 00:14:58 +04:00
|
|
|
bool
|
|
|
|
CheckEventFilters(uint32_t aType, void* aItem, const char* aText)
|
2012-06-12 21:06:21 +04:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
if (!gEventFilter) {
|
2012-06-12 21:06:21 +04:00
|
|
|
return true;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2012-06-12 21:06:21 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
if (aType == eName) {
|
2012-06-12 21:06:21 +04:00
|
|
|
return true;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2012-06-12 21:06:21 +04:00
|
|
|
|
|
|
|
return gEventFilter->EventPasses(aText);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anon namespace
|
|
|
|
|
|
|
|
#endif //MOZ_VISUAL_EVENT_TRACER
|
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
// static
|
2014-05-13 21:41:38 +04:00
|
|
|
void
|
|
|
|
Init()
|
2012-06-12 21:06:21 +04:00
|
|
|
{
|
|
|
|
#ifdef MOZ_VISUAL_EVENT_TRACER
|
2014-05-13 21:41:38 +04:00
|
|
|
const char* logEvents = PR_GetEnv("MOZ_PROFILING_EVENTS");
|
|
|
|
if (logEvents && *logEvents) {
|
2012-06-12 21:06:21 +04:00
|
|
|
gEventFilter = EventFilter::Build(logEvents);
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2012-06-12 21:06:21 +04:00
|
|
|
|
2013-03-28 21:38:05 +04:00
|
|
|
PRStatus status = PR_NewThreadPrivateIndex(&gThreadPrivateIndex, &RecordBatch::Close);
|
2014-05-13 21:41:38 +04:00
|
|
|
if (status != PR_SUCCESS) {
|
2012-06-12 21:06:21 +04:00
|
|
|
return;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2012-06-12 21:06:21 +04:00
|
|
|
|
|
|
|
gMonitor = new mozilla::Monitor("Profiler");
|
2014-05-13 21:41:38 +04:00
|
|
|
if (!gMonitor) {
|
2012-06-12 21:06:21 +04:00
|
|
|
return;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2012-06-12 21:06:21 +04:00
|
|
|
|
2013-03-28 21:38:05 +04:00
|
|
|
gProfilerStart = new mozilla::TimeStamp();
|
|
|
|
gMaxBacklogTime = new mozilla::TimeDuration();
|
2012-06-12 21:06:21 +04:00
|
|
|
|
2013-03-28 21:38:05 +04:00
|
|
|
gInitialized = true;
|
2012-06-12 21:06:21 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
// static
|
2014-05-13 21:41:38 +04:00
|
|
|
void
|
|
|
|
Shutdown()
|
2012-06-12 21:06:21 +04:00
|
|
|
{
|
|
|
|
#ifdef MOZ_VISUAL_EVENT_TRACER
|
2013-03-28 21:38:05 +04:00
|
|
|
gCapture = false;
|
|
|
|
gInitialized = false;
|
2012-06-12 21:06:21 +04:00
|
|
|
|
2013-03-28 21:38:05 +04:00
|
|
|
RecordBatch::DeleteLog();
|
2012-06-12 21:06:21 +04:00
|
|
|
|
|
|
|
if (gMonitor) {
|
|
|
|
delete gMonitor;
|
2012-07-30 18:20:58 +04:00
|
|
|
gMonitor = nullptr;
|
2012-06-12 21:06:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (gEventFilter) {
|
|
|
|
delete gEventFilter;
|
2012-07-30 18:20:58 +04:00
|
|
|
gEventFilter = nullptr;
|
2012-06-12 21:06:21 +04:00
|
|
|
}
|
2013-03-28 21:38:05 +04:00
|
|
|
|
|
|
|
if (gProfilerStart) {
|
|
|
|
delete gProfilerStart;
|
|
|
|
gProfilerStart = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gMaxBacklogTime) {
|
|
|
|
delete gMaxBacklogTime;
|
|
|
|
gMaxBacklogTime = nullptr;
|
|
|
|
}
|
2012-06-12 21:06:21 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
// static
|
2014-05-13 21:41:38 +04:00
|
|
|
void
|
|
|
|
Mark(uint32_t aType, void* aItem, const char* aText, const char* aText2)
|
2012-06-12 21:06:21 +04:00
|
|
|
{
|
|
|
|
#ifdef MOZ_VISUAL_EVENT_TRACER
|
2014-05-13 21:41:38 +04:00
|
|
|
if (!gInitialized || !gCapture) {
|
2012-06-12 21:06:21 +04:00
|
|
|
return;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2012-06-12 21:06:21 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
if (aType == eNone) {
|
2012-06-12 21:06:21 +04:00
|
|
|
return;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2012-06-12 21:06:21 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
if (!CheckEventFilters(aType, aItem, aText)) { // Events use just aText
|
2012-06-12 21:06:21 +04:00
|
|
|
return;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2012-06-12 21:06:21 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
RecordBatch* threadLogPrivate = static_cast<RecordBatch*>(
|
2014-05-05 21:30:39 +04:00
|
|
|
PR_GetThreadPrivate(gThreadPrivateIndex));
|
2012-06-12 21:06:21 +04:00
|
|
|
if (!threadLogPrivate) {
|
2013-03-28 21:38:05 +04:00
|
|
|
threadLogPrivate = RecordBatch::Register();
|
2014-05-13 21:41:38 +04:00
|
|
|
if (!threadLogPrivate) {
|
2013-03-28 21:38:05 +04:00
|
|
|
return;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2013-03-28 21:38:05 +04:00
|
|
|
|
2012-06-12 21:06:21 +04:00
|
|
|
PR_SetThreadPrivate(gThreadPrivateIndex, threadLogPrivate);
|
|
|
|
}
|
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
Record* record = threadLogPrivate->mNextRecord;
|
2012-06-12 21:06:21 +04:00
|
|
|
record->mType = aType;
|
2013-03-28 21:38:05 +04:00
|
|
|
record->mTime = mozilla::TimeStamp::Now();
|
2012-06-12 21:06:21 +04:00
|
|
|
record->mItem = aItem;
|
|
|
|
record->mText = PL_strdup(aText);
|
2012-07-30 18:20:58 +04:00
|
|
|
record->mText2 = aText2 ? PL_strdup(aText2) : nullptr;
|
2012-06-12 21:06:21 +04:00
|
|
|
|
|
|
|
++threadLogPrivate->mNextRecord;
|
|
|
|
if (threadLogPrivate->mNextRecord == threadLogPrivate->mRecordsTail) {
|
2013-03-28 21:38:05 +04:00
|
|
|
// Calls RecordBatch::Close(threadLogPrivate) that marks
|
|
|
|
// the batch as OK to be deleted from memory when no longer needed.
|
2012-07-30 18:20:58 +04:00
|
|
|
PR_SetThreadPrivate(gThreadPrivateIndex, nullptr);
|
2012-06-12 21:06:21 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-03-28 21:38:05 +04:00
|
|
|
|
|
|
|
#ifdef MOZ_VISUAL_EVENT_TRACER
|
|
|
|
|
|
|
|
// The scriptable classes
|
|
|
|
|
2014-07-13 22:16:55 +04:00
|
|
|
class VisualEventTracerLog MOZ_FINAL: public nsIVisualEventTracerLog
|
2013-03-28 21:38:05 +04:00
|
|
|
{
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIVISUALEVENTTRACERLOG
|
|
|
|
|
|
|
|
VisualEventTracerLog(RecordBatch* aBatch)
|
|
|
|
: mBatch(aBatch)
|
|
|
|
, mProfilerStart(*gProfilerStart)
|
2014-05-13 21:41:38 +04:00
|
|
|
{
|
|
|
|
}
|
2014-07-13 22:16:55 +04:00
|
|
|
private:
|
|
|
|
~VisualEventTracerLog();
|
2013-03-28 21:38:05 +04:00
|
|
|
|
|
|
|
protected:
|
2014-05-13 21:41:38 +04:00
|
|
|
RecordBatch* mBatch;
|
2013-03-28 21:38:05 +04:00
|
|
|
TimeStamp mProfilerStart;
|
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(VisualEventTracerLog, nsIVisualEventTracerLog)
|
2013-03-28 21:38:05 +04:00
|
|
|
|
|
|
|
VisualEventTracerLog::~VisualEventTracerLog()
|
|
|
|
{
|
|
|
|
RecordBatch::Delete(mBatch);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:14:58 +04:00
|
|
|
VisualEventTracerLog::GetJSONString(nsACString& aResult)
|
2013-03-28 21:38:05 +04:00
|
|
|
{
|
|
|
|
nsCString buffer;
|
2014-05-22 07:48:52 +04:00
|
|
|
buffer.AssignLiteral("{\n\"version\": 1,\n\"records\":[\n");
|
2013-03-28 21:38:05 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
RecordBatch* batch = mBatch;
|
2013-03-28 21:38:05 +04:00
|
|
|
while (batch) {
|
|
|
|
if (batch != mBatch) {
|
|
|
|
// This is not the first batch we are writting, add comma
|
2014-05-22 07:48:50 +04:00
|
|
|
buffer.AppendLiteral(",\n");
|
2013-03-28 21:38:05 +04:00
|
|
|
}
|
|
|
|
|
2014-05-22 07:48:50 +04:00
|
|
|
buffer.AppendLiteral("{\"thread\":\"");
|
2013-03-28 21:38:05 +04:00
|
|
|
buffer.Append(batch->mThreadNameCopy);
|
2014-05-22 07:48:50 +04:00
|
|
|
buffer.AppendLiteral("\",\"log\":[\n");
|
2013-03-28 21:38:05 +04:00
|
|
|
|
|
|
|
static const int kBufferSize = 2048;
|
|
|
|
char buf[kBufferSize];
|
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
for (Record* record = batch->mRecordsHead;
|
2013-03-28 21:38:05 +04:00
|
|
|
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 20:57:57 +04:00
|
|
|
"{\"e\":\"%c\",\"t\":%llu,\"f\":%d,\"i\":\"%p\",\"n\":\"%s%s\"}%s\n",
|
2013-03-28 21:38:05 +04:00
|
|
|
kTypeChars[type],
|
2013-05-01 20:57:57 +04:00
|
|
|
static_cast<uint64_t>((record->mTime - mProfilerStart).ToMilliseconds()),
|
2013-03-28 21:38:05 +04:00
|
|
|
flags,
|
|
|
|
record->mItem,
|
|
|
|
record->mText,
|
|
|
|
record->mText2 ? record->mText2 : "",
|
|
|
|
(record == batch->mNextRecord - 1) ? "" : ",");
|
|
|
|
|
|
|
|
buffer.Append(buf);
|
|
|
|
}
|
|
|
|
|
2014-05-22 07:48:50 +04:00
|
|
|
buffer.AppendLiteral("]}\n");
|
2013-03-28 21:38:05 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
RecordBatch* next = batch->mNextBatch;
|
2013-03-28 21:38:05 +04:00
|
|
|
batch = next;
|
|
|
|
}
|
|
|
|
|
2014-05-22 07:48:50 +04:00
|
|
|
buffer.AppendLiteral("]}\n");
|
2014-05-15 00:14:58 +04:00
|
|
|
aResult.Assign(buffer);
|
2013-03-28 21:38:05 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-07-25 20:21:58 +04: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 21:30:39 +04:00
|
|
|
0644);
|
2013-07-25 20:21:58 +04: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 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(VisualEventTracer, nsIVisualEventTracer)
|
2013-03-28 21:38:05 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
VisualEventTracer::Start(const uint32_t aMaxBacklogSeconds)
|
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
if (!gInitialized) {
|
2013-03-28 21:38:05 +04:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2013-03-28 21:38:05 +04:00
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
if (!gInitialized) {
|
2013-03-28 21:38:05 +04:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2013-03-28 21:38:05 +04:00
|
|
|
|
|
|
|
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 20:21:58 +04: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 21:38:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:14:58 +04:00
|
|
|
VisualEventTracer::Snapshot(nsIVisualEventTracerLog** aResult)
|
2013-03-28 21:38:05 +04:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
if (!gInitialized) {
|
2013-03-28 21:38:05 +04:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2013-03-28 21:38:05 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
RecordBatch* batch = RecordBatch::CloneLog();
|
2013-03-28 21:38:05 +04:00
|
|
|
|
|
|
|
nsRefPtr<VisualEventTracerLog> log = new VisualEventTracerLog(batch);
|
2014-05-15 00:14:58 +04:00
|
|
|
log.forget(aResult);
|
2013-03-28 21:38:05 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2012-06-12 21:06:21 +04:00
|
|
|
} // eventtracer
|
|
|
|
} // mozilla
|