home *** CD-ROM | disk | FTP | other *** search
/ Enter 1999 November / ENTER11_1.bin / WARSZTAT / SDKJava32.exe / data1.cab / fg_Samples / Samples / Profiler / jviewprf / sampler.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-03-17  |  4.5 KB  |  177 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.     WriteOutputFmt("Total sampling overhead: %u ms\n", m_SamplerOverhead);
  136.     WriteOutputFmt("Sampling frequency: %u ms\n", g_SamplingFrequency);
  137.     WriteOutputFmt("%u total samples\n", m_nSamples);
  138.     WriteOutputFmt("Average accuracy of all samples: %.1f%%\n", (float)m_AccuracySum/m_nSamples);
  139.     WriteOutputFmt("Average accuracy of method samples: %.1f%%\n", (float)m_AccuracySum_WithMethod/m_nSamples_WithMethod);
  140.  
  141.     m_callprof->SortEntries();
  142.  
  143.     CallProfiler::CallProfilerEntry *cur = m_callprof->m_list;
  144.     if (!cur)
  145.         return;
  146.  
  147. #define WIDTH_EXEC_MODEL    4
  148. #define WIDTH_SAMPLES       7
  149. #define WIDTH_ACCURACY      8
  150.  
  151. #define TOTAL_WIDTH_FIXED_FIELDS (1+WIDTH_EXEC_MODEL+1+WIDTH_SAMPLES+1+WIDTH_ACCURACY)
  152.  
  153.     WriteOutputEx(WRITE_FIT, "s", TOTAL_WIDTH_FIXED_FIELDS, 1.0, "Method");
  154.     WriteOutput(" Type Samples Accuracy\n");
  155.  
  156.     IJavaEventMonitorIDInfo *pinfo = m_pmon->GetVMInfoIfc();
  157.  
  158.     do
  159.     {
  160.         PSTR pszMethodUtf8;
  161.         JAVA_EXECUTION_MODEL exec;
  162.         if (SUCCEEDED(pinfo->MethodInformation(cur->id, &pszMethodUtf8, NULL, &exec, NULL, NULL)))
  163.         {
  164.             WriteOutputEx(WRITE_FIT | WRITE_WRAP | WRITE_UTF8, "s ", TOTAL_WIDTH_FIXED_FIELDS, 1.0, pszMethodUtf8);
  165.             WriteOutputEx(WRITE_LEFT_JUSTIFIED, "c ", WIDTH_EXEC_MODEL, CharForExecutionModel(exec));
  166.             WriteOutputEx(WRITE_LEFT_JUSTIFIED, "u ", WIDTH_SAMPLES, cur->nSamples);
  167.             WriteOutputEx(WRITE_LEFT_JUSTIFIED, ".2f\n", WIDTH_ACCURACY, (float)cur->AccuracySum/cur->nSamples);
  168.  
  169.             CoTaskMemFree(pszMethodUtf8);
  170.         }
  171.  
  172.         cur = cur->listnext;
  173.     }
  174.     while (cur);
  175. }
  176.  
  177.