Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

RMI for C++

, 6 Aug 2009
User-friendly remote method invocation in C++.
rcf.zip
RCF
demo
vs2003
RCF
Client
Server
doc
include
RCF
Connection.inl
Marshal.inl
Multiplexer.inl
RCF.inl
RcfServer.inl
ServerStub.inl
Protocol
util
Platform
Compiler
bcc55
cw80
gcc32
icl70
msvc71
Library
bcc55
cw80
gcc32
msvc60
msvc71
Machine
SPARC
x86
OS
Cygwin
Unix
Windows
System
NonWindows
Windows
Threads
SF
src
RCF
Protocol
util
SF
test
Jamfile
Jamrules
borland
Jamfile
Jamrules
vs2003
RCF
RCF
RCFTest
//*****************************************************************************
// Copyright (c) 2003. All rights reserved.
// Developed by Jarl Lindrud.
// Contact: jlindrud@hotmail.com .
//*****************************************************************************

#ifndef _UTIL_PROFILE_HPP_
#define _UTIL_PROFILE_HPP_

#include <sys/types.h>
#include <sys/timeb.h>

#include <map>
#include <memory>
#include <sstream>
#include <vector>

#include "AutoRun.hpp"
#include "ThreadLibrary.hpp"
#include "Trace.hpp"

namespace util {

    class ProfilingResults 
    {
    private:
        ProfilingResults() : profileTraceChannel("Profile") 
        {
            profileTraceChannel.setTraceTarget("stdout");
        }
    public:
        static ProfilingResults &getSingleton()
        {
            static ProfilingResults *pProfilingResults = new ProfilingResults;
            return *pProfilingResults;
        }
        static void deleteSingleton()
        {
            delete &getSingleton();
        }
        void dump()
        {
            Platform::Threads::mutex::scoped_lock lock(mMutex);
            for (unsigned int i=0; i<mResults.size(); i++) 
            {
                std::ostringstream ostr;
                ostr << "*************************\n";
                ostr << "Profiling results for thread #" << i << ":\n";
                
                for (DataT::iterator iter_i = mResults[i]->begin(); iter_i != mResults[i]->end(); iter_i++) {
                    if (!(*iter_i).first.empty()) {
                        ostr << (*iter_i).first << ": " << (*iter_i).second.first / 1000.0 << " s.\n";
                        SubDataT &subData = (*iter_i).second.second;
                        for (SubDataT::iterator iter_j = subData.begin(); iter_j != subData.end(); iter_j++)
                            ostr << "    " << (*iter_j).first << ": " << (*iter_j).second / 1000.0 << "s.\n";
                    }
                }

                ostr << "*************************\n";
                profileTraceChannel.trace(ostr.str());
            }
        }

    private:
        friend class ProfilingData;
        typedef std::map<std::string, unsigned int> SubDataT;
        typedef std::map<std::string, std::pair<unsigned int, SubDataT > > DataT;

        Platform::Threads::mutex mMutex;
        std::vector< DataT * > mResults;
        TraceChannel profileTraceChannel;

        void add( DataT *data ) 
        {
            Platform::Threads::mutex::scoped_lock scoped_lock( mMutex );
            mResults.push_back( data ); 
        }

    };

    class ProfilingData 
    {
    private:
        ProfilingData() : stack_(100), data_(new DataT()) { ProfilingResults::getSingleton().add( data_ );  }
    public:
        static ProfilingData &getThreadSpecificInstance() 
        {
            static Platform::Threads::thread_specific_ptr<ProfilingData>::Val profilingData;

            if (NULL == profilingData.get()) {
                profilingData.reset(new ProfilingData());
                profilingData->push("");
            }
            return *profilingData;
        }

        void push(std::string sz) 
        { 
            stack_.push_back(sz); 
        }

        void pop() 
        { 
            stack_.pop_back(); 
        }

        void add(unsigned int timespan)
        {
            std::string cur = stack_[stack_.size()-1];
            std::string prev = stack_[stack_.size()-2];
            (*data_)[cur].first += timespan;
            (*data_)[prev].second[cur] += timespan;
        }
        
    private:
        typedef ProfilingResults::DataT DataT;
        std::vector<std::string> stack_;
        DataT *data_;
    };

    inline unsigned int getTickCount() 
    {
        struct timeb tp;
        ftime(&tp);
        return static_cast<unsigned int>( 1000*(tp.time&0x0000ffff) + tp.millitm );
    }

    class Profile 
    {
    public:
        Profile(const std::string &name) : name(name), t0(getTickCount())
        {
            ProfilingData::getThreadSpecificInstance().push(name);
        }

        ~Profile()
        {
            ProfilingData::getThreadSpecificInstance().add(getTickCount() - t0);
            ProfilingData::getThreadSpecificInstance().pop();
        }

    private:
        std::string name;
        int t0;
    };

    UTIL_ON_INIT_NAMED( TraceManager::getSingleton(); ProfilingResults::getSingleton(); , ProfilingResultsInitialize)
    UTIL_ON_DEINIT_NAMED( ProfilingResults::getSingleton().dump(); ProfilingResults::deleteSingleton();,  ProfilingResultsDeinitialize)

} // namespace util

//#define PROFILE(desc) util::Profile profile(desc)

#endif //! _UTIL_PROFILE_HPP_

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Jarl Lindrud

Australia Australia
Software developer, ex-resident of Sweden and now living in Canberra, Australia, working on distributed C++ applications. Jarl enjoys programming, but prefers skiing and playing table tennis. He derives immense satisfaction from referring to himself in third person.

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 6 Aug 2009
Article Copyright 2005 by Jarl Lindrud
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid