1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_automation.hxx"
26 
27 
28 #include <tools/time.hxx>
29 #include <tools/string.hxx>
30 #include <unotools/localedatawrapper.hxx>
31 #include <vcl/svapp.hxx>
32 #ifndef _BASIC_TTRESHLP_HXX
33 #include <basic/ttstrhlp.hxx>
34 #endif
35 
36 
37 #include "profiler.hxx"
38 
39 
TTProfiler()40 TTProfiler::TTProfiler()
41 : mpStart( NULL )
42 , mpEnd( NULL )
43 , bIsProfileIntervalStarted( sal_False )
44 , bIsProfilingPerCommand( sal_False )
45 , bIsPartitioning( sal_False )
46 , bIsAutoProfiling( sal_False )
47 , pSysDepStatic( NULL )
48 {
49 	InitSysdepProfiler();
50 	mpStart = new ProfileSnapshot;
51 	mpStart->pSysdepProfileSnapshot = NewSysdepSnapshotData();
52 	mpEnd = new ProfileSnapshot;
53 	mpEnd->pSysdepProfileSnapshot = NewSysdepSnapshotData();
54 	StartProfileInterval();
55 }
56 
~TTProfiler()57 TTProfiler::~TTProfiler()
58 {
59 	if ( IsAutoProfiling() )
60 		StopAutoProfiling();
61 	if ( mpStart )
62 	{
63 		if ( mpStart->pSysdepProfileSnapshot )
64 			DeleteSysdepSnapshotData( mpStart->pSysdepProfileSnapshot );
65 		delete mpStart;
66 		mpStart = NULL;
67 	}
68 	if ( mpEnd )
69 	{
70 		if ( mpEnd->pSysdepProfileSnapshot )
71 			DeleteSysdepSnapshotData( mpEnd->pSysdepProfileSnapshot );
72 		delete mpEnd;
73 		mpEnd = NULL;
74 	}
75 	DeinitSysdepProfiler();
76 }
77 
78 
GetProfileHeader()79 String TTProfiler::GetProfileHeader()
80 {
81 	UniString aReturn;
82 	aReturn += '\n';
83 	if ( !IsAutoProfiling() )
84 		aReturn.AppendAscii("Befehl").Append(TabString(36));
85 
86 	aReturn.AppendAscii("   Zeitdauer");
87 	aReturn.AppendAscii("  Ticks in %");
88 	aReturn.Append( GetSysdepProfileHeader() );
89 	aReturn.AppendAscii("\n");
90 	return aReturn;
91 }
92 
93 
StartProfileInterval(sal_Bool bReadAnyway)94 void TTProfiler::StartProfileInterval( sal_Bool bReadAnyway )
95 {
96 	if ( !bIsProfileIntervalStarted || bReadAnyway )
97 	{
98 		GetProfileSnapshot( mpStart );
99 		GetSysdepProfileSnapshot( mpStart->pSysdepProfileSnapshot, PROFILE_START );
100 		bIsProfileIntervalStarted = sal_True;
101 	}
102 }
103 
GetProfileLine(ProfileSnapshot * pStart,ProfileSnapshot * pEnd)104 String TTProfiler::GetProfileLine( ProfileSnapshot *pStart, ProfileSnapshot *pEnd )
105 {
106 	String aProfileString;
107 
108 	aProfileString += Pad(GetpApp()->GetAppLocaleDataWrapper().getDuration( DIFF( pStart, pEnd, aTime) , sal_True, sal_True ), 12);
109 
110 	sal_uLong nProcessTicks = DIFF( pStart, pEnd, nProcessTicks );
111 	sal_uLong nSystemTicks = DIFF( pStart, pEnd, nSystemTicks );
112 	if ( nSystemTicks )
113 	{
114 		aProfileString += Pad(UniString::CreateFromInt32( (100 * nProcessTicks) / nSystemTicks ), 11);
115 		aProfileString += '%';
116 	}
117 	else
118 		aProfileString += Pad(CUniString("??  "), 12);
119 
120 	return aProfileString;
121 }
122 
123 
GetProfileLine(String & aPrefix)124 String TTProfiler::GetProfileLine( String &aPrefix )
125 {
126 	String aProfileString;
127 	if ( IsProfilingPerCommand() || IsAutoProfiling() )
128 	{
129 		aProfileString = aPrefix;
130 		aProfileString += TabString(35);
131 
132 
133 		aProfileString += GetProfileLine( mpStart, mpEnd );
134 		aProfileString += GetSysdepProfileLine( mpStart->pSysdepProfileSnapshot, mpEnd->pSysdepProfileSnapshot );
135 		aProfileString += '\n';
136 	}
137 
138 	return aProfileString;
139 }
140 
141 
EndProfileInterval()142 void TTProfiler::EndProfileInterval()
143 {
144 	GetProfileSnapshot( mpEnd );
145 	GetSysdepProfileSnapshot( mpEnd->pSysdepProfileSnapshot, PROFILE_END );
146 	bIsProfileIntervalStarted = sal_False;
147 }
148 
149 
GetProfileSnapshot(ProfileSnapshot * pProfileSnapshot)150 void TTProfiler::GetProfileSnapshot( ProfileSnapshot *pProfileSnapshot )
151 {
152 	pProfileSnapshot->aTime = Time();
153 	pProfileSnapshot->nProcessTicks = Time::GetProcessTicks();
154 	pProfileSnapshot->nSystemTicks = Time::GetSystemTicks();
155 }
156 
157 
StartProfilingPerCommand()158 void TTProfiler::StartProfilingPerCommand()		// Jeden Befehl mitschneiden
159 {
160 	bIsProfilingPerCommand = sal_True;
161 }
162 
StopProfilingPerCommand()163 void TTProfiler::StopProfilingPerCommand()
164 {
165 	bIsProfilingPerCommand = sal_False;
166 }
167 
StartPartitioning()168 void TTProfiler::StartPartitioning()
169 {
170 	bIsPartitioning = sal_True;
171 }
172 
StopPartitioning()173 void TTProfiler::StopPartitioning()
174 {
175 	bIsPartitioning = sal_True;
176 }
177 
GetPartitioningTime()178 sal_uLong TTProfiler::GetPartitioningTime()
179 {
180 	return DIFF( mpStart, mpEnd, nSystemTicks );
181 }
182 
183 
184 
StartAutoProfiling(sal_uLong nMSec)185 void TTProfiler::StartAutoProfiling( sal_uLong nMSec )
186 {
187 	if ( !bIsAutoProfiling )
188 	{
189 		pAutoStart = new ProfileSnapshot;
190 		pAutoStart->pSysdepProfileSnapshot = NewSysdepSnapshotData();
191 		pAutoEnd = new ProfileSnapshot;
192 		pAutoEnd->pSysdepProfileSnapshot = NewSysdepSnapshotData();
193 		GetProfileSnapshot( pAutoStart );
194 		GetSysdepProfileSnapshot( pAutoStart->pSysdepProfileSnapshot, PROFILE_START );
195 		SetTimeout( nMSec );
196 		bIsAutoProfiling = sal_True;
197 		Start();
198 	}
199 
200 }
201 
Timeout()202 void TTProfiler::Timeout()
203 {
204 	GetProfileSnapshot( pAutoEnd );
205 	GetSysdepProfileSnapshot( pAutoEnd->pSysdepProfileSnapshot, PROFILE_END );
206 	String aLine;
207 
208 	aLine += GetProfileLine( pAutoStart, pAutoEnd );
209 	aLine += GetSysdepProfileLine( pAutoStart->pSysdepProfileSnapshot, pAutoEnd->pSysdepProfileSnapshot );
210 	aLine += '\n';
211 
212 	aAutoProfileBuffer += aLine;
213 
214 	ProfileSnapshot *pTemp = pAutoStart;		// Tauschen, so da� jetziges Ende n�chsten Start wird
215 	pAutoStart = pAutoEnd;
216 	pAutoEnd = pTemp;
217 
218 	Start();	// Timer neu starten
219 }
220 
GetAutoProfiling()221 String TTProfiler::GetAutoProfiling()
222 {
223 	String aTemp(aAutoProfileBuffer);
224 	aAutoProfileBuffer.Erase();
225 	return aTemp;
226 }
227 
StopAutoProfiling()228 void TTProfiler::StopAutoProfiling()
229 {
230 	if ( bIsAutoProfiling )
231 	{
232 		Stop();
233 		bIsAutoProfiling = sal_False;
234 	}
235 }
236 
237 
238 
239 //String TTProfiler::Hex( sal_uLong nNr )
Dec(sal_uLong nNr)240 String TTProfiler::Dec( sal_uLong nNr )
241 {
242 	String aRet(UniString::CreateFromInt32(nNr));
243 	if ( nNr < 100 )
244 	{
245 		aRet = Pad( aRet, 3);
246 		aRet.SearchAndReplaceAll(' ','0');
247 	}
248 	aRet.Insert( ',', aRet.Len() - 2 );
249 	return aRet;
250 }
251 
Pad(const String aS,xub_StrLen nLen)252 String TTProfiler::Pad( const String aS, xub_StrLen nLen )
253 {
254 	if ( nLen > aS.Len() )
255 		return UniString().Fill( nLen - aS.Len() ).Append( aS );
256 	else
257 		return CUniString(" ").Append( aS );
258 }
259 
260 
261