aboutsummaryrefslogtreecommitdiff
path: root/gprofng/testsuite/gprofng.display/jsynprog/jsynprog.java
blob: ee22bb971cbf60a456aaf48bba0a1bd9e1a3d566 (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
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
/* Copyright (C) 2021-2023 Free Software Foundation, Inc.
   Contributed by Oracle.

   This file is part of GNU Binutils.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   This program 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 for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, 51 Franklin Street - Fifth Floor, Boston,
   MA 02110-1301, USA.  */

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;
	String commands = "memalloc.add_int.add_double.has_inner_class" + 
	  ".recurse.recursedeep.bounce.array_op.vector_op.sys_op" +
	  ".jni_JavaJavaC.JavaCC.JavaCJava.Launcher";

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

	/* check for invocation parameter */
	for (int i = 0; i < args.length; i++) {
	    if (args[i].equals("-fast")) {
		scale = 10000;
	    } else if (args[i].equals("-slow")) {
		scale = 1;
	    } else if (args[i].equals("-j") && i + 1 < args.length) {
		commands = args[++i];
	    } 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();

	if (commands.indexOf("memalloc") >= 0) {
	    recTime(); 
	    rtn.memalloc(10000, scale);
	    printValue("Routine.memalloc", false);
	}

	/* add integers */
	if (commands.indexOf("add_int") >= 0) {
	    recTime(); 
	    ni = new Integer (rtn.add_int(scale));
	    printValue("Routine.add_int", true);
	}

	/* add double */
	if (commands.indexOf("add_double") >= 0) {
	    recTime(); 
	    Double nd = new Double(rtn.add_double(scale)); 
	    printValue("Routine.add_double", true);
	}

	/* call method in derived class */ 
	if (commands.indexOf("add_int") >= 0) {
	    recTime(); 
	    ni = new Integer (sbrt.add_int(scale));
	    printValue("Sub_Routine.add_int", true);
	}

	/* call method that defines an inner class */ 
	if (commands.indexOf("has_inner_class") >= 0) {
	    recTime(); 
	    Integer[] na = rtn.has_inner_class(scale);
	    printValue("Routine.has_inner_class", true);
	}

	/* recursion */ 
	if (commands.indexOf("recurse") >= 0) {
	    recTime(); 
	    rtn.recurse(0,80, scale);
	    printValue("Routine.recurse", true);
	}

	/* deep recursion */ 
	if (commands.indexOf("recursedeep") >= 0) {
	    recTime(); 
	    rtn.recursedeep(0,500, scale);
	    printValue("<Truncated-stack>", true);
	}

	/* indirect recursion */ 
	if (commands.indexOf("bounce") >= 0) {
	    recTime(); 
	    rtn.bounce(0,20, scale);
	    printValue("Routine.bounce", true);
	}

	/* array operations */ 
	if (commands.indexOf("array_op") >= 0) {
	    recTime(); 
	    rtn.array_op(scale);
	    printValue("Routine.array_op", false);
	}

	/* Vector operations */ 
	if (commands.indexOf("vector_op") >= 0) {
	    recTime(); 
	    rtn.vector_op(scale);
	    printValue("Routine.vector_op", false);
	}

	/* spend time in system calls */ 
	if (commands.indexOf("sys_op") >= 0) {
	    recTime(); 
	    rtn.sys_op(scale);
	    printValue("Routine.sys_op", false);
	}

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

	/* java->c->c */
	if (commands.indexOf("JavaCC") >= 0) {
	    recTime(); 
	    JavaCC(scale);
	    printValue("jsynprog.JavaCC", true);
	}

	/* java->c->java */
	if (commands.indexOf("JavaCJava") >= 0) {
	    recTime(); 
	    JavaCJava(scale);
	    printValue("jsynprog.JavaCJava", true);
	}
     
     
	/* dynamically loaded classes */
	if (commands.indexOf("Launcher") >= 0) {
	    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;
   }
}