summaryrefslogtreecommitdiff
path: root/storage/perfschema/pfs_autosize.cc
blob: 38bd36d8321aaf997080c6bca030486b648d6756 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; version 2 of the License.

  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
  GNU General Public License for more details.

  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_autosize.cc
  Private interface for the server (implementation).
*/

#include "my_global.h"
#include "sql_const.h"
#include "pfs_server.h"

#include <algorithm>
using std::min;
using std::max;

static const ulong fixed_mutex_instances= 500;
static const ulong fixed_rwlock_instances= 200;
static const ulong fixed_cond_instances= 50;
static const ulong fixed_file_instances= 200;
static const ulong fixed_socket_instances= 10;
static const ulong fixed_thread_instances= 50;

static const ulong mutex_per_connection= 3;
static const ulong rwlock_per_connection= 1;
static const ulong cond_per_connection= 2;
static const ulong file_per_connection= 0;
static const ulong socket_per_connection= 1;
static const ulong thread_per_connection= 1;

static const ulong mutex_per_handle= 0;
static const ulong rwlock_per_handle= 0;
static const ulong cond_per_handle= 0;
static const ulong file_per_handle= 0;
static const ulong socket_per_handle= 0;
static const ulong thread_per_handle= 0;

static const ulong mutex_per_share= 5;
static const ulong rwlock_per_share= 3;
static const ulong cond_per_share= 1;
static const ulong file_per_share= 3;
static const ulong socket_per_share= 0;
static const ulong thread_per_share= 0;

struct PFS_sizing_data
{
  /** Default value for @c PFS_param.m_account_sizing. */
  ulong m_account_sizing;
  /** Default value for @c PFS_param.m_user_sizing. */
  ulong m_user_sizing;
  /** Default value for @c PFS_param.m_host_sizing. */
  ulong m_host_sizing;

  /** Default value for @c PFS_param.m_events_waits_history_sizing. */
  ulong m_events_waits_history_sizing;
  /** Default value for @c PFS_param.m_events_waits_history_long_sizing. */
  ulong m_events_waits_history_long_sizing;
  /** Default value for @c PFS_param.m_events_stages_history_sizing. */
  ulong m_events_stages_history_sizing;
  /** Default value for @c PFS_param.m_events_stages_history_long_sizing. */
  ulong m_events_stages_history_long_sizing;
  /** Default value for @c PFS_param.m_events_statements_history_sizing. */
  ulong m_events_statements_history_sizing;
  /** Default value for @c PFS_param.m_events_statements_history_long_sizing. */
  ulong m_events_statements_history_long_sizing;
  /** Default value for @c PFS_param.m_digest_sizing. */
  ulong m_digest_sizing;
  /** Default value for @c PFS_param.m_session_connect_attrs_sizing. */
  ulong m_session_connect_attrs_sizing;

  /**
    Minimum number of tables to keep statistics for.
    On small deployments, all the tables can fit into the table definition cache,
    and this value can be 0.
    On big deployments, the table definition cache is only a subset of all the tables
    in the database, which are accounted for here.
  */
  ulong m_min_number_of_tables;

  /**
    Load factor for 'volatile' objects (mutexes, table handles, ...).
    Instrumented objects that:
    - use little memory
    - are created/destroyed very frequently
    should be stored in a low density (mostly empty) memory buffer,
    to optimize for speed.
  */
  float m_load_factor_volatile;
  /**
    Load factor for 'normal' objects (files).
    Instrumented objects that:
    - use a medium amount of memory
    - are created/destroyed 
    should be stored in a medium density memory buffer,
    as a trade off between space and speed.
  */
  float m_load_factor_normal;
  /**
    Load factor for 'static' objects (table shares).
    Instrumented objects that:
    - use a lot of memory
    - are created/destroyed very rarely
    can be stored in a high density (mostly packed) memory buffer,
    to optimize for space.
  */
  float m_load_factor_static;
};

