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