|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 using System; |
|
18 using System.IO; |
|
19 using System.Collections.Generic; |
|
20 using System.Text; |
|
21 using System.Threading; |
|
22 using SymbianUtils.Range; |
|
23 |
|
24 namespace SymbianSymbolLibTest |
|
25 { |
|
26 class Program |
|
27 { |
|
28 class Binary |
|
29 { |
|
30 public string iFileName = string.Empty; |
|
31 public byte[] iData = null; |
|
32 public long iDataStart = 0; |
|
33 public long iDataEnd = 0; |
|
34 } |
|
35 |
|
36 class Binary2 |
|
37 { |
|
38 public string iFileName = string.Empty; |
|
39 public List<string> iData = new List<string>(); |
|
40 } |
|
41 |
|
42 class Store |
|
43 { |
|
44 public Store() |
|
45 { |
|
46 } |
|
47 |
|
48 public void Add( string aFileName, Binary2 aData ) |
|
49 { |
|
50 lock ( iEntries ) |
|
51 { |
|
52 if ( !iEntries.ContainsKey( aFileName ) ) |
|
53 { |
|
54 iEntries.Add( aFileName, aData ); |
|
55 } |
|
56 } |
|
57 } |
|
58 |
|
59 public int Count |
|
60 { |
|
61 get { return iEntries.Count; } |
|
62 } |
|
63 |
|
64 public void Dump() |
|
65 { |
|
66 } |
|
67 |
|
68 private Dictionary<string, Binary2> iEntries = new Dictionary<string, Binary2>(); |
|
69 } |
|
70 |
|
71 class ThreadReader |
|
72 { |
|
73 public delegate void CompletionHandler( ThreadReader aReader ); |
|
74 public event CompletionHandler Completed = null; |
|
75 |
|
76 public ThreadReader( byte[] aData, int aStart, int aCount, Store aStore ) |
|
77 { |
|
78 iData = aData; |
|
79 iStart = aStart; |
|
80 iCount = aCount; |
|
81 iStore = aStore; |
|
82 } |
|
83 |
|
84 public void Read() |
|
85 { |
|
86 //System.Console.WriteLine( string.Format( "Reading starting - pos: 0x{0}, len: {1:d8}", iStart, iCount ) ); |
|
87 DateTime timeStamp = DateTime.Now; |
|
88 |
|
89 Binary2 currentBinary = null; |
|
90 |
|
91 long binaryCount = 0; |
|
92 long lineCount = 0; |
|
93 using ( StreamReader reader = new StreamReader( new MemoryStream( iData, iStart, iCount ) ) ) |
|
94 { |
|
95 string line = reader.ReadLine(); |
|
96 while ( line != null ) |
|
97 { |
|
98 ++lineCount; |
|
99 if ( line.StartsWith( "From " ) ) |
|
100 { |
|
101 currentBinary = new Binary2(); |
|
102 currentBinary.iFileName = line.Substring( 8 ); |
|
103 // |
|
104 ++binaryCount; |
|
105 iStore.Add( currentBinary.iFileName, currentBinary ); |
|
106 } |
|
107 else if ( currentBinary != null ) |
|
108 { |
|
109 currentBinary.iData.Add( line ); |
|
110 } |
|
111 |
|
112 line = reader.ReadLine(); |
|
113 } |
|
114 } |
|
115 |
|
116 DateTime endTime = DateTime.Now; |
|
117 TimeSpan span = ( endTime - timeStamp ); |
|
118 //System.Console.WriteLine( string.Format( "Reading complete - {0} time, {1} lines, {2} binaries", span, lineCount, binaryCount ) ); |
|
119 |
|
120 if ( Completed != null ) |
|
121 { |
|
122 Completed( this ); |
|
123 } |
|
124 } |
|
125 |
|
126 private readonly byte[] iData; |
|
127 private readonly int iStart; |
|
128 private readonly int iCount; |
|
129 private readonly Store iStore; |
|
130 } |
|
131 |
|
132 class SymbolFile |
|
133 { |
|
134 #region Constructors |
|
135 public SymbolFile( FileInfo aFile ) |
|
136 { |
|
137 iFile = aFile; |
|
138 } |
|
139 #endregion |
|
140 |
|
141 #region API |
|
142 public void Read() |
|
143 { |
|
144 Partition(); |
|
145 // |
|
146 iWaiter = new AutoResetEvent( false ); |
|
147 for( int i=iReaders.Count-1; i>=0; i-- ) |
|
148 { |
|
149 ThreadReader reader = iReaders[ i ]; |
|
150 reader.Completed += new ThreadReader.CompletionHandler( Reader_Completed ); |
|
151 ThreadPool.QueueUserWorkItem( new WaitCallback(StartReader ), reader ); |
|
152 } |
|
153 |
|
154 // Now wait |
|
155 using ( iWaiter ) |
|
156 { |
|
157 iWaiter.WaitOne(); |
|
158 } |
|
159 iWaiter = null; |
|
160 } |
|
161 #endregion |
|
162 |
|
163 #region Properties |
|
164 public int Count |
|
165 { |
|
166 get { return iStore.Count; } |
|
167 } |
|
168 #endregion |
|
169 |
|
170 #region Event handlers |
|
171 private void Reader_Completed( ThreadReader aReader ) |
|
172 { |
|
173 iCompleted.Add( aReader ); |
|
174 iReaders.Remove( aReader ); |
|
175 // |
|
176 if ( iReaders.Count == 0 ) |
|
177 { |
|
178 iWaiter.Set(); |
|
179 } |
|
180 } |
|
181 #endregion |
|
182 |
|
183 #region Internal methods |
|
184 private static void StartReader( object aThreadReader ) |
|
185 { |
|
186 ThreadReader reader = (ThreadReader) aThreadReader; |
|
187 reader.Read(); |
|
188 } |
|
189 |
|
190 private void Partition() |
|
191 { |
|
192 // Read entire file into buffer |
|
193 byte[] data = new byte[ iFile.Length ]; |
|
194 using ( Stream reader = new FileStream( iFile.FullName, FileMode.Open ) ) |
|
195 { |
|
196 reader.Read( data, 0, data.Length ); |
|
197 } |
|
198 |
|
199 // This is the pattern we are searching for: |
|
200 byte[] pattern = new byte[] { (byte) 'F', (byte) 'r', (byte) 'o', (byte) 'm', (byte) ' ', (byte) ' ', (byte) ' ', (byte) ' ' }; |
|
201 |
|
202 int threadCount = System.Environment.ProcessorCount; |
|
203 int chunkSize = (int) iFile.Length / threadCount; |
|
204 |
|
205 // |
|
206 int blockPosStart = 0; |
|
207 for ( int i = 0; i < threadCount; i++ ) |
|
208 { |
|
209 int pos = 0; |
|
210 int blockPosEnd = Math.Min( data.Length - 1, blockPosStart + chunkSize ); |
|
211 while ( pos >= 0 ) |
|
212 { |
|
213 pos = Array.IndexOf( data, pattern[ 0 ], blockPosEnd ); |
|
214 if ( pos > 0 ) |
|
215 { |
|
216 if ( pos + 8 >= data.Length ) |
|
217 { |
|
218 break; |
|
219 } |
|
220 else if ( pos + 8 < data.Length && data[ pos + 7 ] == pattern[ 7 ] ) |
|
221 { |
|
222 bool isMatch = CompareByteArrays( pattern, data, pos ); |
|
223 if ( isMatch ) |
|
224 { |
|
225 int length = pos - blockPosStart; |
|
226 System.Console.WriteLine( string.Format( "Block {0:d2} @ 0x{1:x8}, length: {2:d8}", i, blockPosStart, length ) ); |
|
227 // |
|
228 ThreadReader reader = new ThreadReader( data, blockPosStart, length, iStore ); |
|
229 iReaders.Add( reader ); |
|
230 |
|
231 blockPosStart = pos; |
|
232 break; |
|
233 } |
|
234 } |
|
235 else |
|
236 { |
|
237 // Didn't find a match, move forwards |
|
238 blockPosEnd = pos + 1; |
|
239 } |
|
240 } |
|
241 else |
|
242 { |
|
243 // Searched to end of file and didn't find another block, so just create |
|
244 // a new reader for everything that remains. |
|
245 int length2 = data.Length - blockPosStart; |
|
246 System.Console.WriteLine( string.Format( "Block {0:d2} @ 0x{1:x8}, length: {2:d8}", i, blockPosStart, length2 ) ); |
|
247 // |
|
248 ThreadReader reader2 = new ThreadReader( data, blockPosStart, length2, iStore ); |
|
249 iReaders.Add( reader2 ); |
|
250 break; |
|
251 } |
|
252 } |
|
253 } |
|
254 } |
|
255 |
|
256 private static bool CompareByteArrays( byte[] aSearchFor, byte[] aSearchIn, int aStartPos ) |
|
257 { |
|
258 bool areEqual = true; |
|
259 // |
|
260 for ( int i = aStartPos; i < aSearchFor.Length; i++ ) |
|
261 { |
|
262 byte b = aSearchIn[ i ]; |
|
263 byte c = aSearchFor[ i - aStartPos ]; |
|
264 if ( b != c ) |
|
265 { |
|
266 areEqual = false; |
|
267 break; |
|
268 } |
|
269 } |
|
270 // |
|
271 return areEqual; |
|
272 } |
|
273 #endregion |
|
274 |
|
275 #region Data members |
|
276 private readonly FileInfo iFile; |
|
277 private AutoResetEvent iWaiter = null; |
|
278 private Store iStore = new Store(); |
|
279 private List<ThreadReader> iCompleted = new List<ThreadReader>(); |
|
280 private List<ThreadReader> iReaders = new List<ThreadReader>(); |
|
281 #endregion |
|
282 } |
|
283 |
|
284 static void Main( string[] args ) |
|
285 { |
|
286 string path = @"C:\Tool Demo Files\2. Crash Data\File43\CoreImage"; // C:\Tool Demo Files\2. Crash Data\File28\"; |
|
287 DirectoryInfo dir = new DirectoryInfo( path ); |
|
288 FileInfo[] files = dir.GetFiles( "*.symbol" ); |
|
289 // |
|
290 // |
|
291 foreach ( FileInfo file in files ) |
|
292 { |
|
293 // |
|
294 DateTime timeStamp = DateTime.Now; |
|
295 System.Console.WriteLine( string.Format( "[{0}] - Reading starting...", file.Name ) ); |
|
296 |
|
297 SymbolFile symbolFile = new SymbolFile( file ); |
|
298 symbolFile.Read(); |
|
299 |
|
300 DateTime endTime = DateTime.Now; |
|
301 TimeSpan span = ( endTime - timeStamp ); |
|
302 |
|
303 System.Console.WriteLine( string.Format( "[{0}] - Reading complete - {1} time, {2} binaries", file.Name, span, symbolFile.Count ) ); |
|
304 System.Console.WriteLine( " " ); |
|
305 System.Console.WriteLine( " " ); |
|
306 System.Console.WriteLine( " " ); |
|
307 System.Console.ReadKey(); |
|
308 } |
|
309 } |
|
310 } |
|
311 } |