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