PFS_sizing_data small_data=
{
  /* Account / user / host */
  10, 5, 20,
  /* History sizes */
  5, 100, 5, 100, 5, 100,
  /* Digests */
  1000,
  /* Session connect attrs. */
  512,
  /* Min tables */
  200,
  /* Load factors */
  0.90, 0.90, 0.90
};

PFS_sizing_data medium_data=
{
  /* Account / user / host */
  100, 100, 100,
  /* History sizes */
  10, 1000, 10, 1000, 10, 1000,
  /* Digests */
  5000,
  /* Session connect attrs. */
  512,
  /* Min tables */
  500,
  /* Load factors */
  0.70, 0.80, 0.90
};

PFS_sizing_data large_data=
{
  /* Account / user / host */
  100, 100, 100,
  /* History sizes */
  10, 10000, 10, 10000, 10, 10000,
  /* Digests */
  10000,
  /* Session connect attrs. */
  512,
  /* Min tables */
  10000,
  /* Load factors */
  0.50, 0.65, 0.80
};

static inline ulong apply_load_factor(ulong raw_value, float factor)
{
  float value = ((float) raw_value) / factor;
  return (ulong) ceil(value);
}

PFS_sizing_data *estimate_hints(PFS_global_param *param)
{
  if ((param->m_hints.m_max_connections <= MAX_CONNECTIONS_DEFAULT) &&
      (param->m_hints.m_table_definition_cache <= TABLE_DEF_CACHE_DEFAULT) &&
      (param->m_hints.m_table_open_cache <= TABLE_OPEN_CACHE_DEFAULT))
  {
    /* The my.cnf used is either unchanged, or lower than factory defaults. */
    return & small_data;
  }

  if ((param->m_hints.m_max_connections <= MAX_CONNECTIONS_DEFAULT * 2) &&
      (param->m_hints.m_table_definition_cache <= TABLE_DEF_CACHE_DEFAULT * 2) &&
      (param->m_hints.m_table_open_cache <= TABLE_OPEN_CACHE_DEFAULT * 2))
  {
    /* Some defaults have been increased, to "moderate" values. */
    return & medium_data;
  }

  /* Looks like a server in production. */
  return & large_data;
}

