| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- //-------------------------------------------------------------------------------------------------------
- // Copyright (C) Microsoft. All rights reserved.
- // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
- //-------------------------------------------------------------------------------------------------------
- #include "RuntimeBasePch.h"
- #ifdef PROFILE_EXEC
- #include "Base/ScriptContextProfiler.h"
- namespace Js
- {
- ULONG
- ScriptContextProfiler::AddRef()
- {
- return refcount++;
- }
- ULONG
- ScriptContextProfiler::Release()
- {
- ULONG count = --refcount;
- if (count == 0)
- {
- if (recycler != nullptr && this->profiler == recycler->GetProfiler())
- {
- recycler->SetProfiler(nullptr, nullptr);
- }
- NoCheckHeapDelete(this);
- }
- return count;
- }
- ScriptContextProfiler::ScriptContextProfiler() :
- refcount(1), profilerArena(nullptr), profiler(nullptr), backgroundRecyclerProfilerArena(nullptr), backgroundRecyclerProfiler(nullptr), recycler(nullptr), pageAllocator(nullptr), next(nullptr)
- {
- }
- void
- ScriptContextProfiler::Initialize(PageAllocator * pageAllocator, Recycler * recycler)
- {
- Assert(!IsInitialized());
- profilerArena = HeapNew(ArenaAllocator, _u("Profiler"), pageAllocator, Js::Throw::OutOfMemory);
- profiler = Anew(profilerArena, Profiler, profilerArena);
- if (recycler)
- {
- backgroundRecyclerProfilerArena = recycler->AddBackgroundProfilerArena();
- backgroundRecyclerProfiler = Anew(profilerArena, Profiler, backgroundRecyclerProfilerArena);
- #if DBG
- //backgroundRecyclerProfiler is allocated from background and its guaranteed to assert below if we don't disable thread access check.
- backgroundRecyclerProfiler->alloc->GetPageAllocator()->SetDisableThreadAccessCheck();
- #endif
- backgroundRecyclerProfiler->Begin(Js::AllPhase);
- #if DBG
- backgroundRecyclerProfiler->alloc->GetPageAllocator()->SetEnableThreadAccessCheck();
- #endif
- }
- profiler->Begin(Js::AllPhase);
- this->recycler = recycler;
- }
- void
- ScriptContextProfiler::ProfilePrint(Js::Phase phase)
- {
- if (!IsInitialized())
- {
- return;
- }
- profiler->End(Js::AllPhase);
- profiler->Print(phase);
- if (this->backgroundRecyclerProfiler)
- {
- this->backgroundRecyclerProfiler->End(Js::AllPhase);
- this->backgroundRecyclerProfiler->Print(phase);
- this->backgroundRecyclerProfiler->Begin(Js::AllPhase);
- }
- profiler->Begin(Js::AllPhase);
- }
- void
- ScriptContextProfiler::ProfilePrint()
- {
- Js::ScriptContextProfiler* profiler = this;
- if (Js::Configuration::Global.flags.Verbose)
- {
- //Print individual profiler information in verbose mode
- while (profiler)
- {
- profiler->ProfilePrint(Js::Configuration::Global.flags.Profile.GetFirstPhase());
- profiler = profiler->next;
- }
- }
- else
- {
- //Merge all the profiler for single snapshot.
- Js::ScriptContextProfiler* mergeToProfiler = profiler;
- // find the first initialized profiler
- while (mergeToProfiler != nullptr && !mergeToProfiler->IsInitialized())
- {
- mergeToProfiler = mergeToProfiler->next;
- }
- if (mergeToProfiler != nullptr)
- {
- // merge the rest profiler to the above initialized profiler
- profiler = mergeToProfiler->next;
- while (profiler)
- {
- if (profiler->IsInitialized())
- {
- mergeToProfiler->ProfileMerge(profiler);
- }
- profiler = profiler->next;
- }
- mergeToProfiler->ProfilePrint(Js::Configuration::Global.flags.Profile.GetFirstPhase());
- }
- }
- }
- ScriptContextProfiler::~ScriptContextProfiler()
- {
- if (profilerArena)
- {
- HeapDelete(profilerArena);
- }
- if (recycler && backgroundRecyclerProfilerArena)
- {
- #if DBG
- //We are freeing from main thread, disable thread check assert.
- backgroundRecyclerProfilerArena->GetPageAllocator()->SetDisableThreadAccessCheck();
- #endif
- recycler->ReleaseBackgroundProfilerArena(backgroundRecyclerProfilerArena);
- }
- }
- void
- ScriptContextProfiler::ProfileBegin(Js::Phase phase)
- {
- Assert(IsInitialized());
- this->profiler->Begin(phase);
- }
- void
- ScriptContextProfiler::ProfileEnd(Js::Phase phase)
- {
- Assert(IsInitialized());
- this->profiler->End(phase);
- }
- void
- ScriptContextProfiler::ProfileSuspend(Js::Phase phase, Js::Profiler::SuspendRecord * suspendRecord)
- {
- Assert(IsInitialized());
- this->profiler->Suspend(phase, suspendRecord);
- }
- void
- ScriptContextProfiler::ProfileResume(Js::Profiler::SuspendRecord * suspendRecord)
- {
- Assert(IsInitialized());
- this->profiler->Resume(suspendRecord);
- }
- void
- ScriptContextProfiler::ProfileMerge(ScriptContextProfiler * profiler)
- {
- Assert(IsInitialized());
- this->profiler->Merge(profiler->profiler);
- }
- }
- #endif
|