milad lashini
milad lashini

Reputation: 125

How to create automatically a compile time container of strings

I am trying to create a probing tool to measure the various timing aspects of multithreading codes. For this purpose, I have created some recorder objects and as this system uses virtual memory and atomic variable to allocate memory pages to register the information size of the used memory is limited and should be fixed. Every one of these probes has an ID (should be given to constructor) that to make them unique I have used the COUNTER from the compiler. To apply these probes there are Macros that are applied to the under measurement code and they actually create objects and lifetime of these objects are measured time. So no overhead. These probes are given a name and for better and easier usage of these probes information, I need to register these names in some const and static string and later use them to attributes these name to previously cited IDs. in order to archive the goal of no overhead as these probes are measuring tools, a type of const container should be created at compile time containing these names and probably the IDs. Has anybody any idea how such static constant compile-time container could be automatically created while generating Macros of probes could appear anywhere in the code?

In Probe header file:

class str_const {
    // constexpr string
private:
    const char* const p_;
    const std::size_t sz_;
public:
    template<std::size_t N>
    constexpr str_const(const char(&a)[N]) :
    // ctor
    p_(a), sz_(N-1) {}
    constexpr char operator[](std::size_t n) {
        // []
        return n < sz_ ? p_[n] :
                throw std::out_of_range("");
    }
    constexpr std::size_t size() { return sz_; } // size()
};


#define CONCATENATE_DETAIL(x, y) x##y

#define REGISTER_PROBE( name )\
    constexpr str_const  str_##name = #name

#define THREAD_PROBE_DETAIL(n, x) THREADSCOPE::CollectorThread n(x);
#define THREAD_PROBE(x) THREAD_PROBE_DETAIL(CONCATENATE_DETAIL(_threadProbe_,x), __COUNTER__)\
        REGISTER_PROBE(_threadProbe_##x)

#define LOCK_PROBE_DETAIL(n, x) THREADSCOPE::CollectorLock  n(x);
#define LOCK_PROBE_BEGIN(x)  LOCK_PROBE_DETAIL(CONCATENATE_DETAIL(_lockProbe_,x), __COUNTER__)\
        REGISTER_PROBE(_lockProbe_##x)

#define LOCK_PROBE_END(x) CONCATENATE_DETAIL(_lockProbe_,x).end()



later in the code, usage of probes will be like this:

class Worker final : public BASE::ThreadBaseClass
{
private:

public:

    Worker( void )
    {

    }
    virtual ~Worker( void )
    {

    }

    void test()
    {
        THREAD_PROBE(deepfoo);
    }

protected:

    void handle_() override
    {
        while(loopVariable_.load())
        {

            THREAD_PROBE(foo);
            std::cout << "value of depth at start of thread is : " << depth;
            test();
            THREAD_PROBE(bar);
            LOCK_PROBE_BEGIN(x);
            std::cout << "value of depth after two of thread is : "<< depth;

            this->waitForNotification_();
            DelayMilSec(1);
                        LOCK_PROBE_END(x);
            std::cout << "I was notified by the master."<<std::endl;
        }
    }

};

what is needed is that names such as foo, bar, deepfoo, and x are automatically are gathered in a static const container (any) for further interpretation of the acquired information. Thanks in advance for your time and help.

Upvotes: 2

Views: 426

Answers (0)

Related Questions