summaryrefslogtreecommitdiffstats
path: root/src/libavoid/timer.cpp
blob: f8600acbebaf40f9b7bae8d708d5e62c41a841de (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * vim: ts=4 sw=4 et tw=0 wm=0
 *
 * libavoid - Fast, Incremental, Object-avoiding Line Router
 *
 * Copyright (C) 2004-2008  Monash University
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * See the file LICENSE.LGPL distributed with the library.
 *
 * Licensees holding a valid commercial license may use this file in
 * accordance with the commercial license agreement provided with the 
 * library.
 *
 * This library 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.  
 *
 * Author(s):   Michael Wybrow <mjwybrow@users.sourceforge.net>
*/


#include <cstdio>
#include <cstdlib>
#include <climits>

#include "libavoid/timer.h"
#include "libavoid/debug.h"
#include "libavoid/assertions.h"

namespace Avoid {


Timer::Timer()
{
    Reset();
}


void Timer::Reset(void)
{
    for (int i = 0; i < tmCount; i++)
    {
        //tTotal[i] = 0;
        cTotal[i] = cPath[i] = 0;
        cTally[i] = cPathTally[i] = 0;
        cMax[i] = cPathMax[i] = 0;
    }
    running = false;
    count  = 0;
    type = lasttype = tmNon;
}


void Timer::Register(const TimerIndex t, const bool start)
{
    COLA_ASSERT(t != tmNon);

    if (type == tmNon)
    {
        type = t;
    }
    else
    {
        type = tmSev;
    }

    if (start)
    {
        Start();
    }
}

void Timer::Start(void)
{
    COLA_ASSERT(!running);
    cStart[type] = clock();  // CPU time
    running = true;
}


void Timer::Stop(void)
{
    COLA_ASSERT(running);
    clock_t cStop = clock();      // CPU time
    running = false;

    bigclock_t cDiff;
    if (cStop < cStart[type])
    {
        // Uh-oh, the clock value has wrapped around.
        //
        bigclock_t realStop = ((bigclock_t) cStop) + ULONG_MAX + 1;
        cDiff = realStop - cStart[type];
    }
    else
    {
        cDiff = cStop - cStart[type];
    }
    
    COLA_ASSERT(cDiff < LONG_MAX);

    if (type == tmPth)
    {
        cPath[lasttype] += cDiff;
        cPathTally[lasttype]++;
        if (((clock_t) cDiff) > cPathMax[lasttype])
        {
            cPathMax[lasttype] = (clock_t) cDiff;
        }
    }
    else
    {
        cTotal[type] += cDiff;
        cTally[type]++;
        if (((clock_t) cDiff) > cMax[type])
        {
            cMax[type] = (clock_t) cDiff;
        }
        lasttype = type;
    }

    type = tmNon;
}


void Timer::PrintAll(FILE *fp)
{
    for (unsigned int i = 0; i < tmCount; i++)
    {
        Print((TimerIndex) i, fp);
    }
}


#define toMsec(tot) ((bigclock_t) ((tot) / (((double) CLOCKS_PER_SEC) / 1000)))
#define toAvg(tot, cnt) ((((cnt) > 0) ? ((long double) (tot)) / (cnt) : 0))

void Timer::Print(const TimerIndex t, FILE *fp)
{
   bigclock_t avg = toMsec(toAvg(cTotal[t], cTally[t]));
   bigclock_t pind = toMsec(toAvg(cPath[t], cPathTally[t]));
   bigclock_t pavg = toMsec(toAvg(cPath[t], cTally[t]));
   double max = toMsec(cMax[t]); 
   double pmax = toMsec(cPathMax[t]);
   fprintf(fp, "\t%lld %d %lld %.0f %lld %d %lld %.0f %lld\n",
           cTotal[t], cTally[t], avg, max,
           cPath[t], cPathTally[t], pavg, pmax, pind);
}


}