1
/*****************************************************************************
3
Copyright (c) 1994, 2009, Innobase Oy. All Rights Reserved.
4
Copyright (c) 2009, Sun Microsystems, Inc.
6
Portions of this file contain modifications contributed and copyrighted by
7
Sun Microsystems, Inc. Those modifications are gratefully acknowledged and
8
are described briefly in the InnoDB documentation. The contributions by
9
Sun Microsystems are incorporated with their permission, and subject to the
10
conditions contained in the file COPYING.Sun_Microsystems.
12
This program is free software; you can redistribute it and/or modify it under
13
the terms of the GNU General Public License as published by the Free Software
14
Foundation; version 2 of the License.
16
This program is distributed in the hope that it will be useful, but WITHOUT
17
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
20
You should have received a copy of the GNU General Public License along with
21
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22
Place, Suite 330, Boston, MA 02111-1307 USA
24
*****************************************************************************/
26
/***************************************************************//**
1
/*******************************************************************
28
2
Various utilities for Innobase.
4
(c) 1994, 1995 Innobase Oy
30
6
Created 5/11/1994 Heikki Tuuri
31
7
********************************************************************/
58
# include "ha_prototypes.h"
59
35
# include "mysql_com.h" /* NAME_LEN */
60
36
# endif /* DRIZZLE */
61
37
#endif /* UNIV_HOTBACKUP */
65
/** A constant to prevent the compiler from optimizing ut_delay() away. */
66
39
UNIV_INTERN ibool ut_always_false = FALSE;
69
/*****************************************************************//**
42
/*********************************************************************
70
43
NOTE: The Windows epoch starts from 1601/01/01 whereas the Unix
71
44
epoch starts from 1970/1/1. For selection of constant see:
72
45
http://support.microsoft.com/kb/167296/ */
73
46
#define WIN_TO_UNIX_DELTA_USEC ((ib_int64_t) 11644473600000000ULL)
76
/*****************************************************************//**
77
This is the Windows version of gettimeofday(2).
78
@return 0 if all OK else -1 */
49
/*********************************************************************
50
This is the Windows version of gettimeofday(2).*/
83
struct timeval* tv, /*!< out: Values are relative to Unix epoch */
84
void* tz) /*!< in: not used */
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 */
114
/** An alias for gettimeofday(2). On Microsoft Windows, we have to
115
reimplement this function. */
116
87
#define ut_gettimeofday gettimeofday
119
/********************************************************//**
90
/************************************************************
120
91
Gets the high 32 bits in a ulint. That is makes a shift >> 32,
121
92
but since there seem to be compiler bugs in both gcc and Visual C++,
122
we do this by a special conversion.
93
we do this by a special conversion. */
128
ulint a) /*!< in: ulint */
99
ulint a) /* in: ulint */
136
107
return((ulint)i);
139
/**********************************************************//**
110
/**************************************************************
140
111
Returns system time. We do not specify the format of the time returned:
141
the only way to manipulate it is to use the function ut_difftime.
142
@return system time */
112
the only way to manipulate it is to use the function ut_difftime. */
148
118
return(time(NULL));
151
/**********************************************************//**
121
/**************************************************************
152
122
Returns system time.
153
123
Upon successful completion, the value 0 is returned; otherwise the
154
124
value -1 is returned and the global variable errno is set to indicate the
156
@return 0 on success, -1 otherwise */
161
ulint* sec, /*!< out: seconds since the Epoch */
162
ulint* ms) /*!< out: microseconds since the Epoch+*sec */
130
/* out: 0 on success, -1 otherwise */
131
ulint* sec, /* out: seconds since the Epoch */
132
ulint* ms) /* out: microseconds since the Epoch+*sec */
164
134
struct timeval tv;
193
/**********************************************************//**
163
/**************************************************************
194
164
Returns the number of microseconds since epoch. Similar to
195
165
time(3), the return value is also stored in *tloc, provided
196
that tloc is non-NULL.
197
@return us since epoch */
166
that tloc is non-NULL. */
202
ullint* tloc) /*!< out: us since epoch, if non-NULL */
171
/* out: us since epoch */
172
ullint* tloc) /* out: us since epoch, if non-NULL */
204
174
struct timeval tv;
218
/**********************************************************//**
219
Returns the difference of two times in seconds.
220
@return time2 - time1 expressed in seconds */
188
/**************************************************************
189
Returns the difference of two times in seconds. */
225
ib_time_t time2, /*!< in: time */
226
ib_time_t time1) /*!< in: time */
194
/* out: time2 - time1 expressed in seconds */
195
ib_time_t time2, /* in: time */
196
ib_time_t time1) /* in: time */
228
198
return(difftime(time2, time1));
231
/**********************************************************//**
201
/**************************************************************
232
202
Prints a timestamp to a file. */
235
205
ut_print_timestamp(
236
206
/*===============*/
237
FILE* file) /*!< in: file where to print */
207
FILE* file) /* in: file where to print */
240
210
SYSTEMTIME cal_tm;
274
/**********************************************************//**
244
/**************************************************************
275
245
Sprintfs a timestamp to a buffer, 13..14 chars plus terminating NUL. */
278
248
ut_sprintf_timestamp(
279
249
/*=================*/
280
char* buf) /*!< in: buffer where to sprintf */
250
char* buf) /* in: buffer where to sprintf */
283
253
SYSTEMTIME cal_tm;
317
#ifdef UNIV_HOTBACKUP
318
/**********************************************************//**
287
/**************************************************************
319
288
Sprintfs a timestamp to a buffer with no spaces and with ':' characters
320
289
replaced by '_'. */
323
292
ut_sprintf_timestamp_without_extra_chars(
324
293
/*=====================================*/
325
char* buf) /*!< in: buffer where to sprintf */
294
char* buf) /* in: buffer where to sprintf */
328
297
SYSTEMTIME cal_tm;
362
/**********************************************************//**
331
/**************************************************************
363
332
Returns current year, month, day. */
366
335
ut_get_year_month_day(
367
336
/*==================*/
368
ulint* year, /*!< out: current year */
369
ulint* month, /*!< out: month */
370
ulint* day) /*!< out: day */
337
ulint* year, /* out: current year */
338
ulint* month, /* out: month */
339
ulint* day) /* out: day */
373
342
SYSTEMTIME cal_tm;
395
364
*day = (ulint)cal_tm_ptr->tm_mday;
398
#endif /* UNIV_HOTBACKUP */
400
#ifndef UNIV_HOTBACKUP
401
/*************************************************************//**
368
/*****************************************************************
402
369
Runs an idle loop on CPU. The argument gives the desired delay
403
in microseconds on 100 MHz Pentium + Visual C++.
404
@return dummy value */
370
in microseconds on 100 MHz Pentium + Visual C++. */
409
ulint delay) /*!< in: delay in microseconds on 100 MHz Pentium */
375
/* out: dummy value */
376
ulint delay) /* in: delay in microseconds on 100 MHz Pentium */
426
#endif /* !UNIV_HOTBACKUP */
428
/*************************************************************//**
393
/*****************************************************************
429
394
Prints the contents of a memory buffer in hex and ascii. */
434
FILE* file, /*!< in: file where to print */
435
const void* buf, /*!< in: memory buffer */
436
ulint len) /*!< in: length of the buffer */
399
FILE* file, /* in: file where to print */
400
const void* buf, /* in: memory buffer */
401
ulint len) /* in: length of the buffer */
438
403
const byte* data;
461
/*************************************************************//**
462
Calculates fast the number rounded up to the nearest power of 2.
463
@return first power of 2 which is >= n */
426
/*****************************************************************
427
Calculates fast the number rounded up to the nearest power of 2. */
468
ulint n) /*!< in: number != 0 */
432
/* out: first power of 2 which is >= n */
433
ulint n) /* in: number != 0 */
483
/**********************************************************************//**
448
/**************************************************************************
484
449
Outputs a NUL-terminated file name, quoted with apostrophes. */
487
452
ut_print_filename(
488
453
/*==============*/
489
FILE* f, /*!< in: output stream */
490
const char* name) /*!< in: name to print */
454
FILE* f, /* in: output stream */
455
const char* name) /* in: name to print */
508
#ifndef UNIV_HOTBACKUP
509
/**********************************************************************//**
474
/**************************************************************************
510
475
Outputs a fixed-length string, quoted as an SQL identifier.
511
476
If the string contains a slash '/', the string will be
512
477
output as two identifiers separated by a period (.),
518
FILE* f, /*!< in: output stream */
519
trx_t* trx, /*!< in: transaction */
520
ibool table_id,/*!< in: TRUE=print a table name,
483
FILE* f, /* in: output stream */
484
trx_t* trx, /* in: transaction */
485
ibool table_id,/* in: TRUE=print a table name,
521
486
FALSE=print other identifier */
522
const char* name) /*!< in: name to print */
487
const char* name) /* in: name to print */
524
489
ut_print_namel(f, trx, table_id, name, strlen(name));
527
/**********************************************************************//**
492
/**************************************************************************
528
493
Outputs a fixed-length string, quoted as an SQL identifier.
529
494
If the string contains a slash '/', the string will be
530
495
output as two identifiers separated by a period (.),
536
FILE* f, /*!< in: output stream */
537
trx_t* trx, /*!< in: transaction (NULL=no quotes) */
538
ibool table_id,/*!< in: TRUE=print a table name,
501
FILE* f, /* in: output stream */
502
trx_t* trx, /* in: transaction (NULL=no quotes) */
503
ibool table_id,/* in: TRUE=print a table name,
539
504
FALSE=print other identifier */
540
const char* name, /*!< in: name to print */
541
ulint namelen)/*!< in: length of name */
505
const char* name, /* in: name to print */
506
ulint namelen)/* in: length of name */
508
#ifdef UNIV_HOTBACKUP
509
fwrite(name, 1, namelen, f);
543
511
/* 2 * NAME_LEN for database and table name,
544
512
and some slack for the #mysql50# prefix and quotes */
545
513
char buf[3 * NAME_LEN];
553
521
ssize_t ret= fwrite(buf, 1, bufend - buf, f);
554
522
assert(ret==bufend-buf);
557
/**********************************************************************//**
526
/**************************************************************************
558
527
Catenate files. */
563
FILE* dest, /*!< in: output file */
564
FILE* src) /*!< in: input file to be appended to output */
532
FILE* dest, /* in: output file */
533
FILE* src) /* in: input file to be appended to output */
566
535
long len = ftell(src);
581
550
} while (len > 0);
583
#endif /* !UNIV_HOTBACKUP */
553
/**************************************************************************
587
/**********************************************************************//**
588
A substitute for snprintf(3), formatted output conversion into
590
@return number of characters that would have been printed if the size
591
were unlimited, not including the terminating '\0'. */
596
char* str, /*!< out: string */
597
size_t size, /*!< in: str size */
598
const char* fmt, /*!< in: format */
599
...) /*!< in: format values */
560
/* out: number of characters that would
561
have been printed if the size were
562
unlimited, not including the terminating
564
char* str, /* out: string */
565
size_t size, /* in: str size */
566
const char* fmt, /* in: format */
567
...) /* in: format values */