ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/time.C
(Generate patch)

Comparing deliantra/server/common/time.C (file contents):
Revision 1.2 by root, Tue Aug 15 18:07:24 2006 UTC vs.
Revision 1.3 by root, Thu Aug 24 13:13:48 2006 UTC

1/* 1/*
2 * static char *rcsid_time_c = 2 * static char *rcsid_time_c =
3 * "$Id: time.C,v 1.2 2006/08/15 18:07:24 root Exp $"; 3 * "$Id: time.C,v 1.3 2006/08/24 13:13:48 root Exp $";
4 */ 4 */
5 5
6/* 6/*
7 CrossFire, A Multiplayer game for X-windows 7 CrossFire, A Multiplayer game for X-windows
8 8
37#endif /* win32 */ 37#endif /* win32 */
38 38
39/* 39/*
40 * Gloabal variables: 40 * Gloabal variables:
41 */ 41 */
42long max_time = MAX_TIME;
43struct timeval last_time;
44 42
45#define PBUFLEN 100
46long process_utime_save[PBUFLEN];
47long psaveind; 43long psaveind;
48long process_max_utime = 0;
49long process_min_utime = 999999999;
50long process_tot_mtime;
51long pticks; 44long pticks;
52long process_utime_long_count;
53 45
54const char *season_name[] = 46const char *season_name[] =
55{ 47{
56 "The Season of New Year", 48 "The Season of New Year",
57 "The Season of Growth", 49 "The Season of Growth",
88 "Month of the Devourers", 80 "Month of the Devourers",
89 "Month of Sorig", 81 "Month of Sorig",
90 "Month of the Ancient Darkness", 82 "Month of the Ancient Darkness",
91 "Month of Gorokh" 83 "Month of Gorokh"
92}; 84};
93
94/*
95 * Initialise all variables used in the timing routines.
96 */
97
98void
99reset_sleep(void)
100{
101 int i;
102 for(i = 0; i < PBUFLEN; i++)
103 process_utime_save[i] = 0;
104 psaveind = 0;
105 process_max_utime = 0;
106 process_min_utime = 999999999;
107 process_tot_mtime = 0;
108 pticks = 0;
109
110 (void) GETTIMEOFDAY(&last_time);
111}
112
113void
114log_time(long process_utime)
115{
116 pticks++;
117 if (++psaveind >= PBUFLEN)
118 psaveind = 0;
119 process_utime_save[psaveind] = process_utime;
120 if (process_utime > process_max_utime)
121 process_max_utime = process_utime;
122 if (process_utime < process_min_utime)
123 process_min_utime = process_utime;
124 process_tot_mtime += process_utime/1000;
125}
126
127/*
128 * enough_elapsed_time will return true if the time passed since
129 * last tick is more than max-time.
130 */
131
132int
133enough_elapsed_time(void)
134{
135 static struct timeval new_time;
136 long elapsed_utime;
137
138 (void) GETTIMEOFDAY(&new_time);
139
140 elapsed_utime = (new_time.tv_sec - last_time.tv_sec) * 1000000 +
141 new_time.tv_usec - last_time.tv_usec;
142 if (elapsed_utime > max_time) {
143 log_time(elapsed_utime);
144 last_time.tv_sec = new_time.tv_sec;
145 last_time.tv_usec = new_time.tv_usec;
146 return 1;
147 }
148 return 0;
149}
150
151void
152set_max_time(long t) {
153 max_time = t;
154}
155 85
156extern unsigned long todtick; 86extern unsigned long todtick;
157 87
158void 88void
159get_tod(timeofday_t *tod) 89get_tod(timeofday_t *tod)
209 139
210 sprintf(errmsg, "Time of Year: %s", season_name[tod.season]); 140 sprintf(errmsg, "Time of Year: %s", season_name[tod.season]);
211 new_draw_info(NDI_UNIQUE, 0,op,errmsg); 141 new_draw_info(NDI_UNIQUE, 0,op,errmsg);
212} 142}
213 143
214void
215time_info(object *op)
216{
217 int tot = 0, maxt = 0, mint = 99999999, long_count = 0, i;
218
219 print_tod(op);
220 if (!QUERY_FLAG(op,FLAG_WIZ))
221 return;
222
223 new_draw_info (NDI_UNIQUE, 0,op,"Total time:");
224 sprintf(errmsg,"ticks=%ld time=%ld.%2ld",
225 pticks, process_tot_mtime/1000, process_tot_mtime%1000);
226 new_draw_info (NDI_UNIQUE, 0,op,errmsg);
227 sprintf(errmsg,"avg time=%ldms max time=%ldms min time=%ldms",
228 process_tot_mtime/pticks, process_max_utime/1000,
229 process_min_utime/1000);
230 new_draw_info (NDI_UNIQUE, 0,op,errmsg);
231 sprintf(errmsg,"ticks longer than max time (%ldms) = %ld (%ld%%)",
232 max_time/1000,
233 process_utime_long_count, 100*process_utime_long_count/pticks);
234 new_draw_info (NDI_UNIQUE, 0,op,errmsg);
235
236 sprintf(errmsg,"Time last %ld ticks:", pticks > PBUFLEN ? PBUFLEN : pticks);
237 new_draw_info (NDI_UNIQUE, 0,op,errmsg);
238
239 for (i = 0; i < (pticks > PBUFLEN ? PBUFLEN : pticks); i++) {
240 tot += process_utime_save[i];
241 if (process_utime_save[i] > maxt) maxt = process_utime_save[i];
242 if (process_utime_save[i] < mint) mint = process_utime_save[i];
243 if (process_utime_save[i] > max_time) long_count++;
244 }
245
246 sprintf(errmsg,"avg time=%ldms max time=%dms min time=%dms",
247 tot/(pticks > PBUFLEN ? PBUFLEN : pticks)/1000, maxt/1000,
248 mint/1000);
249 new_draw_info (NDI_UNIQUE, 0,op,errmsg);
250 sprintf(errmsg,"ticks longer than max time (%ldms) = %d (%ld%%)",
251 max_time/1000, long_count,
252 100*long_count/(pticks > PBUFLEN ? PBUFLEN : pticks));
253 new_draw_info (NDI_UNIQUE, 0,op,errmsg);
254}
255
256long 144long
257seconds(void) 145seconds(void)
258{ 146{
259 struct timeval now; 147 struct timeval now;
260 148

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines