Skip to content

Commit 69fcec3

Browse files
copy echion to dd-trace-py
1 parent 35307d9 commit 69fcec3

File tree

30 files changed

+5851
-27
lines changed

30 files changed

+5851
-27
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
Language: Cpp
2+
BasedOnStyle: Google
3+
AccessModifierOffset: -4
4+
AlignAfterOpenBracket: Align
5+
AlignConsecutiveAssignments: false
6+
AlignConsecutiveDeclarations: false
7+
AlignEscapedNewlines: Left
8+
AlignOperands: true
9+
AlignTrailingComments: true
10+
AllowAllParametersOfDeclarationOnNextLine: true
11+
AllowShortBlocksOnASingleLine: false
12+
AllowShortCaseLabelsOnASingleLine: false
13+
AllowShortFunctionsOnASingleLine: Empty
14+
AllowShortIfStatementsOnASingleLine: false
15+
AllowShortLoopsOnASingleLine: false
16+
AlwaysBreakAfterDefinitionReturnType: None
17+
AlwaysBreakAfterReturnType: None
18+
AlwaysBreakBeforeMultilineStrings: false
19+
AlwaysBreakTemplateDeclarations: Yes
20+
BinPackArguments: true
21+
BinPackParameters: true
22+
BraceWrapping:
23+
AfterClass: true
24+
AfterControlStatement: true
25+
AfterEnum: true
26+
AfterFunction: true
27+
AfterNamespace: true
28+
AfterStruct: true
29+
AfterUnion: true
30+
BeforeCatch: true
31+
BeforeElse: true
32+
IndentBraces: false
33+
BreakBeforeBinaryOperators: None
34+
BreakBeforeBraces: Custom
35+
BreakBeforeInheritanceComma: false
36+
BreakInheritanceList: BeforeColon
37+
BreakBeforeTernaryOperators: true
38+
BreakConstructorInitializersBeforeComma: false
39+
BreakConstructorInitializers: BeforeColon
40+
BreakAfterJavaFieldAnnotations: false
41+
BreakStringLiterals: true
42+
ColumnLimit: 100
43+
CommentPragmas: '^ IWYU pragma:'
44+
CompactNamespaces: false
45+
ConstructorInitializerAllOnOneLineOrOnePerLine: true
46+
ConstructorInitializerIndentWidth: 4
47+
ContinuationIndentWidth: 4
48+
Cpp11BracedListStyle: true
49+
DerivePointerAlignment: false
50+
DisableFormat: false
51+
ExperimentalAutoDetectBinPacking: false
52+
FixNamespaceComments: true
53+
ForEachMacros:
54+
- foreach
55+
- Q_FOREACH
56+
- BOOST_FOREACH
57+
IncludeBlocks: Preserve
58+
IncludeCategories:
59+
- Regex: '^<.*\.h>'
60+
Priority: 1
61+
- Regex: '^<.*'
62+
Priority: 2
63+
- Regex: '.*'
64+
Priority: 3
65+
IncludeIsMainRegex: '([-_](test|unittest))?$'
66+
IndentCaseLabels: true
67+
IndentPPDirectives: None
68+
IndentWidth: 4
69+
IndentWrappedFunctionNames: false
70+
JavaScriptQuotes: Leave
71+
JavaScriptWrapImports: true
72+
KeepEmptyLinesAtTheStartOfBlocks: false
73+
MacroBlockBegin: ''
74+
MacroBlockEnd: ''
75+
MaxEmptyLinesToKeep: 2
76+
NamespaceIndentation: None
77+
ObjCBinPackProtocolList: Never
78+
ObjCBlockIndentWidth: 4
79+
ObjCSpaceAfterProperty: false
80+
ObjCSpaceBeforeProtocolList: true
81+
PenaltyBreakAssignment: 2
82+
PenaltyBreakBeforeFirstCallParameter: 1
83+
PenaltyBreakComment: 300
84+
PenaltyBreakFirstLessLess: 120
85+
PenaltyBreakString: 1000
86+
PenaltyBreakTemplateDeclaration: 10
87+
PenaltyExcessCharacter: 1000000
88+
PenaltyReturnTypeOnItsOwnLine: 200
89+
PointerAlignment: Left
90+
ReflowComments: true
91+
SortIncludes: true
92+
SortUsingDeclarations: true
93+
SpaceAfterCStyleCast: false
94+
SpaceAfterTemplateKeyword: true
95+
SpaceBeforeAssignmentOperators: true
96+
SpaceBeforeCpp11BracedList: false
97+
SpaceBeforeCtorInitializerColon: true
98+
SpaceBeforeInheritanceColon: true
99+
SpaceBeforeParens: ControlStatements
100+
SpaceBeforeRangeBasedForLoopColon: true
101+
SpaceInEmptyParentheses: false
102+
SpacesBeforeTrailingComments: 2
103+
SpacesInAngles: false
104+
SpacesInContainerLiterals: false
105+
SpacesInCStyleCastParentheses: false
106+
SpacesInParentheses: false
107+
SpacesInSquareBrackets: false
108+
Standard: Cpp11
109+
TabWidth: 4
110+
UseTab: Never

