BVB Source Codes

CRYENGINE Show CodeCoverageManager.cpp Source code

Return Download CRYENGINE: download CodeCoverageManager.cpp Source code - Download CRYENGINE Source code - Type:.cpp
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. // -------------------------------------------------------------------------
  4. //  File name:   CodeCoverageManager.cpp
  5. //  Created:     18/06/2008 by Matthew
  6. //  Description: High-level manager class for code coverage system
  7. //               including file handing of checkpoint lists
  8. // -------------------------------------------------------------------------
  9. //  History:
  10. //
  11. ////////////////////////////////////////////////////////////////////////////
  12.  
  13. /*
  14.  * File format specification:
  15.  *   The "Code Coverage Context" file format is a text file defined as follows:
  16.  *   - The first line is a numeric string giving the number of checkpoints listed in this file
  17.  *   - All subsequent lines are a single alphanumeric string (no character restrictions so far)
  18.  *   - Each line is delimited by a DOS or Unix newline and is limited to 64 character excluding the delimiter
  19.  *
  20.  * Design notes:
  21.  *   We're only interested in checkpoints that we haven't hit yet
  22.  *   Hence we could wait until we have 90% of checkpoints and then load the context, keeping only new labels.
  23.  *   Could be worth file specifying total buffer size required.
  24.  */
  25.  
  26. #include "StdAfx.h"
  27.  
  28. #if !defined(_RELEASE)
  29.  
  30.         #include "CodeCoverageManager.h"
  31.  
  32. const static int LABEL_LENGTH = 64;
  33. const static int BUFF_SIZE = LABEL_LENGTH + 3;
  34.  
  35. //---------------------------------------------------------------------------------------//
  36.  
  37. CCodeCoverageManager::CCodeCoverageManager() :
  38.         m_bContextValid(false)
  39.         , m_pLabelBlock(NULL)
  40.         , m_nTotalCheckpoints(0)
  41. {
  42.         assert(GetTracker());
  43. }
  44.  
  45. //---------------------------------------------------------------------------------------//
  46.  
  47. bool CCodeCoverageManager::ReadCodeCoverageContext(FILE* fp)
  48. {
  49.         assert(fp);
  50.  
  51.         // Temporary buffer
  52.         char cBuffer[BUFF_SIZE];
  53.  
  54.         // Clear any packed string block
  55.         SAFE_DELETE(m_pLabelBlock);
  56.  
  57.         char* pNextLabelInBlock = NULL;
  58.  
  59.         // Clear all state in manager
  60.  
  61.         bool bOk = true;
  62.  
  63.         // Allow a comment to start file?
  64.  
  65.         // Read number of checkpoints
  66.         m_nTotalCheckpoints = 0;
  67.         bOk = bOk && (GetLine(cBuffer, fp) > 0);
  68.         if (bOk)
  69.         {
  70.                 // Grab as an integer and sanity check
  71.                 m_nTotalCheckpoints = atoi(cBuffer);
  72.                 bOk = (m_nTotalCheckpoints > 0 && m_nTotalCheckpoints < 1000000);
  73.                 assert(bOk);
  74.         }
  75.  
  76.         if (bOk)
  77.         {
  78.                 // Allocate packed string block (assuming worst-case of max length labels + \0)
  79.                 m_pLabelBlock = new char[m_nTotalCheckpoints * (BUFF_SIZE + 1)];
  80.                 pNextLabelInBlock = m_pLabelBlock;
  81.         }
  82.  
  83.         for (int i = 0; i < m_nTotalCheckpoints && bOk; ++i)
  84.         {
  85.                 // Get next checkpoint label
  86.                 int nCharsRead = GetLine(pNextLabelInBlock, fp);
  87.                 bOk = nCharsRead > 0;
  88.  
  89.                 if (bOk)
  90.                 {
  91.                         // Do a little check for practicality's sake
  92.                         if (strncmp(pNextLabelInBlock, "AI_", 3) != 0)
  93.                                 AIWarning("CodeCoverageManager: So far only the AI system uses code coverage - and this label doesn't start AI_. \"%s\"", pNextLabelInBlock);
  94.  
  95.                         // Add to set
  96.                         m_setCheckPoints.insert(pNextLabelInBlock);
  97.  
  98.                         // Advance pointer
  99.                         pNextLabelInBlock += nCharsRead;
  100.                 }
  101.         }
  102.  
  103.         m_bContextValid = bOk;
  104.  
  105.         if (bOk)
  106.         {
  107.                 AILogComment("CodeCoverageManager: Read all %d code coverage checkpoints successfully", m_nTotalCheckpoints);
  108.                 // Does this finish initialisation?
  109.                 Update();
  110.         }
  111.         else
  112.         {
  113.                 AIWarning("CodeCoverageManager: Failed! Found file but failed after reading %d code coverage checkpoints", m_nTotalCheckpoints);
  114.         }
  115.  
  116.         return bOk;
  117. }
  118.  
  119. //---------------------------------------------------------------------------------------//
  120.  
  121. void CCodeCoverageManager::GetRemainingCheckpointLabels(std::vector<const char*>& vLabels) const
  122. {
  123.         vLabels.clear();
  124.         vLabels.reserve(m_nTotalCheckpoints - GetTotalRegistered());
  125.  
  126.         for (CheckPointSet::const_iterator it = m_setCheckPoints.begin(); it != m_setCheckPoints.end(); ++it)
  127.         {
  128.                 CCodeCoverageCheckPoint* pCP = GetTracker()->GetCheckPoint(*it);
  129.                 if (!pCP)
  130.                         vLabels.push_back(*it);
  131.         }
  132. }
  133.  
  134. //---------------------------------------------------------------------------------------//
  135.  
  136. int CCodeCoverageManager::GetLine(char* pBuff, FILE* fp)
  137. {
  138.         assert(pBuff && fp);
  139.  
  140.         // Wraps fgets to remove newlines and use correct buffer/string lengths
  141.  
  142.         // Must use CryPak FGets on CryPak files
  143.         if (!gEnv->pCryPak->FGets(pBuff, BUFF_SIZE, fp))
  144.                 return 0;
  145.  
  146.         // Convert newlines to \0 and discover length
  147.         int i = 0;
  148.         int nLimit = LABEL_LENGTH + 1;
  149.         for (; i < nLimit; i++)
  150.         {
  151.                 char c = pBuff[i];
  152.                 if (c == '\n' || c == '\r' || c == '\0')
  153.                         break;
  154.         }
  155.  
  156.         // If i == LABEL_LENGTH, the string was of maximum length
  157.         // If i == LABEL_LENGTH + 1, the string was too long
  158.         if (i == nLimit)
  159.         {
  160.                 // Malformed - fail
  161.                 pBuff[--i] = '\0';
  162.                 AIWarningID("<CCodeCoverageManager>", "Checkpoint label %s... in code coverage context file is too long", pBuff);
  163.                 return 0;
  164.         }
  165.  
  166.         // Overwrite the last character (usually line terminator) with string delimiter
  167.         pBuff[i] = '\0';
  168.  
  169.         if (i == 0)
  170.         {
  171.                 // Malformed - fail
  172.                 AIWarningID("<CCodeCoverageManager>", "Empty line in code coverage context file");
  173.                 return 0;
  174.         }
  175.  
  176.         return i + 1;
  177. }
  178.  
  179. //---------------------------------------------------------------------------------------//
  180.  
  181. void CCodeCoverageManager::Update(void)
  182. {
  183.         const CheckPointVector& lst = GetTracker()->GetRecentCheckpoints();
  184.         for (CheckPointVector::const_iterator it = lst.begin(); it != lst.end(); ++it)
  185.         {
  186.                 if (m_setCheckPoints.find((*it)->GetLabel()) != m_setCheckPoints.end())
  187.                 {
  188.                         // add to map
  189.                         GetTracker()->AddCheckpoint(*it);
  190.                 }
  191.                 else
  192.                 {
  193.                         m_setUnexpectedCheckPoints.insert((*it)->GetLabel());
  194.                 }
  195.         }
  196.  
  197.         GetTracker()->ResetRecentCheckpoints();
  198. }
  199.  
  200. void CCodeCoverageManager::DumpCheckpoints(bool bToFile) const
  201. {
  202.         if (bToFile)
  203.         {
  204.                 const char* sDumpFile = "DumpedPoints.txt";
  205.                 // Should really be in the level folder, when process is mature
  206.                 string filePath = PathUtil::Make(PathUtil::GetGameFolder(), sDumpFile);
  207.                 FILE* stream = gEnv->pCryPak->FOpen(filePath.c_str(), "w");
  208.  
  209.                 // Print the unexpected checkpoints we've hit
  210.                 gEnv->pCryPak->FPrintf(stream, "Unexpected Checkpoints Hit: %" PRISIZE_T "\n", m_setUnexpectedCheckPoints.size());
  211.                 for (CheckPointSet::const_iterator it = m_setUnexpectedCheckPoints.begin(); it != m_setUnexpectedCheckPoints.end(); ++it)
  212.                         gEnv->pCryPak->FPrintf(stream, "%s\n", *it);
  213.  
  214.                 // If we've hit more than 80% of the expected checkpoints, then print the remaining ones
  215.                 if (m_setCheckPoints.size() * .8f < (float)GetTracker()->GetTotalRegistered())
  216.                 {
  217.                         gEnv->pCryPak->FPrintf(stream, "Remaining Checkpoints: %" PRISIZE_T "\n", m_setUnexpectedCheckPoints.size());
  218.  
  219.                         std::vector<const char*> vec;
  220.                         GetRemainingCheckpointLabels(vec);
  221.  
  222.                         for (std::vector<const char*>::iterator it = vec.begin(); it != vec.end(); ++it)
  223.                                 gEnv->pCryPak->FPrintf(stream, "%s\n", *it);
  224.                 }
  225.                 gEnv->pCryPak->FClose(stream);
  226.                 AILogAlways("Dumped checkpoints to file \"%s\"", filePath.c_str());
  227.         }
  228.         else
  229.         {
  230.                 // Print the unexpected checkpoints we've hit
  231.                 gEnv->pLog->Log("CC:Unexpected Checkpoints Hit:");
  232.                 for (CheckPointSet::const_iterator it = m_setUnexpectedCheckPoints.begin(); it != m_setUnexpectedCheckPoints.end(); ++it)
  233.                         gEnv->pLog->Log("  CC:%s", *it);
  234.  
  235.                 // If we've hit more than 30% of the expected checkpoints, then print the remaining ones
  236.                 // If not, probably this dump was done too early
  237.                 if (m_setCheckPoints.size() * .3f < (float)GetTracker()->GetTotalRegistered())
  238.                 {
  239.                         gEnv->pLog->Log("CC:Remaining Checkpoints:");
  240.  
  241.                         for (CheckPointSet::const_iterator it = m_setCheckPoints.begin(); it != m_setCheckPoints.end(); ++it)
  242.                         {
  243.                                 CCodeCoverageCheckPoint* pCP = GetTracker()->GetCheckPoint(*it);
  244.                                 if (!pCP)
  245.                                 {
  246.                                         gEnv->pLog->Log("  CC:%s", *it);
  247.                                 }
  248.                         }
  249.  
  250.                         gEnv->pLog->Log("CC:Checkpoints Hit:");
  251.  
  252.                         for (CheckPointSet::const_iterator it = m_setCheckPoints.begin(); it != m_setCheckPoints.end(); ++it)
  253.                         {
  254.                                 CCodeCoverageCheckPoint* pCP = GetTracker()->GetCheckPoint(*it);
  255.                                 if (pCP)
  256.                                 {
  257.                                         gEnv->pLog->Log("  CC:%s", *it);
  258.                                 }
  259.                         }
  260.  
  261.                 }
  262.                 AILogAlways("Dumped checkpoints to log");
  263.         }
  264. }
  265.  
  266. #endif //_RELEASE
  267.  
downloadCodeCoverageManager.cpp Source code - Download CRYENGINE Source code
Related Source Codes/Software:
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top