[project @ 1996-01-08 20:28:12 by partain]
[ghc.git] / ghc / misc / spat-analysers / spatmain.c
1 #include <stdio.h>
2 #include <varargs.h>
3 #include <sys/time.h>
4 #include <sys/resource.h>
5
6 #define TVTIME(tv) ((tv).tv_sec + (tv).tv_usec / 1e6)
7
8
9 extern char *anal_usage, *anal_version,
10 *shade_bench_path, *shade_ego, *shade_version,
11 *shade_argtrange();
12
13 extern char *ctime();
14 extern int analyze();
15 extern long time();
16 extern void exit(), initialize(), terminate();
17
18
19 FILE *statsfp; /* output stats file */
20 double nina; /* # non-annulled instructions executed */
21
22
23 static double usr, sys, real;
24 static int t_flag,
25 main_stats_analyze();
26 static void main_stats_start(),
27 main_stats_stop();
28
29
30 int
31 shade_main (argc, argv, envp)
32 int argc;
33 char **argv, **envp;
34 {
35 int aargc, ec, i, j, pid = getpid();
36 char **aargv, *cmd = 0, *x;
37
38 argc = shade_splitargs (argv, &aargv, &aargc);
39
40 for (i = j = 1; i < argc; i++)
41 if (argv[i][0] == '-' ||
42 argv[i][0] == '+' && argv[i][1] == 't')
43 switch (argv[i][1]) {
44 case 'c':
45 if (cmd)
46 usage ("too many -c options");
47 if (aargc > 0)
48 usage ("-c not allowed with --");
49 if (argv[i][2] || ++i >= argc)
50 usage
51 ("-c: missing/misplaced command");
52 cmd = argv[i];
53 break;
54 case 'o':
55 if (statsfp)
56 shade_fatal ("too many -o's");
57 if (argv[i][2] || ++i >= argc)
58 usage
59 ("-o: missing/misplaced file name");
60 statsfp = fopen (argv[i], "w");
61 if (!statsfp)
62 usage ("%s: can't open", argv[i]);
63 break;
64 case 't':
65 if (!t_flag++)
66 (void) shade_argtrange (argv[i][0] ==
67 '-' ? "+t," : "-t,");
68 if (x = shade_argtrange (argv[i]))
69 usage ("%s: %s", argv[i], x);
70 /* should print tranges */
71 break;
72 case 'U':
73 usage ("");
74 return (0);
75 case 'V':
76 fprintf (stderr, "%s: version: %s\n",
77 argv[0], anal_version);
78 fprintf (stderr, "shade version: %s\n",
79 shade_version);
80 return (0);
81 default:
82 argv[j++] = argv[i];
83 break;
84 }
85 else argv[j++] = argv[i];
86
87 if (!statsfp)
88 statsfp = stdout;
89
90 argv[argc = j] = 0;
91 initialize (argc, argv, envp);
92
93 main_stats_start();
94
95 if (cmd)
96 ec = shade_sshell (cmd, main_stats_analyze);
97 else if (aargc <= 0)
98 ec = shade_shell (main_stats_analyze);
99 else if (0 > shade_loadp (*aargv, aargv, envp))
100 ec = 1;
101 else ec = main_stats_analyze (aargc, aargv, envp, (char **) 0);
102
103 if (pid == getpid()) {
104 main_stats_stop();
105 terminate();
106 }
107 return (ec);
108 }
109
110
111 usage (va_alist)
112 va_dcl
113 {
114 char *fmt;
115 va_list ap;
116
117 va_start (ap);
118 fmt = va_arg (ap, char *);
119 if (fmt && *fmt) {
120 fprintf (stderr, "%s: ", shade_ego);
121 vfprintf (stderr, fmt, ap);
122 fprintf (stderr, "\n\n");
123 }
124 va_end (ap);
125
126 fprintf (stderr, "usage: %s [-U] [-V] [-o outfile] [+/-t[from],[to]] ",
127 shade_ego);
128 if (anal_usage && *anal_usage)
129 fprintf (stderr, "\\\n\t%s ", anal_usage);
130 fprintf (stderr, "\\\n\t[-c \"command\" | -- bench benchargs]\n");
131
132 exit (1);
133 }
134
135
136 static void
137 getcputime (usr, sys)
138 double *usr, *sys;
139 {
140 struct rusage ru;
141
142 if (-1 == getrusage (RUSAGE_SELF, &ru))
143 *usr = *sys = 0.0;
144 else {
145 *usr = TVTIME (ru.ru_utime) - *usr;
146 *sys = TVTIME (ru.ru_stime) - *sys;
147 }
148 }
149
150
151 static void
152 getrealtime (real)
153 double *real;
154 {
155 struct timeval tv;
156 struct timezone tz;
157
158 tz.tz_dsttime = DST_NONE;
159 tz.tz_minuteswest = 0;
160
161 (void) gettimeofday (&tv, &tz);
162
163 *real = TVTIME (tv) - *real;
164 }
165
166
167 static void
168 main_stats_start()
169 {
170 long start;
171
172 if (statsfp == 0)
173 return;
174
175 fprintf (statsfp, "Analyzer: %s\n", shade_ego);
176 fprintf (statsfp, "Version: %s (shade version: %s)\n",
177 anal_version, shade_version);
178
179 {
180 char host[64];
181
182 if (-1 != gethostname (host, sizeof host))
183 fprintf (statsfp, "Hostname: %s\n", host);
184 }
185
186 (void) time (&start);
187 getrealtime (&real);
188 getcputime (&usr, &sys);
189
190 fprintf (statsfp, "Start: %s", ctime (&start));
191 fflush (statsfp);
192 }
193
194
195 static int
196 main_stats_analyze (argc, argv, envp, iov)
197 int argc;
198 char **argv, **envp, **iov;
199 {
200 int i;
201
202 /* BUG: if t_flag, shouldn't change application program */
203
204 if (statsfp) {
205 fprintf (statsfp, "Application: %s", shade_bench_path);
206 for (i = 1; i < argc; i++)
207 fprintf (statsfp, " %s", argv[i]);
208 if (iov)
209 for (i = 0; iov[i]; i += 2) {
210 fprintf (statsfp, " %s", iov[i]);
211 if (iov[i+1])
212 fprintf (statsfp, " %s", iov[i+1]);
213 }
214 fprintf (statsfp, "\n");
215 fflush (statsfp);
216 }
217
218 return (analyze());
219 }
220
221
222 static void
223 main_stats_stop()
224 {
225 long stop;
226
227 if (statsfp == 0)
228 return;
229
230 (void) time (&stop);
231 getcputime (&usr, &sys);
232 getrealtime (&real);
233
234 fprintf (statsfp, "Stop: %s", ctime (&stop));
235 if (nina > 0)
236 fprintf (statsfp, "Instructions: %.0f\n", nina);
237 fprintf (statsfp, "Time: %.3f usr %.3f sys %.3f real %.3f%%\n",
238 usr, sys, real,
239 real > 0 ? 100. * (usr + sys) / real : 100.);
240 if (usr + sys > 0 && nina > 0)
241 fprintf (statsfp, "Speed: %.3f KIPS\n",
242 nina / (usr + sys) / 1000.);
243 }