summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cmscore/util/StatsSubsystem.java
blob: 222964261a9d1c90be7f0eb88785646ab13c887d (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// --- BEGIN COPYRIGHT BLOCK ---
// This program 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; version 2 of the License.
//
// This program 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 this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
package com.netscape.cmscore.util;

import java.util.Date;
import java.util.Hashtable;
import java.util.Vector;

import com.netscape.certsrv.apps.CMS;
import com.netscape.certsrv.base.EBaseException;
import com.netscape.certsrv.base.IConfigStore;
import com.netscape.certsrv.base.ISubsystem;
import com.netscape.certsrv.util.IStatsSubsystem;
import com.netscape.certsrv.util.StatsEvent;

/**
 * A class represents a internal subsystem. This subsystem
 * can be loaded into cert server kernel to perform
 * statistics collection.
 * <P>
 * 
 * @author thomask
 * @version $Revision$, $Date$
 */
public class StatsSubsystem implements IStatsSubsystem {
    private String mId = null;
    private StatsEvent mAllTrans = new StatsEvent(null);
    private Date mStartTime = new Date();
    private Hashtable<String, Vector<StatsMilestone>> mHashtable = new Hashtable<String, Vector<StatsMilestone>>();

    /**
     * Constructs a certificate server.
     */
    public StatsSubsystem() {
        super();
    }

    /**
     * Retrieves subsystem identifier.
     */
    public String getId() {
        return mId;
    }

    public void setId(String id) throws EBaseException {
        mId = id;
    }

    /**
     * Initializes this subsystem with the given
     * configuration store.
     * It first initializes resident subsystems,
     * and it loads and initializes loadable
     * subsystem specified in the configuration
     * store.
     * <P>
     * Note that individual subsystem should be initialized in a separated thread if it has dependency on the
     * initialization of other subsystems.
     * <P>
     * 
     * @param owner owner of this subsystem
     * @param config configuration store
     */
    public synchronized void init(ISubsystem owner, IConfigStore config)
            throws EBaseException {
    }

    public Date getStartTime() {
        return mStartTime;
    }

    public void startTiming(String id) {
        startTiming(id, false /* not the main */);
    }

    public void startTiming(String id, boolean mainAction) {
        Thread t = Thread.currentThread();
        Vector<StatsMilestone> milestones = null;
        if (mHashtable.containsKey(t.toString())) {
            milestones = mHashtable.get(t.toString());
        } else {
            milestones = new Vector<StatsMilestone>();
            mHashtable.put(t.toString(), milestones);
        }
        long startTime = CMS.getCurrentDate().getTime();
        StatsEvent currentST = null;
        for (int i = 0; i < milestones.size(); i++) {
            StatsMilestone se = (StatsMilestone) milestones.elementAt(i);
            if (currentST == null) {
                currentST = mAllTrans.getSubEvent(se.getId());
            } else {
                currentST = currentST.getSubEvent(se.getId());
            }
        }
        if (currentST == null) {
            if (!mainAction) {
                return; /* ignore none main action */
            }
            currentST = mAllTrans;
        }
        StatsEvent newST = currentST.getSubEvent(id);
        if (newST == null) {
            newST = new StatsEvent(currentST);
            newST.setName(id);
            currentST.addSubEvent(newST);
        }
        milestones.addElement(new StatsMilestone(id, startTime, newST));
    }

    public void endTiming(String id) {
        long endTime = CMS.getCurrentDate().getTime();
        Thread t = Thread.currentThread();
        if (!mHashtable.containsKey(t.toString())) {
            return; /* error */
        }
        Vector<StatsMilestone> milestones = mHashtable.get(t.toString());
        if (milestones.size() == 0) {
            return; /* error */
        }
        StatsMilestone last = milestones.remove(milestones.size() - 1);
        StatsEvent st = last.getStatsEvent();
        st.incNoOfOperations(1);
        st.incTimeTaken(endTime - last.getStartTime());
        if (milestones.size() == 0) {
            mHashtable.remove(t.toString());
        }
    }

    public void resetCounters() {
        mStartTime = CMS.getCurrentDate();
        mAllTrans.resetCounters();
    }

    public StatsEvent getMainStatsEvent() {
        return mAllTrans;
    }

    public void startup() throws EBaseException {
    }

    /**
     * Stops this system.
     */
    public synchronized void shutdown() {
    }

    /*
     * Returns the root configuration storage of this system.
     * <P>
     *
     * @return configuration store of this subsystem
     */
    public IConfigStore getConfigStore() {
        return null;
    }
}

class StatsMilestone {
    private String mId = null;
    private long mStartTime = 0;
    private StatsEvent mST = null;

    public StatsMilestone(String id, long startTime, StatsEvent st) {
        mId = id;
        mStartTime = startTime;
        mST = st;
    }

    public String getId() {
        return mId;
    }

    public long getStartTime() {
        return mStartTime;
    }

    public StatsEvent getStatsEvent() {
        return mST;
    }
}