Coverage Report

Created: 2026-06-30 11:14

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
std::vector<OUString> TraceEvent::getEventVectorAndClear()
87
0
{
88
0
    bool bRecording;
89
0
    std::vector<OUString> aRecording;
90
0
    {
91
0
        std::lock_guard aGuard(g_aMutex);
92
0
        bRecording = s_bRecording;
93
0
        stopRecording();
94
0
        aRecording.swap(g_aRecording);
95
0
    }
96
    // reset start time and nesting level
97
0
    if (bRecording)
98
0
        startRecording();
99
0
    return aRecording;
100
0
}
101
102
css::uno::Sequence<OUString> TraceEvent::getRecordingAndClear()
103
0
{
104
0
    return comphelper::containerToSequence(getEventVectorAndClear());
105
0
}
106
107
void ProfileZone::addRecording()
108
0
{
109
0
    assert(s_bRecording);
110
111
0
    long long nNow = getNow();
112
113
    // Generate a single "Complete Event" (type X)
114
0
    TraceEvent::addRecording("{"
115
0
                             "\"name\":\""
116
0
                             + OUString(m_sName, strlen(m_sName), RTL_TEXTENCODING_UTF8)
117
0
                             + "\","
118
0
                               "\"ph\":\"X\","
119
0
                               "\"ts\":"
120
0
                             + OUString::number(m_nCreateTime)
121
0
                             + ","
122
0
                               "\"dur\":"
123
0
                             + OUString::number(nNow - m_nCreateTime) + m_sArgs
124
0
                             + ","
125
0
                               "\"pid\":"
126
0
                             + OUString::number(m_nPid)
127
0
                             + ","
128
0
                               "\"tid\":"
129
0
                             + OUString::number(osl_getThreadIdentifier(nullptr)) + "},");
130
0
}
131
132
0
int ProfileZone::getNestingLevel() { return nProfileZoneNesting; }
133
134
0
void ProfileZone::setNestingLevel(int nNestingLevel) { nProfileZoneNesting = nNestingLevel; }
135
136
} // namespace comphelper
137
138
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */