home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / Profiler / jviewprf / callprof.hpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-04  |  4.0 KB  |  194 lines

  1. // callprof.hpp
  2. //
  3. // Created 12/17/97
  4. //
  5. // (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  6. //
  7.  
  8. #ifndef __CALLPROF_HPP__
  9. #define __CALLPROF_HPP__
  10.  
  11. #include <jevmon.h>
  12.  
  13. #include "timer.hpp"
  14.  
  15. struct ThreadRecord;
  16. class EventMonitor;
  17.  
  18.  
  19. class CallProfiler
  20. {
  21.     friend class CallProfilerThreadData;
  22.     friend class SamplingProfiler;
  23.     friend class EventMonitor;
  24.  
  25.     EventMonitor *m_pmon;
  26.  
  27.     struct CallProfilerEntry
  28.     {
  29.         MethodID id;
  30.  
  31.         union
  32.         {
  33.             // Sampling and call profiling are not used simultaneously.
  34.  
  35.             struct
  36.             {
  37.                 ULONG nCalls;
  38.                 PROFTIME TotalExclusiveTime;
  39.                 ULONG nExceptionsThrown;
  40.                 BOOL fIncomplete;
  41.             };
  42.  
  43.             struct
  44.             {
  45.                 ULONG nSamples;
  46.                 ULONG AccuracySum;
  47.             };
  48.         };
  49.  
  50.         CallProfilerEntry *next;
  51.         CallProfilerEntry *listnext;
  52.     };
  53.  
  54.     CallProfilerEntry *m_hash[293];
  55.  
  56.     CRITICAL_SECTION m_cs;
  57.  
  58.     CallProfilerEntry *m_list;
  59.  
  60.  
  61.     // TRUE if ent1 should be ordered before ent2
  62.     BOOL CompareEntries (CallProfilerEntry *ent1, CallProfilerEntry *ent2);
  63.  
  64.     VOID LinkEntryInSortOrder (CallProfilerEntry *ent);
  65.     VOID SortEntries ();
  66.  
  67.  
  68.     static VOID MarkIncompleteMethodsCB (ThreadRecord *pThread, PVOID token);
  69.  
  70.  
  71.     CallProfilerEntry *LookupMethod (MethodID id, BOOL fCreate)
  72.     {
  73.         unsigned ibucket = ((unsigned)id % ARRAY_ELEMENTS(m_hash));
  74.         CallProfilerEntry **prev;
  75.         CallProfilerEntry *cur;
  76.  
  77.         prev = &m_hash[ibucket];
  78.  
  79.         for (;;)
  80.         {
  81.             cur = *prev;
  82.             if (!cur)
  83.                 break;
  84.  
  85.             if (cur->id == id)
  86.             {
  87.                 *prev = cur->next;
  88.                 cur->next = m_hash[ibucket];
  89.                 m_hash[ibucket] = cur;
  90.                 return cur;
  91.             }
  92.  
  93.             prev = &cur->next;
  94.         }
  95.  
  96.         if (fCreate)
  97.         {
  98.             Lock();
  99.             {
  100.                 prev = &m_hash[ibucket];
  101.  
  102.                 for (;;)
  103.                 {
  104.                     cur = *prev;
  105.                     if (!cur)
  106.                         break;
  107.  
  108.                     if (cur->id == id)
  109.                     {
  110.                         *prev = cur->next;
  111.                         cur->next = m_hash[ibucket];
  112.                         m_hash[ibucket] = cur;
  113.                         return cur;
  114.                     }
  115.  
  116.                     prev = &cur->next;
  117.                 }
  118.  
  119.                 if (cur = new CallProfilerEntry())
  120.                 {
  121.                     ZeroMemory(cur, sizeof(*cur));
  122.                     cur->id = id;
  123.                     cur->next = m_hash[ibucket];
  124.                     m_hash[ibucket] = cur;
  125.                 }
  126.             }
  127.             Unlock();
  128.         }
  129.  
  130.         return cur;
  131.     }
  132.  
  133.  
  134.     VOID Lock ()
  135.     {
  136.         EnterCriticalSection(&m_cs);
  137.     }
  138.  
  139.     VOID Unlock ()
  140.     {
  141.         LeaveCriticalSection(&m_cs);
  142.     }
  143.  
  144.  
  145. public:
  146.  
  147.     CallProfiler ()
  148.     {
  149.         m_pmon = NULL;
  150.  
  151.         ZeroMemory(&m_hash, sizeof(m_hash));
  152.  
  153.         InitializeCriticalSection(&m_cs);
  154.  
  155.         m_list = NULL;
  156.     }
  157.  
  158.     HRESULT Initialize (EventMonitor *pmon);
  159.  
  160.     VOID Destruct ();
  161.  
  162.  
  163.     VOID EnterMethod (ThreadRecord *pThread, MethodID id)
  164.     {
  165.         ResumeMethod(pThread, id, TRUE);
  166.     }
  167.  
  168.     VOID ResumeMethod (ThreadRecord *pThread, MethodID id, BOOL fJustCalled = FALSE);
  169.  
  170.     VOID ExitMethod (ThreadRecord *pThread);
  171.  
  172.     VOID ExceptionThrown (ThreadRecord *pThread);
  173.  
  174.  
  175.     VOID SpewResults ();
  176. };
  177.  
  178.  
  179. class CallProfilerThreadData
  180. {
  181.     friend class CallProfiler;
  182.     friend class EventMonitor;
  183.  
  184.     // Entry for the method currently executing on the thread
  185.     CallProfiler::CallProfilerEntry *pentCurMethod;
  186.  
  187.     // Time when method became the current method (either entered or returned to)
  188.     PROFTIME StartTime;
  189. };
  190.  
  191.  
  192. #endif /* __CALLPROF_HPP__ */
  193.  
  194.