/* Network-Integrated Multimedia Middleware (NMM)
 * http://www.motama.com/nmm.html
 *  
 * Copyright (C) 2006-2007
 * Motama GmbH, Saarbruecken, Germany
 * http://www.motama.com
 *  
 * This file is part of NMM. 
 * 
 * NMM 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; only version 2 of the License.      
 * 
 * NMM 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 NMM; if not, write to the Free Software Foundation,    
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#ifndef _NMM_CPUMONITOR_HPP_
#define _NMM_CPUMONITOR_HPP_

#include "nmm/nmm_prefix.hpp"

#include "nmm/utils/NamedObject.hpp"

#include <string>
#include <fstream>
#include <unistd.h> // needed for pid_t

namespace NMM {

/**
 * This class reads infos about the cpu utilisation of a thread from proc
 */

class CPUMonitor : public NamedObject {

    /**
     * Checks if a thread exists by trying to open its file in proc
     */
    bool isThreadExisting();

    pid_t thread_pid;
    char proc_cpu_file[32];
    ifstream* cpu_info;

    enum modes_t {CPU,STAT} mode; // read usage data from /proc/.../cpu or /proc/.../stat

  public:

    CPUMonitor(pid_t thread_pid);
    ~CPUMonitor();

    /**
     * \returns how many jiffies the thread occudpied the cpu (user time) or 0 if there was no thread with the specified pid
     */
    const unsigned int getUserTime();

    /**
     * \returns how many jiffies the system was busy because of the thread (system time) or 0 if there was no thread with the specified pid
     */
    const unsigned int getSystemTime();

    /**
     * \returns system and user time of the monitored thread
     */
    const unsigned int getBusyTime() {
        //  (getSystemTime() + getUserTime())
        return (getSystemTime() + getUserTime());
    }

    const pid_t getPID() {
        return thread_pid;
    }

};

}

#include "nmm/nmm_suffix.hpp"

#endif
