pfs_global.cc 7.05 KB
Newer Older
Oleksandr Byelkin's avatar
Oleksandr Byelkin committed
1
/* Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights
Sergei Golubchik's avatar
Sergei Golubchik committed
2
   reserved.
3 4

  This program is free software; you can redistribute it and/or modify
Sergei Golubchik's avatar
Sergei Golubchik committed
5 6 7 8 9 10 11 12 13
  it under the terms of the GNU General Public License, version 2.0,
  as published by the Free Software Foundation.

  This program is also distributed with certain software (including
  but not limited to OpenSSL) that is licensed under separate terms,
  as designated in a particular file or component or in included license
  documentation.  The authors of MySQL hereby grant you an additional
  permission to link the program and your derivative works with the
  separately licensed software that they have included with MySQL.
14 15 16 17

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Sergei Golubchik's avatar
Sergei Golubchik committed
18
  GNU General Public License, version 2.0, for more details.
19 20 21 22 23 24 25 26 27 28 29 30 31

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software Foundation,
  51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */

/**
  @file storage/perfschema/pfs_global.cc
  Miscellaneous global dependencies (implementation).
*/

#include "my_global.h"
#include "my_sys.h"
#include "pfs_global.h"
Sergei Golubchik's avatar
Sergei Golubchik committed
32
#include "pfs_builtin_memory.h"
Sergei Golubchik's avatar
Sergei Golubchik committed
33
#include "log.h"
34 35 36 37 38 39 40

#include <stdlib.h>
#include <string.h>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
Sergei Golubchik's avatar
Sergei Golubchik committed
41 42 43 44 45 46 47 48
#ifdef _WIN32
#include <winsock2.h>
#endif
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
49 50 51 52 53 54
#endif

bool pfs_initialized= false;

/**
  Memory allocation for the performance schema.
Sergei Golubchik's avatar
Sergei Golubchik committed
55 56
  The memory used internally in the performance schema implementation.
  It is allocated at startup, or during runtime with scalable buffers.
57
*/
Sergei Golubchik's avatar
Sergei Golubchik committed
58
void *pfs_malloc(PFS_builtin_memory_class *klass, size_t size, myf flags)
59
{
Sergei Golubchik's avatar
Sergei Golubchik committed
60 61
  assert(klass != NULL);
  assert(size > 0);
62

Sergei Golubchik's avatar
Sergei Golubchik committed
63
  void *ptr= NULL;
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

#ifdef PFS_ALIGNEMENT
#ifdef HAVE_POSIX_MEMALIGN
  /* Linux */
  if (unlikely(posix_memalign(& ptr, PFS_ALIGNEMENT, size)))
    return NULL;
#else
#ifdef HAVE_MEMALIGN
  /* Solaris */
  ptr= memalign(PFS_ALIGNEMENT, size);
  if (unlikely(ptr == NULL))
    return NULL;
#else
#ifdef HAVE_ALIGNED_MALLOC
  /* Windows */
  ptr= _aligned_malloc(size, PFS_ALIGNEMENT);
  if (unlikely(ptr == NULL))
    return NULL;
#else
#error "Missing implementation for PFS_ALIGNENT"
#endif /* HAVE_ALIGNED_MALLOC */
#endif /* HAVE_MEMALIGN */
#endif /* HAVE_POSIX_MEMALIGN */
#else /* PFS_ALIGNMENT */
  /* Everything else */
  ptr= malloc(size);
  if (unlikely(ptr == NULL))
    return NULL;
#endif

Sergei Golubchik's avatar
Sergei Golubchik committed
94 95
  klass->count_alloc(size);

96 97 98 99 100
  if (flags & MY_ZEROFILL)
    memset(ptr, 0, size);
  return ptr;
}

Sergei Golubchik's avatar
Sergei Golubchik committed
101
void pfs_free(PFS_builtin_memory_class *klass, size_t size, void *ptr)
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
{
  if (ptr == NULL)
    return;

#ifdef HAVE_POSIX_MEMALIGN
  /* Allocated with posix_memalign() */
  free(ptr);
#else
#ifdef HAVE_MEMALIGN
  /* Allocated with memalign() */
  free(ptr);
#else
#ifdef HAVE_ALIGNED_MALLOC
  /* Allocated with _aligned_malloc() */
  _aligned_free(ptr);
#else
  /* Allocated with malloc() */
  free(ptr);
#endif /* HAVE_ALIGNED_MALLOC */
#endif /* HAVE_MEMALIGN */
#endif /* HAVE_POSIX_MEMALIGN */

Sergei Golubchik's avatar
Sergei Golubchik committed
124
  klass->count_free(size);
125 126
}

