1 /* SCCS-info %W% %E% */
2 
3 /*--------------------------------------------------------------------*/
4 /*                                                                    */
5 /*              VCG : Visualization of Compiler Graphs                */
6 /*              --------------------------------------                */
7 /*                                                                    */
8 /*   file:         timelim.c                                          */
9 /*   version:      1.00.00                                            */
10 /*   creation:     14.4.1993                                          */
11 /*   author:       I. Lemke  (...-Version 0.99.99)                    */
12 /*                 G. Sander (Version 1.00.00-...)                    */
13 /*                 Universitaet des Saarlandes, 66041 Saarbruecken    */
14 /*                 ESPRIT Project #5399 Compare                       */
15 /*   description:  Time Limit Management                              */
16 /*   status:       in work                                            */
17 /*                                                                    */
18 /*--------------------------------------------------------------------*/
19 
20 #ifndef lint
21 static char *id_string="$Id: timelim.c,v 1.6 1995/02/09 20:15:52 sander Exp $";
22 #endif
23 
24 /*
25  *   Copyright (C) 1993-2005 Saarland University
26  *
27  *  This program and documentation is free software; you can redistribute
28  *  it under the terms of the  GNU General Public License as published by
29  *  the  Free Software Foundation;  either version 2  of the License,  or
30  *  (at your option) any later version.
31  *
32  *  This  program  is  distributed  in  the hope that it will be useful,
33  *  but  WITHOUT ANY WARRANTY;  without  even  the  implied  warranty of
34  *  MERCHANTABILITY  or  FITNESS  FOR  A  PARTICULAR  PURPOSE.  See  the
35  *  GNU General Public License for more details.
36  *
37  *  You  should  have  received a copy of the GNU General Public License
38  *  along  with  this  program;  if  not,  write  to  the  Free Software
39  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
40  *
41  *  The software is available per anonymous ftp at ftp.cs.uni-sb.de.
42  *  Contact  sander@cs.uni-sb.de  for additional information.
43  */
44 
45 
46 /*
47  * $Log: timelim.c,v $
48  * Revision 1.6  1995/02/09  20:15:52  sander
49  * Portability problem with HPUX.
50  *
51  * Revision 1.2  1995/02/08  11:11:14  sander
52  * Distribution version 1.3.
53  *
54  * Revision 1.1  1994/12/23  18:12:45  sander
55  * Initial revision
56  *
57  */
58 
59 
60 /****************************************************************************
61  * This file is a collection of auxiliary functions that implement the
62  * time limit management. Note that this feature need not to be available
63  * on every computer.
64  * The time limit managements allows to limit the layout time. If the time
65  * limit is exceeded, the layout automatically switches to fast and ugly
66  * layout. Further, a time limit is not a hard limit: E.g. the layout of
67  * 10000 nodes always needs more than 1 second, even if we set the time
68  * limit to 1 second.
69  *
70  * Time limits are real time !!!
71  *
72  * This file provides the following functions:
73  *
74  * init_timelimit       gets the limit and initializes the time limit function,
75  *			in the case that it was not initialized before.
76  * free_timelimit       release the time limit function. This means that the
77  *                      next call of reinit_timelimit recognizes, that the
78  *                      time limit must be initialized again.
79  * test_timelimit       gets the percentual part of the limit and returns
80  *			true, if the time limit is exceeded.
81  ***************************************************************************/
82 
83 
84 #include <stdio.h>
85 #include <stdlib.h>
86 #include "globals.h"
87 #ifndef HPUX
88 #include <sys/time.h>
89 #else
90 #ifdef ANSI_C
91 /* for definitions of timeval and timezone: THIS IS DUE TO A BUG IN THE
92  * HP_UX SOURCES. IF THIS BUG DOES NOT EXIST IN NEWER VERSIONS OF THE
93  * HP_UX INCLUDES, THIS MUST BE REMOVED.
94  */
95      struct timeval {
96           unsigned long tv_sec;
97           long          tv_usec;
98      };
99      struct timezone {
100         int     tz_minuteswest;
101         int     tz_dsttime;
102      };
103    extern int gettimeofday _PP((struct timeval *, struct timezone *));
104 #else
105 #include <sys/time.h>
106 #endif
107 #endif
108 #include "timelim.h"
109 
110 
111 #undef DEBUG
112 #undef debugmessage
113 #ifdef DEBUG
114 #define debugmessage(a,b) {FPRINTF(stderr,"Debug: %s %s\n",a,b);}
115 #else
116 #define debugmessage(a,b) /**/
117 #endif
118 
119 
120 /* Local Variables
121  * ---------------
122  */
123 
124 static unsigned long timelimit = 0L;	/* the actual time limit in sec */
125 
126 static struct timeval  tpxstart;	/* the start time     */
127 static struct timezone tzpxstart;	/* and its time zone  */
128 
129 static struct timeval  tpxend;  	/* the stop time      */
130 static struct timezone tzpxend;  	/* and its time zone  */
131 
132 
133 #ifdef NOTIMELIMIT
134 
135 /*--------------------------------------------------------------------*/
136 /* Time limit functions: Dummy's if the time limit is not available   */
137 /*--------------------------------------------------------------------*/
138 
139 #ifdef ANSI_C
init_timelimit(int x)140 void init_timelimit(int x)
141 #else
142 void init_timelimit(x)
143 int x;
144 #endif
145 {
146 	debugmessage("init_timelimit","");
147 }
148 
149 #ifdef ANSI_C
free_timelimit(void)150 void free_timelimit(void)
151 #else
152 void free_timelimit()
153 #endif
154 {
155 	debugmessage("free_timelimit","");
156 }
157 
158 
159 #ifdef ANSI_C
test_timelimit(int perc)160 int test_timelimit(int perc)
161 #else
162 int test_timelimit(perc)
163 int perc;
164 #endif
165 {
166 	debugmessage("test_timelimit","");
167 	return(0);
168 }
169 
170 #else
171 
172 /* Set the time limit to x seconds and start the clock
173  * ---------------------------------------------------
174  * This is only done if it was not done before.
175  */
176 
177 #ifdef ANSI_C
init_timelimit(int x)178 void init_timelimit(int x)
179 #else
180 void init_timelimit(x)
181 int x;
182 #endif
183 {
184 	debugmessage("init_timelimit","");
185 	if (timelimit>0L) return;
186 	if (x<1) x = 1;
187 	timelimit = (unsigned long)x;
188 	gettimeofday(&tpxstart,&tzpxstart);
189 }
190 
191 
192 
193 /* Free the actual time limit
194  * --------------------------
195  */
196 
197 #ifdef ANSI_C
free_timelimit(void)198 void free_timelimit(void)
199 #else
200 void free_timelimit()
201 #endif
202 {
203 	debugmessage("free_timelimit","");
204 	timelimit = (unsigned long)0;
205 }
206 
207 
208 
209 
210 /* Check whether the percentual time limit is reached
211  * --------------------------------------------------
212  * Return true (1) if yes.
213  */
214 
215 #ifdef ANSI_C
test_timelimit(int perc)216 int test_timelimit(int perc)
217 #else
218 int test_timelimit(perc)
219 int perc;
220 #endif
221 {
222 	unsigned long sec;
223 	long int usec;
224 	unsigned long tval;
225 
226 	debugmessage("test_timelimit","");
227 
228 	gettimeofday(&tpxend,&tzpxend);
229 	sec = tpxend.tv_sec-tpxstart.tv_sec;
230 	usec = tpxend.tv_usec-tpxstart.tv_usec;
231 	if (usec<0) { sec--; usec += 1000000; }
232 	tval = (perc*timelimit)/100L;
233 	if (tval==0L) tval = 1L;
234 	if (sec > tval) return(1);
235 	return(0);
236 }
237 
238 #endif /* NOTIMELIMIT */
239 
240 
241