summaryrefslogtreecommitdiff
path: root/comphelper/source/misc/profilezone.cxx
blob: 7b59aa7cc8f71f5181c5149477a9071e51b47a7f (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
/* -*- 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 <comphelper/sequence.hxx>
#include <comphelper/profilezone.hxx>
#include <osl/time.h>
#include <osl/thread.h>

namespace comphelper
{

std::atomic<bool> ProfileZone::s_bRecording = false;

int ProfileZone::s_nNesting = 0; // level of overlapped zones

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

void ProfileZone::startRecording()
{
    ::osl::MutexGuard aGuard(g_aMutex);
    s_nNesting = 0;
    s_bRecording = true;
}

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

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

    TimeValue aSystemTime;
    osl_getSystemTime(&aSystemTime);
    long long nNow = static_cast<long long>(aSystemTime.Seconds) * 1000000 + aSystemTime.Nanosec/1000;

     // Generate a single "Complete Event" (type X)
    OUString sRecordingData("{"
                            "\"name\":\"" + OUString(m_sProfileId, strlen(m_sProfileId), RTL_TEXTENCODING_UTF8) + "\","
                            "\"ph\":\"X\","
                            "\"ts\":" + OUString::number(m_nCreateTime) + ","
                            "\"dur\":" + OUString::number(nNow - m_nCreateTime) + ","
                            "\"pid\":" + OUString::number(m_nPid) + ","
                            "\"tid\":" + OUString::number(osl_getThreadIdentifier(nullptr)) +
                            "},");
    ::osl::MutexGuard aGuard(g_aMutex);

    g_aRecording.emplace_back(sRecordingData);
}

css::uno::Sequence<OUString> ProfileZone::getRecordingAndClear()
{
    bool bRecording;
    std::vector<OUString> aRecording;
    {
        ::osl::MutexGuard aGuard( g_aMutex );
        bRecording = s_bRecording;
        stopRecording();
        aRecording.swap(g_aRecording);
    }
    // reset start time and nesting level
    if (bRecording)
        startRecording();
    return ::comphelper::containerToSequence(aRecording);
}

} // namespace comphelper

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