1
/*******************************************************************
2
Various utilities for Innobase.
4
(c) 1994, 1995 Innobase Oy
6
Created 5/11/1994 Heikki Tuuri
7
********************************************************************/
21
#include "ha_prototypes.h"
23
#if TIME_WITH_SYS_TIME
24
# include <sys/time.h>
28
# include <sys/time.h>
34
ibool ut_always_false = FALSE;
37
/*********************************************************************
38
NOTE: The Windows epoch starts from 1601/01/01 whereas the Unix
39
epoch starts from 1970/1/1. For selection of constant see:
40
http://support.microsoft.com/kb/167296/ */
41
#define WIN_TO_UNIX_DELTA_USEC ((ib_longlong) 11644473600000000ULL)
44
/*********************************************************************
45
This is the Windows version of gettimeofday(2).*/
50
/* out: 0 if all OK else -1 */
51
struct timeval* tv, /* out: Values are relative to Unix epoch */
52
void* tz) /* in: not used */
62
GetSystemTimeAsFileTime(&ft);
64
tm = (ib_longlong) ft.dwHighDateTime << 32;
65
tm |= ft.dwLowDateTime;
67
ut_a(tm >= 0); /* If tm wraps over to negative, the quotient / 10
70
tm /= 10; /* Convert from 100 nsec periods to usec */
72
/* If we don't convert to the Unix epoch the value for
73
struct timeval::tv_sec will overflow.*/
74
tm -= WIN_TO_UNIX_DELTA_USEC;
76
tv->tv_sec = (long) (tm / 1000000L);
77
tv->tv_usec = (long) (tm % 1000000L);
82
#define ut_gettimeofday gettimeofday
85
/************************************************************
86
Gets the high 32 bits in a ulint. That is makes a shift >> 32,
87
but since there seem to be compiler bugs in both gcc and Visual C++,
88
we do this by a special conversion. */
94
ulint a) /* in: ulint */
105
/************************************************************
106
The following function returns elapsed CPU time in milliseconds. */
111
return((clock() * 1000) / CLOCKS_PER_SEC);
114
/**************************************************************
115
Returns system time. We do not specify the format of the time returned:
116
the only way to manipulate it is to use the function ut_difftime. */
125
/**************************************************************
126
Returns system time. */
131
ulint* sec, /* out: seconds since the Epoch */
132
ulint* ms) /* out: microseconds since the Epoch+*sec */
136
ut_gettimeofday(&tv, NULL);
137
*sec = (ulint) tv.tv_sec;
138
*ms = (ulint) tv.tv_usec;
141
/**************************************************************
142
Returns the difference of two times in seconds. */
147
/* out: time2 - time1 expressed in seconds */
148
ib_time_t time2, /* in: time */
149
ib_time_t time1) /* in: time */
151
return(difftime(time2, time1));
154
/**************************************************************
155
Prints a timestamp to a file. */
160
FILE* file) /* in: file where to print */
165
GetLocalTime(&cal_tm);
167
fprintf(file,"%02d%02d%02d %2d:%02d:%02d",
168
(int)cal_tm.wYear % 100,
173
(int)cal_tm.wSecond);
176
struct tm* cal_tm_ptr;
181
#ifdef HAVE_LOCALTIME_R
182
localtime_r(&tm, &cal_tm);
183
cal_tm_ptr = &cal_tm;
185
cal_tm_ptr = localtime(&tm);
187
fprintf(file,"%02d%02d%02d %2d:%02d:%02d",
188
cal_tm_ptr->tm_year % 100,
189
cal_tm_ptr->tm_mon + 1,
197
/**************************************************************
198
Sprintfs a timestamp to a buffer, 13..14 chars plus terminating NUL. */
201
ut_sprintf_timestamp(
202
/*=================*/
203
char* buf) /* in: buffer where to sprintf */
208
GetLocalTime(&cal_tm);
210
sprintf(buf, "%02d%02d%02d %2d:%02d:%02d",
211
(int)cal_tm.wYear % 100,
216
(int)cal_tm.wSecond);
219
struct tm* cal_tm_ptr;
224
#ifdef HAVE_LOCALTIME_R
225
localtime_r(&tm, &cal_tm);
226
cal_tm_ptr = &cal_tm;
228
cal_tm_ptr = localtime(&tm);
230
sprintf(buf, "%02d%02d%02d %2d:%02d:%02d",
231
cal_tm_ptr->tm_year % 100,
232
cal_tm_ptr->tm_mon + 1,
240
/**************************************************************
241
Sprintfs a timestamp to a buffer with no spaces and with ':' characters
245
ut_sprintf_timestamp_without_extra_chars(
246
/*=====================================*/
247
char* buf) /* in: buffer where to sprintf */
252
GetLocalTime(&cal_tm);
254
sprintf(buf, "%02d%02d%02d_%2d_%02d_%02d",
255
(int)cal_tm.wYear % 100,
260
(int)cal_tm.wSecond);
263
struct tm* cal_tm_ptr;
268
#ifdef HAVE_LOCALTIME_R
269
localtime_r(&tm, &cal_tm);
270
cal_tm_ptr = &cal_tm;
272
cal_tm_ptr = localtime(&tm);
274
sprintf(buf, "%02d%02d%02d_%2d_%02d_%02d",
275
cal_tm_ptr->tm_year % 100,
276
cal_tm_ptr->tm_mon + 1,
284
/**************************************************************
285
Returns current year, month, day. */
288
ut_get_year_month_day(
289
/*==================*/
290
ulint* year, /* out: current year */
291
ulint* month, /* out: month */
292
ulint* day) /* out: day */
297
GetLocalTime(&cal_tm);
299
*year = (ulint)cal_tm.wYear;
300
*month = (ulint)cal_tm.wMonth;
301
*day = (ulint)cal_tm.wDay;
304
struct tm* cal_tm_ptr;
309
#ifdef HAVE_LOCALTIME_R
310
localtime_r(&tm, &cal_tm);
311
cal_tm_ptr = &cal_tm;
313
cal_tm_ptr = localtime(&tm);
315
*year = (ulint)cal_tm_ptr->tm_year + 1900;
316
*month = (ulint)cal_tm_ptr->tm_mon + 1;
317
*day = (ulint)cal_tm_ptr->tm_mday;
321
/*****************************************************************
322
Runs an idle loop on CPU. The argument gives the desired delay
323
in microseconds on 100 MHz Pentium + Visual C++. */
328
/* out: dummy value */
329
ulint delay) /* in: delay in microseconds on 100 MHz Pentium */
335
for (i = 0; i < delay * 50; i++) {
339
if (ut_always_false) {
340
ut_always_false = (ibool) j;
346
/*****************************************************************
347
Prints the contents of a memory buffer in hex and ascii. */
352
FILE* file, /* in: file where to print */
353
const void* buf, /* in: memory buffer */
354
ulint len) /* in: length of the buffer */
359
UNIV_MEM_ASSERT_RW(buf, len);
361
fprintf(file, " len %lu; hex ", len);
363
for (data = (const byte*)buf, i = 0; i < len; i++) {
364
fprintf(file, "%02lx", (ulong)*data++);
367
fputs("; asc ", file);
369
data = (const byte*)buf;
371
for (i = 0; i < len; i++) {
372
int c = (int) *data++;
373
putc(isprint(c) ? c : ' ', file);
379
/****************************************************************
380
Sort function for ulint arrays. */
383
ut_ulint_sort(ulint* arr, ulint* aux_arr, ulint low, ulint high)
384
/*============================================================*/
386
UT_SORT_FUNCTION_BODY(ut_ulint_sort, arr, aux_arr, low, high,
390
/*****************************************************************
391
Calculates fast the number rounded up to the nearest power of 2. */
396
/* out: first power of 2 which is >= n */
397
ulint n) /* in: number != 0 */
412
/**************************************************************************
413
Outputs a NUL-terminated file name, quoted with apostrophes. */
418
FILE* f, /* in: output stream */
419
const char* name) /* in: name to print */
438
/**************************************************************************
439
Outputs a fixed-length string, quoted as an SQL identifier.
440
If the string contains a slash '/', the string will be
441
output as two identifiers separated by a period (.),
442
as in SQL database_name.identifier. */
447
FILE* f, /* in: output stream */
448
trx_t* trx, /* in: transaction */
449
ibool table_id,/* in: TRUE=print a table name,
450
FALSE=print other identifier */
451
const char* name) /* in: name to print */
453
ut_print_namel(f, trx, table_id, name, strlen(name));
456
/**************************************************************************
457
Outputs a fixed-length string, quoted as an SQL identifier.
458
If the string contains a slash '/', the string will be
459
output as two identifiers separated by a period (.),
460
as in SQL database_name.identifier. */
465
FILE* f, /* in: output stream */
466
trx_t* trx, /* in: transaction (NULL=no quotes) */
467
ibool table_id,/* in: TRUE=print a table name,
468
FALSE=print other identifier */
469
const char* name, /* in: name to print */
470
ulint namelen)/* in: length of name */
472
#ifdef UNIV_HOTBACKUP
473
fwrite(name, 1, namelen, f);
476
char* slash = memchr(name, '/', namelen);
482
/* Print the database name and table name separately. */
483
innobase_print_identifier(f, trx, TRUE, name, slash - name);
485
innobase_print_identifier(f, trx, TRUE, slash + 1,
486
namelen - (slash - name) - 1);
489
innobase_print_identifier(f, trx, table_id, name, namelen);
494
/**************************************************************************
500
FILE* dest, /* in: output file */
501
FILE* src) /* in: input file to be appended to output */
503
long len = ftell(src);
508
size_t maxs = len < (long) sizeof buf
511
size_t size = fread(buf, 1, maxs, src);
512
assert(fwrite(buf, 1, size, dest)==size);
520
/**************************************************************************
527
/* out: number of characters that would
528
have been printed if the size were
529
unlimited, not including the terminating
531
char* str, /* out: string */
532
size_t size, /* in: str size */
533
const char* fmt, /* in: format */
534
...) /* in: format values */
543
res = _vscprintf(fmt, ap1);
547
_vsnprintf(str, size, fmt, ap2);
549
if ((size_t) res >= size) {
550
str[size - 1] = '\0';