/* resourceusage.cc
****************************************************************************************************************
****************************************************************************************************************

    Copyright (C) 2020, 2021 Askar Almatov

    This file is part of Trafficmeter.

    Trafficmeter 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 3 of the License, or (at your
    option) any later version.

    Trafficmeter 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 Trafficmeter. If not, see
    <http://www.gnu.org/licenses/>.
*/

#include <cstring>

#include "resourceusage.h"

using namespace trafficmeter;

/**************************************************************************************************************/
ResourceUsage::ResourceUsage() :
    isStarted_( false ),
    isStopped_( false ),
    isContinued_( false )
{
}

/**************************************************************************************************************/
void
ResourceUsage::startMeasuring()
{
    getrusage( RUSAGE_SELF, &startUsage_ );
    isStarted_ = true;
    isStopped_ = false;
    isContinued_ = false;
}

/**************************************************************************************************************/
void
ResourceUsage::stopMeasuring()
{
    getrusage( RUSAGE_SELF, &stopUsage_ );
    isStopped_ = true;
}

/**************************************************************************************************************/
void
ResourceUsage::continueMeasuring()
{
    if ( isContinued_ )
    {
        memcpy( &startUsage_, &stopUsage_, sizeof(startUsage_) );
    }

    getrusage( RUSAGE_SELF, &stopUsage_ );
    isContinued_ = true;
}

/**************************************************************************************************************/
double
ResourceUsage::userSeconds() const
{
    if ( !isStarted_ || !(isStopped_ || isContinued_) )
    {
        return 0.0L;
    }

    long    userMicroseconds = stopUsage_.ru_utime.tv_usec - startUsage_.ru_utime.tv_usec;
    long    userSeconds = stopUsage_.ru_utime.tv_sec - startUsage_.ru_utime.tv_sec;

    return userMicroseconds / 1000000.0L + userSeconds;
}

/**************************************************************************************************************/
double
ResourceUsage::systemSeconds() const
{
    if ( !isStarted_ || !(isStopped_ || isContinued_) )
    {
        return 0.0L;
    }

    long    systemMicroseconds = stopUsage_.ru_stime.tv_usec - startUsage_.ru_stime.tv_usec;
    long    systemSeconds = stopUsage_.ru_stime.tv_sec - startUsage_.ru_stime.tv_sec;

    return systemMicroseconds / 1000000.0L + systemSeconds;
}
