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
ibool ut_always_false = FALSE;
26
/*********************************************************************
27
NOTE: The Windows epoch starts from 1601/01/01 whereas the Unix
28
epoch starts from 1970/1/1. For selection of constant see:
29
http://support.microsoft.com/kb/167296/ */
30
#define WIN_TO_UNIX_DELTA_USEC ((ib_longlong) 11644473600000000ULL)
33
/*********************************************************************
34
This is the Windows version of gettimeofday(2).*/
39
/* out: 0 if all OK else -1 */
40
struct timeval* tv, /* out: Values are relative to Unix epoch */
41
void* tz) /* in: not used */
51
GetSystemTimeAsFileTime(&ft);
53
tm = (ib_longlong) ft.dwHighDateTime << 32;
54
tm |= ft.dwLowDateTime;
56
ut_a(tm >= 0); /* If tm wraps over to negative, the quotient / 10
59
tm /= 10; /* Convert from 100 nsec periods to usec */
61
/* If we don't convert to the Unix epoch the value for
62
struct timeval::tv_sec will overflow.*/
63
tm -= WIN_TO_UNIX_DELTA_USEC;
65
tv->tv_sec = (long) (tm / 1000000L);
66
tv->tv_usec = (long) (tm % 1000000L);
71
#define ut_gettimeofday gettimeofday
74
/************************************************************
75
Gets the high 32 bits in a ulint. That is makes a shift >> 32,
76
but since there seem to be compiler bugs in both gcc and Visual C++,
77
we do this by a special conversion. */
83
ulint a) /* in: ulint */
94
/************************************************************
95
The following function returns elapsed CPU time in milliseconds. */
100
return((clock() * 1000) / CLOCKS_PER_SEC);
103
/**************************************************************
104
Returns system time. We do not specify the format of the time returned:
105
the only way to manipulate it is to use the function ut_difftime. */
114
/**************************************************************
115
Returns system time. */
120
ulint* sec, /* out: seconds since the Epoch */
121
ulint* ms) /* out: microseconds since the Epoch+*sec */
125
ut_gettimeofday(&tv, NULL);
126
*sec = (ulint) tv.tv_sec;
127
*ms = (ulint) tv.tv_usec;
130
/**************************************************************
131
Returns the difference of two times in seconds. */
136
/* out: time2 - time1 expressed in seconds */
137
ib_time_t time2, /* in: time */
138
ib_time_t time1) /* in: time */
140
return(difftime(time2, time1));
143
/**************************************************************
144
Prints a timestamp to a file. */
149
FILE* file) /* in: file where to print */
154
GetLocalTime(&cal_tm);
156
fprintf(file,"%02d%02d%02d %2d:%02d:%02d",
157
(int)cal_tm.wYear % 100,
162
(int)cal_tm.wSecond);
165
struct tm* cal_tm_ptr;
170
#ifdef HAVE_LOCALTIME_R
171
localtime_r(&tm, &cal_tm);
172
cal_tm_ptr = &cal_tm;
174
cal_tm_ptr = localtime(&tm);
176
fprintf(file,"%02d%02d%02d %2d:%02d:%02d",
177
cal_tm_ptr->tm_year % 100,
178
cal_tm_ptr->tm_mon + 1,
186
/**************************************************************
187
Sprintfs a timestamp to a buffer, 13..14 chars plus terminating NUL. */
190
ut_sprintf_timestamp(
191
/*=================*/
192
char* buf) /* in: buffer where to sprintf */
197
GetLocalTime(&cal_tm);
199
sprintf(buf, "%02d%02d%02d %2d:%02d:%02d",
200
(int)cal_tm.wYear % 100,
205
(int)cal_tm.wSecond);
208
struct tm* cal_tm_ptr;
213
#ifdef HAVE_LOCALTIME_R
214
localtime_r(&tm, &cal_tm);
215
cal_tm_ptr = &cal_tm;
217
cal_tm_ptr = localtime(&tm);
219
sprintf(buf, "%02d%02d%02d %2d:%02d:%02d",
220
cal_tm_ptr->tm_year % 100,
221
cal_tm_ptr->tm_mon + 1,
229
/**************************************************************
230
Sprintfs a timestamp to a buffer with no spaces and with ':' characters
234
ut_sprintf_timestamp_without_extra_chars(
235
/*=====================================*/
236
char* buf) /* in: buffer where to sprintf */
241
GetLocalTime(&cal_tm);
243
sprintf(buf, "%02d%02d%02d_%2d_%02d_%02d",
244
(int)cal_tm.wYear % 100,
249
(int)cal_tm.wSecond);
252
struct tm* cal_tm_ptr;
257
#ifdef HAVE_LOCALTIME_R
258
localtime_r(&tm, &cal_tm);
259
cal_tm_ptr = &cal_tm;
261
cal_tm_ptr = localtime(&tm);
263
sprintf(buf, "%02d%02d%02d_%2d_%02d_%02d",
264
cal_tm_ptr->tm_year % 100,
265
cal_tm_ptr->tm_mon + 1,
273
/**************************************************************
274
Returns current year, month, day. */
277
ut_get_year_month_day(
278
/*==================*/
279
ulint* year, /* out: current year */
280
ulint* month, /* out: month */
281
ulint* day) /* out: day */
286
GetLocalTime(&cal_tm);
288
*year = (ulint)cal_tm.wYear;
289
*month = (ulint)cal_tm.wMonth;
290
*day = (ulint)cal_tm.wDay;
293
struct tm* cal_tm_ptr;
298
#ifdef HAVE_LOCALTIME_R
299
localtime_r(&tm, &cal_tm);
300
cal_tm_ptr = &cal_tm;
302
cal_tm_ptr = localtime(&tm);
304
*year = (ulint)cal_tm_ptr->tm_year + 1900;
305
*month = (ulint)cal_tm_ptr->tm_mon + 1;
306
*day = (ulint)cal_tm_ptr->tm_mday;
310
/*****************************************************************
311
Runs an idle loop on CPU. The argument gives the desired delay
312
in microseconds on 100 MHz Pentium + Visual C++. */
317
/* out: dummy value */
318
ulint delay) /* in: delay in microseconds on 100 MHz Pentium */
324
for (i = 0; i < delay * 50; i++) {
328
if (ut_always_false) {
329
ut_always_false = (ibool) j;
335
/*****************************************************************
336
Prints the contents of a memory buffer in hex and ascii. */
341
FILE* file, /* in: file where to print */
342
const void* buf, /* in: memory buffer */
343
ulint len) /* in: length of the buffer */
348
UNIV_MEM_ASSERT_RW(buf, len);
350
fprintf(file, " len %lu; hex ", len);
352
for (data = (const byte*)buf, i = 0; i < len; i++) {
353
fprintf(file, "%02lx", (ulong)*data++);
356
fputs("; asc ", file);
358
data = (const byte*)buf;
360
for (i = 0; i < len; i++) {
361
int c = (int) *data++;
362
putc(isprint(c) ? c : ' ', file);
368
/****************************************************************
369
Sort function for ulint arrays. */
372
ut_ulint_sort(ulint* arr, ulint* aux_arr, ulint low, ulint high)
373
/*============================================================*/
375
UT_SORT_FUNCTION_BODY(ut_ulint_sort, arr, aux_arr, low, high,
379
/*****************************************************************
380
Calculates fast the number rounded up to the nearest power of 2. */
385
/* out: first power of 2 which is >= n */
386
ulint n) /* in: number != 0 */
401
/**************************************************************************
402
Outputs a NUL-terminated file name, quoted with apostrophes. */
407
FILE* f, /* in: output stream */
408
const char* name) /* in: name to print */
427
/**************************************************************************
428
Outputs a fixed-length string, quoted as an SQL identifier.
429
If the string contains a slash '/', the string will be
430
output as two identifiers separated by a period (.),
431
as in SQL database_name.identifier. */
436
FILE* f, /* in: output stream */
437
trx_t* trx, /* in: transaction */
438
ibool table_id,/* in: TRUE=print a table name,
439
FALSE=print other identifier */
440
const char* name) /* in: name to print */
442
ut_print_namel(f, trx, table_id, name, strlen(name));
445
/**************************************************************************
446
Outputs a fixed-length string, quoted as an SQL identifier.
447
If the string contains a slash '/', the string will be
448
output as two identifiers separated by a period (.),
449
as in SQL database_name.identifier. */
454
FILE* f, /* in: output stream */
455
trx_t* trx, /* in: transaction (NULL=no quotes) */
456
ibool table_id,/* in: TRUE=print a table name,
457
FALSE=print other identifier */
458
const char* name, /* in: name to print */
459
ulint namelen)/* in: length of name */
461
#ifdef UNIV_HOTBACKUP
462
fwrite(name, 1, namelen, f);
465
char* slash = memchr(name, '/', namelen);
471
/* Print the database name and table name separately. */
472
innobase_print_identifier(f, trx, TRUE, name, slash - name);
474
innobase_print_identifier(f, trx, TRUE, slash + 1,
475
namelen - (slash - name) - 1);
478
innobase_print_identifier(f, trx, table_id, name, namelen);
483
/**************************************************************************
489
FILE* dest, /* in: output file */
490
FILE* src) /* in: input file to be appended to output */
492
long len = ftell(src);
497
size_t maxs = len < (long) sizeof buf
500
size_t size = fread(buf, 1, maxs, src);
501
fwrite(buf, 1, size, dest);
509
/**************************************************************************
516
/* out: number of characters that would
517
have been printed if the size were
518
unlimited, not including the terminating
520
char* str, /* out: string */
521
size_t size, /* in: str size */
522
const char* fmt, /* in: format */
523
...) /* in: format values */
532
res = _vscprintf(fmt, ap1);
536
_vsnprintf(str, size, fmt, ap2);
538
if ((size_t) res >= size) {
539
str[size - 1] = '\0';