LibreOffice Module comphelper (master)  1
profilezone.cxx
Go to the documentation of this file.
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 <iostream>
14 #include <string_view>
15 
16 #include <comphelper/sequence.hxx>
18 #include <osl/time.h>
19 #include <osl/thread.h>
20 
21 namespace comphelper
22 {
23 
24 std::atomic<bool> ProfileZone::g_bRecording(false);
25 
26 namespace ProfileRecording
27 {
28 
29 static std::vector<OUString> g_aRecording; // recorded data
30 static long long g_aSumTime(0); // overall zone time in microsec
31 static int g_aNesting; // level of overlapped zones
32 static long long g_aStartTime; // start time of recording
33 static ::osl::Mutex g_aMutex;
34 
35 void startRecording(bool bStartRecording)
36 {
37  if (bStartRecording)
38  {
39  TimeValue systemTime;
40  osl_getSystemTime( &systemTime );
41  ::osl::MutexGuard aGuard( g_aMutex );
42  g_aStartTime = static_cast<long long>(systemTime.Seconds) * 1000000 + systemTime.Nanosec/1000;
43  g_aNesting = 0;
44  }
45  ProfileZone::g_bRecording = bStartRecording;
46 }
47 
48 long long addRecording(const char * aProfileId, long long aCreateTime)
49 {
50  assert( ProfileZone::g_bRecording );
51 
52  TimeValue systemTime;
53  osl_getSystemTime( &systemTime );
54  long long aTime = static_cast<long long>(systemTime.Seconds) * 1000000 + systemTime.Nanosec/1000;
55 
56  if (!aProfileId)
57  aProfileId = "(null)";
58  OUString aString(aProfileId, strlen(aProfileId), RTL_TEXTENCODING_UTF8);
59 
60  OUString sRecordingData(OUString::number(osl_getThreadIdentifier(nullptr)) + " " +
61  OUString::number(aTime/1000000.0) + " " + aString + ": " +
62  (aCreateTime == 0 ? std::u16string_view(u"start") : std::u16string_view(u"stop")) +
63  (aCreateTime != 0 ? (" " + OUString::number((aTime - aCreateTime)/1000.0) + " ms") : OUString("")));
64 
65  ::osl::MutexGuard aGuard( g_aMutex );
66 
67  g_aRecording.emplace_back(sRecordingData);
68  if (aCreateTime == 0)
69  {
70  g_aNesting++;
71  return aTime;
72  }
73  // neglect ProfileZones created before startRecording
74  else if (aCreateTime >= g_aStartTime)
75  {
76  if (g_aNesting > 0)
77  g_aNesting--;
78  if (g_aNesting == 0)
79  g_aSumTime += aTime - aCreateTime;
80  }
81  return 0;
82 }
83 
84 css::uno::Sequence<OUString> getRecordingAndClear()
85 {
86  bool bRecording;
87  std::vector<OUString> aRecording;
88  {
89  ::osl::MutexGuard aGuard( g_aMutex );
90  bRecording = ProfileZone::g_bRecording;
91  startRecording(false);
92  aRecording.swap(g_aRecording);
93  long long aSumTime = g_aSumTime;
94  aRecording.insert(aRecording.begin(), OUString::number(aSumTime/1000000.0));
95  }
96  // reset start time and nesting level
97  startRecording(bRecording);
99 }
100 
101 } // namespace ProfileRecording
102 
103 void ProfileZone::startConsole() { m_aCreateTime = osl_getGlobalTimer(); }
104 
105 void ProfileZone::stopConsole()
106 {
107  sal_uInt32 nEndTime = osl_getGlobalTimer();
108  std::cerr << "comphelper::ProfileZone: " << m_sProfileId << " finished in "
109  << nEndTime - m_aCreateTime << " ms" << std::endl;
110 }
111 
112 } // namespace comphelper
113 
114 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static long long g_aSumTime(0)
css::uno::Sequence< OUString > getRecordingAndClear()
Definition: profilezone.cxx:84
static long long g_aStartTime
Definition: profilezone.cxx:32
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
float u
static std::vector< OUString > g_aRecording
Definition: profilezone.cxx:29
long long addRecording(const char *aProfileId, long long aCreateTime)
Definition: profilezone.cxx:48
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
Copy from a container into a Sequence.
Definition: sequence.hxx:182
void startRecording(bool bStartRecording)
Definition: profilezone.cxx:35
if(!pCandidateA->getEnd().equal(pCandidateB->getStart()))