summaryrefslogtreecommitdiff
path: root/include/comphelper/traceevent.hxx
blob: cc3c390d7e59d304826266693417977198c85807 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * 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/.
*/

#ifndef INCLUDED_COMPHELPER_TRACEEVENT_HXX
#define INCLUDED_COMPHELPER_TRACEEVENT_HXX

#include <sal/config.h>

#include <algorithm>
#include <atomic>
#include <map>
#include <memory>
#include <vector>

#include <osl/process.h>
#include <osl/thread.h>
#include <osl/time.h>
#include <com/sun/star/uno/Sequence.h>
#include <comphelper/comphelperdllapi.h>
#include <rtl/ustrbuf.hxx>
#include <rtl/ustring.hxx>

// implementation of XToolkitExperimental profiling API

namespace comphelper
{
class COMPHELPER_DLLPUBLIC TraceEvent
{
private:
    static int getPid()
    {
        oslProcessInfo aProcessInfo;
        aProcessInfo.Size = sizeof(oslProcessInfo);
        if (osl_getProcessInfo(nullptr, osl_Process_IDENTIFIER, &aProcessInfo)
            == osl_Process_E_None)
            return aProcessInfo.Ident;
        return -1;
    }

protected:
    static std::atomic<bool> s_bRecording; // true during recording

    static void addRecording(const OUString& sObject);

    static long long getNow()
    {
        TimeValue systemTime;
        osl_getSystemTime(&systemTime);
        return static_cast<long long>(systemTime.Seconds) * 1000000 + systemTime.Nanosec / 1000;
    }

    static OUString createArgsString(const std::map<OUString, OUString>& args)
    {
        if (args.size() == 0)
            return "";

        OUStringBuffer sResult;
        sResult.append(",\"args\":{");
        bool first = true;
        for (auto i : args)
        {
            if (!first)
                sResult.append(',');
            sResult.append('"');
            sResult.append(i.first);
            sResult.append("\":\"");
            sResult.append(i.second);
            sResult.append('"');
            first = false;
        }
        sResult.append('}');

        return sResult.makeStringAndClear();
    }

    const int m_nPid;
    const OUString m_sArgs;

    TraceEvent(const OUString& sArgs)
        : m_nPid(getPid())
        , m_sArgs(sArgs)
    {
    }

    TraceEvent(std::map<OUString, OUString> args)
        : TraceEvent(createArgsString(args))
    {
    }

public:
    static void addInstantEvent(const char* sName, const std::map<OUString, OUString>& args
                                                   = std::map<OUString, OUString>());

    static void startRecording();
    static void stopRecording();

    static std::vector<OUString> getEventVectorAndClear();

    static css::uno::Sequence<OUString> getRecordingAndClear();
};

class COMPHELPER_DLLPUBLIC NamedEvent : public TraceEvent
{
protected:
    const char* m_sName;

    NamedEvent(const char* sName, const OUString& sArgs)
        : TraceEvent(sArgs)
        , m_sName(sName ? sName : "(null)")
    {
    }

