Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

Tools/Xabsl2/GT/GTXabsl2Profiler.h

Go to the documentation of this file.
00001 /**
00002 * @file GTXabsl2Profiler.h
00003 * 
00004 * Definition and Implementation of classes GTXabsl2Profiler, GTXabsl2ProfilerNameTable
00005 *
00006 * @author Michael Spranger
00007 */
00008 #ifndef __GTXabsl2Profiler_h_
00009 #define __GTXabsl2Profiler_h_
00010 
00011 #ifdef _WIN32
00012 #pragma warning(disable:4786) 
00013 // the constructor with all it's long parameter type names results in a too
00014 // long debug identifier
00015 #endif
00016 
00017 #include <string>
00018 #include <vector>
00019 #include <deque>
00020 
00021 #include "Platform/GTAssert.h"
00022 
00023 #include "Tools/Streams/OutStreams.h"
00024 #include "Platform/SystemCall.h"
00025 
00026 #include "Tools/Xabsl2/Xabsl2Engine/Xabsl2Array.h"
00027 #include "Tools/Xabsl2/Xabsl2Engine/Xabsl2Engine.h"
00028 #include "Tools/Debugging/Debugging.h"
00029 
00030 #include "Tools/Streams/InStreams.h"
00031 #include "Tools/Module/SolutionRequest.h"
00032 
00033 /* Nr of Logelements written out per call to writeLog */
00034 #define NROFELEMW 2
00035 
00036 /*
00037 * @class GTXabsl2ProfilerNameTableEntry
00038 * Class that encapsulates string representations for options, state in options and optionparameters
00039 * @author Michael Spranger
00040 */
00041 class GTXabsl2ProfilerNameTableEntry{
00042   public:
00043     /** The name of the Option represented by this entry */
00044     std::string optionName;
00045     /**The vector of state names */
00046     std::vector<std::string> states;
00047     /** A vector of parameter names */
00048     std::vector<std::string> parameters;
00049     /** The maximal depth of this option in the option-tree (XABSL)*/
00050     int maxdepth;
00051     
00052     /**
00053     * Constructor
00054     * Initializes the strings as zero length strings.
00055     */
00056     GTXabsl2ProfilerNameTableEntry(){};
00057 
00058     
00059     /**
00060     * Constructor
00061     * @param option The option name
00062     * @param state The names of all state within the corresponding option
00063     * @param params The names of all parameters for this Option
00064     * @param depth The maximal depth of this option in the option tree.
00065     */
00066     GTXabsl2ProfilerNameTableEntry(const std::string option,std::vector<std::string> state, std::vector<std::string> params, const int depth)
00067       : optionName(option), states(state), parameters(params), maxdepth(depth){}
00068 };
00069 
00070 /*
00071 * @class GTXabsl2ProfilerNameTable
00072 * Dynamic Array for keeping a correlation of Xabsl2Options and States to Numbers for profiling purposes (remembering activation paths).
00073 * @author Michael Spranger
00074 */
00075 class GTXabsl2ProfilerNameTable
00076 {
00077   /** The array */
00078   GTXabsl2ProfilerNameTableEntry* data;
00079   
00080   
00081   /** The number of elements in the array */
00082   int usedSize, length;
00083   public:
00084     /** Constructor 
00085     * Initializes all members with 0.
00086     */
00087     GTXabsl2ProfilerNameTable():usedSize(0), length(0), data(NULL)
00088     {}
00089     
00090     /** Destructor 
00091     * Deletes the data
00092     */
00093     ~GTXabsl2ProfilerNameTable() 
00094     { 
00095       if(data)
00096         delete[] data;
00097     }
00098     /**
00099     * Initializes the Array to a given length
00100     * @param length_ The length of the initialised array
00101     */
00102     void init(int length_){
00103       if(data){
00104         delete[] data; data = NULL;
00105       }
00106       usedSize = 0;
00107       length = length_;
00108       data = new GTXabsl2ProfilerNameTableEntry[length];
00109     }
00110     
00111     /** Clears the array */
00112     void clear()
00113     { 
00114       if(data){
00115         delete[] data;
00116         usedSize = 0;
00117         data = new GTXabsl2ProfilerNameTableEntry[length];
00118       }
00119       
00120     }
00121     
00122     /** 
00123     * Returns the Position for a given optionname
00124     * Note that the function crashes if the element does not exist.
00125     */
00126     int getOptionPosition(const std::string optionName) const
00127     {
00128       return findOption(optionName);
00129     }
00130 
00131     /** 
00132     * Returns the Position for a given state within the option
00133     * Note that the function crashes if the element does not exist.
00134     * @param optionName Name of the option the state is in
00135     * @param state Name of the state the position is returned for
00136     * @return The Position of the state within NameTableEntry
00137     */
00138     int getStatePosition(const std::string optionName, const std::string state){
00139       return findState(optionName, state);
00140     }
00141     
00142     /**
00143     * The function appends a new GTXabsl2ProfilerNameTableEntry to the array.
00144     * @param optionName A string label for the option..
00145     * @param states The names of the state in the corresponding option.
00146     * @param params The names of the parameters for this option.
00147     * @param depth The maximal depth of this Entry/Option in the option tree.
00148     */  
00149     void append(const std::string optionName, const std::vector<std::string> states, const std::vector<std::string> params, int depth)
00150     {
00151       if(usedSize == length)
00152       {
00153         length += length > 1 ? length / 2 : 2; 
00154         
00155         GTXabsl2ProfilerNameTableEntry* temp = new GTXabsl2ProfilerNameTableEntry[length];
00156         if(data){
00157           for(int i = 0; i < getSize(); ++i)
00158             temp[i] = data[i];
00159           delete[] data;
00160         }
00161         data = temp;
00162       }
00163       data[usedSize++]=GTXabsl2ProfilerNameTableEntry(optionName, states, params, depth);
00164     }
00165     
00166     
00167     /**
00168     * The function sets the value of an element in the array.
00169     * Note that the function crashes if the element does not exist.
00170     * @param pos The position of the element in the array.
00171     * @param value The new element.
00172     */
00173     void setElement(int pos, GTXabsl2ProfilerNameTableEntry value)
00174     {
00175       data[pos] = value;
00176     }
00177     
00178     /**
00179     * The function returns the number of elements in the array.
00180     * @return The length of the list.
00181     */
00182     int getSize() const {return usedSize;}
00183     
00184     /** 
00185     * Returns the value for a given array position.
00186     * Note that the function crashes if the required position is bigger than the 
00187     * size of the array.
00188     */
00189     GTXabsl2ProfilerNameTableEntry& operator[](int pos) const
00190     {
00191       return data[pos];
00192     }
00193     
00194     /** Returns whether an element for the given name exists 
00195     * @param optionName Name of the option searched for
00196     * @param stateName Name of the state that in conjunction with optionName makes the request unique
00197     */
00198     bool exists(const std::string optionName, const std::string stateName) const
00199     {
00200       return find(optionName, stateName) >= 0;
00201     }
00202 
00203     /** Returns whether an element for the given name exists 
00204     * @param option Name of the option searched for
00205     */
00206     bool existsOption(const std::string option){
00207       return findOption(option) >=0;
00208     }
00209     
00210    protected:
00211    /** 
00212    * Finds the index of an element with the given names.
00213    * @return The index of the element of -1 if the name does not exist.
00214    */
00215      int find(const std::string optionName, const std::string state) const
00216      {
00217        for(int i = 0; i < getSize(); ++i){
00218          
00219          if(data[i].optionName  == optionName){
00220            for(std::vector<std::string>::iterator j = data[i].states.begin(); j != data[i].states.end(); ++j){
00221              if((*j) == state)
00222                return i;
00223            }
00224            
00225          }
00226        }
00227        return -1;
00228      }
00229      
00230      /** 
00231      * Finds the index of an element with the given names.
00232      * @return The index of the element of -1 if the name does not exist.
00233      */
00234      int findState(const std::string optionName, const std::string state) const
00235      {
00236        for(int i = 0; i < getSize(); ++i){
00237         
00238          if(data[i].optionName  == optionName){
00239            for(int j = 0; j < (int) data[i].states.size(); ++j){
00240              if(state == data[i].states[j])
00241                return j;
00242            }
00243            
00244          }
00245        }
00246        return -1;
00247      }
00248 
00249      /** 
00250      * Finds the index of an element with the given names.
00251      * @return The index of the element of -1 if the name does not exist.
00252      */
00253      int findOption(const std::string optionName) const
00254      {
00255        for(int i = 0; i < getSize(); ++i)
00256          if(data[i].optionName  == optionName) 
00257            return i;
00258          return -1;
00259      }
00260 };
00261 
00262 /*
00263 * @class GTXabsl2ActiveOption
00264 * Encapsulation of a part of a LogEntry. Wraps the knowledge about a active option. That is an option which is active at a given point in time, while the profiler is running.
00265 * @author Michael Spranger
00266 */
00267 class GTXabsl2ActiveOption{
00268 public:
00269   /** Number of the option. Index within the NameTable used for lookup */
00270   int optionNumber;
00271   
00272   /** Number of the state that is active within the option. Index within a NameTableEntry used for lookup */
00273   int stateNumber;
00274   
00275   /** Values for parameters of the option, where the position is the same as the names within a NametableEntry */
00276   std::vector<double> parameters;
00277 
00278   /*
00279    *  Constructor
00280    * Initialises to non-possible values
00281    */
00282   GTXabsl2ActiveOption(): optionNumber(-1), stateNumber(-1){}
00283   
00284   /*
00285    * Constructor
00286    * Initialises to corresponding values
00287    */
00288   GTXabsl2ActiveOption(int o, int st, std::vector<double> p): optionNumber(o), stateNumber(st), parameters(p){}
00289   
00290   /*
00291    *  Equality operator. Based on memberwise comparison. 
00292    */
00293   bool operator ==(const GTXabsl2ActiveOption other)const
00294   {
00295     if(this->optionNumber == other.optionNumber && this->stateNumber == other.stateNumber
00296       && this->parameters == other.parameters)
00297       return true;
00298     return false;
00299   }
00300 };
00301 
00302 /**
00303 * @class GTXabsl2LogEntry
00304 * Class for keeping numerical activation paths and framenumbers
00305 * @author Michael Spranger
00306 */
00307 class GTXabsl2LogEntry{
00308 public:
00309   /** framenumber of the entry */
00310   unsigned long framenumber;
00311 
00312   /** activation path within XABSL-tree */
00313   std::vector<GTXabsl2ActiveOption> activeOptions;
00314 
00315   /*
00316    *  Constructor
00317    */
00318   GTXabsl2LogEntry(unsigned long fn, std::vector<GTXabsl2ActiveOption> aO):framenumber(fn), activeOptions(aO){}
00319   GTXabsl2LogEntry(unsigned long fn):framenumber(fn){}
00320 };
00321 
00322 /**
00323 * @class GTXabsl2Profiler
00324 * Class for profiling and analysis of Xabsl-Activation-Paths
00325 * @author Michael Spranger
00326 */
00327 class GTXabsl2Profiler
00328 {
00329 private:
00330 
00331   /** NameTable for keeping corresponding names to numerical logs */
00332   GTXabsl2ProfilerNameTable nameTable;
00333 
00334   /** Numerical Log where indices are usually corresponding to indices in the NameTable*/
00335   std::deque<GTXabsl2LogEntry> log;
00336 
00337   /** Name of the file the Log is written to */
00338   std::string outFileName;
00339 
00340   /** Maximal depth of the XABSL-Option-Tree */
00341   int maxDepth;
00342 
00343   
00344   /*
00345    * Registers Option, calls itself recursively for subsequent options, registering all subsequent options as well
00346    * @param  option Option to be registered
00347    * @param depth depth of the option to be registered
00348    */
00349   void registerOptions(const Xabsl2Option* option, int depth);
00350 
00351   /*
00352    *  Does a depth count for the option, where depth is initial depth
00353    */
00354   void doDepthCount(const Xabsl2Option* option, int depth);
00355 
00356   /*
00357    *  Writes the number of NROFELEMW to the stream, erasing the logentries from the log.
00358    */
00359   void writeLogToStream(Out&);
00360 
00361   /*
00362    *  Writes complete log to stream. Erases complete log.
00363    */
00364   void writeCompleteLogToStream(Out&);
00365 
00366   /*
00367    *  Write NameTable to Stream
00368    */
00369   void writeNameTableToStream(Out&);
00370 
00371   /** A reference to a variable containing the current frame number */
00372   const unsigned long* frameNumber;
00373 
00374 public:
00375   /** The Xabsl-Output-Symbols for the Profiler */
00376   enum { dontCollectProfiles, collectProfiles} profilerCollectMode;
00377   enum { dontWriteProfiles, writeProfiles, writeCompleteProfiles} profilerWriteMode;
00378   
00379   /*
00380    * Called by Xabls-Engine to make profiler symbols visible to behavior
00381    */
00382   void registerSymbols(Xabsl2Engine&);
00383 
00384   GTXabsl2Profiler();
00385   /** Constructor
00386   * @param id The id of the observed xabsl engine 
00387   * @param frameNumber A reference to the frame number
00388   */ 
00389   GTXabsl2Profiler(SolutionRequest::xabsl2EngineID id,
00390       const unsigned long* frameNumber);
00391 
00392   /*
00393    *  Initialises Engine, creates nametable and writes it to outfilename
00394    * @param pEnginge Reference to the engine to be profiled
00395    * @param length if known length refers to the number of options, so the nametable can be iniialised
00396    */
00397   void init(Xabsl2Engine& pEngine,int length =1);
00398 
00399   /*
00400    * Runs through the Engine determine active options, states and their parameters, writes the option-activation-path to log
00401    * @param pEngine XabslEngine to be profiled
00402    */
00403   void doProfiling(Xabsl2Engine& pEngine);
00404 
00405   /*
00406    * Write a certain number (NROFELEMW) of logs to outfilename, erasing them from the log.
00407    */
00408   void recordCollectedLogs();
00409 
00410   /*
00411    * Writes the complete log to outfilename, erasing all up to now collected logs.
00412    */
00413   void recordCompleteLog();
00414   
00415   /*
00416    * Writes an XML-representation of the current held log to the stream.
00417    */
00418   void writeXMLtoStream(Out& out);
00419 
00420   /*
00421    * Write an XML-representation of the current log to the file outf
00422    */
00423   void exportXMLFile(const char* outf);
00424 
00425   /*
00426    * Imports a log from a logfile
00427    * @param filename name of the logfile to be imported
00428    */
00429   bool importLogFile(const char* filename);
00430 
00431   /*
00432    * Gets all the options that were active at specified point in time
00433    * @param time framenumber of the active-option-path to be returned
00434    * @return active-option-path
00435    */
00436   std::vector<GTXabsl2ActiveOption> getActiveOptionsAtFrame(int time);
00437 
00438   /*
00439    * Returns corresponding name to optionnumber from nametable
00440    * @param optionnumber number of the option (index to nametable)
00441    */
00442   std::string getOptionName(int optionNumber);
00443   /*
00444    * Returns corresponding name to statenumber from nametable
00445    * @param optionnumber number of the option (index to nametable)
00446    */
00447   std::string getStateName(int optionNumber, int stateNumber);
00448 
00449   /*
00450    * @return The maximal depth of the option in the option tree
00451    */
00452   int getDepth(char* optionName);
00453 
00454   /*
00455    * @return The maximal depth of the option in the option tree
00456    */
00457   int getDepth(int optionNumber);
00458 
00459   /*
00460    * @return the maximal depth of the whole option-tree
00461    */
00462   int getMaxDepth(){ return maxDepth; }
00463 
00464   /*
00465    * @return the beginning framenumber of the current log
00466    */
00467   int getBeginningFramenumber(){ return log.size()?log.front().framenumber:0; }
00468 
00469   
00470   /*
00471    * @return the last framenumber of the current log
00472    */
00473   int getLastFramenumber(){ return log.size()?log.back().framenumber:0;}
00474 
00475   /*
00476    * @return the current number of log entries
00477    */
00478   int getNumberofLogEntries();
00479 
00480   /*
00481    * @return a representation of the active options and their parameters and the active states at framenumber
00482    */
00483   std::vector<GTXabsl2ActiveOption> getActiveOptionsAtNumber(int frameNumber);
00484 
00485   /*
00486    * Gets the active options at the position index in log
00487    * @return numerical representation of active options at logentry index
00488    */
00489   GTXabsl2LogEntry getLogEntryAtIndex(int index);
00490 
00491   /*
00492    * Gets the following framenumber of frame
00493    */
00494   int getFollowingFramenumber(int frame); 
00495   
00496   /*
00497    * Gets the index to the frameNumber
00498    */
00499   int getIndex(int frameNumber);
00500 
00501 
00502   /*
00503    * Access to the log
00504    */
00505   GTXabsl2LogEntry& operator[](int i);
00506 
00507   /*
00508    * Gets size of log.
00509    */
00510   int size();
00511 
00512   /*
00513    * Gets complete nametableentryfor index(which is equal to an optionnumber)
00514    */
00515   GTXabsl2ProfilerNameTableEntry& getNameTableEntry(int index);
00516 
00517   /*
00518    * Gets an activeOption at index with a maximum depth
00519    * @param index index of the logentry
00520    * @param maxdepth maximum depth of the returned option
00521    * @param Reference to option, holding an option of maximumdeppth at index
00522    * @return wether there was an option found at specified maxdepth and index
00523    */
00524   bool getActiveOption(int index, int maxdepth, GTXabsl2ActiveOption& retour);
00525 };
00526 
00527 #endif// __GTXabsl2Profiler_h_
00528 
00529 
00530 /*
00531 * Change Log:
00532 *
00533 * $Log: GTXabsl2Profiler.h,v $
00534 * Revision 1.7  2004/09/09 10:15:57  spranger
00535 * fixed doxygen-errors
00536 *
00537 * Revision 1.6  2004/07/21 15:40:29  spranger
00538 * fixed some comments
00539 *
00540 * Revision 1.5  2004/06/14 17:15:51  spranger
00541 * added documentation
00542 *
00543 * Revision 1.4  2004/05/27 18:42:24  loetzsch
00544 * removed warnings
00545 *
00546 * Revision 1.3  2004/05/24 16:34:43  spranger
00547 * removed DebugLogToStream, bugfix
00548 *
00549 * Revision 1.2  2004/05/24 13:17:51  spranger
00550 * comment out xml-export
00551 *
00552 * Revision 1.1  2004/05/23 18:59:42  spranger
00553 * changed interface, added framenumber to Constructor
00554 *
00555 * Revision 1.10  2004/05/22 16:58:04  spranger
00556 * changed dialog appearance, added features (including extended profiler-interface)
00557 *
00558 * Revision 1.9  2004/05/04 15:35:02  spranger
00559 * extended interface
00560 *
00561 * Revision 1.8  2004/05/03 13:21:57  spranger
00562 * extended interface
00563 *
00564 * Revision 1.7  2004/04/30 09:26:23  spranger
00565 * redone interface, added xml-export
00566 *
00567 * Revision 1.6  2004/04/28 17:31:40  spranger
00568 * redesign of nametable, added import facilities
00569 *
00570 * Revision 1.5  2004/04/22 12:21:33  spranger
00571 * removed compiler warnings
00572 *
00573 * Revision 1.4  2004/04/22 11:33:19  spranger
00574 * different log handling (queue), added writingoglogs which does not write out whole log
00575 *
00576 * Revision 1.3  2004/04/20 18:17:01  spranger
00577 * added nametable-structure, added log functionality
00578 *
00579 * Revision 1.2  2004/04/14 13:33:54  loetzsch
00580 * added change log
00581 *
00582 */

Generated on Thu Sep 23 19:57:42 2004 for GT2004 by doxygen 1.3.6