1 /*
2    Copyright (c) 2014, 2021, Oracle and/or its affiliates.
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License, version 2.0,
6    as published by the Free Software Foundation.
7 
8    This program is also distributed with certain software (including
9    but not limited to OpenSSL) that is licensed under separate terms,
10    as designated in a particular file or component or in included license
11    documentation.  The authors of MySQL hereby grant you an additional
12    permission to link the program and your derivative works with the
13    separately licensed software that they have included with MySQL.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License, version 2.0, for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software Foundation,
22    51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
23 
24 // First include (the generated) my_config.h, to get correct platform defines.
25 #include "my_config.h"
26 #include "opt_costmodel.h"
27 #include "opt_costconstantcache.h"              // Cost_constant_cache
28 #include "table.h"                              // TABLE
29 
30 extern Cost_constant_cache *cost_constant_cache;// defined in
31                                                 // opt_costconstantcache.cc
32 
33 
~Cost_model_server()34 Cost_model_server::~Cost_model_server()
35 {
36   if (m_cost_constants)
37   {
38     cost_constant_cache->release_cost_constants(m_cost_constants);
39     m_cost_constants= NULL;
40   }
41 }
42 
43 
init()44 void Cost_model_server::init()
45 {
46   if (m_server_cost_constants == NULL)
47   {
48     // Get the current set of cost constants
49     m_cost_constants= cost_constant_cache->get_cost_constants();
50     assert(m_cost_constants != NULL);
51 
52     // Get the cost constants for server operations
53     m_server_cost_constants= m_cost_constants->get_server_cost_constants();
54     assert(m_server_cost_constants != NULL);
55 
56 #if !defined(NDEBUG)
57     m_initialized= true;
58 #endif
59   }
60 }
61 
62 
init(const Cost_model_server * cost_model_server,const TABLE * table)63 void Cost_model_table::init(const Cost_model_server *cost_model_server,
64                             const TABLE *table)
65 {
66   assert(cost_model_server != NULL);
67   assert(table != NULL);
68 
69   m_cost_model_server= cost_model_server;
70   m_table= table;
71 
72   // Find the cost constant object to be used for this table
73   m_se_cost_constants=
74     m_cost_model_server->get_cost_constants()->get_se_cost_constants(table);
75   assert(m_se_cost_constants != NULL);
76 
77 #if !defined(NDEBUG)
78   m_initialized= true;
79 #endif
80 }
81 
82 
page_read_cost(double pages) const83 double Cost_model_table::page_read_cost(double pages) const
84 {
85   assert(m_initialized);
86   assert(pages >= 0.0);
87 
88   const double in_mem= m_table->file->table_in_memory_estimate();
89 
90   const double pages_in_mem= pages * in_mem;
91   const double pages_on_disk= pages - pages_in_mem;
92   assert(pages_on_disk >= 0.0);
93 
94   const double cost= buffer_block_read_cost(pages_in_mem) +
95     io_block_read_cost(pages_on_disk);
96 
97   return cost;
98 }
99 
100 
page_read_cost_index(uint index,double pages) const101 double Cost_model_table::page_read_cost_index(uint index, double pages) const
102 {
103   assert(m_initialized);
104   assert(pages >= 0.0);
105 
106   double in_mem= m_table->file->index_in_memory_estimate(index);
107 
108   const double pages_in_mem= pages * in_mem;
109   const double pages_on_disk= pages - pages_in_mem;
110 
111   const double cost= buffer_block_read_cost(pages_in_mem) +
112     io_block_read_cost(pages_on_disk);
113 
114   return cost;
115 }
116