    NamedEvent(const char* sName, const std::map<OUString, OUString>& aArgs)
        : TraceEvent(aArgs)
        , m_sName(sName ? sName : "(null)")
    {
    }
};

// An AsyncEvent generates a 'b' (begin) event when constructed and an 'e' (end) event when it
// itself or its nesting parent (if there is one) is destructed (or earlier, if requested)

// There are two kinds of async event pairs: Freestanding ones that are not related to other events
// at all, and nested ones that have to be nested between the 'b' and 'e' events of a parent Async
// event.

// To generate a pair of 'b' and 'e' events, create an AsyncEvent object using the AsyncEvent(const
// char* sName) constructor when you want the 'b' event to be generated, and destroy it when you
// want the corresponding 'e' event to be generated.

// To generate a pair of 'b' and 'e' events that is nested inside an outer 'b' and 'e' event pair,
// create an AsyncEvent object using the createWithParent() function. It returns a weak reference
// (weak_ptr) to the AsyncEvent. The parent keeps a strong reference (shared_ptr) to it.

// The 'e' event will be generated when the parent is about to go away, before the parent's 'e'
// event. When the parent has gone away, the weak reference will have expired. You can also generate
// it explicitly by calling the finish() function. (But in that case you could as well have used a
// freestanding AsyncEvent object, I think.)

class COMPHELPER_DLLPUBLIC AsyncEvent : public NamedEvent,
                                        public std::enable_shared_from_this<AsyncEvent>
{
    static int s_nIdCounter;
    int m_nId;
    std::vector<std::shared_ptr<AsyncEvent>> m_aChildren;
    std::weak_ptr<AsyncEvent> m_pParent;
    bool m_bBeginRecorded;

    AsyncEvent(const char* sName, int nId, const std::map<OUString, OUString>& args)
        : NamedEvent(sName, args)
        , m_nId(nId)
        , m_bBeginRecorded(false)
    {
        if (s_bRecording)
        {
            long long nNow = getNow();

            // Generate a "Begin " (type b) event
            TraceEvent::addRecording("{"
                                     "\"name\":\""
                                     + OUString(m_sName, strlen(m_sName), RTL_TEXTENCODING_UTF8)
                                     + "\","
                                       "\"ph\":\"b\""
                                       ","
                                       "\"id\":"
                                     + OUString::number(m_nId) + m_sArgs
                                     + ","
                                       "\"ts\":"
                                     + OUString::number(nNow)
                                     + ","
                                       "\"pid\":"
                                     + OUString::number(m_nPid)
                                     + ","
                                       "\"tid\":"
                                     + OUString::number(osl_getThreadIdentifier(nullptr)) + "},");
            m_bBeginRecorded = true;
        }
    }

    void generateEnd()
    {
        if (m_bBeginRecorded)
        {
            m_bBeginRecorded = false;

            // In case somebody is holding on to a hard reference to a child we need to tell the
            // children to finish up explicitly, we can't rely on our pointers to them being the
            // only ones.
            for (auto& i : m_aChildren)
                i->generateEnd();

            m_aChildren.clear();

            long long nNow = getNow();
            // Generate a "Env " (type e) event
            TraceEvent::addRecording("{"
                                     "\"name\":\""
                                     + OUString(m_sName, strlen(m_sName), RTL_TEXTENCODING_UTF8)
                                     + "\","
                                       "\"ph\":\"e\""
                                       ","
                                       "\"id\":"
                                     + OUString::number(m_nId) + m_sArgs
                                     + ","
                                       "\"ts\":"
                                     + OUString::number(nNow)
                                     + ","
                                       "\"pid\":"
                                     + OUString::number(m_nPid)
                                     + ","
                                       "\"tid\":"
                                     + OUString::number(osl_getThreadIdentifier(nullptr)) + "},");
        }
    }

public:
    AsyncEvent(const char* sName,
               const std::map<OUString, OUString>& args = std::map<OUString, OUString>())
        : AsyncEvent(sName, s_nIdCounter++, args)
    {
    }

    ~AsyncEvent() { generateEnd(); }

    static std::weak_ptr<AsyncEvent>
    createWithParent(const char* sName, std::shared_ptr<AsyncEvent> pParent,
                     const std::map<OUString, OUString>& args = std::map<OUString, OUString>())
    {
        std::shared_ptr<AsyncEvent> pResult;

        if (s_bRecording && pParent->m_bBeginRecorded)
        {
            pResult.reset(new AsyncEvent(sName, pParent->m_nId, args));
            pParent->m_aChildren.push_back(pResult);
            pResult->m_pParent = pParent;
        }

        return pResult;
    }

    void finish()
    {
        generateEnd();

        auto pParent = m_pParent.lock();
        if (!pParent)
            return;

        pParent->m_aChildren.erase(std::remove(pParent->m_aChildren.begin(),
                                               pParent->m_aChildren.end(), shared_from_this()),
                                   pParent->m_aChildren.end());
        m_pParent.reset();
    }
};

} // namespace comphelper

#endif // INCLUDED_COMPHELPER_TRACEEVENT_HXX

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