/*----------------------------------------------------------------------
Prof-It for C#
Copyright (c) 2004 Klaus Lehner, University of Linz
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
----------------------------------------------------------------------*/
using System;
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using at.jku.ssw.ProfIt.Components;
using at.jku.ssw.ProfIt.Runtime;
namespace at.jku.ssw.ProfIt.CodeGen{
/// <summary>
/// This class converts a ProfilingResult to an integer array
/// and the other way round to increase performance at runtime
/// </summary>
public class CounterConverter {
/// <summary>
/// Loads the integer array from the CounterFile of the specified
/// solution and returns a ProfilingResult
/// </summary>
public static ProfilingResult LoadCounters(Solution solution) {
FileStream myStream = null;
try {
if (solution == null) return null;
if (!File.Exists(solution.CounterFileName)) return null;
myStream = File.OpenRead(solution.CounterFileName);
BinaryFormatter formatter = new BinaryFormatter();
CounterArray counterArray = (CounterArray)formatter.Deserialize(myStream);
myStream.Close();
myStream = File.OpenRead(solution.CounterFileName2);
formatter = new BinaryFormatter();
counterArray.Counters = (ArrayList)formatter.Deserialize(myStream);
myStream.Close();
int index=0;
if (counterArray != null) {
//coll.LastModification = File.GetLastWriteTime(solution.CounterFileName);
//ProfilingResult result = new ProfilingResult(counterArray.Files);
foreach (CounterCollection cc in counterArray.Counters) {
foreach (IBlock block in cc.blocks) {
if (block is StatementBlock) {
((StatementBlock)block).SetCounter(block.Counter + counterArray.cnt[index++]);
}
}
}
}
ProfilingResult result = new ProfilingResult(counterArray.Counters);
result.LastModification = File.GetLastWriteTime(solution.CounterFileName);
return result;
} catch (Exception e) {
Console.WriteLine(e.StackTrace);
} finally {
if (myStream != null) myStream.Close();
}
return null;
}
/// <summary>
/// Stores the CounterCollection of the solution to a file to make
/// it possible to deserialize the data structure in another session
/// </summary>
public static void StoreCounters(Solution solution) {
if (solution == null) return;
FileStream myStream = File.Create(solution.CounterFileName);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(myStream, solution.CounterArray);
myStream.Close();
myStream = File.Create(solution.CounterFileName2);
formatter = new BinaryFormatter();
formatter.Serialize(myStream, solution.CounterArray.Counters);
myStream.Close();
}
}
}
|