summaryrefslogtreecommitdiffstats
path: root/comphelper/source/misc/traceevent.cxx
blob: fb07e1caa771b71420a9d44d38768a7cb072d5d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <sal/config.h>

#include <atomic>
#include <mutex>
#include <iostream>

#include <comphelper/profilezone.hxx>
#include <comphelper/sequence.hxx>
#include <comphelper/traceevent.hxx>

namespace comphelper
{
#ifdef DBG_UTIL
std::atomic<bool> TraceEvent::s_bRecording = (getenv("TRACE_EVENT_RECORDING") != nullptr);
#else
std::atomic<bool> TraceEvent::s_bRecording = false;
#endif

std::size_t TraceEvent::s_nBufferSize = 0;
void (*TraceEvent::s_pBufferFullCallback)() = nullptr;

int AsyncEvent::s_nIdCounter = 0;

static thread_local int nProfileZoneNesting = 0; // Level of Nested Profile Zones

namespace
{
std::vector<OUString> g_aRecording; // recorded data
std::mutex g_aMutex;
}

void TraceEvent::addRecording(const OUString& sObject)
{
    std::lock_guard aGuard(g_aMutex);

    g_aRecording.emplace_back(sObject);

    if (s_nBufferSize > 0 && g_aRecording.size() >= s_nBufferSize)
    {
        if (s_pBufferFullCallback != nullptr)
            (*s_pBufferFullCallback)();
    }
}

void TraceEvent::addInstantEvent(const char* sName, const std::map<OUString, OUString>& args)
{
    long long nNow = getNow();

    int nPid = 0;
    oslProcessInfo aProcessInfo;
    aProcessInfo.Size = sizeof(oslProcessInfo);
    if (osl_getProcessInfo(nullptr, osl_Process_IDENTIFIER, &aProcessInfo) == osl_Process_E_None)
        nPid = aProcessInfo.Ident;

    addRecording("{"
                 "\"name:\""
                 + OUString(sName, strlen(sName), RTL_TEXTENCODING_UTF8)
                 + "\","
                   "\"ph\":\"i\""
                 + createArgsString(args) + ",\"ts\":" + OUString::number(nNow)
                 + ","
                   "\"pid\":"
                 + OUString::number(nPid)
                 + ","
                   "\"tid\":"
                 + OUString::number(osl_getThreadIdentifier(nullptr)) + "},");
}

void TraceEvent::startRecording()
{
    std::lock_guard aGuard(g_aMutex);
    s_bRecording = true;
}

void TraceEvent::stopRecording() { s_bRecording = false; }

void TraceEvent::setBufferSizeAndCallback(std::size_t bufferSize, void (*bufferFullCallback)())
{
    s_nBufferSize = bufferSize;
    s_pBufferFullCallback = bufferFullCallback;
}

std::vector<OUString> TraceEvent::getEventVectorAndClear()
{
    bool bRecording;
    std::vector<OUString> aRecording;
    {
        std::lock_guard aGuard(g_aMutex);
        bRecording = s_bRecording;
        stopRecording();
        aRecording.swap(g_aRecording);
    }
    // reset start time and nesting level
    if (bRecording)
        startRecording();
    return aRecording;
}

css::uno::Sequence<OUString> TraceEvent::getRecordingAndClear()
{
    return comphelper::containerToSequence(getEventVectorAndClear());
}

void ProfileZone::addRecording()
{
    assert(s_bRecording);

    long long nNow = getNow();

    // Generate a single "Complete Event" (type X)
    TraceEvent::addRecording("{"
                             "\"name\":\""
                             + OUString(m_sName, strlen(m_sName), RTL_TEXTENCODING_UTF8)
                             + "\","
                               "\"ph\":\"X\","
                               "\"ts\":"
                             + OUString::number(m_nCreateTime)
                             + ","
                               "\"dur\":"
                             + OUString::number(nNow - m_nCreateTime) + m_sArgs
                             + ","
                               "\"pid\":"
                             + OUString::number(m_nPid)
                             + ","
                               "\"tid\":"
                             + OUString::number(osl_getThreadIdentifier(nullptr)) + "},");
}

int ProfileZone::getNestingLevel() { return nProfileZoneNesting; }

void ProfileZone::setNestingLevel(int nNestingLevel) { nProfileZoneNesting = nNestingLevel; }

} // namespace comphelper

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */