| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* Copyright (c) 2023 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 5 |
* * |
| 6 |
* This file is part of FreeCAD. * |
| 7 |
* * |
| 8 |
* FreeCAD is free software: you can redistribute it and/or modify it * |
| 9 |
* under the terms of the GNU Lesser General Public License as * |
| 10 |
* published by the Free Software Foundation, either version 2.1 of the * |
| 11 |
* License, or (at your option) any later version. * |
| 12 |
* * |
| 13 |
* FreeCAD is distributed in the hope that it will be useful, but * |
| 14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 16 |
* Lesser General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Lesser General Public * |
| 19 |
* License along with FreeCAD. If not, see * |
| 20 |
* <https://www.gnu.org/licenses/>. * |
| 21 |
* * |
| 22 |
**************************************************************************/ |
| 23 |
|
| 24 |
|
| 25 |
#include <QApplication> |
| 26 |
#include <QKeyEvent> |
| 27 |
#include <QGuiApplication> |
| 28 |
|
| 29 |
|
| 30 |
#include <chrono> |
| 31 |
|
| 32 |
#include "PythonTracing.h" |
| 33 |
#include <App/Application.h> |
| 34 |
#include <Base/Interpreter.h> |
| 35 |
|
| 36 |
using namespace Gui; |
| 37 |
|
| 38 |
using Clock = std::chrono::steady_clock; |
| 39 |
using TimePoint = std::chrono::time_point<Clock>; |
| 40 |
|
| 41 |
|
| 42 |
struct PythonTracing::Private |
| 43 |
{ |
| 44 |
bool active {false}; |
| 45 |
int timeout {200}; // NOLINT |
| 46 |
|
| 47 |
// NOLINTBEGIN |
| 48 |
static int profilerInterval; |
| 49 |
static bool profilerDisabled; |
| 50 |
// NOLINTEND |
| 51 |
}; |
| 52 |
|
| 53 |
// NOLINTBEGIN |
| 54 |
int PythonTracing::Private::profilerInterval = 200; |
| 55 |
bool PythonTracing::Private::profilerDisabled = false; |
| 56 |
// NOLINTEND |
| 57 |
|
| 58 |
PythonTracing::PythonTracing() |
| 59 |
: d {std::make_unique<Private>()} |
| 60 |
{} |
| 61 |
|
| 62 |
PythonTracing::~PythonTracing() = default; |
| 63 |
|
| 64 |
bool PythonTracing::isActive() const |
| 65 |
{ |
| 66 |
return d->active; |
| 67 |
} |
| 68 |
|
| 69 |
void PythonTracing::activate() |
| 70 |
{ |
| 71 |
d->active = true; |
| 72 |
setPythonTraceEnabled(true); |
| 73 |
} |
| 74 |
|
| 75 |
void PythonTracing::deactivate() |
| 76 |
{ |
| 77 |
d->active = false; |
| 78 |
setPythonTraceEnabled(false); |
| 79 |
} |
| 80 |
|
| 81 |
void PythonTracing::fetchFromSettings() |
| 82 |
{ |
| 83 |
const long defaultTimeout = 200; |
| 84 |
|
| 85 |
auto parameterGroup = App::GetApplication().GetParameterGroupByPath( |
| 86 |
"User parameter:BaseApp/Preferences/PythonConsole" |
| 87 |
); |
| 88 |
int interval = static_cast<int>(parameterGroup->GetInt("ProfilerInterval", defaultTimeout)); |
| 89 |
setTimeout(interval); |
| 90 |
} |
| 91 |
|
| 92 |
bool PythonTracing::interrupt() const |
| 93 |
{ |
| 94 |
if (isActive()) { |
| 95 |
PyErr_SetInterrupt(); |
| 96 |
return true; |
| 97 |
} |
| 98 |
|
| 99 |
return false; |
| 100 |
} |
| 101 |
|
| 102 |
void PythonTracing::setTimeout(int ms) |
| 103 |
{ |
| 104 |
d->timeout = ms; |
| 105 |
} |
| 106 |
|
| 107 |
int PythonTracing::timeout() const |
| 108 |
{ |
| 109 |
return d->timeout; |
| 110 |
} |
| 111 |
|
| 112 |
void PythonTracing::setPythonTraceEnabled(bool enabled) const |
| 113 |
{ |
| 114 |
Py_tracefunc trace = nullptr; |
| 115 |
if (enabled && timeout() > 0) { |
| 116 |
Private::profilerInterval = timeout(); |
| 117 |
trace = &tracer_callback; |
| 118 |
} |
| 119 |
else { |
| 120 |
Private::profilerDisabled = true; |
| 121 |
} |
| 122 |
|
| 123 |
Base::PyGILStateLocker lock; |
| 124 |
PyEval_SetTrace(trace, nullptr); |
| 125 |
} |
| 126 |
|
| 127 |
/* |
| 128 |
* This callback ensures that Qt runs its event loop (i.e. updates the GUI, processes keyboard and |
| 129 |
* mouse events, etc.) at least every 200 ms, even when there is long-running Python code on the |
| 130 |
* main thread. It is registered as the global trace function of the Python environment. |
| 131 |
* |
| 132 |
* WARNING! THIS IS PERFORMANCE-CRITICAL CODE! |
| 133 |
* This callback is even called for per-opcode events in the Python code, so unoptimized code could |
| 134 |
* dramatically slow down the execution of Python code and FreeCAD macros. |
| 135 |
*/ |
| 136 |
int PythonTracing::tracer_callback(PyObject* obj, PyFrameObject* frame, int what, PyObject* arg) |
| 137 |
{ |
| 138 |
Q_UNUSED(obj) |
| 139 |
Q_UNUSED(frame) |
| 140 |
Q_UNUSED(what) |
| 141 |
Q_UNUSED(arg) |
| 142 |
|
| 143 |
// no need to check the time at every single Python opcode execution |
| 144 |
static int skipCounter = 0; |
| 145 |
if (++skipCounter < 1000) { |
| 146 |
return 0; |
| 147 |
} |
| 148 |
skipCounter = 0; |
| 149 |
|
| 150 |
static TimePoint lastCalledTime = Clock::now(); |
| 151 |
TimePoint currTime = Clock::now(); |
| 152 |
|
| 153 |
// if previous code object was executed |
| 154 |
if (Private::profilerDisabled) { |
| 155 |
Private::profilerDisabled = false; |
| 156 |
lastCalledTime = currTime; |
| 157 |
} |
| 158 |
|
| 159 |
const std::chrono::duration<double> duration = currTime - lastCalledTime; |
| 160 |
if (1000.0 * duration.count() >= static_cast<double>(Private::profilerInterval)) { |
| 161 |
lastCalledTime = currTime; |
| 162 |
QGuiApplication::processEvents(); |
| 163 |
} |
| 164 |
|
| 165 |
return 0; |
| 166 |
} |
| 167 |
|
| 168 |
// ------------------------------------------------------------------------------------------------ |
| 169 |
|
| 170 |
PythonTracingLocker::PythonTracingLocker(PythonTracing& trace) |
| 171 |
: trace {trace} |
| 172 |
{ |
| 173 |
trace.activate(); |
| 174 |
} |
| 175 |
|
| 176 |
PythonTracingLocker::~PythonTracingLocker() |
| 177 |
{ |
| 178 |
trace.deactivate(); |
| 179 |
} |
| 180 |
|
| 181 |
// ------------------------------------------------------------------------------------------------ |
| 182 |
|
| 183 |
PythonTracingWatcher::PythonTracingWatcher(QObject* parent) |
| 184 |
: QObject(parent) |
| 185 |
{ |
| 186 |
qApp->installEventFilter(this); |
| 187 |
} |
| 188 |
|
| 189 |
PythonTracingWatcher::~PythonTracingWatcher() |
| 190 |
{ |
| 191 |
qApp->removeEventFilter(this); |
| 192 |
} |
| 193 |
|
| 194 |
bool PythonTracingWatcher::eventFilter(QObject* object, QEvent* event) |
| 195 |
{ |
| 196 |
if (event && event->type() == QEvent::ShortcutOverride) { |
| 197 |
auto kevent = static_cast<QKeyEvent*>(event); |
| 198 |
if (kevent->key() == Qt::Key_C && kevent->modifiers() == Qt::ControlModifier) { |
| 199 |
if (trace.interrupt()) { |
| 200 |
return true; |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
return QObject::eventFilter(object, event); |
| 206 |
} |
| 207 |
|
| 208 |
PythonTracing& PythonTracingWatcher::getTrace() |
| 209 |
{ |
| 210 |
trace.fetchFromSettings(); |
| 211 |
return trace; |
| 212 |
} |