drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
ut0ut.cc
1/*****************************************************************************
2
3Copyright (c) 2011, Oracle Corpn. All Rights Reserved.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License as published by the Free Software
7Foundation; version 2 of the License.
8
9This program is distributed in the hope that it will be useful, but WITHOUT
10ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12
13You should have received a copy of the GNU General Public License along with
14this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
15St, Fifth Floor, Boston, MA 02110-1301 USA
16
17*****************************************************************************/
18
19/***************************************************************/
25
26#include "ut0ut.h"
27
28#ifdef UNIV_NONINL
29#include "ut0ut.ic"
30#endif
31
32#include <stdarg.h>
33#include <string.h>
34#include <ctype.h>
35#include <sys/time.h>
36#include <time.h>
37
38#ifndef UNIV_HOTBACKUP
39# include "trx0trx.h"
40# if defined(BUILD_DRIZZLE)
41# include "drizzled/common.h"
42# else
43# include "ha_prototypes.h"
44# include "mysql_com.h" /* NAME_LEN */
45# endif /* DRIZZLE */
46#endif /* UNIV_HOTBACKUP */
47#include <errno.h>
48#include <assert.h>
49
51UNIV_INTERN ibool ut_always_false = FALSE;
52
53#ifdef __WIN__
54/*****************************************************************/
58#define WIN_TO_UNIX_DELTA_USEC ((ib_int64_t) 11644473600000000ULL)
59
60
61/*****************************************************************/
64static
65int
66ut_gettimeofday(
67/*============*/
68 struct timeval* tv,
69 void* tz)
70{
71 FILETIME ft;
72 ib_int64_t tm;
73
74 if (!tv) {
75 errno = EINVAL;
76 return(-1);
77 }
78
79 GetSystemTimeAsFileTime(&ft);
80
81 tm = (ib_int64_t) ft.dwHighDateTime << 32;
82 tm |= ft.dwLowDateTime;
83
84 ut_a(tm >= 0); /* If tm wraps over to negative, the quotient / 10
85 does not work */
86
87 tm /= 10; /* Convert from 100 nsec periods to usec */
88
89 /* If we don't convert to the Unix epoch the value for
90 struct timeval::tv_sec will overflow.*/
91 tm -= WIN_TO_UNIX_DELTA_USEC;
92
93 tv->tv_sec = (long) (tm / 1000000L);
94 tv->tv_usec = (long) (tm % 1000000L);
95
96 return(0);
97}
98#else
101#define ut_gettimeofday gettimeofday
102#endif
103
104/********************************************************/
109UNIV_INTERN
110ulint
112/*==========*/
113 ulint a)
114{
115 ib_int64_t i;
116
117 i = (ib_int64_t)a;
118
119 i = i >> 32;
120
121 return((ulint)i);
122}
123
124/**********************************************************/
128UNIV_INTERN
131/*=========*/
132{
133 return(time(NULL));
134}
135
136#ifndef UNIV_HOTBACKUP
137/**********************************************************/
143UNIV_INTERN
144int
146/*========*/
147 ulint* sec,
148 ulint* ms)
149{
150 struct timeval tv;
151 int ret;
152 int errno_gettimeofday;
153 int i;
154
155 for (i = 0; i < 10; i++) {
156
157 ret = ut_gettimeofday(&tv, NULL);
158
159 if (ret == -1) {
160 errno_gettimeofday = errno;
161 ut_print_timestamp(stderr);
162 fprintf(stderr, " InnoDB: gettimeofday(): %s\n",
163 strerror(errno_gettimeofday));
164 os_thread_sleep(100000); /* 0.1 sec */
165 errno = errno_gettimeofday;
166 } else {
167 break;
168 }
169 }
170
171 if (ret != -1) {
172 *sec = (ulint) tv.tv_sec;
173 *ms = (ulint) tv.tv_usec;
174 }
175
176 return(ret);
177}
178
179/**********************************************************/
184UNIV_INTERN
185ullint
187/*=======*/
188 ullint* tloc)
189{
190 struct timeval tv;
191 ullint us;
192
193 ut_gettimeofday(&tv, NULL);
194
195 us = (ullint) tv.tv_sec * 1000000 + tv.tv_usec;
196
197 if (tloc != NULL) {
198 *tloc = us;
199 }
200
201 return(us);
202}
203
204/**********************************************************/
209UNIV_INTERN
210ulint
212/*============*/
213{
214 struct timeval tv;
215
216 ut_gettimeofday(&tv, NULL);
217
218 return((ulint) tv.tv_sec * 1000 + tv.tv_usec / 1000);
219}
220#endif /* !UNIV_HOTBACKUP */
221
222/**********************************************************/
225UNIV_INTERN
226double
228/*========*/
229 ib_time_t time2,
230 ib_time_t time1)
231{
232 return(difftime(time2, time1));
233}
234
235/**********************************************************/
237UNIV_INTERN
238void
240/*===============*/
241 FILE* file)
242{
243#ifdef __WIN__
244 SYSTEMTIME cal_tm;
245
246 GetLocalTime(&cal_tm);
247
248 fprintf(file,"%02d%02d%02d %2d:%02d:%02d",
249 (int)cal_tm.wYear % 100,
250 (int)cal_tm.wMonth,
251 (int)cal_tm.wDay,
252 (int)cal_tm.wHour,
253 (int)cal_tm.wMinute,
254 (int)cal_tm.wSecond);
255#else
256 struct tm cal_tm;
257 struct tm* cal_tm_ptr;
258 time_t tm;
259
260 time(&tm);
261
262#ifdef HAVE_LOCALTIME_R
263 localtime_r(&tm, &cal_tm);
264 cal_tm_ptr = &cal_tm;
265#else
266 cal_tm_ptr = localtime(&tm);
267#endif
268 fprintf(file,"%02d%02d%02d %2d:%02d:%02d",
269 cal_tm_ptr->tm_year % 100,
270 cal_tm_ptr->tm_mon + 1,
271 cal_tm_ptr->tm_mday,
272 cal_tm_ptr->tm_hour,
273 cal_tm_ptr->tm_min,
274 cal_tm_ptr->tm_sec);
275#endif
276}
277
278/**********************************************************/
280UNIV_INTERN
281void
283/*=================*/
284 char* buf)
285{
286#ifdef __WIN__
287 SYSTEMTIME cal_tm;
288
289 GetLocalTime(&cal_tm);
290
291 sprintf(buf, "%02d%02d%02d %2d:%02d:%02d",
292 (int)cal_tm.wYear % 100,
293 (int)cal_tm.wMonth,
294 (int)cal_tm.wDay,
295 (int)cal_tm.wHour,
296 (int)cal_tm.wMinute,
297 (int)cal_tm.wSecond);
298#else
299 struct tm cal_tm;
300 struct tm* cal_tm_ptr;
301 time_t tm;
302
303 time(&tm);
304
305#ifdef HAVE_LOCALTIME_R
306 localtime_r(&tm, &cal_tm);
307 cal_tm_ptr = &cal_tm;
308#else
309 cal_tm_ptr = localtime(&tm);
310#endif
311 sprintf(buf, "%02d%02d%02d %2d:%02d:%02d",
312 cal_tm_ptr->tm_year % 100,
313 cal_tm_ptr->tm_mon + 1,
314 cal_tm_ptr->tm_mday,
315 cal_tm_ptr->tm_hour,
316 cal_tm_ptr->tm_min,
317 cal_tm_ptr->tm_sec);
318#endif
319}
320
321#ifdef UNIV_HOTBACKUP
322/**********************************************************/
325UNIV_INTERN
326void
327ut_sprintf_timestamp_without_extra_chars(
328/*=====================================*/
329 char* buf)
330{
331#ifdef __WIN__
332 SYSTEMTIME cal_tm;
333
334 GetLocalTime(&cal_tm);
335
336 sprintf(buf, "%02d%02d%02d_%2d_%02d_%02d",
337 (int)cal_tm.wYear % 100,
338 (int)cal_tm.wMonth,
339 (int)cal_tm.wDay,
340 (int)cal_tm.wHour,
341 (int)cal_tm.wMinute,
342 (int)cal_tm.wSecond);
343#else
344 struct tm cal_tm;
345 struct tm* cal_tm_ptr;
346 time_t tm;
347
348 time(&tm);
349
350#ifdef HAVE_LOCALTIME_R
351 localtime_r(&tm, &cal_tm);
352 cal_tm_ptr = &cal_tm;
353#else
354 cal_tm_ptr = localtime(&tm);
355#endif
356 sprintf(buf, "%02d%02d%02d_%2d_%02d_%02d",
357 cal_tm_ptr->tm_year % 100,
358 cal_tm_ptr->tm_mon + 1,
359 cal_tm_ptr->tm_mday,
360 cal_tm_ptr->tm_hour,
361 cal_tm_ptr->tm_min,
362 cal_tm_ptr->tm_sec);
363#endif
364}
365
366/**********************************************************/
368UNIV_INTERN
369void
370ut_get_year_month_day(
371/*==================*/
372 ulint* year,
373 ulint* month,
374 ulint* day)
375{
376#ifdef __WIN__
377 SYSTEMTIME cal_tm;
378
379 GetLocalTime(&cal_tm);
380
381 *year = (ulint)cal_tm.wYear;
382 *month = (ulint)cal_tm.wMonth;
383 *day = (ulint)cal_tm.wDay;
384#else
385 struct tm cal_tm;
386 struct tm* cal_tm_ptr;
387 time_t tm;
388
389 time(&tm);
390
391#ifdef HAVE_LOCALTIME_R
392 localtime_r(&tm, &cal_tm);
393 cal_tm_ptr = &cal_tm;
394#else
395 cal_tm_ptr = localtime(&tm);
396#endif
397 *year = (ulint)cal_tm_ptr->tm_year + 1900;
398 *month = (ulint)cal_tm_ptr->tm_mon + 1;
399 *day = (ulint)cal_tm_ptr->tm_mday;
400#endif
401}
402#endif /* UNIV_HOTBACKUP */
403
404#ifndef UNIV_HOTBACKUP
405/*************************************************************/
409UNIV_INTERN
410ulint
412/*=====*/
413 ulint delay)
414{
415 ulint i, j;
416
417 j = 0;
418
419 for (i = 0; i < delay * 50; i++) {
420 j += i;
421 UT_RELAX_CPU();
422 }
423
424 if (ut_always_false) {
425 ut_always_false = (ibool) j;
426 }
427
428 return(j);
429}
430#endif /* !UNIV_HOTBACKUP */
431
432/*************************************************************/
434UNIV_INTERN
435void
437/*=========*/
438 FILE* file,
439 const void* buf,
440 ulint len)
441{
442 const byte* data;
443 ulint i;
444
445 UNIV_MEM_ASSERT_RW(buf, len);
446
447 fprintf(file, " len %lu; hex ", len);
448
449 for (data = (const byte*)buf, i = 0; i < len; i++) {
450 fprintf(file, "%02lx", (ulong)*data++);
451 }
452
453 fputs("; asc ", file);
454
455 data = (const byte*)buf;
456
457 for (i = 0; i < len; i++) {
458 int c = (int) *data++;
459 putc(isprint(c) ? c : ' ', file);
460 }
461
462 putc(';', file);
463}
464
465/*************************************************************/
468UNIV_INTERN
469ulint
471/*==========*/
472 ulint n)
473{
474 ulint res;
475
476 res = 1;
477
478 ut_ad(n > 0);
479
480 while (res < n) {
481 res = res * 2;
482 }
483
484 return(res);
485}
486
487/**********************************************************************/
489UNIV_INTERN
490void
492/*==============*/
493 FILE* f,
494 const char* name)
495{
496 putc('\'', f);
497 for (;;) {
498 int c = *name++;
499 switch (c) {
500 case 0:
501 goto done;
502 case '\'':
503 putc(c, f);
504 /* fall through */
505 default:
506 putc(c, f);
507 }
508 }
509done:
510 putc('\'', f);
511}
512#ifndef UNIV_HOTBACKUP
513/**********************************************************************/
518UNIV_INTERN
519void
521/*==========*/
522 FILE* f,
523 trx_t* trx,
524 ibool table_id,
526 const char* name)
527{
528 ut_print_namel(f, trx, table_id, name, strlen(name));
529}
530
531/**********************************************************************/
536UNIV_INTERN
537void
539/*===========*/
540 FILE* f,
541 trx_t* trx,
542 ibool table_id,
544 const char* name,
545 ulint namelen)
546{
547 /* 2 * NAME_LEN for database and table name,
548 and some slack for the #mysql50# prefix and quotes */
549 char buf[3 * NAME_LEN];
550 const char* bufend;
551
552 bufend = innobase_convert_name(buf, sizeof buf,
553 name, namelen,
554 trx ? trx->mysql_thd : NULL,
555 table_id);
556
557 ssize_t ret= fwrite(buf, 1, bufend - buf, f);
558 assert(ret==bufend-buf);
559}
560
561/**********************************************************************/
563UNIV_INTERN
564void
566/*=========*/
567 FILE* dest,
568 FILE* src)
569{
570 long len = ftell(src);
571 char buf[4096];
572
573 rewind(src);
574 do {
575 size_t maxs = len < (long) sizeof buf
576 ? (size_t) len
577 : sizeof buf;
578 size_t size = fread(buf, 1, maxs, src);
579 size_t ret= fwrite(buf, 1, size, dest);
580 assert(ret==size);
581 len -= (long) size;
582 if (size < maxs) {
583 break;
584 }
585 } while (len > 0);
586}
587#endif /* !UNIV_HOTBACKUP */
588
589#ifdef __WIN__
590# include <stdarg.h>
591/**********************************************************************/
596UNIV_INTERN
597int
599/*========*/
600 char* str,
601 size_t size,
602 const char* fmt,
603 ...)
604{
605 int res;
606 va_list ap1;
607 va_list ap2;
608
609 va_start(ap1, fmt);
610 va_start(ap2, fmt);
611
612 res = _vscprintf(fmt, ap1);
613 ut_a(res != -1);
614
615 if (size > 0) {
616 _vsnprintf(str, size, fmt, ap2);
617
618 if ((size_t) res >= size) {
619 str[size - 1] = '\0';
620 }
621 }
622
623 va_end(ap1);
624 va_end(ap2);
625
626 return(res);
627}
628#endif /* __WIN__ */
629
630/*************************************************************/
634UNIV_INTERN
635const char*
637/*======*/
638 enum db_err num)
639{
640 switch (num) {
641 case DB_SUCCESS:
642 return("Success");
644 return("Success, record lock created");
645 case DB_ERROR:
646 return("Generic error");
647 case DB_INTERRUPTED:
648 return("Operation interrupted");
649 case DB_OUT_OF_MEMORY:
650 return("Cannot allocate memory");
651 case DB_OUT_OF_FILE_SPACE:
652 return("Out of disk space");
653 case DB_LOCK_WAIT:
654 return("Lock wait");
655 case DB_DEADLOCK:
656 return("Deadlock");
657 case DB_ROLLBACK:
658 return("Rollback");
659 case DB_DUPLICATE_KEY:
660 return("Duplicate key");
661 case DB_QUE_THR_SUSPENDED:
662 return("The queue thread has been suspended");
663 case DB_MISSING_HISTORY:
664 return("Required history data has been deleted");
665 case DB_CLUSTER_NOT_FOUND:
666 return("Cluster not found");
667 case DB_TABLE_NOT_FOUND:
668 return("Table not found");
669 case DB_MUST_GET_MORE_FILE_SPACE:
670 return("More file space needed");
671 case DB_TABLE_IS_BEING_USED:
672 return("Table is being used");
673 case DB_TOO_BIG_RECORD:
674 return("Record too big");
675 case DB_LOCK_WAIT_TIMEOUT:
676 return("Lock wait timeout");
677 case DB_NO_REFERENCED_ROW:
678 return("Referenced key value not found");
679 case DB_ROW_IS_REFERENCED:
680 return("Row is referenced");
681 case DB_CANNOT_ADD_CONSTRAINT:
682 return("Cannot add constraint");
683 case DB_CORRUPTION:
684 return("Data structure corruption");
685 case DB_COL_APPEARS_TWICE_IN_INDEX:
686 return("Column appears twice in index");
687 case DB_CANNOT_DROP_CONSTRAINT:
688 return("Cannot drop constraint");
689 case DB_NO_SAVEPOINT:
690 return("No such savepoint");
691 case DB_TABLESPACE_ALREADY_EXISTS:
692 return("Tablespace already exists");
693 case DB_TABLESPACE_DELETED:
694 return("No such tablespace");
695 case DB_LOCK_TABLE_FULL:
696 return("Lock structs have exhausted the buffer pool");
697 case DB_FOREIGN_DUPLICATE_KEY:
698 return("Foreign key activated with duplicate keys");
699 case DB_FOREIGN_EXCEED_MAX_CASCADE:
700 return("Foreign key cascade delete/update exceeds max depth");
701 case DB_TOO_MANY_CONCURRENT_TRXS:
702 return("Too many concurrent transactions");
703 case DB_UNSUPPORTED:
704 return("Unsupported");
705 case DB_PRIMARY_KEY_IS_NULL:
706 return("Primary key is NULL");
707 case DB_STATS_DO_NOT_EXIST:
708 return("Persistent statistics do not exist");
709 case DB_FAIL:
710 return("Failed, retry may succeed");
711 case DB_OVERFLOW:
712 return("Overflow");
713 case DB_UNDERFLOW:
714 return("Underflow");
715 case DB_STRONG_FAIL:
716 return("Failed, retry will not succeed");
717 case DB_ZIP_OVERFLOW:
718 return("Zip overflow");
719 case DB_RECORD_NOT_FOUND:
720 return("Record not found");
721 case DB_CHILD_NO_INDEX:
722 return("No index on referencing keys in referencing table");
723 case DB_PARENT_NO_INDEX:
724 return("No index on referenced keys in referenced table");
725 case DB_END_OF_INDEX:
726 return("End of index");
727 /* do not add default: in order to produce a warning if new code
728 is added to the enum but not added here */
729 }
730
731 /* we abort here because if unknown error code is given, this could
732 mean that memory corruption has happened and someone's error-code
733 variable has been overwritten with bogus data */
734 ut_error;
735
736 /* NOT REACHED */
737 return("Unknown error");
738}
db_err
Definition db0err.h:31
@ DB_SUCCESS_LOCKED_REC
Definition db0err.h:32
UNIV_INTERN char * innobase_convert_name(char *buf, ulint buflen, const char *id, ulint idlen, drizzled::Session *session, ibool table_id)
UNIV_INTERN void os_thread_sleep(ulint tm)
Definition os0thread.cc:265
drizzled::Session * mysql_thd
Definition trx0trx.h:559
#define ut_error
Definition ut0dbg.h:115
#define ut_ad(EXPR)
Definition ut0dbg.h:127
#define ut_a(EXPR)
Definition ut0dbg.h:105
UNIV_INTERN ulint ut_2_power_up(ulint n) __attribute__((const))
UNIV_INTERN ulint ut_time_ms(void)
Definition ut0ut.cc:211
UNIV_INTERN ib_time_t ut_time(void)
Definition ut0ut.cc:130
UNIV_INTERN void ut_print_buf(FILE *file, const void *buf, ulint len)
Definition ut0ut.cc:436
UNIV_INTERN int ut_usectime(ulint *sec, ulint *ms)
Definition ut0ut.cc:145
UNIV_INTERN void ut_print_timestamp(FILE *file)
Definition ut0ut.cc:239
UNIV_INTERN void ut_print_name(FILE *f, struct trx_struct *trx, ibool table_id, const char *name)
Definition ut0ut.cc:520
UNIV_INTERN ulint ut_delay(ulint delay)
Definition ut0ut.cc:411
#define ut_snprintf
Definition ut0ut.h:398
UNIV_INTERN void ut_sprintf_timestamp(char *buf)
Definition ut0ut.cc:282
UNIV_INTERN void ut_copy_file(FILE *dest, FILE *src)
Definition ut0ut.cc:565
UNIV_INTERN void ut_print_namel(FILE *f, struct trx_struct *trx, ibool table_id, const char *name, ulint namelen)
Definition ut0ut.cc:538
UNIV_INTERN ulint ut_get_high32(ulint a)
Definition ut0ut.cc:111
UNIV_INTERN void ut_print_filename(FILE *f, const char *name)
Definition ut0ut.cc:491
UNIV_INTERN ullint ut_time_us(ullint *tloc)
Definition ut0ut.cc:186
UNIV_INTERN const char * ut_strerr(enum db_err num)
Definition ut0ut.cc:636
UNIV_INTERN double ut_difftime(ib_time_t time2, ib_time_t time1)
Definition ut0ut.cc:227
time_t ib_time_t
Definition ut0ut.h:56