|
1 /* |
|
2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * |
|
5 * Redistribution and use in source and binary forms, with or without |
|
6 * modification, are permitted provided that the following conditions are met: |
|
7 * |
|
8 * - Redistributions of source code must retain the above copyright notice, |
|
9 * this list of conditions and the following disclaimer. |
|
10 * - Redistributions in binary form must reproduce the above copyright notice, |
|
11 * this list of conditions and the following disclaimer in the documentation |
|
12 * and/or other materials provided with the distribution. |
|
13 * - Neither the name of Nokia Corporation nor the names of its contributors |
|
14 * may be used to endorse or promote products derived from this software |
|
15 * without specific prior written permission. |
|
16 * |
|
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
27 * POSSIBILITY OF SUCH DAMAGE. |
|
28 * |
|
29 * Initial Contributors: |
|
30 * Nokia Corporation - initial contribution. |
|
31 * |
|
32 * Contributors: |
|
33 * |
|
34 * Description: |
|
35 * |
|
36 */ |
|
37 |
|
38 using System; |
|
39 using System.Collections.Generic; |
|
40 using System.Text; |
|
41 using System.Text.RegularExpressions; |
|
42 using SymbianUtils; |
|
43 |
|
44 namespace HeapComparisonLib.CSV.Thread.Parsers |
|
45 { |
|
46 internal class CSVThreadParserFormatNew |
|
47 { |
|
48 #region Constructors & destructor |
|
49 public CSVThreadParserFormatNew( CSVThreadParser aParser ) |
|
50 { |
|
51 iParser = aParser; |
|
52 } |
|
53 #endregion |
|
54 |
|
55 #region API |
|
56 public CSVThread ParseLine( string aLine ) |
|
57 { |
|
58 if ( !TryToParseEntry( aLine ) ) |
|
59 { |
|
60 if ( !TryToParseTimestamp( aLine ) ) |
|
61 { |
|
62 TryToParseSupportedTags( aLine ); |
|
63 } |
|
64 } |
|
65 |
|
66 // Return a constructed entry (or null) if we have one ready |
|
67 CSVThread ret = iConstructedEntry; |
|
68 iConstructedEntry = null; |
|
69 return ret; |
|
70 } |
|
71 #endregion |
|
72 |
|
73 #region Internal enumerations |
|
74 private enum TState |
|
75 { |
|
76 EStateIdle = 0, |
|
77 EStateSeenEntryStart, |
|
78 EStateSeenThreadName, |
|
79 EStateSeenProcessName, |
|
80 EStateSeenChunkName, |
|
81 EStateSeenFieldBody, |
|
82 EStateSeenEntryEnd, |
|
83 } |
|
84 #endregion |
|
85 |
|
86 #region Internal methods |
|
87 private bool TryToParseEntry( string aLine ) |
|
88 { |
|
89 Match matchEntry = KRegExEntry.Match( aLine ); |
|
90 if ( matchEntry.Success ) |
|
91 { |
|
92 // Check if it's an opening or closing tag. |
|
93 bool isOpen = string.IsNullOrEmpty( matchEntry.Groups[ "TagType" ].Value ); |
|
94 int index = int.Parse( matchEntry.Groups[ "Index" ].Value ); |
|
95 if ( isOpen ) |
|
96 { |
|
97 // Opening tag - starting a new entry, save entry id |
|
98 ChangeState( TState.EStateSeenEntryStart ); |
|
99 SaveCurrentEntryIfValidState(); |
|
100 |
|
101 iCurrentEntryId = index; |
|
102 iWorkInProgressThread = CSVThread.New(); |
|
103 } |
|
104 else |
|
105 { |
|
106 // Closing tag, we should've finished an entry now. |
|
107 // Validate the index is as we expect |
|
108 CheckExpectedIndexId( index ); |
|
109 ChangeState( TState.EStateSeenEntryEnd ); |
|
110 SaveCurrentEntryIfValidState(); |
|
111 } |
|
112 } |
|
113 |
|
114 return matchEntry.Success; |
|
115 } |
|
116 |
|
117 private bool TryToParseTimestamp( string aLine ) |
|
118 { |
|
119 Match match = KRegExTimestamp.Match( aLine ); |
|
120 if ( match.Success ) |
|
121 { |
|
122 // Get time value |
|
123 long timestamp = long.Parse( match.Groups[ "Timestamp" ].Value ); |
|
124 if ( iParser.CurrentDataSet != null ) |
|
125 { |
|
126 iParser.CurrentDataSet.TimeStamp = timestamp; |
|
127 } |
|
128 } |
|
129 |
|
130 return match.Success; |
|
131 } |
|
132 |
|
133 private bool TryToParseSupportedTags( string aLine ) |
|
134 { |
|
135 Match matchStandardFields = KRegExTagTypes.Match( aLine ); |
|
136 if ( matchStandardFields.Success ) |
|
137 { |
|
138 if ( iWorkInProgressThread == null ) |
|
139 { |
|
140 throw new Exception( "Corruption detected - work in progress thread is null" ); |
|
141 } |
|
142 |
|
143 // Check the index is what we expect it to be |
|
144 int index = int.Parse( matchStandardFields.Groups[ "Index" ].Value ); |
|
145 CheckExpectedIndexId( index ); |
|
146 |
|
147 // Now digest the tag body |
|
148 string body = matchStandardFields.Groups[ "Body" ].Value; |
|
149 string tagName = matchStandardFields.Groups[ "TagName" ].Value; |
|
150 if ( tagName == "THREAD_NAME" ) |
|
151 { |
|
152 iWorkInProgressThread.ThreadName = body; |
|
153 ChangeState( TState.EStateSeenThreadName ); |
|
154 } |
|
155 else if ( tagName == "PROCESS_NAME" ) |
|
156 { |
|
157 iWorkInProgressThread.ProcessName = body; |
|
158 ChangeState( TState.EStateSeenProcessName ); |
|
159 } |
|
160 else if ( tagName == "CHUNK_NAME" ) |
|
161 { |
|
162 iWorkInProgressThread.ChunkName = body; |
|
163 ChangeState( TState.EStateSeenChunkName ); |
|
164 } |
|
165 else if ( tagName == "FIELDS" ) |
|
166 { |
|
167 string[] elements = body.Trim().Split( ',' ); |
|
168 ExtractFields( elements ); |
|
169 ChangeState( TState.EStateSeenFieldBody ); |
|
170 } |
|
171 } |
|
172 |
|
173 return matchStandardFields.Success; |
|
174 } |
|
175 |
|
176 private void ChangeState( TState aState ) |
|
177 { |
|
178 iState = aState; |
|
179 } |
|
180 |
|
181 private void CheckExpectedIndexId( int aValue ) |
|
182 { |
|
183 if ( aValue != iCurrentEntryId ) |
|
184 { |
|
185 throw new Exception( "Corruption detected - index id incorrect" ); |
|
186 } |
|
187 } |
|
188 |
|
189 private void SaveCurrentEntryIfValidState() |
|
190 { |
|
191 if ( iWorkInProgressThread != null ) |
|
192 { |
|
193 // Perhaps we didn't encounter the closing tag for some odd reason? |
|
194 if ( iState == TState.EStateSeenFieldBody || iState == TState.EStateSeenEntryEnd ) |
|
195 { |
|
196 iConstructedEntry = iWorkInProgressThread; |
|
197 iWorkInProgressThread = null; |
|
198 } |
|
199 } |
|
200 } |
|
201 |
|
202 private void ExtractFields( string[] aValues ) |
|
203 { |
|
204 if ( aValues.Length != KExpectedFieldCount ) |
|
205 { |
|
206 throw new Exception( "Corruption detected - field count incorrect" ); |
|
207 } |
|
208 |
|
209 // TID |
|
210 iWorkInProgressThread.ThreadId = ParseDecimalValue( aValues[ 0 ] ); |
|
211 |
|
212 // CHUNK |
|
213 iWorkInProgressThread.ChunkHandle = ParseHexValue( aValues[ 1 ] ); |
|
214 iWorkInProgressThread.ChunkBaseAddress = ParseHexValue( aValues[ 2 ] ); |
|
215 |
|
216 // HEAP |
|
217 iWorkInProgressThread.SizeCurrent = ParseDecimalValue( aValues[ 3 ] ); |
|
218 iWorkInProgressThread.SizeMin = ParseDecimalValue( aValues[ 4 ] ); |
|
219 iWorkInProgressThread.SizeMax = ParseDecimalValue( aValues[ 5 ] ); |
|
220 |
|
221 // FIRST FREE CELL |
|
222 iWorkInProgressThread.FirstFreeCellAddress = ParseHexValue( aValues[ 6 ] ); |
|
223 iWorkInProgressThread.FirstFreeCellLength = ParseDecimalValue( aValues[ 7 ] ); |
|
224 |
|
225 // OTHER FREE CELL INFO |
|
226 iWorkInProgressThread.FreeCellCount = ParseDecimalValue( aValues[ 8 ] ); |
|
227 iWorkInProgressThread.FreeSpaceTotal = ParseDecimalValue( aValues[ 9 ] ); |
|
228 iWorkInProgressThread.FreeSpaceSlack = ParseDecimalValue( aValues[ 10 ] ); |
|
229 iWorkInProgressThread.FreeCellLargest = ParseDecimalValue( aValues[ 11 ] ); |
|
230 |
|
231 // ALLOC CELL INFO |
|
232 iWorkInProgressThread.AllocCellLargest = ParseDecimalValue( aValues[ 12 ] ); |
|
233 iWorkInProgressThread.AllocCellCount = ParseDecimalValue( aValues[ 13 ] ); |
|
234 iWorkInProgressThread.AllocSpaceTotal = ParseDecimalValue( aValues[ 15 ] ); // NB: this is item 15, not 14! |
|
235 |
|
236 // MISC |
|
237 iWorkInProgressThread.MinCellSize = ParseDecimalValue( aValues[ 14 ] ); |
|
238 iWorkInProgressThread.IsSharedHeap = ( ParseDecimalValue( aValues[ 16 ] ) != 0 ); |
|
239 } |
|
240 |
|
241 private static long ParseHexValue( string aItem ) |
|
242 { |
|
243 long ret = 0; |
|
244 // |
|
245 if ( aItem.Length > 0 ) |
|
246 { |
|
247 const string KHexPrefix = "0x"; |
|
248 if ( aItem.IndexOf( KHexPrefix ) == 0 ) |
|
249 { |
|
250 aItem = aItem.Substring( KHexPrefix.Length ); |
|
251 } |
|
252 |
|
253 ret = System.Convert.ToInt32( aItem, 16 ); |
|
254 } |
|
255 // |
|
256 return ret; |
|
257 } |
|
258 |
|
259 private static long ParseDecimalValue( string aItem ) |
|
260 { |
|
261 long ret = 0; |
|
262 // |
|
263 if ( aItem.Length > 0 ) |
|
264 { |
|
265 ret = System.Convert.ToInt32( aItem ); |
|
266 } |
|
267 // |
|
268 return ret; |
|
269 } |
|
270 #endregion |
|
271 |
|
272 #region Internal constants |
|
273 private const int KExpectedFieldCount = 17; |
|
274 #endregion |
|
275 |
|
276 #region Internal regular expression |
|
277 private static Regex KRegExEntry = new Regex( |
|
278 "\\<(?<TagType>/|)ENTRY_(?<Index>[0-9]{4})\\>", |
|
279 RegexOptions.Singleline |
|
280 | RegexOptions.CultureInvariant |
|
281 | RegexOptions.IgnorePatternWhitespace |
|
282 | RegexOptions.Compiled |
|
283 ); |
|
284 |
|
285 private static Regex KRegExTimestamp = new Regex( |
|
286 "\\<TIMESTAMP\\>(?<Timestamp>[0-9].+?)\\</TIMESTAMP\\>", |
|
287 RegexOptions.Singleline |
|
288 | RegexOptions.CultureInvariant |
|
289 | RegexOptions.IgnorePatternWhitespace |
|
290 | RegexOptions.Compiled |
|
291 ); |
|
292 |
|
293 // <summary> |
|
294 // Regular expression built for C# on: Wed, Sep 10, 2008, 09:39:13 AM |
|
295 // Using Expresso Version: 3.0.2766, http://www.ultrapico.com |
|
296 // |
|
297 // A description of the regular expression: |
|
298 // |
|
299 // Literal < |
|
300 // [TagName]: A named capture group. [THREAD_NAME|PROCESS_NAME|CHUNK_NAME|FIELDS] |
|
301 // Select from 4 alternatives |
|
302 // THREAD_NAME |
|
303 // THREAD_NAME |
|
304 // PROCESS_NAME |
|
305 // PROCESS_NAME |
|
306 // CHUNK_NAME |
|
307 // CHUNK_NAME |
|
308 // FIELDS |
|
309 // FIELDS |
|
310 // _ |
|
311 // [Index]: A named capture group. [[0-9]{4}] |
|
312 // Any character in this class: [0-9], exactly 4 repetitions |
|
313 // Literal > |
|
314 // [Body]: A named capture group. [.+?] |
|
315 // Any character, one or more repetitions, as few as possible |
|
316 // \</ |
|
317 // Literal < |
|
318 // / |
|
319 // Match expression but don't capture it. [THREAD_NAME|PROCESS_NAME|CHUNK_NAME|FIELDS] |
|
320 // Select from 4 alternatives |
|
321 // THREAD_NAME |
|
322 // THREAD_NAME |
|
323 // PROCESS_NAME |
|
324 // PROCESS_NAME |
|
325 // CHUNK_NAME |
|
326 // CHUNK_NAME |
|
327 // FIELDS |
|
328 // FIELDS |
|
329 // _ |
|
330 // Match expression but don't capture it. [[0-9]{4}] |
|
331 // Any character in this class: [0-9], exactly 4 repetitions |
|
332 // Literal > |
|
333 // |
|
334 // |
|
335 // </summary> |
|
336 private static Regex KRegExTagTypes = new Regex( |
|
337 "\\<(?<TagName>THREAD_NAME|PROCESS_NAME|CHUNK_NAME|FIELDS)_(?" + |
|
338 "<Index>[0-9]{4})\\>(?<Body>.+?)\\</(?:THREAD_NAME|PROCESS_NA" + |
|
339 "ME|CHUNK_NAME|FIELDS)_(?:[0-9]{4})\\>", |
|
340 RegexOptions.Singleline |
|
341 | RegexOptions.CultureInvariant |
|
342 | RegexOptions.IgnorePatternWhitespace |
|
343 | RegexOptions.Compiled |
|
344 ); |
|
345 #endregion |
|
346 |
|
347 #region Data members |
|
348 private readonly CSVThreadParser iParser; |
|
349 private TState iState = TState.EStateIdle; |
|
350 private int iCurrentEntryId = -1; |
|
351 private CSVThread iWorkInProgressThread = null; |
|
352 private CSVThread iConstructedEntry = null; |
|
353 #endregion |
|
354 } |
|
355 } |