home *** CD-ROM | disk | FTP | other *** search
- // sampler.cpp
- //
- // Created 01/18/99
- //
- // (C)Copyright 1999 Microsoft Corporation, All rights reserved.
- //
-
- #include "project.hpp"
- #pragma hdrstop
-
- #include "sampler.hpp"
- #include "jviewprf.hpp"
- #include "utils.hpp"
-
-
- //static
- VOID SamplingProfiler::GetThreadSampleCB (ThreadRecord *pThread, PVOID token)
- {
- ((SamplingProfiler*)token)->GetThreadSample(pThread);
- }
-
-
- VOID SamplingProfiler::GetThreadSample (ThreadRecord *pThread)
- {
- if (!pThread->vmid)
- return;
-
- JVM_METHOD_SAMPLE sample;
- sample.flags = JVM_SAMPLE_METHOD_ID;
- if (FAILED(m_pmon->GetVMInfoIfc2()->SampleThreadLocation(pThread->vmid, &sample)))
- {
- sample.flags = 0;
- sample.accuracy = JVM_SAMPLE_NONE;
- }
-
- if (sample.flags & JVM_SAMPLE_METHOD_ID)
- {
- CallProfiler::CallProfilerEntry *pent = m_callprof->LookupMethod(sample.method_id, TRUE);
- if (pent)
- {
- pent->nSamples++;
- pent->AccuracySum += sample.accuracy;
- }
-
- m_nSamples_WithMethod++;
- m_AccuracySum_WithMethod += sample.accuracy;
- }
-
- m_AccuracySum += sample.accuracy;
- m_nSamples++;
- }
-
-
- //static
- DWORD WINAPI SamplingProfiler::SamplerThreadEntry (LPVOID lpThreadParameter)
- {
- return ((SamplingProfiler*)lpThreadParameter)->SamplerThread();
- }
-
-
- DWORD SamplingProfiler::SamplerThread ()
- {
- m_SamplerOverhead = 0;
- m_nSamples = 0;
- m_AccuracySum = 0;
- m_nSamples_WithMethod = 0;
- m_AccuracySum_WithMethod = 0;
-
- SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
-
- while (!m_pmon->IsVMTerminating())
- {
- Sleep(g_SamplingFrequency);
-
- PROFTIME start = GetProfilerTime();
-
- m_pmon->IterateThreads(&GetThreadSampleCB, this);
-
- PROFTIME end = GetProfilerTime();
- m_SamplerOverhead += ConvertProfilerTimeToMS(end-start);
- }
-
- return 0;
- }
-
-
- SamplingProfiler::SamplingProfiler ()
- {
- m_pmon = NULL;
- m_callprof = NULL;
- m_hSamplerThread = INVALID_HANDLE_VALUE;
- }
-
-
- HRESULT SamplingProfiler::Initialize (EventMonitor *pmon, CallProfiler *callprof)
- {
- (m_pmon = pmon)->AddRef();
- m_callprof = callprof;
- return S_OK;
- }
-
-
- VOID SamplingProfiler::Destruct ()
- {
- if (m_hSamplerThread != INVALID_HANDLE_VALUE)
- {
- TerminateThread(m_hSamplerThread, -1);
- CloseHandle(m_hSamplerThread);
- }
-
- if (m_pmon)
- m_pmon->Release();
- }
-
-
- VOID SamplingProfiler::OnVMInitialization ()
- {
- if (!(g_ProfOptions & OPT_SAMPLE))
- return;
-
- DWORD tid;
- m_hSamplerThread = CreateThread(NULL, 0, &SamplerThreadEntry, this, 0, &tid);
- if (m_hSamplerThread == INVALID_HANDLE_VALUE)
- {
- // TODO: error message
- }
- }
-
-
- VOID SamplingProfiler::SpewSamplerResults ()
- {
- if (!(g_ProfOptions & OPT_SAMPLE))
- return;
-
- WriteOutput("Total sampling overhead: ");
- WriteOutputFixedFloat(0, m_SamplerOverhead, 1);
- WriteOutputFmt(
- " ms\n"
- "Sampling frequency: %u ms\n"
- "%u total samples\n",
- g_SamplingFrequency, m_nSamples);
-
- if (m_nSamples)
- {
- WriteOutput("Average accuracy of all samples: ");
- WriteOutputFixedFloat(0, (float)m_AccuracySum/m_nSamples, 1);
- WriteOutput("%\n");
-
- if (m_nSamples_WithMethod)
- {
- WriteOutput("Average accuracy of method samples: ");
- WriteOutputFixedFloat(0, (float)m_AccuracySum_WithMethod/m_nSamples_WithMethod, 1);
- WriteOutput("%\n");
- }
- }
-
- m_callprof->SortEntries();
-
- CallProfiler::CallProfilerEntry *cur = m_callprof->m_list;
- if (!cur)
- return;
-
- #define WIDTH_EXEC_MODEL 4
- #define WIDTH_SAMPLES 7
- #define WIDTH_ACCURACY 8
-
- #define TOTAL_WIDTH_FIXED_FIELDS (1+WIDTH_EXEC_MODEL+1+WIDTH_SAMPLES+1+WIDTH_ACCURACY)
-
- WriteOutputFitString(WRITE_FIT | WRITE_SPACE, TOTAL_WIDTH_FIXED_FIELDS, 100, "Method");
- WriteOutput("Type Samples Accuracy\n");
-
- IJavaEventMonitorIDInfo *pinfo = m_pmon->GetVMInfoIfc();
-
- do
- {
- PSTR pszMethodUtf8;
- JAVA_EXECUTION_MODEL exec;
- if (SUCCEEDED(pinfo->MethodInformation(cur->id, &pszMethodUtf8, NULL, &exec, NULL, NULL)))
- {
- WriteOutputFitString(WRITE_FIT | WRITE_WRAP | WRITE_UTF8 | WRITE_SPACE, TOTAL_WIDTH_FIXED_FIELDS, 100, pszMethodUtf8);
- WriteOutputColumnString(WRITE_LEFT_JUSTIFIED | WRITE_SPACE, WIDTH_EXEC_MODEL, StringForExecutionModel(exec));
- WriteOutputColumnULONG(WRITE_LEFT_JUSTIFIED | WRITE_SPACE, WIDTH_SAMPLES, cur->nSamples);
- WriteOutputFixedFloat(0, (float)cur->AccuracySum/cur->nSamples, 2);
- WriteOutput("\n");
-
- CoTaskMemFree(pszMethodUtf8);
- }
-
- cur = cur->listnext;
- }
- while (cur);
- }
-
-