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 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
|
/*___INFO__MARK_BEGIN__*/
/*************************************************************************
*
* The Contents of this file are made available subject to the terms of
* the Sun Industry Standards Source License Version 1.2
*
* Sun Microsystems Inc., March, 2001
*
*
* Sun Industry Standards Source License Version 1.2
* =================================================
* The contents of this file are subject to the Sun Industry Standards
* Source License Version 1.2 (the "License"); You may not use this file
* except in compliance with the License. You may obtain a copy of the
* License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
*
* Software provided under this License is provided on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
* WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
* MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
* See the License for the specific provisions governing your rights and
* obligations concerning the Software.
*
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
*
* Copyright: 2001 by Sun Microsystems, Inc.
*
* All Rights Reserved.
*
************************************************************************/
/*___INFO__MARK_END__*/
#define _POSIX_C_SOURCE 200112L /* try to avoid old nanosleep returning void */
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <sys/types.h>
#include <sys/times.h>
#ifndef WIN32NATIVE
# include <sys/time.h>
#else
# include <winsock2.h>
#endif
#include "uti/sge_dstring.h"
#include "uti/sge_time.h"
#include "uti/sge_unistd.h"
#include "uti/sge_log.h"
#ifdef WIN32
int gettimeofday(struct timeval *tz, struct timezone *tzp);
#endif
#define NESTLEVEL 5
/* MT-NOTE: stopwatch profiling used in qmaster, qstat and qmon only */
/* MT-NOTE: stopwatch is phased out and will be replaced by libs/uti/sge_profiling */
static struct tms begin[NESTLEVEL];
static struct tms end[NESTLEVEL];
static time_t wtot[NESTLEVEL];
static time_t wbegin[NESTLEVEL];
static time_t wprev[NESTLEVEL];
static time_t wdiff[NESTLEVEL];
static int clock_tick;
static int time_log_interval[NESTLEVEL] = { -1, -1, -1, -1, -1 };
#ifdef TIMES_RETURNS_ZERO
static time_t inittime;
#endif
static void sge_stopwatch_stop(int i);
/* MT-NOTE: sge_stopwatch_stop() is not MT safe due to access to global variables */
static void sge_stopwatch_stop(int i)
{
time_t wend;
if (i < 0 || i >= NESTLEVEL) {
return;
}
if (time_log_interval[i] == -1) {
return;
}
wend = times(&end[i]);
#ifdef TIMES_RETURNS_ZERO
/* times() returns 0 on these machines */
wend = (sge_get_gmt() - inittime) * clock_tick;
#endif
end[i].tms_utime = end[i].tms_utime - begin[i].tms_utime;
end[i].tms_stime = end[i].tms_stime - begin[i].tms_stime;
end[i].tms_cutime = end[i].tms_cutime - begin[i].tms_cutime;
end[i].tms_cstime = end[i].tms_cstime - begin[i].tms_cstime;
wtot[i] = wend - wbegin[i];
wdiff[i] = wend - wprev[i];
wprev[i] = wend;
}
/****** uti/time/sge_get_gmt() ************************************************
* NAME
* sge_get_gmt() -- Return current time
*
* SYNOPSIS
* u_long32 sge_get_gmt()
*
* FUNCTION
* Return current time
*
* NOTES
* MT-NOTE: sge_get_gmt() is MT safe (except for WIN32NATIVE)
*
* RESULT
* u_long32 - 32 bit time value
******************************************************************************/
u_long32 sge_get_gmt()
{
#ifndef WIN32NATIVE
struct timeval now;
gettimeofday(&now, NULL);
return (u_long32)now.tv_sec;
#else
time_t long_time;
struct tm *gmtimeval;
time(&long_time); /* Get time as long integer. */
/* MT-NOTE: gmtime() is not MT safe (WIN32NATIVE) */
gmtimeval = gmtime(&long_time); /* Convert to local time. */
long_time = mktime(gmtimeval);
return long_time;
#endif
}
/****** uti/time/append_time() **************************************************
* NAME
* append_time() -- Convert time value into string
*
* SYNOPSIS
* const char* append_time(time_t i, dstring *buffer)
*
* FUNCTION
* Convert time value into string
*
* INPUTS
* time_t i - time value
* dstring *buffer - dstring
* bool is_xml - write in XML dateTime format?
*
* RESULT
* const char* - time string (current time if 'i' was 0)
* dstring *buffer - buffer provided by caller
*
* NOTES
* MT-NOTE: append_time() is MT safe if localtime_r() can be used
*
* SHOULD BE REPLACED BY: sge_dstring_append_time()
*
******************************************************************************/
void append_time(time_t i, dstring *buffer, bool is_xml)
{
struct tm *tm;
struct tm tm_buffer;
tm = localtime_r(&i, &tm_buffer);
if (is_xml) {
sge_dstring_sprintf_append(buffer, "%04d-%02d-%02dT%02d:%02d:%02d",
1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec);
} else {
sge_dstring_sprintf_append(buffer, "%02d/%02d/%04d %02d:%02d:%02d",
tm->tm_mon + 1, tm->tm_mday, 1900 + tm->tm_year,
tm->tm_hour, tm->tm_min, tm->tm_sec);
}
}
/****** uti/time/sge_ctime() **************************************************
* NAME
* sge_ctime() -- Convert time value into string
*
* SYNOPSIS
* const char* sge_ctime(time_t i, dstring *buffer)
*
* FUNCTION
* Convert time value into string
*
* INPUTS
* time_t i - 0 or time value
*
* RESULT
* const char* - time string (current time if 'i' was 0)
* dstring *buffer - buffer provided by caller
*
* NOTES
* MT-NOTE: sge_at_time() is MT safe if localtime_r() can be used
*
* SEE ALSO
* uti/time/sge_ctime32()
******************************************************************************/
const char *sge_ctime(time_t i, dstring *buffer)
{
struct tm tm_buffer;
struct tm *tm;
if (!i)
i = (time_t)sge_get_gmt();
tm = localtime_r(&i, &tm_buffer);
sge_dstring_sprintf(buffer, "%02d/%02d/%04d %02d:%02d:%02d",
tm->tm_mon + 1, tm->tm_mday, 1900 + tm->tm_year,
tm->tm_hour, tm->tm_min, tm->tm_sec);
return sge_dstring_get_string(buffer);
}
/* TODO: should be replaced by sge_dstring_append_time() */
const char *sge_ctimeXML(time_t i, dstring *buffer)
{
struct tm tm_buffer;
struct tm *tm;
if (!i)
i = (time_t)sge_get_gmt();
tm = localtime_r(&i, &tm_buffer);
sge_dstring_sprintf(buffer, "%04d-%02d-%02dT%02d:%02d:%02d",
1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec);
return sge_dstring_get_string(buffer);
}
/****** uti/time/sge_ctime32() ************************************************
* NAME
* sge_ctime32() -- Convert time value into string (64 bit time_t)
*
* SYNOPSIS
* const char* sge_ctime32(u_long32 *i, dstring *buffer)
*
* FUNCTION
* Convert time value into string. This function is needed for
* systems with a 64 bit time_t because the ctime function would
* otherwise try to interpret the u_long32 value as
* a 64 bit value => $&&%$?$%!
*
* INPUTS
* u_long32 *i - 0 or time value
* dstring *buffer - buffer provided by caller
*
* RESULT
* const char* - time string (current time if 'i' was 0)
*
* NOTE
* MT-NOTE: if ctime_r() is not available sge_ctime32() is not MT safe
*
* SEE ALSO
* uti/time/sge_ctime()
******************************************************************************/
const char *sge_ctime32(u_long32 *i, dstring *buffer)
{
const char *s;
char str[128];
#if SOLARIS64
volatile
#endif
time_t temp = (time_t)*i;
s = ctime_r((time_t *)&temp, str);
if (!s)
return NULL;
return sge_dstring_copy_string(buffer, s);
}
/****** uti/time/sge_at_time() ************************************************
* NAME
* sge_at_time() -- ???
*
* SYNOPSIS
* const char* sge_at_time(time_t i, dstring *buffer)
*
* FUNCTION
* ???
*
* INPUTS
* time_t i - 0 or time value
* dstring *buffer - buffer provided by caller
*
* RESULT
* const char* - time string (current time if 'i' was 0)
*
* NOTES
* MT-NOTE: sge_at_time() is MT safe if localtime_r() can be used
*
* SEE ALSO
* uti/time/sge_ctime()
******************************************************************************/
const char *sge_at_time(time_t i, dstring *buffer)
{
struct tm tm_buffer;
struct tm *tm;
if (!i)
i = (time_t)sge_get_gmt();
tm = localtime_r(&i, &tm_buffer);
return sge_dstring_sprintf(buffer, "%04d%02d%02d%02d%02d.%02d",
tm->tm_year+1900, tm->tm_mon + 1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec);
}
/****** uti/time/sge_stopwatch_log() ******************************************
* NAME
* sge_stopwatch_log() -- ???
*
* SYNOPSIS
* void sge_stopwatch_log(int i, const char *str)
*
* FUNCTION
* ???
*
* INPUTS
* int i - ???
* const char *str - ???
*
* NOTES
* MT-NOTE: sge_stopwatch_log() is not MT safe due to access to global variables
*
* SEE ALSO
* uti/time/sge_stopwatch_start()
******************************************************************************/
void sge_stopwatch_log(int i, const char *str)
{
if (i < 0 || i >= NESTLEVEL) {
return;
}
if (time_log_interval[i] == -1) {
return;
}
sge_stopwatch_stop(i);
if ((wdiff[i] * 1000) / clock_tick >= time_log_interval[i]) {
static char SGE_FUNC[] = "";
WARNING((SGE_EVENT, "%-30s: %d/%d/%d", str,
(int) ((wtot[i] * 1000) / clock_tick),
(int) ((end[i].tms_utime * 1000) / clock_tick),
(int) ((end[i].tms_stime * 1000) / clock_tick)));
}
}
/****** uti/time/sge_stopwatch_start() ****************************************
* NAME
* sge_stopwatch_start() -- ???
*
* SYNOPSIS
* void sge_stopwatch_start(int i)
*
* FUNCTION
* ???
*
* INPUTS
* int i - ???
*
* NOTES
* MT-NOTE: sge_stopwatch_start() is not MT safe due to access to global variables
*
* SEE ALSO
* uti/time/sge_stopwatch_log()
******************************************************************************/
void sge_stopwatch_start(int i)
{
static int first = 1;
int j;
char *cp;
if (first) {
char buf[24];
clock_tick = sysconf(_SC_CLK_TCK);
for (j = 0; j < NESTLEVEL; j++) {
wtot[j] = wbegin[j] = wprev[j] = wdiff[j] = 0;
sprintf(buf, "SGE_TIMELOG%d", j);
if ((cp = getenv(buf)) && (atoi(cp) >= 0)) {
time_log_interval[j] = atoi(cp);
} else {
time_log_interval[j] = -1;
}
}
first = 0;
}
if (i < 0 || i >= NESTLEVEL) {
return;
}
if (time_log_interval[i] == -1) {
return;
}
wbegin[i] = times(&begin[i]);
#ifdef TIMES_RETURNS_ZERO
/* times() return 0 on these machines */
wbegin[i] = (sge_get_gmt() - inittime) * clock_tick;
#endif
wprev[i] = wbegin[i];
}
/****** uti/time/duration_add_offset() ****************************************
* NAME
* duration_add_offset() -- add function for time add
*
* SYNOPSIS
* u_long32 duration_add_offset(u_long32 duration, u_long32 offset)
*
* FUNCTION
* add function to catch ulong overflow. Returns max ulong value if necessary
*
* INPUTS
* u_long32 duration - duration in seconds
* u_long32 offset - offset in seconds
*
* RESULT
* u_long32 - value < U_LONG32_MAX
*
* NOTES
* MT-NOTE: duration_add_offset() is not MT safe
*******************************************************************************/
u_long32 duration_add_offset(u_long32 duration, u_long32 offset)
{
if (duration == U_LONG32_MAX || offset == U_LONG32_MAX) {
return U_LONG32_MAX;
}
if ((U_LONG32_MAX-offset) < duration) {
duration = U_LONG32_MAX;
} else {
duration += offset;
}
return duration;
}
/****** uti/time/sge_usleep() ****************************************
* NAME
* sge_usleep() -- Mimics a non-iterruptable usleep()
*
* SYNOPSIS
* void sge_usleep(int sleep_time)
*
* FUNCTION
* Mimics a non-iterruptable usleep() to the caller.
*
* INPUTS
* int sleep_time - requested sleep time in microseconds
*
* RESULT
* n/a
*
* NOTES
* None.
*******************************************************************************/
void sge_usleep(int sleep_time)
{
struct timespec req = {sleep_time / 1000000, 1000 * (sleep_time % 1000000)},
rem;
while (nanosleep(&req, &rem)) {
req.tv_sec = rem.tv_sec;
req.tv_nsec = rem.tv_nsec;
}
}
|