/************************************************************************
method CompilerTrackingInfo::printToFile

helper to simply print the tracker info into the specified file. 

************************************************************************/
void
CompilerTrackingInfo::printToFile()
{
  const char *trackerLogFilename = getCompilerTrackingLogFilename();

  CMPASSERT( NULL != trackerLogFilename );

  // the pointer to the process info for this tracker
  CmpProcess *p = processInfo_;

  CMPASSERT( NULL != p );

  ofstream fileout(trackerLogFilename, ios::app);
    
  fileout << "--------------------------------\n";
  fileout << "         Start Interval\n";
  fileout << endl;
  char beginTime[100];
  char endTime[100];  
  getTimestampAsBuffer(beginIntervalTime(), beginTime);
  getTimestampAsBuffer(endIntervalTime(), endTime);
  fileout << "Logged Interval At\t: " << endTime << endl;
  fileout << "Interval Start Time\t: " << beginTime << endl;  
  fileout << "Interval Duration\t: " << currentIntervalDuration(endIntervalTime()) 
                << " minutes" << endl;

  fileout << endl;
     
  char compilerId[COMPILER_ID_LEN];
  p->getCompilerId(compilerId, COMPILER_ID_LEN);
  fileout << "Compiler ID\t\t: " << compilerId << endl; 
  fileout << "Process ID\t\t: " << p->getPin() << endl;  
  fileout << "Compiler Age\t\t: " << compilerAge() << " minutes\n";
  fileout << "Successful Compilations : " << successfulQueryCount() << endl;
  fileout << "Failed Compilations \t: " << failedQueryCount() << endl;
  fileout << "Recompiles \t\t: " << qCacheRecompiles() << endl;
  fileout << "Sessions \t\t: " << sessionCount() << endl;
  fileout << "Caught Exceptions \t: " << caughtExceptionCount() << endl;
  fileout << endl;
  fileout << "Interval CPU time\t: " << cpuPathLength() << endl;
  fileout << "Longest Compile (CPU)\t: " << longestCompile() << endl;
  //
  // additional compiler info
  fileout << endl;
  fileout << "Compiler Info\t\t: " << compilerInfo() << endl;
  //
  // heap/cache table
  fileout << endl;  
  fileout.width(CACHE_HEAP_HEADER_LEN);
  fileout << "";
  fileout.width(CACHE_HEAP_VALUE_LEN);
  fileout << "CurrentSize";
  fileout.width(CACHE_HEAP_VALUE_LEN);
  fileout << "HighWaterMark";
  fileout.width(CACHE_HEAP_VALUE_LEN);
  fileout << "Hits";
  fileout.width(CACHE_HEAP_VALUE_LEN);
  fileout << "Lookups";
  fileout << endl;

  fileout.width(CACHE_HEAP_HEADER_LEN);
  fileout << "";
  fileout.width(CACHE_HEAP_VALUE_LEN*4);
  fileout << "----------------------------------------------------------------";
  fileout << "\n\n";    
  //
  // system heap
  fileout.width(CACHE_HEAP_HEADER_LEN);
  fileout << "System Heap: ";
  fileout.width(CACHE_HEAP_VALUE_LEN);
  fileout << p->getCurrentSystemHeapSize();
  fileout.width(CACHE_HEAP_VALUE_LEN);
  fileout << systemHeapIntervalWaterMark();
  fileout.width(CACHE_HEAP_VALUE_LEN);
  fileout << "N/A";
  fileout.width(CACHE_HEAP_VALUE_LEN);
  fileout << "N/A";
  fileout << endl;
  //
  // context heap
  fileout.width(CACHE_HEAP_HEADER_LEN);
  fileout << "Context Heap: ";
  fileout.width(CACHE_HEAP_VALUE_LEN);
  fileout << cxtHeapCurrentSize();
  fileout.width(CACHE_HEAP_VALUE_LEN);
  fileout << cxtHeapIntervalWaterMark();
  fileout.width(CACHE_HEAP_VALUE_LEN);
  fileout << "N/A";
  fileout.width(CACHE_HEAP_VALUE_LEN);
  fileout << "N/A";
  fileout << endl;
  //
  // statement heap
  fileout.width(CACHE_HEAP_HEADER_LEN);
  fileout << "Statement Heap: "; 
  fileout.width(CACHE_HEAP_VALUE_LEN);
  fileout << "N/A";
  fileout.width(CACHE_HEAP_VALUE_LEN);
  fileout << stmtHeapIntervalWaterMark();
  fileout.width(CACHE_HEAP_VALUE_LEN);
  fileout << "N/A";
  fileout.width(CACHE_HEAP_VALUE_LEN);
  fileout << "N/A";
  fileout << endl;
  //
  // metadata cache
  fileout.width(CACHE_HEAP_HEADER_LEN);
  fileout << "Metadata Cache: ";
  fileout.width(CACHE_HEAP_VALUE_LEN);
  fileout << metaDataCacheCurrentSize();
  fileout.width(CACHE_HEAP_VALUE_LEN);
  fileout << metaDataCacheIntervalWaterMark();
  fileout.width(CACHE_HEAP_VALUE_LEN);
  fileout << metaDataCacheHits();
  fileout.width(CACHE_HEAP_VALUE_LEN);
  fileout << metaDataCacheLookups();  
  fileout << endl;
  //
  // query cache
  fileout.width(CACHE_HEAP_HEADER_LEN);
  fileout << "Query Cache: ";
  fileout.width(CACHE_HEAP_VALUE_LEN);
  fileout << qCacheCurrentSize();
  fileout.width(CACHE_HEAP_VALUE_LEN);
  fileout << qCacheIntervalWaterMark();
  fileout.width(CACHE_HEAP_VALUE_LEN);
  fileout << qCacheHits();
  fileout.width(CACHE_HEAP_VALUE_LEN);
  fileout << qCacheLookups();    
  fileout << endl;
  //
  // histogram cache
  fileout.width(CACHE_HEAP_HEADER_LEN);
  fileout << "Histogram Cache: ";
  fileout.width(CACHE_HEAP_VALUE_LEN);
  fileout << hCacheCurrentSize();
  fileout.width(CACHE_HEAP_VALUE_LEN);
  fileout << hCacheIntervalWaterMark();
  fileout.width(CACHE_HEAP_VALUE_LEN);
  fileout << hCacheHits();
  fileout.width(CACHE_HEAP_VALUE_LEN);
  fileout << hCacheLookups();    
  fileout << endl;  
  
  fileout << endl;
  fileout << "         End Interval\n";
  fileout << "--------------------------------\n";
  fileout.close();
}
void
CompilerTrackingInfo::logIntervalInPrivateTable()
{
  /* ------------------------------------------------------
     -- for debugging purposes we may need to dump the tracking
     -- information in a private table. The DDL of this table
     -- should be as follows: (change the name as needed)

     create table STATE_TRACKING_COMPILERS_TABLE_PRIVATE
     (
        LOGGED_AT_LCT_TS            TIMESTAMP(6) NOT NULL,
        COMPILER_ID                 CHAR(28) CHARACTER SET UCS2 NOT NULL,
        PROCESS_ID                  INT UNSIGNED NOT NULL,
        INTERVAL_START_LCT_TS       TIMESTAMP(6),
        INTERVAL_PATH_LEN           LARGEINT,
        LONGEST_COMPILE_PATH        LARGEINT,
        COMPILER_AGE                LARGEINT,
        NUM_SESSIONS                INT UNSIGNED,
        STMT_HEAP_HWTR_MARK         LARGEINT,
        CONTEXT_HEAP_SIZE           LARGEINT,
        CONTEXT_HEAP_HWTR_MARK      LARGEINT,
        SYSTEM_HEAP_SIZE            LARGEINT,
        SYSTEM_HEAP_HWTR_MARK       LARGEINT, 
        METADATA_CACHE_SIZE         LARGEINT,
        METADATA_CACHE_HWTR_MARK    LARGEINT,
        METADATA_CACHE_HITS         LARGEINT,
        METADATA_CACHE_LOOKUPS      LARGEINT,
        QUERY_CACHE_SIZE            LARGEINT,
        QUERY_CACHE_HWTR_MARK       LARGEINT,
        QUERY_CACHE_HITS            LARGEINT,
        QUERY_CACHE_LOOKUPS         LARGEINT,
        HISTOGRAM_CACHE_SIZE        LARGEINT,
        HISTOGRAM_CACHE_HWTR_MARK   LARGEINT,
        HISTOGRAM_CACHE_HITS        LARGEINT,
        HISTOGRAM_CACHE_LOOKUPS     LARGEINT,
        NUM_QUERIES_COMPILED        LARGEINT,
        NUM_FAILED_QUERIES          LARGEINT,
        NUM_CAUGHT_EXCEPTIONS       LARGEINT,
        NUM_RECOMPILES              LARGEINT,
        COMPILER_INFO               VARCHAR(256) CHARACTER SET UCS2,
        primary key (LOGGED_AT_LCT_TS, COMPILER_ID)
     );

     ------------------------------------------------------ */

  // the pointer to the process info for this tracker
  CmpProcess *p = processInfo_;
  char beginTime[100];
  char endTime[100]; 
  char compilerId[COMPILER_ID_LEN];

  CMPASSERT( NULL != p );

  NAString dmlprep = "insert into %s values("; // INSERT INTO TABLE VALUES
  dmlprep += "timestamp '%s'";           // LOGGED_AT_LCT_TS
  dmlprep += ",'%s'";                    // COMPILER_ID
  dmlprep += ",%d";                      // PROCESS_ID
  dmlprep += ",timestamp '%s'";          // INTERVAL_START_LCT_TS
  dmlprep += ",%d";                      // INTERVAL_PATH_LEN
  dmlprep += ",%d";                      // LONGEST_COMPILE_PATH
  dmlprep += ",%d";                      // COMPILER_AGE
  dmlprep += ",%d";                      // NUM_SESSIONS
  dmlprep += ",%d";                      // STMT_HEAP_HWTR_MARK
  dmlprep += ",%d";                      // CONTEXT_HEAP_SIZE
  dmlprep += ",%d";                      // CONTEXT_HEAP_HWTR_MARK
  dmlprep += ",%d";                      // SYSTEM_HEAP_SIZE
  dmlprep += ",%d";                      // SYSTEM_HEAP_HWTR_MARK
  dmlprep += ",%d";                      // METADATA_CACHE_SIZE
  dmlprep += ",%d";                      // METADATA_CACHE_HWTR_MARK
  dmlprep += ",%d";                      // METADATA_CACHE_HITS
  dmlprep += ",%d";                      // METADATA_CACHE_LOOKUPS
  dmlprep += ",%d";                      // QUERY_CACHE_SIZE
  dmlprep += ",%d";                      // QUERY_CACHE_HWTR_MARK
  dmlprep += ",%d";                      // QUERY_CACHE_HITS
  dmlprep += ",%d";                      // QUERY_CACHE_LOOKUPS
  dmlprep += ",%d";                      // HISTOGRAM_CACHE_SIZE
  dmlprep += ",%d";                      // HISTOGRAM_CACHE_HWTR_MARK
  dmlprep += ",%d";                      // HISTOGRAM_CACHE_HITS
  dmlprep += ",%d";                      // HISTOGRAM_CACHE_LOOKUPS
  dmlprep += ",%d";                      // NUM_QUERIES_COMPILED
  dmlprep += ",%d";                      // NUM_FAILED_QUERIES
  dmlprep += ",%d";                      // NUM_CAUGHT_EXCEPTIONS
  dmlprep += ",%d";                      // NUM_RECOMPILES
  dmlprep += ",'%s'";                      // COMPILER_INFO
  dmlprep += ");";                                  

  getTimestampAsBuffer(beginIntervalTime(), beginTime);
  getTimestampAsBuffer(endIntervalTime(), endTime);  
  p->getCompilerId(compilerId, COMPILER_ID_LEN);
  NAString tableName = ActiveSchemaDB()->getDefaultSchema().getSchemaNameAsAnsiString() + "." + COMPILER_TRACKING_TABLE_NAME_PRIVATE;
  //
  // update the fields
  char dmlbuffer[8192];  
  str_sprintf( dmlbuffer, 
           (const char*)dmlprep,
           tableName.data(),
           endTime,
           compilerId,
           p->getPin(),
           beginTime,
           cpuPathLength(),
           longestCompile(),
           compilerAge(),
           sessionCount(), 
           stmtHeapIntervalWaterMark(),
           cxtHeapCurrentSize(),
           cxtHeapIntervalWaterMark(),
           p->getCurrentSystemHeapSize(),
           systemHeapIntervalWaterMark(),
           metaDataCacheCurrentSize(),
           metaDataCacheIntervalWaterMark(),
           metaDataCacheHits(),
           metaDataCacheLookups(),
           qCacheCurrentSize(),
           qCacheIntervalWaterMark(),
           qCacheHits(),
           qCacheLookups(),
           hCacheCurrentSize(),
           hCacheIntervalWaterMark(),
           hCacheHits(),
           hCacheLookups(),
           successfulQueryCount(),
           failedQueryCount(),
           caughtExceptionCount(),
           qCacheRecompiles(),
           compilerInfo());

  ExeCliInterface cliInterface(
       CmpCommon::statementHeap(),
       0,
       0, 
       CmpCommon::context()->sqlSession()->getParentQid());

  if( cliInterface.beginWork() >= 0 )
  {
    if( cliInterface.executeImmediate(dmlbuffer) >= 0 )
      cliInterface.commitWork();
    else
      cliInterface.rollbackWork();
  }
}