Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example profiler that does stack sampling with ICorProfilerInfo10::SuspendRuntime #32988

Merged
merged 3 commits into from
Mar 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
cmake_minimum_required (VERSION 3.14)

project(CorProfiler)

if(NOT WIN32)
set(BASE_SOURCES )
add_compile_options(-Wno-invalid-noreturn -Wno-pragma-pack -Wno-int-to-pointer-cast -fPIC -fms-extensions -DBIT64 -DPAL_STDCPP_COMPAT -DPLATFORM_UNIX -DHOST_64BIT -std=c++11)
add_link_options(--no-undefined -pthread)

include_directories($ENV{CORECLR_PATH}/src/pal/inc/rt $ENV{CORECLR_PATH}/src/pal/inc $ENV{CORECLR_PATH}/src/inc)
endif(NOT WIN32)

if (WIN32)
set(BASE_SOURCES src/CorProfiler.def)
endif(WIN32)

include_directories($CORECLR_BIN/inc $ENV{CORECLR_PATH}/src/pal/prebuilt/inc)

set(SOURCES ${BASE_SOURCES} src/ClassFactory.cpp src/CorProfiler.cpp src/dllmain.cpp src/sampler.cpp $ENV{CORECLR_PATH}/src/pal/prebuilt/idl/corprof_i.cpp)

add_library(CorProfiler SHARED ${SOURCES})
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
@echo off
setlocal

if not defined BuildOS (
set BuildOS=Windows
)

if not defined BuildArch (
set BuildArch=x64
)

if not defined BuildType (
set BuildType=Debug
)

if not defined CORECLR_PATH (
set CORECLR_PATH=C:/git/runtime/src/coreclr
)

if not defined CORECLR_BIN (
set CORECLR_BIN=C:/git/runtime/artifacts/bin/coreclr/%BuildOS%.%BuildArch%.%BuildType%
)

set VS_COM_CMD_PATH="C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\VsDevCmd.bat"

if not defined VS_CMD_PATH (
if exist %VS_COM_CMD_PATH% (
set VS_CMD_PATH=%VS_COM_CMD_PATH%
) else (
echo No VS developer command prompt detected!
goto :EOF
)
)

echo CORECLR_PATH : %CORECLR_PATH%
echo BuildOS : %BuildOS%
echo BuildArch : %BuildArch%
echo BuildType : %BuildType%
echo VS PATH : %VS_CMD_PATH%

echo.
echo Building

if not exist bin\ (
mkdir bin
)

pushd bin

cmake -G "Visual Studio 16 2019" ..\ -DCMAKE_BUILD_TYPE=Debug

echo Calling VS Developer Command Prompt to build
call %VS_CMD_PATH%

msbuild -v:m CorProfiler.sln

popd

echo.
echo.
echo.
echo Done building

echo Copying binary to main directory
copy /y bin\Debug\CorProfiler.dll .

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash

[ -z "${BuildOS:-}" ] && export BuildOS=Linux
[ -z "${BuildArch:-}" ] && export BuildArch=x64
[ -z "${BuildType:-}" ] && export BuildType=Debug

[ -z "${CORECLR_PATH:-}" ] && export CORECLR_PATH=~/git/runtime/src/coreclr
[ -z "${CORECLR_BIN:-}" ] && export CORECLR_BIN=~/git/runtime/artifacts/bin/coreclr/$BuildOS.$BuildArch.$BuildType

printf ' CORECLR_PATH : %s\n' "$CORECLR_PATH"
printf ' BuildOS : %s\n' "$BuildOS"
printf ' BuildArch : %s\n' "$BuildArch"
printf ' BuildType : %s\n' "$BuildType"

printf ' Building ...'

if [ ! -d "bin/" ]; then
mkdir bin/
fi

pushd bin

export CC=/usr/bin/clang
export CXX=/usr/bin/clang++
cmake ../ -DCMAKE_BUILD_TYPE=Debug

make -j8

popd

printf ' Copying libCorProfiler.so to main directory\n'
cp bin/libCorProfiler.so .

printf 'Done.\n'
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#include "ClassFactory.h"
#include "CorProfiler.h"

ClassFactory::ClassFactory() : refCount(0)
{
}

ClassFactory::~ClassFactory()
{
}

HRESULT STDMETHODCALLTYPE ClassFactory::QueryInterface(REFIID riid, void **ppvObject)
{
if (riid == IID_IUnknown || riid == IID_IClassFactory)
{
*ppvObject = this;
this->AddRef();
return S_OK;
}

*ppvObject = nullptr;
return E_NOINTERFACE;
}

ULONG STDMETHODCALLTYPE ClassFactory::AddRef()
{
return std::atomic_fetch_add(&this->refCount, 1) + 1;
}

ULONG STDMETHODCALLTYPE ClassFactory::Release()
{
int count = std::atomic_fetch_sub(&this->refCount, 1) - 1;
if (count <= 0)
{
delete this;
}

return count;
}

HRESULT STDMETHODCALLTYPE ClassFactory::CreateInstance(IUnknown *pUnkOuter, REFIID riid, void **ppvObject)
{
if (pUnkOuter != nullptr)
{
*ppvObject = nullptr;
return CLASS_E_NOAGGREGATION;
}

CorProfiler* profiler = new CorProfiler();
if (profiler == nullptr)
{
return E_FAIL;
}

return profiler->QueryInterface(riid, ppvObject);
}

HRESULT STDMETHODCALLTYPE ClassFactory::LockServer(BOOL fLock)
{
return S_OK;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#pragma once

#include "unknwn.h"
#include <atomic>

class ClassFactory : public IClassFactory
{
private:
std::atomic<int> refCount;
public:
ClassFactory();
virtual ~ClassFactory();
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void **ppvObject) override;
ULONG STDMETHODCALLTYPE AddRef(void) override;
ULONG STDMETHODCALLTYPE Release(void) override;
HRESULT STDMETHODCALLTYPE CreateInstance(IUnknown *pUnkOuter, REFIID riid, void **ppvObject) override;
HRESULT STDMETHODCALLTYPE LockServer(BOOL fLock) override;
};
Loading