ddtrace/internal/datadog/profiling/stack_v2/CMakeLists.txt

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,26 +47,12 @@ if(NOT Threads_FOUND OR NOT CMAKE_USE_PTHREADS_INIT)
4747
message(FATAL_ERROR "pthread compatible library not found")
4848
endif()
4949

50-
# Add echion
51-
set(ECHION_COMMIT
52-
"43432c5c0a89617b06533215a15d0d6ffbbfd02b" # https://github.com/P403n1x87/echion/commit/43432c5c0a89617b06533215a15d0d6ffbbfd02b
53-
CACHE STRING "Commit hash of echion to use")
54-
FetchContent_Declare(
55-
echion
56-
GIT_REPOSITORY "https://github.com/P403n1x87/echion.git"
57-
GIT_TAG ${ECHION_COMMIT})
58-
FetchContent_GetProperties(echion)
59-
60-
if(NOT echion_POPULATED)
61-
FetchContent_Populate(echion)
62-
endif()
63-
6450
# Specify the target C-extension that we want to build
6551
add_library(
6652
${EXTENSION_NAME} SHARED
67-
${echion_SOURCE_DIR}/echion/frame.cc
68-
${echion_SOURCE_DIR}/echion/render.cc
69-
${echion_SOURCE_DIR}/echion/danger.cc
53+
src/echion/danger.cc
54+
src/echion/frame.cc
55+
src/echion/render.cc
7056
src/sampler.cpp
7157
src/stack_renderer.cpp
7258
src/stack_v2.cpp
@@ -82,7 +68,6 @@ add_cppcheck_target(
8268
${CMAKE_CURRENT_SOURCE_DIR}/include
8369
${CMAKE_CURRENT_SOURCE_DIR}/include/util
8470
${CMAKE_CURRENT_SOURCE_DIR}/..
85-
${echion_SOURCE_DIR}
8671
SRC
8772
${CMAKE_CURRENT_SOURCE_DIR}/src)
8873

@@ -93,8 +78,8 @@ target_compile_definitions(${EXTENSION_NAME} PRIVATE UNWIND_NATIVE_DISABLE)
9378
target_include_directories(
9479
${EXTENSION_NAME} PRIVATE .. # include dd_wrapper from the root in order to make its paths transparent in the code
9580
include)
96-
target_include_directories(${EXTENSION_NAME} SYSTEM PRIVATE ${echion_SOURCE_DIR} ${Python3_INCLUDE_DIRS}
97-
include/vendored include/util)
81+
target_include_directories(${EXTENSION_NAME} SYSTEM PRIVATE ${Python3_INCLUDE_DIRS} echion include/vendored
82+
include/util)
9883

9984
# Echion sources need to be given the current platform
10085
if(APPLE)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// This file is part of "echion" which is released under MIT.
2+
//
3+
// Copyright (c) 2023 Gabriele N. Tornetta <phoenix1987@gmail.com>.
4+
5+
#pragma once
6+
7+
#include <functional>
8+
#include <list>
9+
#include <memory>
10+
#include <unordered_map>
11+
12+
#include <echion/errors.h>
13+
14+
#define CACHE_MAX_ENTRIES 2048
15+
16+
template <typename K, typename V>
17+
class LRUCache
18+
{
19+
public:
20+
LRUCache(size_t capacity) : capacity(capacity) {}
21+
22+
Result<std::reference_wrapper<V>> lookup(const K& k);
23+
24+
void store(const K& k, std::unique_ptr<V> v);
25+
26+
private:
27+
size_t capacity;
28+
std::list<std::pair<K, std::unique_ptr<V>>> items;
29+
std::unordered_map<K, typename std::list<std::pair<K, std::unique_ptr<V>>>::iterator> index;
30+
};
31+
32+
template <typename K, typename V>
33+
void LRUCache<K, V>::store(const K& k, std::unique_ptr<V> v)
34+
{
35+
// Check if cache is full
36+
if (items.size() >= capacity)
37+
{
38+
index.erase(items.back().first);
39+
items.pop_back();
40+
}
41+
42+
// Insert the new item at front of the list
43+
items.emplace_front(k, std::move(v));
44+
45+
// Insert in the map
46+
index[k] = items.begin();
47+
}
48+
49+
template <typename K, typename V>
50+
Result<std::reference_wrapper<V>> LRUCache<K, V>::lookup(const K& k)
51+
{
52+
auto itr = index.find(k);
53+
if (itr == index.end())
54+
return ErrorKind::LookupError;
55+
56+
// Move to the front of the list
57+
items.splice(items.begin(), items, itr->second);
58+
59+
return std::reference_wrapper<V>(*(itr->second->second.get()));
60+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// This file is part of "echion" which is released under MIT.
2+
//
3+
// Copyright (c) 2023 Gabriele N. Tornetta <phoenix1987@gmail.com>.
4+
5+
#pragma once
6+
7+
#define PY_SSIZE_T_CLEAN
8+
#include <Python.h>
9+
10+
#include <string>
11+
12+
// Sampling interval
13+
inline unsigned int interval = 1000;
14+
15+
// CPU Time mode
16+
inline int cpu = 0;
17+
18+
// For cpu time mode, Echion only unwinds threads that're running by default.
19+
// Set this to false to unwind all threads.
20+
inline bool ignore_non_running_threads = true;
21+
22+
// Memory events
23+
inline int memory = 0;
24+
25+
// Native stack sampling
26+
inline int native = 0;
27+
28+
// Where mode
29+
inline int where = 0;
30+
31+
// Maximum number of frames to unwind
32+
inline unsigned int max_frames = 2048;
33+
34+
// Pipe name (where mode IPC)
35+
inline std::string pipe_name;
36+
37+
// ----------------------------------------------------------------------------
38+
static PyObject* set_interval(PyObject* Py_UNUSED(m), PyObject* args)
39+
{
40+
unsigned int new_interval;
41+
if (!PyArg_ParseTuple(args, "I", &new_interval))
42+
return NULL;
43+
44+
interval = new_interval;
45+
46+
Py_RETURN_NONE;
47+
}
48+
49+
// ----------------------------------------------------------------------------
50+
inline void _set_cpu(int new_cpu)
51+
{
52+
cpu = new_cpu;
53+
}
54+
55+
// ----------------------------------------------------------------------------
56+
inline void _set_ignore_non_running_threads(bool new_ignore_non_running_threads)
57+
{
58+
ignore_non_running_threads = new_ignore_non_running_threads;
59+
}
60+
61+
// ----------------------------------------------------------------------------
62+
static PyObject* set_cpu(PyObject* Py_UNUSED(m), PyObject* args)
63+
{
64+
int new_cpu;
65+
if (!PyArg_ParseTuple(args, "p", &new_cpu))
66+
return NULL;
67+
68+
_set_cpu(new_cpu);
69+
70+
Py_RETURN_NONE;
71+
}
72+
73+
// ----------------------------------------------------------------------------
74+
static PyObject* set_memory(PyObject* Py_UNUSED(m), PyObject* args)
75+
{
76+
int new_memory;
77+
if (!PyArg_ParseTuple(args, "p", &new_memory))
78+
return NULL;
79+
80+
memory = new_memory;
81+
82+
Py_RETURN_NONE;
83+
}
84+
85+
// ----------------------------------------------------------------------------
86+
static PyObject* set_native(PyObject* Py_UNUSED(m), PyObject* Py_UNUSED(args))
87+
{
88+
#ifndef UNWIND_NATIVE_DISABLE
89+
int new_native;
90+
if (!PyArg_ParseTuple(args, "p", &new_native))
91+
return NULL;
92+
93+
native = new_native;
94+
#else
95+
PyErr_SetString(PyExc_RuntimeError,
96+
"Native profiling is disabled, please re-build/install echion without "
97+
"UNWIND_NATIVE_DISABLE env var/preprocessor flag");
98+
return NULL;
99+
#endif // UNWIND_NATIVE_DISABLE
100+
Py_RETURN_NONE;
101+
}
102+
103+
// ----------------------------------------------------------------------------
104+
static PyObject* set_where(PyObject* Py_UNUSED(m), PyObject* args)
105+
{
106+
int value;
107+
if (!PyArg_ParseTuple(args, "p", &value))
108+
return NULL;
109+
110+
where = value;
111+
112+
Py_RETURN_NONE;
113+
}
114+
115+
// ----------------------------------------------------------------------------
116+
static PyObject* set_pipe_name(PyObject* Py_UNUSED(m), PyObject* args)
117+
{
118+
const char* name;
119+
if (!PyArg_ParseTuple(args, "s", &name))
120+
return NULL;
121+
122+
pipe_name = name;
123+
124+
Py_RETURN_NONE;
125+
}
126+
127+
// ----------------------------------------------------------------------------
128+
static PyObject* set_max_frames(PyObject* Py_UNUSED(m), PyObject* args)
129+
{
130+
unsigned int new_max_frames;
131+
if (!PyArg_ParseTuple(args, "I", &new_max_frames))
132+
return NULL;
133+
134+
max_frames = new_max_frames;
135+
136+
Py_RETURN_NONE;
137+
}

0 commit comments

Comments
 (0)