Xyst test code coverage report
Current view: top level - Main - Init.cpp (source / functions) Hit Total Coverage
Commit: 5689ba12dc66a776d3d75f1ee48cc7d78eaa18dc Lines: 52 59 88.1 %
Date: 2024-11-22 19:17:03 Functions: 6 6 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 72 162 44.4 %

           Branch data     Line data    Source code
       1                 :            : // *****************************************************************************
       2                 :            : /*!
       3                 :            :   \file      src/Main/Init.cpp
       4                 :            :   \copyright 2012-2015 J. Bakosi,
       5                 :            :              2016-2018 Los Alamos National Security, LLC.,
       6                 :            :              2019-2021 Triad National Security, LLC.,
       7                 :            :              2022-2024 J. Bakosi
       8                 :            :              All rights reserved. See the LICENSE file for details.
       9                 :            :   \brief     Common initialization routines for main() functions for multiple
      10                 :            :      exectuables
      11                 :            :   \details   Common initialization routines for main() functions for multiple
      12                 :            :      exectuables. The functions in this file are used by multiple execitables
      13                 :            :      to ensure code-reuse and a uniform screen-output.
      14                 :            : */
      15                 :            : // *****************************************************************************
      16                 :            : 
      17                 :            : #include <ctime>
      18                 :            : #include <unistd.h>
      19                 :            : 
      20                 :            : #include "XystConfig.hpp"
      21                 :            : #include "Exception.hpp"
      22                 :            : #include "Init.hpp"
      23                 :            : 
      24                 :            : namespace tk {
      25                 :            : 
      26                 :        275 : static std::string workdir()
      27                 :            : // *****************************************************************************
      28                 :            : // Wrapper for POSIX API's getcwd() from unistd.h
      29                 :            : //! \return A stirng containing the current working directory
      30                 :            : // *****************************************************************************
      31                 :            : {
      32                 :            :   char cwd[1024];
      33                 :            : 
      34         [ +  - ]:        275 :   if ( getcwd(cwd, sizeof(cwd)) != nullptr )
      35         [ +  - ]:        275 :     return std::string( cwd );
      36                 :            :   else
      37 [ -  - ][ -  - ]:          0 :     Throw( "Error from POSIX API's getcwd()" );
                 [ -  - ]
      38                 :            : }
      39                 :            : 
      40                 :        275 : std::string curtime()
      41                 :            : // *****************************************************************************
      42                 :            : //  Wrapper for the standard C library's gettimeofday() from
      43                 :            : //! \return A stirng containing the current date and time
      44                 :            : // *****************************************************************************
      45                 :            : {
      46                 :            :   time_t current_time;
      47                 :            :   char* c_time_string;
      48                 :            : 
      49                 :            :   // Obtain current time as seconds elapsed since the Epoch
      50                 :        275 :   current_time = time( nullptr );
      51                 :            : 
      52         [ -  + ]:        275 :   if (current_time == static_cast<time_t>(-1))
      53 [ -  - ][ -  - ]:          0 :     Throw( "Failure to compute the current time" );
                 [ -  - ]
      54                 :            : 
      55                 :            :   // Convert to local time format
      56                 :        275 :   c_time_string = ctime(&current_time);
      57                 :            : 
      58         [ -  + ]:        275 :   if (c_time_string == nullptr)
      59 [ -  - ][ -  - ]:          0 :     Throw( "Failure to convert the current time" );
                 [ -  - ]
      60                 :            : 
      61                 :            :   // Convert to std::string and remove trailing newline
      62         [ +  - ]:        275 :   std::string str( c_time_string );
      63 [ +  - ][ +  - ]:        275 :   str.erase( std::remove(str.begin(), str.end(), '\n'), str.end() );
      64                 :            : 
      65                 :        550 :   return str;
      66                 :          0 : }
      67                 :            : 
      68                 :        275 : void echoHeader( HeaderType header )
      69                 :            : // *****************************************************************************
      70                 :            : //  Echo program header
      71                 :            : //! \param[in] header Header type enum indicating which header to print
      72                 :            : // *****************************************************************************
      73                 :            : {
      74                 :        275 :   tk::Print print;
      75         [ +  + ]:        275 :   if ( header == HeaderType::INCITER )
      76         [ +  - ]:        253 :     print.headerInciter();
      77         [ +  + ]:         22 :   else if ( header == HeaderType::UNITTEST )
      78         [ +  - ]:          2 :     print.headerUnitTest();
      79         [ +  - ]:         20 :   else if ( header == HeaderType::MESHCONV )
      80         [ +  - ]:         20 :     print.headerMeshConv();
      81                 :            :   else
      82 [ -  - ][ -  - ]:          0 :     Throw( "Header not available" );
                 [ -  - ]
      83                 :        275 : }
      84                 :            : 
      85                 :        275 : void echoBuildEnv( const std::string& executable )
      86                 :            : // *****************************************************************************
      87                 :            : //  Echo build environment
      88                 :            : //! \details Echo information read from build_dir/Base/Config.h filled by
      89                 :            : //!    CMake based on src/Main/Config.h.in.
      90                 :            : //! \param[in] executable Name of the executable
      91                 :            : // *****************************************************************************
      92                 :            : {
      93                 :        275 :   tk::Print print;
      94 [ +  - ][ +  - ]:        275 :   print.section( "Build environment" );
      95 [ +  - ][ +  - ]:        275 :   print.item( "Hostname", build_hostname() );
                 [ +  - ]
      96 [ +  - ][ +  - ]:        275 :   print.item( "Executable", executable );
      97 [ +  - ][ +  - ]:        275 :   if (!git_commit().empty()) print.item( "Git commit", git_commit() );
         [ +  - ][ +  - ]
                 [ +  - ]
      98 [ +  - ][ +  - ]:        275 :   print.item( "Build type", build_type() );
                 [ +  - ]
      99 [ +  - ][ +  - ]:        275 :   print.item( "C++ compiler", compiler() );
                 [ +  - ]
     100 [ +  - ][ +  - ]:        275 :   print.item( "Build date", build_date() );
                 [ +  - ]
     101                 :        275 : }
     102                 :            : 
     103                 :        275 : void echoRunEnv( int argc, char** argv, int quiescence )
     104                 :            : // *****************************************************************************
     105                 :            : //  Echo runtime environment
     106                 :            : //! \param[in] argc Number of command-line arguments to executable
     107                 :            : //! \param[in] argv C-style string array to command-line arguments to executable
     108                 :            : //! \param[in] quiescence True if quiescence detection is enabled
     109                 :            : // *****************************************************************************
     110                 :            : {
     111                 :        275 :   tk::Print print;
     112 [ +  - ][ +  - ]:        275 :   print.section( "Run-time environment" );
     113                 :            : 
     114 [ +  - ][ +  - ]:        275 :   print.item( "Date, time", curtime() );
                 [ +  - ]
     115 [ +  - ][ +  - ]:        275 :   print.item( "Work directory", workdir() );
                 [ +  - ]
     116 [ +  - ][ +  - ]:        275 :   print.item( "Executable (relative to work dir)", argv[0] );
     117                 :            : 
     118         [ +  - ]:        275 :   print << "Command line arguments: \'";
     119         [ +  + ]:        275 :   if (argc>1) {
     120 [ +  - ][ +  - ]:       1725 :     for (auto i=1; i<argc-1; ++i) print << argv[i] << ' ';
                 [ +  + ]
     121         [ +  - ]:        274 :     print << argv[argc-1];
     122                 :            :   }
     123         [ +  - ]:        275 :   print << "'\n";
     124                 :            : 
     125 [ +  - ][ +  - ]:        275 :   print.item( "Number of processing elements",
     126 [ +  - ][ +  - ]:        550 :               std::to_string( CkNumPes() ) + " (" +
     127 [ +  - ][ +  - ]:       1100 :               std::to_string( CkNumNodes() ) + 'x' +
     128         [ +  - ]:       1100 :               std::to_string( CkNumPes()/CkNumNodes() ) + ')' );
     129 [ +  + ][ +  - ]:        275 :   print.item( "Quiescence detection", quiescence ? "on" : "off" );
                 [ +  - ]
     130                 :        275 : }
     131                 :            : 
     132                 :            : // *****************************************************************************
     133                 :            : //  Finalize function for different executables
     134                 :            : //! \param[in] timer Vector of timers, held by the main chare
     135                 :            : //! \param[in,out] timestamp Vector of time stamps in h:m:s with labels
     136                 :            : //! \param[in] clean True if we should exit with a zero exit code, false to
     137                 :            : //!   exit with a nonzero exit code
     138                 :            : // *****************************************************************************
     139                 :        273 : void finalize( const std::vector< tk::Timer >& timer,
     140                 :            :                std::vector< std::pair< std::string,
     141                 :            :                                        tk::Timer::Watch > >& timestamp,
     142                 :            :                bool clean )
     143                 :            : {
     144                 :            :   try {
     145                 :            : 
     146         [ +  - ]:        273 :     if (!timer.empty()) {
     147 [ +  - ][ +  - ]:        273 :       timestamp.emplace_back( "Total runtime", timer[0].hms() );
     148 [ +  - ][ +  - ]:        273 :       tk::Print().time( "Timers (h:m:s)", timestamp );
     149                 :            :     }
     150                 :            : 
     151 [ +  - ][ -  - ]:        273 :     if (clean) CkExit(); else CkAbort("Failed");
     152                 :            : 
     153         [ -  - ]:          0 :   } catch (...) { tk::processExceptionCharm(); }
     154                 :          0 : }
     155                 :            : 
     156                 :            : } // tk::

Generated by: LCOV version 1.16