1
/*******************************************************************
2
Various utilities for Innobase.
4
(c) 1994, 1995 Innobase Oy
6
Created 5/11/1994 Heikki Tuuri
7
********************************************************************/
20
#include "ha_prototypes.h"
21
#ifndef UNIV_HOTBACKUP
22
# include "mysql_com.h" /* NAME_LEN */
23
#endif /* UNIV_HOTBACKUP */
25
UNIV_INTERN ibool ut_always_false = FALSE;
28
/*********************************************************************
29
NOTE: The Windows epoch starts from 1601/01/01 whereas the Unix
30
epoch starts from 1970/1/1. For selection of constant see:
31
http://support.microsoft.com/kb/167296/ */
32
#define WIN_TO_UNIX_DELTA_USEC ((ib_int64_t) 11644473600000000ULL)
35
/*********************************************************************
36
This is the Windows version of gettimeofday(2).*/
41
/* out: 0 if all OK else -1 */
42
struct timeval* tv, /* out: Values are relative to Unix epoch */
43
void* tz) /* in: not used */
53
GetSystemTimeAsFileTime(&ft);
55
tm = (ib_int64_t) ft.dwHighDateTime << 32;
56
tm |= ft.dwLowDateTime;
58
ut_a(tm >= 0); /* If tm wraps over to negative, the quotient / 10
61
tm /= 10; /* Convert from 100 nsec periods to usec */
63
/* If we don't convert to the Unix epoch the value for
64
struct timeval::tv_sec will overflow.*/
65
tm -= WIN_TO_UNIX_DELTA_USEC;
67
tv->tv_sec = (long) (tm / 1000000L);
68
tv->tv_usec = (long) (tm % 1000000L);
73
#define ut_gettimeofday gettimeofday
76
/************************************************************
77
Gets the high 32 bits in a ulint. That is makes a shift >> 32,
78
but since there seem to be compiler bugs in both gcc and Visual C++,
79
we do this by a special conversion. */
85
ulint a) /* in: ulint */
96
/**************************************************************
97
Returns system time. We do not specify the format of the time returned:
98
the only way to manipulate it is to use the function ut_difftime. */
107
/**************************************************************
108
Returns system time. */
113
ulint* sec, /* out: seconds since the Epoch */
114
ulint* ms) /* out: microseconds since the Epoch+*sec */
118
ut_gettimeofday(&tv, NULL);
119
*sec = (ulint) tv.tv_sec;
120
*ms = (ulint) tv.tv_usec;
123
/**************************************************************
124
Returns the number of microseconds since epoch. Similar to
125
time(3), the return value is also stored in *tloc, provided
126
that tloc is non-NULL. */
131
/* out: us since epoch */
132
ullint* tloc) /* out: us since epoch, if non-NULL */
137
ut_gettimeofday(&tv, NULL);
139
us = (ullint) tv.tv_sec * 1000000 + tv.tv_usec;
148
/**************************************************************
149
Returns the difference of two times in seconds. */
154
/* out: time2 - time1 expressed in seconds */
155
ib_time_t time2, /* in: time */
156
ib_time_t time1) /* in: time */
158
return(difftime(time2, time1));
161
/**************************************************************
162
Prints a timestamp to a file. */
167
FILE* file) /* in: file where to print */
172
GetLocalTime(&cal_tm);
174
fprintf(file,"%02d%02d%02d %2d:%02d:%02d",
175
(int)cal_tm.wYear % 100,
180
(int)cal_tm.wSecond);
183
struct tm* cal_tm_ptr;
188
#ifdef HAVE_LOCALTIME_R
189
localtime_r(&tm, &cal_tm);
190
cal_tm_ptr = &cal_tm;
192
cal_tm_ptr = localtime(&tm);
194
fprintf(file,"%02d%02d%02d %2d:%02d:%02d",
195
cal_tm_ptr->tm_year % 100,
196
cal_tm_ptr->tm_mon + 1,
204
/**************************************************************
205
Sprintfs a timestamp to a buffer, 13..14 chars plus terminating NUL. */
208
ut_sprintf_timestamp(
209
/*=================*/
210
char* buf) /* in: buffer where to sprintf */
215
GetLocalTime(&cal_tm);
217
sprintf(buf, "%02d%02d%02d %2d:%02d:%02d",
218
(int)cal_tm.wYear % 100,
223
(int)cal_tm.wSecond);
226
struct tm* cal_tm_ptr;
231
#ifdef HAVE_LOCALTIME_R
232
localtime_r(&tm, &cal_tm);
233
cal_tm_ptr = &cal_tm;
235
cal_tm_ptr = localtime(&tm);
237
sprintf(buf, "%02d%02d%02d %2d:%02d:%02d",
238
cal_tm_ptr->tm_year % 100,
239
cal_tm_ptr->tm_mon + 1,
247
/**************************************************************
248
Sprintfs a timestamp to a buffer with no spaces and with ':' characters
252
ut_sprintf_timestamp_without_extra_chars(
253
/*=====================================*/
254
char* buf) /* in: buffer where to sprintf */
259
GetLocalTime(&cal_tm);
261
sprintf(buf, "%02d%02d%02d_%2d_%02d_%02d",
262
(int)cal_tm.wYear % 100,
267
(int)cal_tm.wSecond);
270
struct tm* cal_tm_ptr;
275
#ifdef HAVE_LOCALTIME_R
276
localtime_r(&tm, &cal_tm);
277
cal_tm_ptr = &cal_tm;
279
cal_tm_ptr = localtime(&tm);
281
sprintf(buf, "%02d%02d%02d_%2d_%02d_%02d",
282
cal_tm_ptr->tm_year % 100,
283
cal_tm_ptr->tm_mon + 1,
291
/**************************************************************
292
Returns current year, month, day. */
295
ut_get_year_month_day(
296
/*==================*/
297
ulint* year, /* out: current year */
298
ulint* month, /* out: month */
299
ulint* day) /* out: day */
304
GetLocalTime(&cal_tm);
306
*year = (ulint)cal_tm.wYear;
307
*month = (ulint)cal_tm.wMonth;
308
*day = (ulint)cal_tm.wDay;
311
struct tm* cal_tm_ptr;
316
#ifdef HAVE_LOCALTIME_R
317
localtime_r(&tm, &cal_tm);
318
cal_tm_ptr = &cal_tm;
320
cal_tm_ptr = localtime(&tm);
322
*year = (ulint)cal_tm_ptr->tm_year + 1900;
323
*month = (ulint)cal_tm_ptr->tm_mon + 1;
324
*day = (ulint)cal_tm_ptr->tm_mday;
328
/*****************************************************************
329
Runs an idle loop on CPU. The argument gives the desired delay
330
in microseconds on 100 MHz Pentium + Visual C++. */
335
/* out: dummy value */
336
ulint delay) /* in: delay in microseconds on 100 MHz Pentium */
342
for (i = 0; i < delay * 50; i++) {
346
if (ut_always_false) {
347
ut_always_false = (ibool) j;
353
/*****************************************************************
354
Prints the contents of a memory buffer in hex and ascii. */
359
FILE* file, /* in: file where to print */
360
const void* buf, /* in: memory buffer */
361
ulint len) /* in: length of the buffer */
366
UNIV_MEM_ASSERT_RW(buf, len);
368
fprintf(file, " len %lu; hex ", len);
370
for (data = (const byte*)buf, i = 0; i < len; i++) {
371
fprintf(file, "%02lx", (ulong)*data++);
374
fputs("; asc ", file);
376
data = (const byte*)buf;
378
for (i = 0; i < len; i++) {
379
int c = (int) *data++;
380
putc(isprint(c) ? c : ' ', file);
386
/*****************************************************************
387
Calculates fast the number rounded up to the nearest power of 2. */
392
/* out: first power of 2 which is >= n */
393
ulint n) /* in: number != 0 */
408
/**************************************************************************
409
Outputs a NUL-terminated file name, quoted with apostrophes. */
414
FILE* f, /* in: output stream */
415
const char* name) /* in: name to print */
434
/**************************************************************************
435
Outputs a fixed-length string, quoted as an SQL identifier.
436
If the string contains a slash '/', the string will be
437
output as two identifiers separated by a period (.),
438
as in SQL database_name.identifier. */
443
FILE* f, /* in: output stream */
444
trx_t* trx, /* in: transaction */
445
ibool table_id,/* in: TRUE=print a table name,
446
FALSE=print other identifier */
447
const char* name) /* in: name to print */
449
ut_print_namel(f, trx, table_id, name, strlen(name));
452
/**************************************************************************
453
Outputs a fixed-length string, quoted as an SQL identifier.
454
If the string contains a slash '/', the string will be
455
output as two identifiers separated by a period (.),
456
as in SQL database_name.identifier. */
461
FILE* f, /* in: output stream */
462
trx_t* trx, /* in: transaction (NULL=no quotes) */
463
ibool table_id,/* in: TRUE=print a table name,
464
FALSE=print other identifier */
465
const char* name, /* in: name to print */
466
ulint namelen)/* in: length of name */
468
#ifdef UNIV_HOTBACKUP
469
fwrite(name, 1, namelen, f);
471
/* 2 * NAME_LEN for database and table name,
472
and some slack for the #mysql50# prefix and quotes */
473
char buf[3 * NAME_LEN];
476
bufend = innobase_convert_name(buf, sizeof buf,
478
trx ? trx->mysql_thd : NULL,
481
fwrite(buf, 1, bufend - buf, f);
485
/**************************************************************************
491
FILE* dest, /* in: output file */
492
FILE* src) /* in: input file to be appended to output */
494
long len = ftell(src);
499
size_t maxs = len < (long) sizeof buf
502
size_t size = fread(buf, 1, maxs, src);
503
fwrite(buf, 1, size, dest);
511
/**************************************************************************
518
/* out: number of characters that would
519
have been printed if the size were
520
unlimited, not including the terminating
522
char* str, /* out: string */
523
size_t size, /* in: str size */
524
const char* fmt, /* in: format */
525
...) /* in: format values */
534
res = _vscprintf(fmt, ap1);
538
_vsnprintf(str, size, fmt, ap2);
540
if ((size_t) res >= size) {
541
str[size - 1] = '\0';