1
/*****************************************************************************
3
Copyright (C) 1994, 2010, 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., 51 Franklin
22
St, Fifth Floor, Boston, MA 02110-1301 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 */
148
118
return(time(NULL));
151
#ifndef UNIV_HOTBACKUP
152
/**********************************************************//**
121
/**************************************************************
153
122
Returns system time.
154
123
Upon successful completion, the value 0 is returned; otherwise the
155
124
value -1 is returned and the global variable errno is set to indicate the
157
@return 0 on success, -1 otherwise */
162
ulint* sec, /*!< out: seconds since the Epoch */
163
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 */
165
134
struct timeval tv;
194
/**********************************************************//**
163
/**************************************************************
195
164
Returns the number of microseconds since epoch. Similar to
196
165
time(3), the return value is also stored in *tloc, provided
197
that tloc is non-NULL.
198
@return us since epoch */
166
that tloc is non-NULL. */
203
ullint* tloc) /*!< out: us since epoch, if non-NULL */
171
/* out: us since epoch */
172
ullint* tloc) /* out: us since epoch, if non-NULL */
205
174
struct timeval tv;
219
/**********************************************************//**
220
Returns the number of milliseconds since some epoch. The
221
value may wrap around. It should only be used for heuristic
223
@return ms since epoch */
231
ut_gettimeofday(&tv, NULL);
233
return((ulint) tv.tv_sec * 1000 + tv.tv_usec / 1000);
235
#endif /* !UNIV_HOTBACKUP */
237
/**********************************************************//**
238
Returns the difference of two times in seconds.
239
@return time2 - time1 expressed in seconds */
188
/**************************************************************
189
Returns the difference of two times in seconds. */
244
ib_time_t time2, /*!< in: time */
245
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 */
247
198
return(difftime(time2, time1));
250
/**********************************************************//**
201
/**************************************************************
251
202
Prints a timestamp to a file. */
254
205
ut_print_timestamp(
255
206
/*===============*/
256
FILE* file) /*!< in: file where to print */
207
FILE* file) /* in: file where to print */
259
210
SYSTEMTIME cal_tm;
381
/**********************************************************//**
331
/**************************************************************
382
332
Returns current year, month, day. */
385
335
ut_get_year_month_day(
386
336
/*==================*/
387
ulint* year, /*!< out: current year */
388
ulint* month, /*!< out: month */
389
ulint* day) /*!< out: day */
337
ulint* year, /* out: current year */
338
ulint* month, /* out: month */
339
ulint* day) /* out: day */
392
342
SYSTEMTIME cal_tm;
414
364
*day = (ulint)cal_tm_ptr->tm_mday;
417
#endif /* UNIV_HOTBACKUP */
419
#ifndef UNIV_HOTBACKUP
420
/*************************************************************//**
368
/*****************************************************************
421
369
Runs an idle loop on CPU. The argument gives the desired delay
422
in microseconds on 100 MHz Pentium + Visual C++.
423
@return dummy value */
370
in microseconds on 100 MHz Pentium + Visual C++. */
428
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 */
445
#endif /* !UNIV_HOTBACKUP */
447
/*************************************************************//**
393
/*****************************************************************
448
394
Prints the contents of a memory buffer in hex and ascii. */
453
FILE* file, /*!< in: file where to print */
454
const void* buf, /*!< in: memory buffer */
455
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 */
457
403
const byte* data;
537
FILE* f, /*!< in: output stream */
538
trx_t* trx, /*!< in: transaction */
539
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,
540
486
FALSE=print other identifier */
541
const char* name) /*!< in: name to print */
487
const char* name) /* in: name to print */
543
489
ut_print_namel(f, trx, table_id, name, strlen(name));
546
/**********************************************************************//**
492
/**************************************************************************
547
493
Outputs a fixed-length string, quoted as an SQL identifier.
548
494
If the string contains a slash '/', the string will be
549
495
output as two identifiers separated by a period (.),
555
FILE* f, /*!< in: output stream */
556
trx_t* trx, /*!< in: transaction (NULL=no quotes) */
557
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,
558
504
FALSE=print other identifier */
559
const char* name, /*!< in: name to print */
560
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);
562
511
/* 2 * NAME_LEN for database and table name,
563
512
and some slack for the #mysql50# prefix and quotes */
564
513
char buf[3 * NAME_LEN];
572
521
ssize_t ret= fwrite(buf, 1, bufend - buf, f);
573
522
assert(ret==bufend-buf);
576
/**********************************************************************//**
526
/**************************************************************************
577
527
Catenate files. */
582
FILE* dest, /*!< in: output file */
583
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 */
585
535
long len = ftell(src);
600
550
} while (len > 0);
602
#endif /* !UNIV_HOTBACKUP */
553
/**************************************************************************
606
/**********************************************************************//**
607
A substitute for snprintf(3), formatted output conversion into
609
@return number of characters that would have been printed if the size
610
were unlimited, not including the terminating '\0'. */
615
char* str, /*!< out: string */
616
size_t size, /*!< in: str size */
617
const char* fmt, /*!< in: format */
618
...) /*!< 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 */
643
592
#endif /* __WIN__ */
645
/*************************************************************//**
646
Convert an error number to a human readable text message. The
647
returned string is static and should not be freed or modified.
648
@return string, describing the error */
653
enum db_err num) /*!< in: error number */
658
case DB_SUCCESS_LOCKED_REC:
659
return("Success, record lock created");
661
return("Generic error");
663
return("Operation interrupted");
664
case DB_OUT_OF_MEMORY:
665
return("Cannot allocate memory");
666
case DB_OUT_OF_FILE_SPACE:
667
return("Out of disk space");
674
case DB_DUPLICATE_KEY:
675
return("Duplicate key");
676
case DB_QUE_THR_SUSPENDED:
677
return("The queue thread has been suspended");
678
case DB_MISSING_HISTORY:
679
return("Required history data has been deleted");
680
case DB_CLUSTER_NOT_FOUND:
681
return("Cluster not found");
682
case DB_TABLE_NOT_FOUND:
683
return("Table not found");
684
case DB_MUST_GET_MORE_FILE_SPACE:
685
return("More file space needed");
686
case DB_TABLE_IS_BEING_USED:
687
return("Table is being used");
688
case DB_TOO_BIG_RECORD:
689
return("Record too big");
690
case DB_LOCK_WAIT_TIMEOUT:
691
return("Lock wait timeout");
692
case DB_NO_REFERENCED_ROW:
693
return("Referenced key value not found");
694
case DB_ROW_IS_REFERENCED:
695
return("Row is referenced");
696
case DB_CANNOT_ADD_CONSTRAINT:
697
return("Cannot add constraint");
699
return("Data structure corruption");
700
case DB_COL_APPEARS_TWICE_IN_INDEX:
701
return("Column appears twice in index");
702
case DB_CANNOT_DROP_CONSTRAINT:
703
return("Cannot drop constraint");
704
case DB_NO_SAVEPOINT:
705
return("No such savepoint");
706
case DB_TABLESPACE_ALREADY_EXISTS:
707
return("Tablespace already exists");
708
case DB_TABLESPACE_DELETED:
709
return("No such tablespace");
710
case DB_LOCK_TABLE_FULL:
711
return("Lock structs have exhausted the buffer pool");
712
case DB_FOREIGN_DUPLICATE_KEY:
713
return("Foreign key activated with duplicate keys");
714
case DB_FOREIGN_EXCEED_MAX_CASCADE:
715
return("Foreign key cascade delete/update exceeds max depth");
716
case DB_TOO_MANY_CONCURRENT_TRXS:
717
return("Too many concurrent transactions");
719
return("Unsupported");
720
case DB_PRIMARY_KEY_IS_NULL:
721
return("Primary key is NULL");
722
case DB_STATS_DO_NOT_EXIST:
723
return("Persistent statistics do not exist");
725
return("Failed, retry may succeed");
731
return("Failed, retry will not succeed");
732
case DB_ZIP_OVERFLOW:
733
return("Zip overflow");
734
case DB_RECORD_NOT_FOUND:
735
return("Record not found");
736
case DB_CHILD_NO_INDEX:
737
return("No index on referencing keys in referencing table");
738
case DB_PARENT_NO_INDEX:
739
return("No index on referenced keys in referenced table");
740
case DB_END_OF_INDEX:
741
return("End of index");
742
/* do not add default: in order to produce a warning if new code
743
is added to the enum but not added here */
746
/* we abort here because if unknown error code is given, this could
747
mean that memory corruption has happened and someone's error-code
748
variable has been overwritten with bogus data */
752
return("Unknown error");