mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Nicolas Capponi
28.34.2014 1d5d1a6a4a0a58d6bb4803527dacb6641c027816
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
 * or http://forgerock.org/license/CDDLv1.0.html.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at legal-notices/CDDLv1_0.txt.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Copyright 2006-2008 Sun Microsystems, Inc.
 *      Portions Copyright 2014 ForgeRock AS
 */
package org.opends.server.plugins.profiler;
 
 
 
import java.util.Arrays;
import java.util.HashMap;
 
import org.forgerock.i18n.slf4j.LocalizedLogger;
 
 
/**
 * This class defines a data structure for holding information about a stack
 * frame captured by the Directory Server profiler.  It will contain the class
 * and method name for this frame, the set of line numbers within that method
 * that were captured along with the number of times they were seen, as well as
 * references to subordinate frames that were encountered.
 */
public class ProfileStackFrame
       implements Comparable
{
  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
 
 
 
 
  // The mapping between the line numbers for this stack frame and the
  // number of times that they were encountered.
  private HashMap<Integer,Long> lineNumbers;
 
  // The mapping for subordinate frames.  It is mapped to itself because we
  // use a fuzzy equality comparison and sets do not have a get method that
  // can be used to retrieve a specified object.
  private HashMap<ProfileStackFrame,ProfileStackFrame> subordinateFrames;
 
  // The class name for this stack frame.
  private String className;
 
  // The method name for this stack frame.
  private String methodName;
 
 
 
  /**
   * Creates a new profile stack frame with the provided information.
   *
   * @param  className   The class name for use in this stack frame.
   * @param  methodName  The method name for use in this stack frame.
   */
  public ProfileStackFrame(String className, String methodName)
  {
    this.className  = className;
    this.methodName = methodName;
 
    lineNumbers       = new HashMap<Integer,Long>();
    subordinateFrames = new HashMap<ProfileStackFrame,ProfileStackFrame>();
  }
 
 
 
  /**
   * Retrieves the class name for this stack frame.
   *
   * @return  The class name for this stack frame.
   */
  public String getClassName()
  {
    return className;
  }
 
 
 
  /**
   * Retrieves the method name for this stack frame.
   *
   * @return  The method name for this stack frame.
   */
  public String getMethodName()
  {
    return methodName;
  }
 
 
 
  /**
   * Retrieves the method name for this stack frame in a manner that will be
   * safe for use in an HTML context.  Currently, this simply replaces angle
   * brackets with the appropriate HTML equivalent.
   *
   * @return  The generated safe name.
   */
  public String getHTMLSafeMethodName()
  {
    int length = methodName.length();
    StringBuilder buffer = new StringBuilder(length + 6);
 
    for (int i=0; i < length; i++)
    {
      char c = methodName.charAt(i);
      if (c == '<')
      {
        buffer.append("&lt;");
      }
      else if (c == '>')
      {
        buffer.append("&gt;");
      }
      else
      {
        buffer.append(c);
      }
    }
 
    return buffer.toString();
  }
 
 
 
  /**
   * Retrieves the mapping between the line numbers associated with this method
   * and the number of occurrences for each of those line numbers.
   *
   * @return  The mapping between the line numbers associated with this method
   *          and the number of occurrences for each of those line numbers.
   */
  public HashMap<Integer,Long> getLineNumbers()
  {
    return lineNumbers;
  }
 
 
 
  /**
   * Updates the count for the number of occurrences of a given stack frame
   * for the specified line number.
   *
   * @param  lineNumber      The line number for which to update the count.
   * @param  numOccurrences  The number of times the specified line was
   *                         encountered for this stack frame.
   */
  public void updateLineNumberCount(int lineNumber, long numOccurrences)
  {
    Long existingCount = lineNumbers.get(lineNumber);
    if (existingCount == null)
    {
      lineNumbers.put(lineNumber, numOccurrences);
    }
    else
    {
      lineNumbers.put(lineNumber, existingCount+numOccurrences);
    }
  }
 
 
 
  /**
   * Retrieves the total number of times that a frame with this class and
   * method name was seen by the profiler thread.
   *
   * @return  The total number of times that a frame with this class and method
   *          name was seen by the profiler thread.
   */
  public long getTotalCount()
  {
    long totalCount = 0;
 
    for (Long l : lineNumbers.values())
    {
      totalCount += l;
    }
 
    return totalCount;
  }
 
 
 
  /**
   * Retrieves an array containing the subordinate frames that were seen below
   * this frame in stack traces.  The elements of the array will be sorted in
   * descending order of the number of occurrences.
   *
   * @return  An array containing the subordinate frames that were seen below
   *          this frame in stack traces.
   */
  public ProfileStackFrame[] getSubordinateFrames()
  {
    ProfileStackFrame[] subFrames = new ProfileStackFrame[0];
    subFrames = subordinateFrames.values().toArray(subFrames);
 
    Arrays.sort(subFrames);
 
    return subFrames;
  }
 
 
 
  /**
   * Indicates whether this stack frame has one or more subordinate frames.
   *
   * @return  <CODE>true</CODE> if this stack frame has one or more subordinate
   *          frames, or <CODE>false</CODE> if not.
   */
  public boolean hasSubFrames()
  {
    return (! subordinateFrames.isEmpty());
  }
 
 
 
  /**
   * Recursively processes the frames of the provided stack, adding them as
   * nested subordinate frames of this stack frame.
   *
   * @param  stack           The stack trace to use to obtain the frames.
   * @param  depth           The slot of the next frame to process in the
   *                         provided array.
   * @param  count           The number of occurrences for the provided stack.
   * @param  stacksByMethod  The set of stack traces mapped from method name to
   *                         their corresponding stack traces.
   */
  public void recurseSubFrames(ProfileStack stack, int depth, long count,
                   HashMap<String,HashMap<ProfileStack,Long>> stacksByMethod)
  {
    if (depth < 0)
    {
      return;
    }
 
    String cName = stack.getClassName(depth);
    String mName = stack.getMethodName(depth);
    ProfileStackFrame f = new ProfileStackFrame(cName, mName);
 
    int lineNumber = stack.getLineNumber(depth);
 
    ProfileStackFrame subFrame = subordinateFrames.get(f);
    if (subFrame == null)
    {
      subFrame = f;
      subordinateFrames.put(subFrame, subFrame);
    }
 
    subFrame.updateLineNumberCount(lineNumber, count);
 
 
    String classAndMethod = cName + "." + mName;
    HashMap<ProfileStack,Long> stackMap = stacksByMethod.get(classAndMethod);
    if (stackMap == null)
    {
      stackMap = new HashMap<ProfileStack,Long>();
      stacksByMethod.put(classAndMethod, stackMap);
    }
    stackMap.put(stack, count);
 
    subFrame.recurseSubFrames(stack, (depth-1), count, stacksByMethod);
  }
 
 
 
  /**
   * Retrieves the hash code for this stack frame.  It will be the sum of the
   * hash codes for the class and method name.
   *
   * @return  The hash code for this stack frame.
   */
  @Override
  public int hashCode()
  {
    return (className.hashCode() + methodName.hashCode());
  }
 
 
 
  /**
   * Indicates whether the provided object is equal to this stack frame.  It
   * will be considered equal if it is a profile stack frame with the same class
   * and method name.
   *
   * @param  o  The object for which to make the determination.
   *
   * @return  <CODE>true</CODE> if the provided object may be considered equal
   *          to this stack frame, or <CODE>false</CODE> if not.
   */
  @Override
  public boolean equals(Object o)
  {
    if (o == null)
    {
      return false;
    }
    else if (this == o)
    {
      return true;
    }
 
    try
    {
      ProfileStackFrame f = (ProfileStackFrame) o;
      return (className.equals(f.className) && methodName.equals(f.methodName));
    }
    catch (Exception e)
    {
      logger.traceException(e);
 
      return false;
    }
  }
 
 
 
  /**
   * Indicates the order of this profile stack frame relative to the provided
   * object in a sorted list.  The order will be primarily based on number of
   * occurrences, with an equivalent number of occurrences falling back on
   * alphabetical by class and method names.
   *
   * @param  o  The objectfor which to make the comparison.
   *
   * @return  A negative integer if this stack frame should come before the
   *          provided object in a sorted list, a positive integer if it should
   *          come after the provided object, or zero if they should have
   *          equivalent order.
   *
   * @throws  ClassCastException  If the provided object is not a profile stack
   *                              frame.
   */
  @Override
  public int compareTo(Object o)
         throws ClassCastException
  {
    ProfileStackFrame f = (ProfileStackFrame) o;
 
    long thisCount = getTotalCount();
    long thatCount = f.getTotalCount();
    if (thisCount > thatCount)
    {
      return -1;
    }
    else if (thisCount < thatCount)
    {
      return 1;
    }
 
    int value = className.compareTo(f.className);
    if (value == 0)
    {
      value = methodName.compareTo(f.methodName);
    }
 
    return value;
  }
 
 
 
  /**
   * Retrieves a string representation of this stack frame.  It will contain the
   * total number of matching frames, the class name, and the method name.
   *
   * @return  A string representation of this stack frame.
   */
  @Override
  public String toString()
  {
    StringBuilder buffer = new StringBuilder();
    buffer.append(getTotalCount());
    buffer.append("    ");
    buffer.append(className);
    buffer.append('.');
    buffer.append(methodName);
 
    return buffer.toString();
  }
}