aboutsummaryrefslogtreecommitdiff
path: root/gprofng/testsuite/gprofng.display/jsynprog/jsynprog.java
blob: eb98b5ea7fd930ac625bf83c4a4c699ad950e7f3 (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
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
// Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.
// @(#)jsynprog.java SMI

import java.util.*;
import java.io.*;
import java.text.*;

class jsynprog 
{
    private static String	dir_home;
    private static PrintWriter	log;
    private static double 	pstart, cstart;

    /* JNI calls */
    public  static native double Timer();
    private static native double cTimer();
    private static native double computeSet();
    private static native int JavaJavaC(int np, int scale);
    private static native void JavaCC(int scale);
    private static native void JavaCJava(int scale);
    private static native int isJVMPI();

    public static double testtime = 3.0 * 1e9;

    public static void main (String [] args)
    {
	jsynprog jsyn_obj = new jsynprog();
	Integer ni;
	int scale = 1000;

	createAcct();
	LoadJNILibrary(args);
        testtime = computeSet();

	/* check for invocation parameter */
	if (args.length != 0) {
	    if (args[0].equals("fast")) {
		scale = 10000;
	    } else if (args[0].equals("slow")) {
		scale = 1;
	    } else {
		System.err.println("fatal: unexpected argument: " + args[0] );
		System.exit(1);
	    }
	}

	/* large memory allocations, trigger gc */
	Routine rtn = new Routine();
	Sub_Routine sbrt = new Sub_Routine();
	recTime(); 
	rtn.memalloc(10000, scale);
	printValue("Routine.memalloc", false);

	/* add integers */
	recTime(); 
	ni = new Integer (rtn.add_int(scale));
	printValue("Routine.add_int", true);

	/* add double */
	recTime(); 
	Double nd = new Double(rtn.add_double(scale)); 
	printValue("Routine.add_double", true);

	/* call method in derived class */ 
	recTime(); 
	ni = new Integer (sbrt.add_int(scale));
	printValue("Sub_Routine.add_int", true);

	/* call method that defines an inner class */ 
	recTime(); 
	Integer[] na = rtn.has_inner_class(scale);
	printValue("Routine.has_inner_class", true);

	/* recursion */ 
	recTime(); 
	rtn.recurse(0,80, scale);
	printValue("Routine.recurse", true);

	/* deep recursion */ 
	recTime(); 
	rtn.recursedeep(0,500, scale);
	printValue("<Truncated-stack>", true);

	/* indirect recursion */ 
	recTime(); 
	rtn.bounce(0,20, scale);
	printValue("Routine.bounce", true);

	/* array operations */ 
	recTime(); 
	rtn.array_op(scale);
	printValue("Routine.array_op", false);

	/* Vector operations */ 
	recTime(); 
	rtn.vector_op(scale);
	printValue("Routine.vector_op", false);

	/* spend time in system calls */ 
	recTime(); 
	rtn.sys_op(scale);
	printValue("Routine.sys_op", false);

	/* java->java->c */
	recTime(); 
	int np = 0;
	jni_JavaJavaC(np, scale);
	printValue("jsynprog.jni_JavaJavaC", true);

	/* java->c->c */
	recTime(); 
	JavaCC(scale);
	printValue("jsynprog.JavaCC", true);

	/* java->c->java */
	recTime(); 
	JavaCJava(scale);
	printValue("jsynprog.JavaCJava", true);
     
     
	/* dynamically loaded classes */
	String java_ver = System.getProperty("java.version");
	Launcher lnch = new Launcher();
	String[] params = new String[]{"DynLoadedClass"};
	recTime();
	lnch.main(params);
	printValue("Launcher.main", true);

	System.gc();
   }

   /* 
    ** Create accounting file 
    */
   private static void createAcct() {
	System.out.println ("Directing output to acct file...");
	try {
	   log = new PrintWriter (new FileWriter("jsynprog.acct"), true);
	} catch (IOException ioe) {
	   ioe.printStackTrace();
	   System.err.println("fatal: Cannot create accounting file ");
	   System.exit(1);
	}

	log.println("X\tLWPTime\tCPUTime\tFunction");
   }

   /* 
    ** Print output in acct file 
    */
   private static void printValue (String fname, boolean noignore) {
	double	p_end = Timer();	 // Global.Timer();
	double	c_end = cTimer();	// Global.cTimer();
	double	prog_elapsed = p_end - pstart;
	double	cpu_elapsed = c_end - cstart;
	DecimalFormat     format_decimal = new DecimalFormat("0.000");

	System.out.println("Running " + fname + "; T = " + format_decimal.format(prog_elapsed * 0.000000001)
		+" UCPU = " + format_decimal.format(cpu_elapsed * 0.000000001));
	log.print( (noignore == true?  "X" : "Y")
		+ "\t" + format_decimal.format(prog_elapsed * 0.000000001) + "\t"
		+ format_decimal.format(cpu_elapsed * 0.000000001) + "\t");
	log.println(fname);
   }

   /*
    ** Record intial times
    */
   private static void recTime() {
	pstart = Timer();	 // Global.Timer();
	cstart = cTimer();	// Global.cTimer();
   }

   /*
    ** Load dynamic shared library for JNI
    */
   private static void LoadJNILibrary(String[] args) {

	try {
	   dir_home = (new File(".")).getCanonicalPath();
	} catch (IOException e) {
	   dir_home = "..";
	}
	System.out.println("libpath:"+dir_home);

	// Find which JVM was invoked
	String jvm_format = System.getProperty("java.vm.name"); 
	System.out.println("jvm "+ jvm_format);
 
	try {
	    System.out.println("Loading library.... " + dir_home + "/libcloop.so");
	    System.load(dir_home + "/libcloop.so");
	} catch (UnsatisfiedLinkError e) {
	   System.err.println("fatal: Cannot load shared library " + e);
	   System.exit(1);
	}
   }

   /*
    ** Makes a lot of JNI calls
    */ 
   private static void jni_JavaJavaC(int np, int scale) {
	int ret = 0;
	int jmax = 10000;
	System.out.println("Entering jni_JavaJavaC, scale = " + scale);
	double tEnd = Timer() + testtime;
	do {
	for (int j =0 ; j<jmax; j++) {
	    ret = JavaJavaC(np, scale);
	}
	} while (Timer() < tEnd);
   }

   public static int javafunc (int scale) {
	int jmax = 200*scale;
	int imax = 40;
	int np = 0;
	// System.out.println("Entering javafunc, scale = " + scale);
	double tEnd = Timer() + testtime;
	do { np = 0;
	for (int j =0 ; j<jmax; j++) {
	    for (int i =0 ; i<imax; i++) {
		np = (i%2==0)?np:(np + 1);
	    }
	}
	} while (Timer() < tEnd);
	return np;
   }
}