1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_automation.hxx"
30 
31 
32 #include <procfs.h>
33 #include <tools/stream.hxx>
34 #include "profiler.hxx"
35 
36 
37 struct SysdepProfileSnapshot
38 {
39 	pstatus mpstatus;
40 	psinfo mpsinfo;
41 	prusage mprusage;
42 };
43 
44 
45 struct SysdepStaticData
46 {
47 	// Hier steht alles, was w�hrend des Profiles st�ndig gebraucht wird
48 };
49 
50 
51 void TTProfiler::InitSysdepProfiler()
52 {
53 	if ( !pSysDepStatic )	// Sollte immer so sein!!
54 		pSysDepStatic = new SysdepStaticData;
55 	// Hier initialisieren
56 
57 };
58 
59 void TTProfiler::DeinitSysdepProfiler()
60 {
61 	if ( pSysDepStatic )	// Sollte immer so sein!!
62 	{
63 		// Hier aufr�umen und eventuell Speicher freigeben
64 
65 		delete pSysDepStatic;
66 	}
67 };
68 
69 SysdepProfileSnapshot *TTProfiler::NewSysdepSnapshotData()
70 {
71 	return new SysdepProfileSnapshot;
72 };
73 
74 void TTProfiler::DeleteSysdepSnapshotData( SysdepProfileSnapshot *pSysdepProfileSnapshot )
75 {
76 	delete pSysdepProfileSnapshot;
77 };
78 
79 
80 // Titelzeile f�r Logdatei
81 String TTProfiler::GetSysdepProfileHeader()
82 {
83 	return String::CreateFromAscii(" Size(Kb) ResidentSZ  rtime  ktime  utime  total");
84 };
85 
86 
87 // Zustand merken
88 void TTProfiler::GetSysdepProfileSnapshot( SysdepProfileSnapshot *pSysdepProfileSnapshot, sal_uInt16 )
89 {
90 	SvFileStream aStream( String::CreateFromAscii("/proc/self/psinfo"), STREAM_READ );		// Das ist ein expliziter Pfad f�r UNXSOL!
91 	if ( aStream.IsOpen() )
92 	{
93 		aStream.Read( &(pSysdepProfileSnapshot->mpsinfo), sizeof( psinfo ) );
94 		aStream.Close();
95 	}
96 	SvFileStream anotherStream( String::CreateFromAscii("/proc/self/status"), STREAM_READ );		// Das ist ein expliziter Pfad f�r UNXSOL!
97 	if ( anotherStream.IsOpen() )
98 	{
99 		anotherStream.Read( &(pSysdepProfileSnapshot->mpstatus), sizeof( pstatus ) );
100 		anotherStream.Close();
101 	}
102 	SvFileStream YetAnotherStream( String::CreateFromAscii("/proc/self/usage"), STREAM_READ );		// Das ist ein expliziter Pfad f�r UNXSOL!
103 	if ( YetAnotherStream.IsOpen() )
104 	{
105 		YetAnotherStream.Read( &(pSysdepProfileSnapshot->mprusage), sizeof( prusage ) );
106 		YetAnotherStream.Close();
107 	}
108 };
109 
110 #define DIFF2( aFirst, aSecond, Membername ) ( aSecond.Membername - aFirst.Membername )
111 #define CALC_MS( nSec, nNSec ) ( nSec * 1000 + (nNSec+500000) / 1000000 )
112 #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 ) )
113 // Informationszeile zusammenbauen
114 String TTProfiler::GetSysdepProfileLine( SysdepProfileSnapshot *pStart, SysdepProfileSnapshot *pStop )
115 {
116 	String aProfile;
117 
118 	aProfile += Pad( String::CreateFromInt64(pStop->mpsinfo.pr_size), 9);
119 	aProfile += Pad( String::CreateFromInt64(pStop->mpsinfo.pr_rssize), 11);
120 
121 
122 	aProfile += Pad( String::CreateFromInt64(DIFF_MS( pStart, pStop, mprusage.pr_rtime ) / AVER( pStart, pStop, mprusage.pr_count )), 7 );
123 
124 
125 	sal_uLong d_utime = DIFF_MS( pStart, pStop, mpstatus.pr_utime ) + DIFF_MS( pStart, pStop, mpstatus.pr_cutime );
126 	sal_uLong d_stime = DIFF_MS( pStart, pStop, mpstatus.pr_stime ) + DIFF_MS( pStart, pStop, mpstatus.pr_cstime );
127 
128 	aProfile += Pad( String::CreateFromInt64(d_utime), 7 );
129 	aProfile += Pad( String::CreateFromInt64(d_stime), 7 );
130 	aProfile += Pad( String::CreateFromInt64(d_utime + d_stime), 7 );
131 
132 	return aProfile;
133 };
134 
135 
136 
137