1 /* Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved.
2 
3   This program is free software; you can redistribute it and/or modify
4   it under the terms of the GNU General Public License, version 2.0,
5   as published by the Free Software Foundation.
6 
7   This program is also distributed with certain software (including
8   but not limited to OpenSSL) that is licensed under separate terms,
9   as designated in a particular file or component or in included license
10   documentation.  The authors of MySQL hereby grant you an additional
11   permission to link the program and your derivative works with the
12   separately licensed software that they have included with MySQL.
13 
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License, version 2.0, for more details.
18 
19   You should have received a copy of the GNU General Public License
20   along with this program; if not, write to the Free Software
21   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 #include "storage/perfschema/unittest/pfs_unit_test_conf.h"
24 
25 #include <string.h> /* memset */
26 
27 #include "my_thread.h"
28 #include "storage/perfschema/pfs_buffer_container.h"
29 #include "storage/perfschema/pfs_global.h"
30 #include "storage/perfschema/pfs_instr.h"
31 #include "storage/perfschema/pfs_stat.h"
32 #include "storage/perfschema/pfs_user.h"
33 #include "storage/perfschema/unittest/stub_pfs_global.h"
34 #include "storage/perfschema/unittest/stub_pfs_plugin_table.h"
35 #include "unittest/mytap/tap.h"
36 
test_oom()37 static void test_oom() {
38   int rc;
39   PFS_global_param param;
40   PSI_thread_service_t *thread_service;
41   PSI_thread_bootstrap *thread_boot;
42   PSI_mutex_bootstrap *mutex_boot;
43   PSI_rwlock_bootstrap *rwlock_boot;
44   PSI_cond_bootstrap *cond_boot;
45   PSI_file_bootstrap *file_boot;
46   PSI_socket_bootstrap *socket_boot;
47   PSI_table_bootstrap *table_boot;
48   PSI_mdl_bootstrap *mdl_boot;
49   PSI_idle_bootstrap *idle_boot;
50   PSI_stage_bootstrap *stage_boot;
51   PSI_statement_bootstrap *statement_boot;
52   PSI_transaction_bootstrap *transaction_boot;
53   PSI_memory_bootstrap *memory_boot;
54   PSI_error_bootstrap *error_boot;
55   PSI_data_lock_bootstrap *data_lock_boot;
56   PSI_system_bootstrap *system_boot;
57   PSI_tls_channel_bootstrap *tls_channel_boot;
58 
59   memset(&param, 0xFF, sizeof(param));
60   param.m_enabled = true;
61   param.m_mutex_class_sizing = 0;
62   param.m_rwlock_class_sizing = 0;
63   param.m_cond_class_sizing = 0;
64   param.m_thread_class_sizing = 10;
65   param.m_table_share_sizing = 0;
66   param.m_file_class_sizing = 0;
67   param.m_socket_class_sizing = 0;
68   param.m_mutex_sizing = 0;
69   param.m_rwlock_sizing = 0;
70   param.m_cond_sizing = 0;
71   param.m_thread_sizing = 1000;
72   param.m_table_sizing = 0;
73   param.m_file_sizing = 0;
74   param.m_file_handle_sizing = 0;
75   param.m_socket_sizing = 0;
76   param.m_events_waits_history_sizing = 10;
77   param.m_events_waits_history_long_sizing = 0;
78   param.m_setup_actor_sizing = 0;
79   param.m_setup_object_sizing = 0;
80   param.m_host_sizing = 0;
81   param.m_user_sizing = 1000;
82   param.m_account_sizing = 0;
83   param.m_stage_class_sizing = 50;
84   param.m_events_stages_history_sizing = 0;
85   param.m_events_stages_history_long_sizing = 0;
86   param.m_statement_class_sizing = 50;
87   param.m_events_statements_history_sizing = 0;
88   param.m_events_statements_history_long_sizing = 0;
89   param.m_events_transactions_history_sizing = 0;
90   param.m_events_transactions_history_long_sizing = 0;
91   param.m_digest_sizing = 0;
92   param.m_session_connect_attrs_sizing = 0;
93   param.m_program_sizing = 0;
94   param.m_statement_stack_sizing = 0;
95   param.m_memory_class_sizing = 10;
96   param.m_metadata_lock_sizing = 0;
97   param.m_max_digest_length = 0;
98   param.m_max_sql_text_length = 0;
99   param.m_error_sizing = 0;
100 
101   /* Setup */
102 
103   stub_alloc_always_fails = false;
104   stub_alloc_fails_after_count = 1000;
105 
106   pre_initialize_performance_schema();
107   rc = initialize_performance_schema(
108       &param, &thread_boot, &mutex_boot, &rwlock_boot, &cond_boot, &file_boot,
109       &socket_boot, &table_boot, &mdl_boot, &idle_boot, &stage_boot,
110       &statement_boot, &transaction_boot, &memory_boot, &error_boot,
111       &data_lock_boot, &system_boot, &tls_channel_boot);
112   ok(rc == 0, "init ok");
113   thread_service = (PSI_thread_service_t *)thread_boot->get_interface(
114       PSI_CURRENT_THREAD_VERSION);
115 
116   PSI_thread_key thread_key_1;
117   PSI_thread_info all_thread[] = {{&thread_key_1, "T-1", 0, 0, ""}};
118   thread_service->register_thread("test", all_thread, 1);
119 
120   PSI_thread *thread_1 = thread_service->new_thread(thread_key_1, nullptr, 0);
121   thread_service->set_thread(thread_1);
122 
123   /* Tests */
124 
125   int first_fail = 1;
126   stub_alloc_fails_after_count = first_fail;
127   thread_service->set_thread_account("user1", 5, "", 0);
128   ok(global_user_container.m_lost == 1, "oom (user)");
129 
130   stub_alloc_fails_after_count = first_fail + 1;
131   thread_service->set_thread_account("user2", 5, "", 0);
132   ok(global_user_container.m_lost == 2, "oom (user waits)");
133 
134   stub_alloc_fails_after_count = first_fail + 2;
135   thread_service->set_thread_account("user3", 5, "", 0);
136   ok(global_user_container.m_lost == 3, "oom (user stages)");
137 
138   stub_alloc_fails_after_count = first_fail + 3;
139   thread_service->set_thread_account("user4", 5, "", 0);
140   ok(global_user_container.m_lost == 4, "oom (user statements)");
141 
142   stub_alloc_fails_after_count = first_fail + 4;
143   thread_service->set_thread_account("user5", 5, "", 0);
144   ok(global_user_container.m_lost == 5, "oom (user transactions)");
145 
146   stub_alloc_fails_after_count = first_fail + 5;
147   thread_service->set_thread_account("user6", 5, "", 0);
148   ok(global_user_container.m_lost == 6, "oom (user memory)");
149 
150   shutdown_performance_schema();
151 }
152 
do_all_tests()153 static void do_all_tests() { test_oom(); }
154 
main(int,char **)155 int main(int, char **) {
156   plan(7);
157   MY_INIT("pfs_user-oom-t");
158   do_all_tests();
159   my_end(0);
160   return (exit_status());
161 }
162