Skip to content
Draft
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
1 change: 1 addition & 0 deletions ddtrace/internal/datadog/profiling/echion/.clang-format
153 changes: 153 additions & 0 deletions ddtrace/internal/datadog/profiling/echion/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# Protobuf compiler
protoc*

# VS Code
.vscode/

# setuptools-scm
_version.py

# profiles
profiles/

# Cursor
.cursor/
69 changes: 69 additions & 0 deletions ddtrace/internal/datadog/profiling/echion/echion/cache.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// This file is part of "echion" which is released under MIT.
//
// Copyright (c) 2023 Gabriele N. Tornetta <[email protected]>.

#pragma once

#include <functional>
#include <list>
#include <memory>
#include <unordered_map>

#include <echion/errors.h>

#define CACHE_MAX_ENTRIES 2048

template <typename K, typename V>
class LRUCache
{
public:
LRUCache(size_t capacity) : capacity(capacity) {}

Result<std::reference_wrapper<V>> lookup(const K& k);

void store(const K& k, std::unique_ptr<V> v);

class LookupError : public std::exception
{
public:
const char* what() const noexcept override
{
return "Key not found in cache";
}
};

private:
size_t capacity;
std::list<std::pair<K, std::unique_ptr<V>>> items;
std::unordered_map<K, typename std::list<std::pair<K, std::unique_ptr<V>>>::iterator> index;
};

template <typename K, typename V>
void LRUCache<K, V>::store(const K& k, std::unique_ptr<V> v)
{
// Check if cache is full
if (items.size() >= capacity)
{
index.erase(items.back().first);
items.pop_back();
}

// Insert the new item at front of the list
items.emplace_front(k, std::move(v));

// Insert in the map
index[k] = items.begin();
}

template <typename K, typename V>
Result<std::reference_wrapper<V>> LRUCache<K, V>::lookup(const K& k)
{
auto itr = index.find(k);
if (itr == index.end())
return ErrorKind::LookupError;

// Move to the front of the list
items.splice(items.begin(), items, itr->second);

return std::reference_wrapper<V>(*(itr->second->second.get()));
}
137 changes: 137 additions & 0 deletions ddtrace/internal/datadog/profiling/echion/echion/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// This file is part of "echion" which is released under MIT.
//
// Copyright (c) 2023 Gabriele N. Tornetta <[email protected]>.

#pragma once

#define PY_SSIZE_T_CLEAN
#include <Python.h>

#include <string>

// Sampling interval
inline unsigned int interval = 1000;

// CPU Time mode
inline int cpu = 0;

// For cpu time mode, Echion only unwinds threads that're running by default.
// Set this to false to unwind all threads.
inline bool ignore_non_running_threads = true;

// Memory events
inline int memory = 0;

// Native stack sampling
inline int native = 0;

// Where mode
inline int where = 0;

// Maximum number of frames to unwind
inline unsigned int max_frames = 2048;

// Pipe name (where mode IPC)
inline std::string pipe_name;

// ----------------------------------------------------------------------------
static PyObject* set_interval(PyObject* Py_UNUSED(m), PyObject* args)
{
unsigned int new_interval;
if (!PyArg_ParseTuple(args, "I", &new_interval))
return NULL;

interval = new_interval;

Py_RETURN_NONE;
}

// ----------------------------------------------------------------------------
inline void _set_cpu(int new_cpu)
{
cpu = new_cpu;
}

// ----------------------------------------------------------------------------
inline void _set_ignore_non_running_threads(bool new_ignore_non_running_threads)
{
ignore_non_running_threads = new_ignore_non_running_threads;
}

// ----------------------------------------------------------------------------
static PyObject* set_cpu(PyObject* Py_UNUSED(m), PyObject* args)
{
int new_cpu;
if (!PyArg_ParseTuple(args, "p", &new_cpu))
return NULL;

_set_cpu(new_cpu);

Py_RETURN_NONE;
}

// ----------------------------------------------------------------------------
static PyObject* set_memory(PyObject* Py_UNUSED(m), PyObject* args)
{
int new_memory;
if (!PyArg_ParseTuple(args, "p", &new_memory))
return NULL;

memory = new_memory;

Py_RETURN_NONE;
}

// ----------------------------------------------------------------------------
static PyObject* set_native(PyObject* Py_UNUSED(m), PyObject* args)
{
#ifndef UNWIND_NATIVE_DISABLE
int new_native;
if (!PyArg_ParseTuple(args, "p", &new_native))
return NULL;

native = new_native;
#else
PyErr_SetString(PyExc_RuntimeError,
"Native profiling is disabled, please re-build/install echion without "
"UNWIND_NATIVE_DISABLE env var/preprocessor flag");
return NULL;
#endif // UNWIND_NATIVE_DISABLE
Py_RETURN_NONE;
}

// ----------------------------------------------------------------------------
static PyObject* set_where(PyObject* Py_UNUSED(m), PyObject* args)
{
int value;
if (!PyArg_ParseTuple(args, "p", &value))
return NULL;

where = value;

Py_RETURN_NONE;
}

// ----------------------------------------------------------------------------
static PyObject* set_pipe_name(PyObject* Py_UNUSED(m), PyObject* args)
{
const char* name;
if (!PyArg_ParseTuple(args, "s", &name))
return NULL;

pipe_name = name;

Py_RETURN_NONE;
}

// ----------------------------------------------------------------------------
static PyObject* set_max_frames(PyObject* Py_UNUSED(m), PyObject* args)
{
unsigned int new_max_frames;
if (!PyArg_ParseTuple(args, "I", &new_max_frames))
return NULL;

max_frames = new_max_frames;

Py_RETURN_NONE;
}
Loading
Loading