diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.max.base/src/com/sun/max/profile/ContextTree.java	Sat Dec 17 19:59:18 2011 +0100
@@ -0,0 +1,98 @@
+/*
+ * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code 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
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.sun.max.profile;
+
+import com.sun.max.util.timer.*;
+
+/**
+ * This class represents a context in which profiling is performed. Typically,
+ * a profiling context is thread-specific, meaning that it collects metrics such as
+ * execution times on a per-thread basis.
+ */
+public class ContextTree {
+
+    protected static class Node {
+        protected final long id;
+        protected Node sibling;
+        protected Node child;
+        protected Timer timer;
+
+        public Node(long id) {
+            this.id = id;
+        }
+
+        public Node findChild(long searchId) {
+            Node pos = child;
+            while (pos != null) {
+                if (pos.id == this.id) {
+                    return pos;
+                }
+                pos = pos.sibling;
+            }
+            return null;
+        }
+
+        public Node addChild(long searchId, Clock clock) {
+            Node foundChild = findChild(searchId);
+            if (foundChild == null) {
+                foundChild = new Node(searchId);
+                foundChild.timer = new SingleUseTimer(clock);
+                foundChild.sibling = this.child;
+                this.child = foundChild;
+            }
+            return foundChild;
+        }
+    }
+
+    public static final int MAXIMUM_DEPTH = 1024;
+
+    protected final Clock clock;
+    protected final Node[] stack;
+    protected int depth;
+
+    public ContextTree(Clock clock) {
+        this.clock = clock;
+        this.stack = new Node[MAXIMUM_DEPTH];
+        depth = 0;
+        stack[0] = new Node(Long.MAX_VALUE);
+    }
+
+    public void enter(long id) {
+        final Node top = stack[depth];
+        final Node child = top.addChild(id, clock);
+        // push a new profiling node onto the stack
+        stack[++depth] = child;
+        child.timer.start();
+    }
+
+    public void exit(long id) {
+        while (depth > 0) {
+            // pop all profiling nodes until we find the correct ID.
+            final Node top = stack[depth--];
+            top.timer.stop();
+            if (top.id == id) {
+                break;
+            }
+        }
+    }
+}