comparison graal/com.oracle.max.base/src/com/sun/max/profile/ContextTree.java @ 3733:e233f5660da4

Added Java files from Maxine project.
author Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
date Sat, 17 Dec 2011 19:59:18 +0100
parents
children bc8527f3071c
comparison
equal deleted inserted replaced
3732:3e2e8b8abdaf 3733:e233f5660da4
1 /*
2 * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23 package com.sun.max.profile;
24
25 import com.sun.max.util.timer.*;
26
27 /**
28 * This class represents a context in which profiling is performed. Typically,
29 * a profiling context is thread-specific, meaning that it collects metrics such as
30 * execution times on a per-thread basis.
31 */
32 public class ContextTree {
33
34 protected static class Node {
35 protected final long id;
36 protected Node sibling;
37 protected Node child;
38 protected Timer timer;
39
40 public Node(long id) {
41 this.id = id;
42 }
43
44 public Node findChild(long searchId) {
45 Node pos = child;
46 while (pos != null) {
47 if (pos.id == this.id) {
48 return pos;
49 }
50 pos = pos.sibling;
51 }
52 return null;
53 }
54
55 public Node addChild(long searchId, Clock clock) {
56 Node foundChild = findChild(searchId);
57 if (foundChild == null) {
58 foundChild = new Node(searchId);
59 foundChild.timer = new SingleUseTimer(clock);
60 foundChild.sibling = this.child;
61 this.child = foundChild;
62 }
63 return foundChild;
64 }
65 }
66
67 public static final int MAXIMUM_DEPTH = 1024;
68
69 protected final Clock clock;
70 protected final Node[] stack;
71 protected int depth;
72
73 public ContextTree(Clock clock) {
74 this.clock = clock;
75 this.stack = new Node[MAXIMUM_DEPTH];
76 depth = 0;
77 stack[0] = new Node(Long.MAX_VALUE);
78 }
79
80 public void enter(long id) {
81 final Node top = stack[depth];
82 final Node child = top.addChild(id, clock);
83 // push a new profiling node onto the stack
84 stack[++depth] = child;
85 child.timer.start();
86 }
87
88 public void exit(long id) {
89 while (depth > 0) {
90 // pop all profiling nodes until we find the correct ID.
91 final Node top = stack[depth--];
92 top.timer.stop();
93 if (top.id == id) {
94 break;
95 }
96 }
97 }
98 }