Mercurial > hg > graal-compiler
comparison graal/com.oracle.max.base/src/com/sun/max/profile/Clock.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 |
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 /** | |
26 * The {@code Clock} class represents a clock source that has a continuously increasing tick | |
27 * count. A clock can be used to produce a relative timing between events by comparing the | |
28 * number of ticks between events. This can be used to implement time-based profiling | |
29 * by using a clock based on the system time. | |
30 */ | |
31 public abstract class Clock { | |
32 | |
33 public abstract long getTicks(); | |
34 | |
35 /** | |
36 * Gets the resolution of this clock as the number of {@linkplain #getTicks() ticks} per second. | |
37 * @return the resolution of the clock | |
38 */ | |
39 public abstract long getHZ(); | |
40 | |
41 public static final Clock SYSTEM_NANOSECONDS = new SystemNS(); | |
42 public static final Clock SYSTEM_MILLISECONDS = new SystemMS(); | |
43 | |
44 private static class SystemMS extends Clock { | |
45 @Override | |
46 public long getTicks() { | |
47 return System.currentTimeMillis(); | |
48 } | |
49 @Override | |
50 public long getHZ() { | |
51 return 1000; | |
52 } | |
53 } | |
54 | |
55 private static class SystemNS extends Clock { | |
56 @Override | |
57 public long getTicks() { | |
58 return System.nanoTime(); | |
59 } | |
60 @Override | |
61 public long getHZ() { | |
62 return 1000000000; | |
63 } | |
64 } | |
65 | |
66 public static int[] sampleDeltas(Clock clock, int numberOfSamples) { | |
67 final int[] result = new int[numberOfSamples]; | |
68 long sample = clock.getTicks(); | |
69 for (int i = 0; i < numberOfSamples; i++) { | |
70 final long newSample = clock.getTicks(); | |
71 result[i] = (int) (newSample - sample); | |
72 sample = newSample; | |
73 } | |
74 return result; | |
75 } | |
76 | |
77 public static void sample5(Clock clock, long[] samples) { | |
78 final long sample0 = clock.getTicks(); | |
79 final long sample1 = clock.getTicks(); | |
80 final long sample2 = clock.getTicks(); | |
81 final long sample3 = clock.getTicks(); | |
82 final long sample4 = clock.getTicks(); | |
83 samples[0] = sample0; | |
84 samples[1] = sample1; | |
85 samples[2] = sample2; | |
86 samples[3] = sample3; | |
87 samples[4] = sample4; | |
88 } | |
89 } |