Sergei Golubchik's avatar
Sergei Golubchik committed
127 128 129
/**
  Array allocation for the performance schema.
  Checks for overflow of n * size before allocating.
Sergei Golubchik's avatar
Sergei Golubchik committed
130 131
  @param klass performance schema memory class
  @param n     number of array elements
Sergei Golubchik's avatar
Sergei Golubchik committed
132 133 134 135
  @param size  element size
  @param flags malloc flags
  @return pointer to memory on success, else NULL
*/
Sergei Golubchik's avatar
Sergei Golubchik committed
136
void *pfs_malloc_array(PFS_builtin_memory_class *klass, size_t n, size_t size, myf flags)
Sergei Golubchik's avatar
Sergei Golubchik committed
137
{
Sergei Golubchik's avatar
Sergei Golubchik committed
138 139 140
  assert(klass != NULL);
  assert(n > 0);
  assert(size > 0);
Sergei Golubchik's avatar
Sergei Golubchik committed
141
  void *ptr= NULL;
Sergei Golubchik's avatar
Sergei Golubchik committed
142 143 144
  size_t array_size= n * size;
  /* Check for overflow before allocating. */
  if (is_overflow(array_size, n, size))
Sergei Golubchik's avatar
Sergei Golubchik committed
145 146 147 148
  {
    sql_print_warning("Failed to allocate memory for %zu chunks each of size "
                      "%zu for buffer '%s' due to overflow", n, size,
                      klass->m_class.m_name);
Sergei Golubchik's avatar
Sergei Golubchik committed
149
    return NULL;
Sergei Golubchik's avatar
Sergei Golubchik committed
150 151 152 153 154 155 156 157
  }

  if(NULL == (ptr= pfs_malloc(klass, array_size, flags)))
  {
    sql_print_warning("Failed to allocate %zu bytes for buffer '%s' due to "
                      "out-of-memory", array_size, klass->m_class.m_name);
  }
  return ptr;
Sergei Golubchik's avatar
Sergei Golubchik committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
}

/**
  Free array allocated by @sa pfs_malloc_array.
  @param klass performance schema memory class
  @param n     number of array elements
  @param size  element size
  @param ptr   pointer to memory
*/
void pfs_free_array(PFS_builtin_memory_class *klass, size_t n, size_t size, void *ptr)
{
  if (ptr == NULL)
    return;
  size_t array_size= n * size;
  /* Overflow should have been detected by pfs_malloc_array. */
Sergei Golubchik's avatar
Sergei Golubchik committed
173
  assert(!is_overflow(array_size, n, size));
Sergei Golubchik's avatar
Sergei Golubchik committed
174
  return pfs_free(klass, array_size, ptr);
Sergei Golubchik's avatar
Sergei Golubchik committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
}

/**
  Detect multiplication overflow.
  @param product  multiplication product
  @param n1  operand
  @param n2  operand
  @return true if multiplication caused an overflow.
*/
bool is_overflow(size_t product, size_t n1, size_t n2)
{
  if (n1 != 0 && (product / n1 != n2))
    return true;
  else
    return false;
}

Sergei Golubchik's avatar
Sergei Golubchik committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
void pfs_print_error(const char *format, ...)
{
  va_list args;
  va_start(args, format);
  /*
    Printing to anything else, like the error log, would generate even more
    recursive calls to the performance schema implementation
    (file io is instrumented), so that could lead to catastrophic results.
    Printing to something safe, and low level: stderr only.
  */
  vfprintf(stderr, format, args);
  va_end(args);
  fflush(stderr);
}


208 209 210 211 212 213 214 215
/** Convert raw ip address into readable format. Do not do a reverse DNS lookup. */

uint pfs_get_socket_address(char *host,
                            uint host_len,
                            uint *port,
                            const struct sockaddr_storage *src_addr,
                            socklen_t src_len)
{
Sergei Golubchik's avatar
Sergei Golubchik committed
216 217 218
  assert(host);
  assert(src_addr);
  assert(port);
219 220 221 222 223 224 225 226 227 228 229

  memset(host, 0, host_len);
  *port= 0;

  switch (src_addr->ss_family)
  {
    case AF_INET:
    {
      if (host_len < INET_ADDRSTRLEN+1)
        return 0;
      struct sockaddr_in *sa4= (struct sockaddr_in *)(src_addr);
Sergei Golubchik's avatar
Sergei Golubchik committed
230
    #ifdef _WIN32
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
      /* Older versions of Windows do not support inet_ntop() */
      getnameinfo((struct sockaddr *)sa4, sizeof(struct sockaddr_in),
                  host, host_len, NULL, 0, NI_NUMERICHOST);
    #else
      inet_ntop(AF_INET, &(sa4->sin_addr), host, INET_ADDRSTRLEN);
    #endif
      *port= ntohs(sa4->sin_port);
    }
    break;

#ifdef HAVE_IPV6
    case AF_INET6:
    {
      if (host_len < INET6_ADDRSTRLEN+1)
        return 0;
      struct sockaddr_in6 *sa6= (struct sockaddr_in6 *)(src_addr);
Sergei Golubchik's avatar
Sergei Golubchik committed
247
    #ifdef _WIN32
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
      /* Older versions of Windows do not support inet_ntop() */
      getnameinfo((struct sockaddr *)sa6, sizeof(struct sockaddr_in6),
                  host, host_len, NULL, 0, NI_NUMERICHOST);
    #else
      inet_ntop(AF_INET6, &(sa6->sin6_addr), host, INET6_ADDRSTRLEN);
    #endif
      *port= ntohs(sa6->sin6_port);
    }
    break;
#endif

    default:
      break;
  }

  /* Return actual IP address string length */
  return (strlen((const char*)host));
}