home *** CD-ROM | disk | FTP | other *** search
- // callprof.hpp
- //
- // Created 12/17/97
- //
- // (C) Copyright 1995 - 1999 Microsoft Corporation. All rights reserved.
- //
-
- #ifndef __CALLPROF_HPP__
- #define __CALLPROF_HPP__
-
- #include <jevmon.h>
-
- #include "timer.hpp"
-
- struct ThreadRecord;
- class EventMonitor;
-
-
- class CallProfiler
- {
- friend class CallProfilerThreadData;
- friend class SamplingProfiler;
- friend class EventMonitor;
-
- EventMonitor *m_pmon;
-
- struct CallProfilerEntry
- {
- MethodID id;
-
- union
- {
- // Sampling and call profiling are not used simultaneously.
-
- struct
- {
- ULONG nCalls;
- PROFTIME TotalExclusiveTime;
- ULONG nExceptionsThrown;
- BOOL fIncomplete;
- };
-
- struct
- {
- ULONG nSamples;
- ULONG AccuracySum;
- };
- };
-
- CallProfilerEntry *next;
- CallProfilerEntry *listnext;
- };
-
- CallProfilerEntry *m_hash[293];
-
- CRITICAL_SECTION m_cs;
-
- CallProfilerEntry *m_list;
-
-
- // TRUE if ent1 should be ordered before ent2
- BOOL CompareEntries (CallProfilerEntry *ent1, CallProfilerEntry *ent2);
-
- VOID LinkEntryInSortOrder (CallProfilerEntry *ent);
- VOID SortEntries ();
-
-
- static VOID MarkIncompleteMethodsCB (ThreadRecord *pThread, PVOID token);
-
-
- CallProfilerEntry *LookupMethod (MethodID id, BOOL fCreate)
- {
- unsigned ibucket = ((unsigned)id % ARRAY_ELEMENTS(m_hash));
- CallProfilerEntry **prev;
- CallProfilerEntry *cur;
-
- prev = &m_hash[ibucket];
-
- for (;;)
- {
- cur = *prev;
- if (!cur)
- break;
-
- if (cur->id == id)
- {
- *prev = cur->next;
- cur->next = m_hash[ibucket];
- m_hash[ibucket] = cur;
- return cur;
- }
-
- prev = &cur->next;
- }
-
- if (fCreate)
- {
- Lock();
- {
- prev = &m_hash[ibucket];
-
- for (;;)
- {
- cur = *prev;
- if (!cur)
- break;
-
- if (cur->id == id)
- {
- *prev = cur->next;
- cur->next = m_hash[ibucket];
- m_hash[ibucket] = cur;
- return cur;
- }
-
- prev = &cur->next;
- }
-
- if (cur = new CallProfilerEntry())
- {
- ZeroMemory(cur, sizeof(*cur));
- cur->id = id;
- cur->next = m_hash[ibucket];
- m_hash[ibucket] = cur;
- }
- }
- Unlock();
- }
-
- return cur;
- }
-
-
- VOID Lock ()
- {
- EnterCriticalSection(&m_cs);
- }
-
- VOID Unlock ()
- {
- LeaveCriticalSection(&m_cs);
- }
-
-
- public:
-
- CallProfiler ()
- {
- m_pmon = NULL;
-
- ZeroMemory(&m_hash, sizeof(m_hash));
-
- InitializeCriticalSection(&m_cs);
-
- m_list = NULL;
- }
-
- HRESULT Initialize (EventMonitor *pmon);
-
- VOID Destruct ();
-
-
- VOID EnterMethod (ThreadRecord *pThread, MethodID id)
- {
- ResumeMethod(pThread, id, TRUE);
- }
-
- VOID ResumeMethod (ThreadRecord *pThread, MethodID id, BOOL fJustCalled = FALSE);
-
- VOID ExitMethod (ThreadRecord *pThread);
-
- VOID ExceptionThrown (ThreadRecord *pThread);
-
-
- VOID SpewResults ();
- };
-
-
- class CallProfilerThreadData
- {
- friend class CallProfiler;
- friend class EventMonitor;
-
- // Entry for the method currently executing on the thread
- CallProfiler::CallProfilerEntry *pentCurMethod;
-
- // Time when method became the current method (either entered or returned to)
- PROFTIME StartTime;
- };
-
-
- #endif /* __CALLPROF_HPP__ */
-
-