summaryrefslogtreecommitdiff
path: root/cpp/src/qpid/sys/ActivityTimer.h
blob: d49e16bc4fdfa6daca64df4f47a172537d8bfcd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#ifndef QPID_SYS_ACTIVITYTIMER_H
#define QPID_SYS_ACTIVITYTIMER_H

/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

#include "qpid/sys/Time.h"
#include "qpid/sys/Thread.h"
#include <boost/current_function.hpp>
#include <stdio.h>

namespace qpid {
namespace sys {

/** 
 * Measures and reports time spent in a particular segment of code.
 * This is real time so it includes time blocked/sleeping as well as time on CPU.
 *
 * Intended to be used via the QPID_ACTIVITY_TIMER macro for profiling
 * during development & debugging
 */
class ActivityTimer
{
  public:

    struct Stat {               // Must be a POD
        uint64_t total, count;
        void sample(uint64_t value) { total += value; ++count; }
        uint64_t mean() { return count ? total/count : 0; }
        void reset() { total = count = 0; }
    };

    struct Data {               // Must be a POD
        uint64_t start, entered;
        Stat active;

        void reset() {
            start = entered = 0;
            active.reset();
        }

        void enter(uint64_t now) {
            entered=now;
            if (!start) start = Duration(now);
        }

        void exit(uint64_t now) {
            active.sample(now - entered);
        }
    };

    ActivityTimer(Data& d, const char* fn, const char* file, int line, uint64_t reportInterval) : data(d) {
        uint64_t now = Duration(qpid::sys::now());
        if (data.start) {
            interval = now-data.start;
            if (interval > reportInterval)
                report(fn, file, line);
        }
        data.enter(now);
    }

    ~ActivityTimer() {
        data.exit(Duration(now()));
    }

  private:
    Data& data;
    uint64_t interval;

    void report(const char* fn, const char* file, int line) {
        long rate = (data.active.count*TIME_SEC)/interval;
        double percent = (data.active.total*100.0)/interval;
        printf("%s:%d: TIMER %ld/sec %f%% [%lu] %s\n",
               file, line, rate, percent, Thread::current().id(), fn);
        data.reset();
    }
};

}} // namespace qpid::sys

/** Measures time between the point of declaration and the end of the innermost enclosing scope.
 * Can only have one in a given scope.
 */
#define ACTIVITY_TIMER(REPORT_INTERVAL_SECS) \
    static __thread ::qpid::sys::ActivityTimer::Data qpid__ActivityTimerData__ = { 0, 0, { 0,0 }}; \
    ::qpid::sys::ActivityTimer qpid__ActivityTimerInstance__(qpid__ActivityTimerData__, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__, 2*::qpid::sys::TIME_SEC)

#endif  /*!QPID_SYS_ACTIVITYTIMER_H*/