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
# if defined(BUILD_DRIZZLE)
23
# include <drizzled/common.h>
24
# if TIME_WITH_SYS_TIME
25
# include <sys/time.h>
29
# include <sys/time.h>
35
# include "mysql_com.h" /* NAME_LEN */
37
#endif /* UNIV_HOTBACKUP */
39
UNIV_INTERN ibool ut_always_false = FALSE;
42
/*********************************************************************
43
NOTE: The Windows epoch starts from 1601/01/01 whereas the Unix
44
epoch starts from 1970/1/1. For selection of constant see:
45
http://support.microsoft.com/kb/167296/ */
46
#define WIN_TO_UNIX_DELTA_USEC ((ib_int64_t) 11644473600000000ULL)
49
/*********************************************************************
50
This is the Windows version of gettimeofday(2).*/
55
/* out: 0 if all OK else -1 */
56
struct timeval* tv, /* out: Values are relative to Unix epoch */
57
void* tz) /* in: not used */
67
GetSystemTimeAsFileTime(&ft);
69
tm = (ib_int64_t) ft.dwHighDateTime << 32;
70
tm |= ft.dwLowDateTime;
72
ut_a(tm >= 0); /* If tm wraps over to negative, the quotient / 10
75
tm /= 10; /* Convert from 100 nsec periods to usec */
77
/* If we don't convert to the Unix epoch the value for
78
struct timeval::tv_sec will overflow.*/
79
tm -= WIN_TO_UNIX_DELTA_USEC;
81
tv->tv_sec = (long) (tm / 1000000L);
82
tv->tv_usec = (long) (tm % 1000000L);
87
#define ut_gettimeofday gettimeofday
90
/************************************************************
91
Gets the high 32 bits in a ulint. That is makes a shift >> 32,
92
but since there seem to be compiler bugs in both gcc and Visual C++,
93
we do this by a special conversion. */
99
ulint a) /* in: ulint */
110
/**************************************************************
111
Returns system time. We do not specify the format of the time returned:
112
the only way to manipulate it is to use the function ut_difftime. */
121
/**************************************************************
122
Returns system time. */
127
ulint* sec, /* out: seconds since the Epoch */
128
ulint* ms) /* out: microseconds since the Epoch+*sec */
132
ut_gettimeofday(&tv, NULL);
133
*sec = (ulint) tv.tv_sec;
134
*ms = (ulint) tv.tv_usec;
137
/**************************************************************
138
Returns the number of microseconds since epoch. Similar to
139
time(3), the return value is also stored in *tloc, provided
140
that tloc is non-NULL. */
145
/* out: us since epoch */
146
ullint* tloc) /* out: us since epoch, if non-NULL */
151
ut_gettimeofday(&tv, NULL);
153
us = (ullint) tv.tv_sec * 1000000 + tv.tv_usec;
162
/**************************************************************
163
Returns the difference of two times in seconds. */
168
/* out: time2 - time1 expressed in seconds */
169
ib_time_t time2, /* in: time */
170
ib_time_t time1) /* in: time */
172
return(difftime(time2, time1));
175
/**************************************************************
176
Prints a timestamp to a file. */
181
FILE* file) /* in: file where to print */
186
GetLocalTime(&cal_tm);
188
fprintf(file,"%02d%02d%02d %2d:%02d:%02d",
189
(int)cal_tm.wYear % 100,
194
(int)cal_tm.wSecond);
197
struct tm* cal_tm_ptr;
202
#ifdef HAVE_LOCALTIME_R
203
localtime_r(&tm, &cal_tm);
204
cal_tm_ptr = &cal_tm;
206
cal_tm_ptr = localtime(&tm);
208
fprintf(file,"%02d%02d%02d %2d:%02d:%02d",
209
cal_tm_ptr->tm_year % 100,
210
cal_tm_ptr->tm_mon + 1,
218
/**************************************************************
219
Sprintfs a timestamp to a buffer, 13..14 chars plus terminating NUL. */
222
ut_sprintf_timestamp(
223
/*=================*/
224
char* buf) /* in: buffer where to sprintf */
229
GetLocalTime(&cal_tm);
231
sprintf(buf, "%02d%02d%02d %2d:%02d:%02d",
232
(int)cal_tm.wYear % 100,
237
(int)cal_tm.wSecond);
240
struct tm* cal_tm_ptr;
245
#ifdef HAVE_LOCALTIME_R
246
localtime_r(&tm, &cal_tm);
247
cal_tm_ptr = &cal_tm;
249
cal_tm_ptr = localtime(&tm);
251
sprintf(buf, "%02d%02d%02d %2d:%02d:%02d",
252
cal_tm_ptr->tm_year % 100,
253
cal_tm_ptr->tm_mon + 1,
261
/**************************************************************
262
Sprintfs a timestamp to a buffer with no spaces and with ':' characters
266
ut_sprintf_timestamp_without_extra_chars(
267
/*=====================================*/
268
char* buf) /* in: buffer where to sprintf */
273
GetLocalTime(&cal_tm);
275
sprintf(buf, "%02d%02d%02d_%2d_%02d_%02d",
276
(int)cal_tm.wYear % 100,
281
(int)cal_tm.wSecond);
284
struct tm* cal_tm_ptr;
289
#ifdef HAVE_LOCALTIME_R
290
localtime_r(&tm, &cal_tm);
291
cal_tm_ptr = &cal_tm;
293
cal_tm_ptr = localtime(&tm);
295
sprintf(buf, "%02d%02d%02d_%2d_%02d_%02d",
296
cal_tm_ptr->tm_year % 100,
297
cal_tm_ptr->tm_mon + 1,
305
/**************************************************************
306
Returns current year, month, day. */
309
ut_get_year_month_day(
310
/*==================*/
311
ulint* year, /* out: current year */
312
ulint* month, /* out: month */
313
ulint* day) /* out: day */
318
GetLocalTime(&cal_tm);
320
*year = (ulint)cal_tm.wYear;
321
*month = (ulint)cal_tm.wMonth;
322
*day = (ulint)cal_tm.wDay;
325
struct tm* cal_tm_ptr;
330
#ifdef HAVE_LOCALTIME_R
331
localtime_r(&tm, &cal_tm);
332
cal_tm_ptr = &cal_tm;
334
cal_tm_ptr = localtime(&tm);
336
*year = (ulint)cal_tm_ptr->tm_year + 1900;
337
*month = (ulint)cal_tm_ptr->tm_mon + 1;
338
*day = (ulint)cal_tm_ptr->tm_mday;
342
/*****************************************************************
343
Runs an idle loop on CPU. The argument gives the desired delay
344
in microseconds on 100 MHz Pentium + Visual C++. */
349
/* out: dummy value */
350
ulint delay) /* in: delay in microseconds on 100 MHz Pentium */
356
for (i = 0; i < delay * 50; i++) {
360
if (ut_always_false) {
361
ut_always_false = (ibool) j;
367
/*****************************************************************
368
Prints the contents of a memory buffer in hex and ascii. */
373
FILE* file, /* in: file where to print */
374
const void* buf, /* in: memory buffer */
375
ulint len) /* in: length of the buffer */
380
UNIV_MEM_ASSERT_RW(buf, len);
382
fprintf(file, " len %lu; hex ", len);
384
for (data = (const byte*)buf, i = 0; i < len; i++) {
385
fprintf(file, "%02lx", (ulong)*data++);
388
fputs("; asc ", file);
390
data = (const byte*)buf;
392
for (i = 0; i < len; i++) {
393
int c = (int) *data++;
394
putc(isprint(c) ? c : ' ', file);
400
/*****************************************************************
401
Calculates fast the number rounded up to the nearest power of 2. */
406
/* out: first power of 2 which is >= n */
407
ulint n) /* in: number != 0 */
422
/**************************************************************************
423
Outputs a NUL-terminated file name, quoted with apostrophes. */
428
FILE* f, /* in: output stream */
429
const char* name) /* in: name to print */
448
/**************************************************************************
449
Outputs a fixed-length string, quoted as an SQL identifier.
450
If the string contains a slash '/', the string will be
451
output as two identifiers separated by a period (.),
452
as in SQL database_name.identifier. */
457
FILE* f, /* in: output stream */
458
trx_t* trx, /* in: transaction */
459
ibool table_id,/* in: TRUE=print a table name,
460
FALSE=print other identifier */
461
const char* name) /* in: name to print */
463
ut_print_namel(f, trx, table_id, name, strlen(name));
466
/**************************************************************************
467
Outputs a fixed-length string, quoted as an SQL identifier.
468
If the string contains a slash '/', the string will be
469
output as two identifiers separated by a period (.),
470
as in SQL database_name.identifier. */
475
FILE* f, /* in: output stream */
476
trx_t* trx, /* in: transaction (NULL=no quotes) */
477
ibool table_id,/* in: TRUE=print a table name,
478
FALSE=print other identifier */
479
const char* name, /* in: name to print */
480
ulint namelen)/* in: length of name */
482
#ifdef UNIV_HOTBACKUP
483
fwrite(name, 1, namelen, f);
485
/* 2 * NAME_LEN for database and table name,
486
and some slack for the #mysql50# prefix and quotes */
487
char buf[3 * NAME_LEN];
490
bufend = innobase_convert_name(buf, sizeof buf,
492
trx ? trx->mysql_thd : NULL,
495
ssize_t ret= fwrite(buf, 1, bufend - buf, f);
496
assert(ret==bufend-buf);
500
/**************************************************************************
506
FILE* dest, /* in: output file */
507
FILE* src) /* in: input file to be appended to output */
509
long len = ftell(src);
514
size_t maxs = len < (long) sizeof buf
517
size_t size = fread(buf, 1, maxs, src);
518
size_t ret= fwrite(buf, 1, size, dest);
527
/**************************************************************************
534
/* out: number of characters that would
535
have been printed if the size were
536
unlimited, not including the terminating
538
char* str, /* out: string */
539
size_t size, /* in: str size */
540
const char* fmt, /* in: format */
541
...) /* in: format values */
550
res = _vscprintf(fmt, ap1);
554
_vsnprintf(str, size, fmt, ap2);
556
if ((size_t) res >= size) {
557
str[size - 1] = '\0';