1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_automation.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #include <tools/time.hxx>
33*cdf0e10cSrcweir #include <tools/string.hxx>
34*cdf0e10cSrcweir #include <unotools/localedatawrapper.hxx>
35*cdf0e10cSrcweir #include <vcl/svapp.hxx>
36*cdf0e10cSrcweir #ifndef _BASIC_TTRESHLP_HXX
37*cdf0e10cSrcweir #include <basic/ttstrhlp.hxx>
38*cdf0e10cSrcweir #endif
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir #include "profiler.hxx"
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir TTProfiler::TTProfiler()
45*cdf0e10cSrcweir : mpStart( NULL )
46*cdf0e10cSrcweir , mpEnd( NULL )
47*cdf0e10cSrcweir , bIsProfileIntervalStarted( sal_False )
48*cdf0e10cSrcweir , bIsProfilingPerCommand( sal_False )
49*cdf0e10cSrcweir , bIsPartitioning( sal_False )
50*cdf0e10cSrcweir , bIsAutoProfiling( sal_False )
51*cdf0e10cSrcweir , pSysDepStatic( NULL )
52*cdf0e10cSrcweir {
53*cdf0e10cSrcweir 	InitSysdepProfiler();
54*cdf0e10cSrcweir 	mpStart = new ProfileSnapshot;
55*cdf0e10cSrcweir 	mpStart->pSysdepProfileSnapshot = NewSysdepSnapshotData();
56*cdf0e10cSrcweir 	mpEnd = new ProfileSnapshot;
57*cdf0e10cSrcweir 	mpEnd->pSysdepProfileSnapshot = NewSysdepSnapshotData();
58*cdf0e10cSrcweir 	StartProfileInterval();
59*cdf0e10cSrcweir }
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir TTProfiler::~TTProfiler()
62*cdf0e10cSrcweir {
63*cdf0e10cSrcweir 	if ( IsAutoProfiling() )
64*cdf0e10cSrcweir 		StopAutoProfiling();
65*cdf0e10cSrcweir 	if ( mpStart )
66*cdf0e10cSrcweir 	{
67*cdf0e10cSrcweir 		if ( mpStart->pSysdepProfileSnapshot )
68*cdf0e10cSrcweir 			DeleteSysdepSnapshotData( mpStart->pSysdepProfileSnapshot );
69*cdf0e10cSrcweir 		delete mpStart;
70*cdf0e10cSrcweir 		mpStart = NULL;
71*cdf0e10cSrcweir 	}
72*cdf0e10cSrcweir 	if ( mpEnd )
73*cdf0e10cSrcweir 	{
74*cdf0e10cSrcweir 		if ( mpEnd->pSysdepProfileSnapshot )
75*cdf0e10cSrcweir 			DeleteSysdepSnapshotData( mpEnd->pSysdepProfileSnapshot );
76*cdf0e10cSrcweir 		delete mpEnd;
77*cdf0e10cSrcweir 		mpEnd = NULL;
78*cdf0e10cSrcweir 	}
79*cdf0e10cSrcweir 	DeinitSysdepProfiler();
80*cdf0e10cSrcweir }
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir String TTProfiler::GetProfileHeader()
84*cdf0e10cSrcweir {
85*cdf0e10cSrcweir 	UniString aReturn;
86*cdf0e10cSrcweir 	aReturn += '\n';
87*cdf0e10cSrcweir 	if ( !IsAutoProfiling() )
88*cdf0e10cSrcweir 		aReturn.AppendAscii("Befehl").Append(TabString(36));
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir 	aReturn.AppendAscii("   Zeitdauer");
91*cdf0e10cSrcweir 	aReturn.AppendAscii("  Ticks in %");
92*cdf0e10cSrcweir 	aReturn.Append( GetSysdepProfileHeader() );
93*cdf0e10cSrcweir 	aReturn.AppendAscii("\n");
94*cdf0e10cSrcweir 	return aReturn;
95*cdf0e10cSrcweir }
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir void TTProfiler::StartProfileInterval( sal_Bool bReadAnyway )
99*cdf0e10cSrcweir {
100*cdf0e10cSrcweir 	if ( !bIsProfileIntervalStarted || bReadAnyway )
101*cdf0e10cSrcweir 	{
102*cdf0e10cSrcweir 		GetProfileSnapshot( mpStart );
103*cdf0e10cSrcweir 		GetSysdepProfileSnapshot( mpStart->pSysdepProfileSnapshot, PROFILE_START );
104*cdf0e10cSrcweir 		bIsProfileIntervalStarted = sal_True;
105*cdf0e10cSrcweir 	}
106*cdf0e10cSrcweir }
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir String TTProfiler::GetProfileLine( ProfileSnapshot *pStart, ProfileSnapshot *pEnd )
109*cdf0e10cSrcweir {
110*cdf0e10cSrcweir 	String aProfileString;
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir 	aProfileString += Pad(GetpApp()->GetAppLocaleDataWrapper().getDuration( DIFF( pStart, pEnd, aTime) , sal_True, sal_True ), 12);
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir 	sal_uLong nProcessTicks = DIFF( pStart, pEnd, nProcessTicks );
115*cdf0e10cSrcweir 	sal_uLong nSystemTicks = DIFF( pStart, pEnd, nSystemTicks );
116*cdf0e10cSrcweir 	if ( nSystemTicks )
117*cdf0e10cSrcweir 	{
118*cdf0e10cSrcweir 		aProfileString += Pad(UniString::CreateFromInt32( (100 * nProcessTicks) / nSystemTicks ), 11);
119*cdf0e10cSrcweir 		aProfileString += '%';
120*cdf0e10cSrcweir 	}
121*cdf0e10cSrcweir 	else
122*cdf0e10cSrcweir 		aProfileString += Pad(CUniString("??  "), 12);
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir 	return aProfileString;
125*cdf0e10cSrcweir }
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir String TTProfiler::GetProfileLine( String &aPrefix )
129*cdf0e10cSrcweir {
130*cdf0e10cSrcweir 	String aProfileString;
131*cdf0e10cSrcweir 	if ( IsProfilingPerCommand() || IsAutoProfiling() )
132*cdf0e10cSrcweir 	{
133*cdf0e10cSrcweir 		aProfileString = aPrefix;
134*cdf0e10cSrcweir 		aProfileString += TabString(35);
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir 		aProfileString += GetProfileLine( mpStart, mpEnd );
138*cdf0e10cSrcweir 		aProfileString += GetSysdepProfileLine( mpStart->pSysdepProfileSnapshot, mpEnd->pSysdepProfileSnapshot );
139*cdf0e10cSrcweir 		aProfileString += '\n';
140*cdf0e10cSrcweir 	}
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir 	return aProfileString;
143*cdf0e10cSrcweir }
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir void TTProfiler::EndProfileInterval()
147*cdf0e10cSrcweir {
148*cdf0e10cSrcweir 	GetProfileSnapshot( mpEnd );
149*cdf0e10cSrcweir 	GetSysdepProfileSnapshot( mpEnd->pSysdepProfileSnapshot, PROFILE_END );
150*cdf0e10cSrcweir 	bIsProfileIntervalStarted = sal_False;
151*cdf0e10cSrcweir }
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir void TTProfiler::GetProfileSnapshot( ProfileSnapshot *pProfileSnapshot )
155*cdf0e10cSrcweir {
156*cdf0e10cSrcweir 	pProfileSnapshot->aTime = Time();
157*cdf0e10cSrcweir 	pProfileSnapshot->nProcessTicks = Time::GetProcessTicks();
158*cdf0e10cSrcweir 	pProfileSnapshot->nSystemTicks = Time::GetSystemTicks();
159*cdf0e10cSrcweir }
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir void TTProfiler::StartProfilingPerCommand()		// Jeden Befehl mitschneiden
163*cdf0e10cSrcweir {
164*cdf0e10cSrcweir 	bIsProfilingPerCommand = sal_True;
165*cdf0e10cSrcweir }
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir void TTProfiler::StopProfilingPerCommand()
168*cdf0e10cSrcweir {
169*cdf0e10cSrcweir 	bIsProfilingPerCommand = sal_False;
170*cdf0e10cSrcweir }
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir void TTProfiler::StartPartitioning()
173*cdf0e10cSrcweir {
174*cdf0e10cSrcweir 	bIsPartitioning = sal_True;
175*cdf0e10cSrcweir }
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir void TTProfiler::StopPartitioning()
178*cdf0e10cSrcweir {
179*cdf0e10cSrcweir 	bIsPartitioning = sal_True;
180*cdf0e10cSrcweir }
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir sal_uLong TTProfiler::GetPartitioningTime()
183*cdf0e10cSrcweir {
184*cdf0e10cSrcweir 	return DIFF( mpStart, mpEnd, nSystemTicks );
185*cdf0e10cSrcweir }
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir void TTProfiler::StartAutoProfiling( sal_uLong nMSec )
190*cdf0e10cSrcweir {
191*cdf0e10cSrcweir 	if ( !bIsAutoProfiling )
192*cdf0e10cSrcweir 	{
193*cdf0e10cSrcweir 		pAutoStart = new ProfileSnapshot;
194*cdf0e10cSrcweir 		pAutoStart->pSysdepProfileSnapshot = NewSysdepSnapshotData();
195*cdf0e10cSrcweir 		pAutoEnd = new ProfileSnapshot;
196*cdf0e10cSrcweir 		pAutoEnd->pSysdepProfileSnapshot = NewSysdepSnapshotData();
197*cdf0e10cSrcweir 		GetProfileSnapshot( pAutoStart );
198*cdf0e10cSrcweir 		GetSysdepProfileSnapshot( pAutoStart->pSysdepProfileSnapshot, PROFILE_START );
199*cdf0e10cSrcweir 		SetTimeout( nMSec );
200*cdf0e10cSrcweir 		bIsAutoProfiling = sal_True;
201*cdf0e10cSrcweir 		Start();
202*cdf0e10cSrcweir 	}
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir }
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir void TTProfiler::Timeout()
207*cdf0e10cSrcweir {
208*cdf0e10cSrcweir 	GetProfileSnapshot( pAutoEnd );
209*cdf0e10cSrcweir 	GetSysdepProfileSnapshot( pAutoEnd->pSysdepProfileSnapshot, PROFILE_END );
210*cdf0e10cSrcweir 	String aLine;
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir 	aLine += GetProfileLine( pAutoStart, pAutoEnd );
213*cdf0e10cSrcweir 	aLine += GetSysdepProfileLine( pAutoStart->pSysdepProfileSnapshot, pAutoEnd->pSysdepProfileSnapshot );
214*cdf0e10cSrcweir 	aLine += '\n';
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir 	aAutoProfileBuffer += aLine;
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir 	ProfileSnapshot *pTemp = pAutoStart;		// Tauschen, so da� jetziges Ende n�chsten Start wird
219*cdf0e10cSrcweir 	pAutoStart = pAutoEnd;
220*cdf0e10cSrcweir 	pAutoEnd = pTemp;
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir 	Start();	// Timer neu starten
223*cdf0e10cSrcweir }
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir String TTProfiler::GetAutoProfiling()
226*cdf0e10cSrcweir {
227*cdf0e10cSrcweir 	String aTemp(aAutoProfileBuffer);
228*cdf0e10cSrcweir 	aAutoProfileBuffer.Erase();
229*cdf0e10cSrcweir 	return aTemp;
230*cdf0e10cSrcweir }
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir void TTProfiler::StopAutoProfiling()
233*cdf0e10cSrcweir {
234*cdf0e10cSrcweir 	if ( bIsAutoProfiling )
235*cdf0e10cSrcweir 	{
236*cdf0e10cSrcweir 		Stop();
237*cdf0e10cSrcweir 		bIsAutoProfiling = sal_False;
238*cdf0e10cSrcweir 	}
239*cdf0e10cSrcweir }
240*cdf0e10cSrcweir 
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir 
243*cdf0e10cSrcweir //String TTProfiler::Hex( sal_uLong nNr )
244*cdf0e10cSrcweir String TTProfiler::Dec( sal_uLong nNr )
245*cdf0e10cSrcweir {
246*cdf0e10cSrcweir 	String aRet(UniString::CreateFromInt32(nNr));
247*cdf0e10cSrcweir 	if ( nNr < 100 )
248*cdf0e10cSrcweir 	{
249*cdf0e10cSrcweir 		aRet = Pad( aRet, 3);
250*cdf0e10cSrcweir 		aRet.SearchAndReplaceAll(' ','0');
251*cdf0e10cSrcweir 	}
252*cdf0e10cSrcweir 	aRet.Insert( ',', aRet.Len() - 2 );
253*cdf0e10cSrcweir 	return aRet;
254*cdf0e10cSrcweir }
255*cdf0e10cSrcweir 
256*cdf0e10cSrcweir String TTProfiler::Pad( const String aS, xub_StrLen nLen )
257*cdf0e10cSrcweir {
258*cdf0e10cSrcweir 	if ( nLen > aS.Len() )
259*cdf0e10cSrcweir 		return UniString().Fill( nLen - aS.Len() ).Append( aS );
260*cdf0e10cSrcweir 	else
261*cdf0e10cSrcweir 		return CUniString(" ").Append( aS );
262*cdf0e10cSrcweir }
263*cdf0e10cSrcweir 
264*cdf0e10cSrcweir 
265