static void apply_heuristic(PFS_global_param *p, PFS_sizing_data *h)
{
  ulong count;
  ulong con = p->m_hints.m_max_connections;
  ulong handle = p->m_hints.m_table_open_cache;
  ulong share = p->m_hints.m_table_definition_cache;
  ulong file = p->m_hints.m_open_files_limit;

  if (p->m_table_sizing < 0)
  {
    count= handle;

    p->m_table_sizing= apply_load_factor(count, h->m_load_factor_volatile);
  }

  if (p->m_table_share_sizing < 0)
  {
    count= share;

    count= max<ulong>(count, h->m_min_number_of_tables);
    p->m_table_share_sizing= apply_load_factor(count, h->m_load_factor_static);
  }

  if (p->m_account_sizing < 0)
  {
    p->m_account_sizing= h->m_account_sizing;
  }

  if (p->m_user_sizing < 0)
  {
    p->m_user_sizing= h->m_user_sizing;
  }

  if (p->m_host_sizing < 0)
  {
    p->m_host_sizing= h->m_host_sizing;
  }

  if (p->m_events_waits_history_sizing < 0)
  {
    p->m_events_waits_history_sizing= h->m_events_waits_history_sizing;
  }

  if (p->m_events_waits_history_long_sizing < 0)
  {
    p->m_events_waits_history_long_sizing= h->m_events_waits_history_long_sizing;
  }

  if (p->m_events_stages_history_sizing < 0)
  {
    p->m_events_stages_history_sizing= h->m_events_stages_history_sizing;
  }

  if (p->m_events_stages_history_long_sizing < 0)
  {
    p->m_events_stages_history_long_sizing= h->m_events_stages_history_long_sizing;
  }

  if (p->m_events_statements_history_sizing < 0)
  {
    p->m_events_statements_history_sizing= h->m_events_statements_history_sizing;
  }

  if (p->m_events_statements_history_long_sizing < 0)
  {
    p->m_events_statements_history_long_sizing= h->m_events_statements_history_long_sizing;
  }

  if (p->m_digest_sizing < 0)
  {
    p->m_digest_sizing= h->m_digest_sizing;
  }

  if (p->m_session_connect_attrs_sizing < 0)
  {
    p->m_session_connect_attrs_sizing= h->m_session_connect_attrs_sizing;
  }

  if (p->m_mutex_sizing < 0)
  {
    count= fixed_mutex_instances
      + con * mutex_per_connection
      + handle * mutex_per_handle
      + share * mutex_per_share;

    p->m_mutex_sizing= apply_load_factor(count, h->m_load_factor_volatile);
  }

  if (p->m_rwlock_sizing < 0)
  {
    count= fixed_rwlock_instances
      + con * rwlock_per_connection
      + handle * rwlock_per_handle
      + share * rwlock_per_share;

    p->m_rwlock_sizing= apply_load_factor(count, h->m_load_factor_volatile);
  }

  if (p->m_cond_sizing < 0)
  {
    ulong count;
    count= fixed_cond_instances
      + con * cond_per_connection
      + handle * cond_per_handle
      + share * cond_per_share;

    p->m_cond_sizing= apply_load_factor(count, h->m_load_factor_volatile);
  }

  if (p->m_file_sizing < 0)
  {
    count= fixed_file_instances
      + con * file_per_connection
      + handle * file_per_handle
      + share * file_per_share;

    count= max<ulong>(count, file);
    p->m_file_sizing= apply_load_factor(count, h->m_load_factor_normal);
  }

  if (p->m_socket_sizing < 0)
  {
    count= fixed_socket_instances
      + con * socket_per_connection
      + handle * socket_per_handle
      + share * socket_per_share;

    p->m_socket_sizing= apply_load_factor(count, h->m_load_factor_volatile);
  }

  if (p->m_thread_sizing < 0)
  {
    count= fixed_thread_instances
      + con * thread_per_connection
      + handle * thread_per_handle
      + share * thread_per_share;

    p->m_thread_sizing= apply_load_factor(count, h->m_load_factor_volatile);
  }
}

void pfs_automated_sizing(PFS_global_param *param)
{
  PFS_sizing_data *heuristic;
  heuristic= estimate_hints(param);
  apply_heuristic(param, heuristic);

  DBUG_ASSERT(param->m_account_sizing >= 0);
  DBUG_ASSERT(param->m_digest_sizing >= 0);
  DBUG_ASSERT(param->m_host_sizing >= 0);
  DBUG_ASSERT(param->m_user_sizing >= 0);

  DBUG_ASSERT(param->m_events_waits_history_sizing >= 0);
  DBUG_ASSERT(param->m_events_waits_history_long_sizing >= 0);
  DBUG_ASSERT(param->m_events_stages_history_sizing >= 0);
  DBUG_ASSERT(param->m_events_stages_history_long_sizing >= 0);
  DBUG_ASSERT(param->m_events_statements_history_sizing >= 0);
  DBUG_ASSERT(param->m_events_statements_history_long_sizing >= 0);
  DBUG_ASSERT(param->m_session_connect_attrs_sizing >= 0);

  DBUG_ASSERT(param->m_mutex_sizing >= 0);
  DBUG_ASSERT(param->m_rwlock_sizing >= 0);
  DBUG_ASSERT(param->m_cond_sizing >= 0);
  DBUG_ASSERT(param->m_file_sizing >= 0);
  DBUG_ASSERT(param->m_socket_sizing >= 0);
  DBUG_ASSERT(param->m_thread_sizing >= 0);
  DBUG_ASSERT(param->m_table_sizing >= 0);
  DBUG_ASSERT(param->m_table_share_sizing >= 0);
}