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

  1. // sampler.cpp
  2. //
  3. // Created 01/18/99
  4. //
  5. // (C)Copyright 1999 Microsoft Corporation, All rights reserved.
  6. //
  7.  
  8. #include "project.hpp"
  9. #pragma hdrstop
  10.  
  11. #include "sampler.hpp"
  12. #include "jviewprf.hpp"
  13. #include "utils.hpp"
  14.  
  15.  
  16. //static
  17. VOID SamplingProfiler::GetThreadSampleCB (ThreadRecord *pThread, PVOID token)
  18. {
  19.     ((SamplingProfiler*)token)->GetThreadSample(pThread);
  20. }
  21.  
  22.  
  23. VOID SamplingProfiler::GetThreadSample (ThreadRecord *pThread)
  24. {
  25.     if (!pThread->vmid)
  26.         return;
  27.  
  28.     JVM_METHOD_SAMPLE sample;
  29.     sample.flags = JVM_SAMPLE_METHOD_ID;
  30.     if (FAILED(m_pmon->GetVMInfoIfc2()->SampleThreadLocation(pThread->vmid, &sample)))
  31.     {
  32.         sample.flags = 0;
  33.         sample.accuracy = JVM_SAMPLE_NONE;
  34.     }
  35.     
  36.     if (sample.flags & JVM_SAMPLE_METHOD_ID)
  37.     {
  38.         CallProfiler::CallProfilerEntry *pent = m_callprof->LookupMethod(sample.method_id, TRUE);
  39.         if (pent)
  40.         {
  41.             pent->nSamples++;
  42.             pent->AccuracySum += sample.accuracy;
  43.         }
  44.  
  45.         m_nSamples_WithMethod++;
  46.         m_AccuracySum_WithMethod += sample.accuracy;
  47.     }
  48.  
  49.     m_AccuracySum += sample.accuracy;
  50.     m_nSamples++;
  51. }
  52.  
  53.  
  54. //static
  55. DWORD WINAPI SamplingProfiler::SamplerThreadEntry (LPVOID lpThreadParameter)
  56. {
  57.     return ((SamplingProfiler*)lpThreadParameter)->SamplerThread();
  58. }
  59.  
  60.  
  61. DWORD SamplingProfiler::SamplerThread ()
  62. {
  63.     m_SamplerOverhead = 0;
  64.     m_nSamples = 0;
  65.     m_AccuracySum = 0;
  66.     m_nSamples_WithMethod = 0;
  67.     m_AccuracySum_WithMethod = 0;
  68.  
  69.     SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
  70.  
  71.     while (!m_pmon->IsVMTerminating())
  72.     {
  73.         Sleep(g_SamplingFrequency);
  74.  
  75.         PROFTIME start = GetProfilerTime();
  76.  
  77.         m_pmon->IterateThreads(&GetThreadSampleCB, this);
  78.  
  79.         PROFTIME end = GetProfilerTime();
  80.         m_SamplerOverhead += ConvertProfilerTimeToMS(end-start);
  81.     }
  82.  
  83.     return 0;
  84. }
  85.  
  86.  
  87. SamplingProfiler::SamplingProfiler ()
  88. {
  89.     m_pmon = NULL;
  90.     m_callprof = NULL;
  91.     m_hSamplerThread = INVALID_HANDLE_VALUE;
  92. }
  93.  
  94.  
  95. HRESULT SamplingProfiler::Initialize (EventMonitor *pmon, CallProfiler *callprof)
  96. {
  97.     (m_pmon = pmon)->AddRef();
  98.     m_callprof = callprof;
  99.     return S_OK;
  100. }
  101.  
  102.  
  103. VOID SamplingProfiler::Destruct ()
  104. {
  105.     if (m_hSamplerThread != INVALID_HANDLE_VALUE)
  106.     {
  107.         TerminateThread(m_hSamplerThread, -1);
  108.         CloseHandle(m_hSamplerThread);
  109.     }
  110.  
  111.     if (m_pmon)
  112.         m_pmon->Release();
  113. }
  114.  
  115.  
  116. VOID SamplingProfiler::OnVMInitialization ()
  117. {
  118.     if (!(g_ProfOptions & OPT_SAMPLE))
  119.         return;
  120.  
  121.     DWORD tid;
  122.     m_hSamplerThread = CreateThread(NULL, 0, &SamplerThreadEntry, this, 0, &tid);
  123.     if (m_hSamplerThread == INVALID_HANDLE_VALUE)
  124.     {
  125.         // TODO: error message
  126.     }
  127. }
  128.  
  129.  
  130. VOID SamplingProfiler::SpewSamplerResults ()
  131. {
  132.     if (!(g_ProfOptions & OPT_SAMPLE))
  133.         return;
  134.  
  135.     WriteOutput("Total sampling overhead: ");
  136.     WriteOutputFixedFloat(0, m_SamplerOverhead, 1);
  137.     WriteOutputFmt(
  138.             " ms\n"
  139.             "Sampling frequency: %u ms\n"
  140.             "%u total samples\n",
  141.             g_SamplingFrequency, m_nSamples);
  142.  
  143.     if (m_nSamples)
  144.     {
  145.         WriteOutput("Average accuracy of all samples: ");
  146.         WriteOutputFixedFloat(0, (float)m_AccuracySum/m_nSamples, 1);
  147.         WriteOutput("%\n");
  148.  
  149.         if (m_nSamples_WithMethod)
  150.         {
  151.             WriteOutput("Average accuracy of method samples: ");
  152.             WriteOutputFixedFloat(0, (float)m_AccuracySum_WithMethod/m_nSamples_WithMethod, 1);
  153.             WriteOutput("%\n");
  154.         }
  155.     }
  156.  
  157.     m_callprof->SortEntries();
  158.  
  159.     CallProfiler::CallProfilerEntry *cur = m_callprof->m_list;
  160.     if (!cur)
  161.         return;
  162.  
  163. #define WIDTH_EXEC_MODEL    4
  164. #define WIDTH_SAMPLES       7
  165. #define WIDTH_ACCURACY      8
  166.  
  167. #define TOTAL_WIDTH_FIXED_FIELDS (1+WIDTH_EXEC_MODEL+1+WIDTH_SAMPLES+1+WIDTH_ACCURACY)
  168.  
  169.     WriteOutputFitString(WRITE_FIT | WRITE_SPACE, TOTAL_WIDTH_FIXED_FIELDS, 100, "Method");
  170.     WriteOutput("Type Samples Accuracy\n");
  171.  
  172.     IJavaEventMonitorIDInfo *pinfo = m_pmon->GetVMInfoIfc();
  173.  
  174.     do
  175.     {
  176.         PSTR pszMethodUtf8;
  177.         JAVA_EXECUTION_MODEL exec;
  178.         if (SUCCEEDED(pinfo->MethodInformation(cur->id, &pszMethodUtf8, NULL, &exec, NULL, NULL)))
  179.         {
  180.             WriteOutputFitString(WRITE_FIT | WRITE_WRAP | WRITE_UTF8 | WRITE_SPACE, TOTAL_WIDTH_FIXED_FIELDS, 100, pszMethodUtf8);
  181.             WriteOutputColumnString(WRITE_LEFT_JUSTIFIED | WRITE_SPACE, WIDTH_EXEC_MODEL, StringForExecutionModel(exec));
  182.             WriteOutputColumnULONG(WRITE_LEFT_JUSTIFIED | WRITE_SPACE, WIDTH_SAMPLES, cur->nSamples);
  183.             WriteOutputFixedFloat(0, (float)cur->AccuracySum/cur->nSamples, 2);
  184.             WriteOutput("\n");
  185.  
  186.             CoTaskMemFree(pszMethodUtf8);
  187.         }
  188.  
  189.         cur = cur->listnext;
  190.     }
  191.     while (cur);
  192. }
  193.  
  194.