Coverage Report

Created: 2025-11-16 09:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/comphelper/source/misc/traceevent.cxx
Line
Count
Source
1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
5
 * This Source Code Form is subject to the terms of the Mozilla Public
6
 * License, v. 2.0. If a copy of the MPL was not distributed with this
7
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
 */
9
10
#include <sal/config.h>
11
12
#include <atomic>
13
#include <mutex>
14
15
#include <comphelper/profilezone.hxx>
16
#include <comphelper/sequence.hxx>
17
#include <comphelper/traceevent.hxx>
18
19
namespace comphelper
20
{
21
#ifdef DBG_UTIL
22
std::atomic<bool> TraceEvent::s_bRecording = (getenv("TRACE_EVENT_RECORDING") != nullptr);
23
#else
24
std::atomic<bool> TraceEvent::s_bRecording = false;
25
#endif
26
27
std::size_t TraceEvent::s_nBufferSize = 0;
28
void (*TraceEvent::s_pBufferFullCallback)() = nullptr;
29
30
int AsyncEvent::s_nIdCounter = 0;
31
32
static thread_local int nProfileZoneNesting = 0; // Level of Nested Profile Zones
33
34
namespace
35
{
36
std::vector<OUString> g_aRecording; // recorded data
37
std::mutex g_aMutex;
38
}
39
40
void TraceEvent::addRecording(const OUString& sObject)
41
0
{
42
0
    bool bEmitCallback;
43
0
    {
44
0
        std::lock_guard aGuard(g_aMutex);
45
46
0
        g_aRecording.emplace_back(sObject);
47
48
0
        bEmitCallback = s_nBufferSize > 0 && g_aRecording.size() >= s_nBufferSize;
49
0
    }
50
0
    if (bEmitCallback && s_pBufferFullCallback != nullptr)
51
0
        (*s_pBufferFullCallback)();
52
0
}
53
54
void TraceEvent::addInstantEvent(const char* sName, const std::map<OUString, OUString>& args)
55
0
{
56
0
    long long nNow = getNow();
57
58
0
    int nPid = 0;
59
0
    oslProcessInfo aProcessInfo;
60
0
    aProcessInfo.Size = sizeof(oslProcessInfo);
61
0
    if (osl_getProcessInfo(nullptr, osl_Process_IDENTIFIER, &aProcessInfo) == osl_Process_E_None)
62
0
        nPid = aProcessInfo.Ident;
63
64
0
    addRecording("{"
65
0
                 "\"name:\""
66
0
                 + OUString(sName, strlen(sName), RTL_TEXTENCODING_UTF8)
67
0
                 + "\","
68
0
                   "\"ph\":\"i\""
69
0
                 + createArgsString(args) + ",\"ts\":" + OUString::number(nNow)
70
0
                 + ","
71
0
                   "\"pid\":"
72
0
                 + OUString::number(nPid)
73
0
                 + ","
74
0
                   "\"tid\":"
75
0
                 + OUString::number(osl_getThreadIdentifier(nullptr)) + "},");
76
0
}
77
78
void TraceEvent::startRecording()
79
0
{
80
0
    std::lock_guard aGuard(g_aMutex);
81
0
    s_bRecording = true;
82
0
}
83
84
0
void TraceEvent::stopRecording() { s_bRecording = false; }
85
86
void TraceEvent::setBufferSizeAndCallback(std::size_t bufferSize, void (*bufferFullCallback)())
87
0
{
88
0
    s_nBufferSize = bufferSize;
89
0
    s_pBufferFullCallback = bufferFullCallback;
90
0
}
91
92
std::vector<OUString> TraceEvent::getEventVectorAndClear()
93
0
{
94
0
    bool bRecording;
95
0
    std::vector<OUString> aRecording;
96
0
    {
97
0
        std::lock_guard aGuard(g_aMutex);
98
0
        bRecording = s_bRecording;
99
0
        stopRecording();
100
0
        aRecording.swap(g_aRecording);
101
0
    }
102
    // reset start time and nesting level
103
0
    if (bRecording)
104
0
        startRecording();
105
0
    return aRecording;
106
0
}
107
108
css::uno::Sequence<OUString> TraceEvent::getRecordingAndClear()
109
0
{
110
0
    return comphelper::containerToSequence(getEventVectorAndClear());
111
0
}
112
113
void ProfileZone::addRecording()
114
0
{
115
0
    assert(s_bRecording);
116
117
0
    long long nNow = getNow();
118
119
    // Generate a single "Complete Event" (type X)
120
0
    TraceEvent::addRecording("{"
121
0
                             "\"name\":\""
122
0
                             + OUString(m_sName, strlen(m_sName), RTL_TEXTENCODING_UTF8)
123
0
                             + "\","
124
0
                               "\"ph\":\"X\","
125
0
                               "\"ts\":"
126
0
                             + OUString::number(m_nCreateTime)
127
0
                             + ","
128
0
                               "\"dur\":"
129
0
                             + OUString::number(nNow - m_nCreateTime) + m_sArgs
130
0
                             + ","
131
0
                               "\"pid\":"
132
0
                             + OUString::number(m_nPid)
133
0
                             + ","
134
0
                               "\"tid\":"
135
0
                             + OUString::number(osl_getThreadIdentifier(nullptr)) + "},");
136
0
}
137
138
0
int ProfileZone::getNestingLevel() { return nProfileZoneNesting; }
139
140
0
void ProfileZone::setNestingLevel(int nNestingLevel) { nProfileZoneNesting = nNestingLevel; }
141
142
} // namespace comphelper
143
144
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */