Drizzled Public API Documentation

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