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 <procfs.h>
29cdf0e10cSrcweir #include <tools/stream.hxx>
30cdf0e10cSrcweir #include "profiler.hxx"
31cdf0e10cSrcweir 
32cdf0e10cSrcweir 
33cdf0e10cSrcweir struct SysdepProfileSnapshot
34cdf0e10cSrcweir {
35cdf0e10cSrcweir 	pstatus mpstatus;
36cdf0e10cSrcweir 	psinfo mpsinfo;
37cdf0e10cSrcweir 	prusage mprusage;
38cdf0e10cSrcweir };
39cdf0e10cSrcweir 
40cdf0e10cSrcweir 
41cdf0e10cSrcweir struct SysdepStaticData
42cdf0e10cSrcweir {
43cdf0e10cSrcweir 	// Hier steht alles, was w�hrend des Profiles st�ndig gebraucht wird
44cdf0e10cSrcweir };
45cdf0e10cSrcweir 
46cdf0e10cSrcweir 
InitSysdepProfiler()47cdf0e10cSrcweir void TTProfiler::InitSysdepProfiler()
48cdf0e10cSrcweir {
49cdf0e10cSrcweir 	if ( !pSysDepStatic )	// Sollte immer so sein!!
50cdf0e10cSrcweir 		pSysDepStatic = new SysdepStaticData;
51cdf0e10cSrcweir 	// Hier initialisieren
52cdf0e10cSrcweir 
53cdf0e10cSrcweir };
54cdf0e10cSrcweir 
DeinitSysdepProfiler()55cdf0e10cSrcweir void TTProfiler::DeinitSysdepProfiler()
56cdf0e10cSrcweir {
57cdf0e10cSrcweir 	if ( pSysDepStatic )	// Sollte immer so sein!!
58cdf0e10cSrcweir 	{
59cdf0e10cSrcweir 		// Hier aufr�umen und eventuell Speicher freigeben
60cdf0e10cSrcweir 
61cdf0e10cSrcweir 		delete pSysDepStatic;
62cdf0e10cSrcweir 	}
63cdf0e10cSrcweir };
64cdf0e10cSrcweir 
NewSysdepSnapshotData()65cdf0e10cSrcweir SysdepProfileSnapshot *TTProfiler::NewSysdepSnapshotData()
66cdf0e10cSrcweir {
67cdf0e10cSrcweir 	return new SysdepProfileSnapshot;
68cdf0e10cSrcweir };
69cdf0e10cSrcweir 
DeleteSysdepSnapshotData(SysdepProfileSnapshot * pSysdepProfileSnapshot)70cdf0e10cSrcweir void TTProfiler::DeleteSysdepSnapshotData( SysdepProfileSnapshot *pSysdepProfileSnapshot )
71cdf0e10cSrcweir {
72cdf0e10cSrcweir 	delete pSysdepProfileSnapshot;
73cdf0e10cSrcweir };
74cdf0e10cSrcweir 
75cdf0e10cSrcweir 
76cdf0e10cSrcweir // Titelzeile f�r Logdatei
GetSysdepProfileHeader()77cdf0e10cSrcweir String TTProfiler::GetSysdepProfileHeader()
78cdf0e10cSrcweir {
79cdf0e10cSrcweir 	return String::CreateFromAscii(" Size(Kb) ResidentSZ  rtime  ktime  utime  total");
80cdf0e10cSrcweir };
81cdf0e10cSrcweir 
82cdf0e10cSrcweir 
83cdf0e10cSrcweir // Zustand merken
GetSysdepProfileSnapshot(SysdepProfileSnapshot * pSysdepProfileSnapshot,sal_uInt16)84cdf0e10cSrcweir void TTProfiler::GetSysdepProfileSnapshot( SysdepProfileSnapshot *pSysdepProfileSnapshot, sal_uInt16 )
85cdf0e10cSrcweir {
86cdf0e10cSrcweir 	SvFileStream aStream( String::CreateFromAscii("/proc/self/psinfo"), STREAM_READ );		// Das ist ein expliziter Pfad f�r UNXSOL!
87cdf0e10cSrcweir 	if ( aStream.IsOpen() )
88cdf0e10cSrcweir 	{
89cdf0e10cSrcweir 		aStream.Read( &(pSysdepProfileSnapshot->mpsinfo), sizeof( psinfo ) );
90cdf0e10cSrcweir 		aStream.Close();
91cdf0e10cSrcweir 	}
92cdf0e10cSrcweir 	SvFileStream anotherStream( String::CreateFromAscii("/proc/self/status"), STREAM_READ );		// Das ist ein expliziter Pfad f�r UNXSOL!
93cdf0e10cSrcweir 	if ( anotherStream.IsOpen() )
94cdf0e10cSrcweir 	{
95cdf0e10cSrcweir 		anotherStream.Read( &(pSysdepProfileSnapshot->mpstatus), sizeof( pstatus ) );
96cdf0e10cSrcweir 		anotherStream.Close();
97cdf0e10cSrcweir 	}
98cdf0e10cSrcweir 	SvFileStream YetAnotherStream( String::CreateFromAscii("/proc/self/usage"), STREAM_READ );		// Das ist ein expliziter Pfad f�r UNXSOL!
99cdf0e10cSrcweir 	if ( YetAnotherStream.IsOpen() )
100cdf0e10cSrcweir 	{
101cdf0e10cSrcweir 		YetAnotherStream.Read( &(pSysdepProfileSnapshot->mprusage), sizeof( prusage ) );
102cdf0e10cSrcweir 		YetAnotherStream.Close();
103cdf0e10cSrcweir 	}
104cdf0e10cSrcweir };
105cdf0e10cSrcweir 
106cdf0e10cSrcweir #define DIFF2( aFirst, aSecond, Membername ) ( aSecond.Membername - aFirst.Membername )
107cdf0e10cSrcweir #define CALC_MS( nSec, nNSec ) ( nSec * 1000 + (nNSec+500000) / 1000000 )
108cdf0e10cSrcweir #define DIFF_MS( pStart, pEnd, Member ) ( CALC_MS( pEnd->Member.tv_sec, pEnd->Member.tv_nsec ) - CALC_MS( pStart->Member.tv_sec, pStart->Member.tv_nsec ) )
109cdf0e10cSrcweir // Informationszeile zusammenbauen
GetSysdepProfileLine(SysdepProfileSnapshot * pStart,SysdepProfileSnapshot * pStop)110cdf0e10cSrcweir String TTProfiler::GetSysdepProfileLine( SysdepProfileSnapshot *pStart, SysdepProfileSnapshot *pStop )
111cdf0e10cSrcweir {
112cdf0e10cSrcweir 	String aProfile;
113cdf0e10cSrcweir 
114cdf0e10cSrcweir 	aProfile += Pad( String::CreateFromInt64(pStop->mpsinfo.pr_size), 9);
115cdf0e10cSrcweir 	aProfile += Pad( String::CreateFromInt64(pStop->mpsinfo.pr_rssize), 11);
116cdf0e10cSrcweir 
117cdf0e10cSrcweir 
118cdf0e10cSrcweir 	aProfile += Pad( String::CreateFromInt64(DIFF_MS( pStart, pStop, mprusage.pr_rtime ) / AVER( pStart, pStop, mprusage.pr_count )), 7 );
119cdf0e10cSrcweir 
120cdf0e10cSrcweir 
121cdf0e10cSrcweir 	sal_uLong d_utime = DIFF_MS( pStart, pStop, mpstatus.pr_utime ) + DIFF_MS( pStart, pStop, mpstatus.pr_cutime );
122cdf0e10cSrcweir 	sal_uLong d_stime = DIFF_MS( pStart, pStop, mpstatus.pr_stime ) + DIFF_MS( pStart, pStop, mpstatus.pr_cstime );
123cdf0e10cSrcweir 
124cdf0e10cSrcweir 	aProfile += Pad( String::CreateFromInt64(d_utime), 7 );
125cdf0e10cSrcweir 	aProfile += Pad( String::CreateFromInt64(d_stime), 7 );
126cdf0e10cSrcweir 	aProfile += Pad( String::CreateFromInt64(d_utime + d_stime), 7 );
127cdf0e10cSrcweir 
128cdf0e10cSrcweir 	return aProfile;
129cdf0e10cSrcweir };
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 
132cdf0e10cSrcweir 
133