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