Mysql Source Learning notes Peeping thread _mysql

Source: Internet
Author: User
Feel the code is a bit messy, annotation code are written more casual, as if there is no uniform norms, different files in the code style also have differences, may be MySQL after a lot of cattle of the hands of the people, set the long bar. Also may be I see more shallow, adapt to their own code style, in the frog, in short, or with a sense of awe to start our source of the trip it. I rookie, big God tap.

MySQL can start up, how should learn it? Can not start from main to step by step look, MySQL as a comparison of the bottom of the large software, involved in all aspects of database implementation, there is no solid database theory and the various modules of MySQL is quite familiar with, starting from main will be bound to introduce themselves to a dead end ah, see everything, Finally nothing, I can't afford to hurt.

After thinking, I would like to or through the client to debug the server, so learn the server code more realistic. That is, through the action of the client, look at the response of the server. For example, from the client login action to see how the server for communication, user identification, authentication and assignment of tasks, through the Create TABLE, see how the server resolves DDL statements and different storage engine to take different physical storage, through the INSERT statement, See how the server is doing btree. See how the SQL statement syntax tree is created and optimized through the SELECT statement, and roll back to see how the server transaction is implemented. Here is mainly through the tracking code to learn the idea of MySQL database implementation, for the specific code does not do too much to investigate (mainly I am not very familiar with C + +), good reading, superficial understanding, hehe.
As a result, the following SQL statements have been prepared for the time being for server analysis
Copy Code code as follows:

1. Login (Login)
Mysql.exe–uroot–p
2. DDL (Table statement)
CREATE TABLE Tb_myisam (c1 int, C2 varchar (256)) engine = MyISAM;
CREATE TABLE Tb_innodb (c1 int, C2 varchar (256)) engine = InnoDB;
3, INSERT
Insert into Tb_myisam values (1, ' lonely fat ');
Insert into Tb_innodb values (1, ' lonely fat ');
4, SELECT
Select C1 from Tb_myisam;
Select * from Tb_innodb;
5, ROLLBACK

As we all know, MySQL can be through multiple clients, concurrent operations, of course, including login. When someone else is logged in, other users may be doing something else, so for login we suspect that there should be a dedicated thread responsible for the creation of client and server connections to ensure the timeliness of login, for each connected user should use a separate thread for the execution of the task.
First of all, introduce the function of creating a thread in MySQL, the function of creating a thread seems to be _begin_thread,createthread, we find it through vs in the whole solution, bingo! A function pthread_create called _begin_thread was found in the MY_WINTHREAD.C, and a function called CreateThread was found in OS0THREAD.C. How does a system encapsulate two system functions?? Take a closer look and find that MY_WINTHREAD.C is under Project Mysys, and OS0THREAD.C is under Project Innobase. innobase!! This is not the InnoDB plug-in storage engine, originally this is the storage engine of their own encapsulation of the underlying function, brother heart suddenly enlightened. I think the MySQL application scope is so wide, in addition to open source, plug-in storage engine is not ah, users can according to their actual application to take different storage engine, for large companies, estimates will develop their own storage engine.
The following analysis below Pthread_create is how to call _begin_thread, first rough look at the source code.
Copy Code code as follows:

int Pthread_create (pthread_t *thread_id, pthread_attr_t *attr,
Pthread_handler func, void *param)
{
HANDLE Hthread;
struct Pthread_map *map;
Dbug_enter ("Pthread_create");
if (!) ( Map=malloc (sizeof (*MAP)))
Dbug_return (-1);
map->func=func;
map->param=param;
Pthread_mutex_lock (&thr_lock_thread);
#ifdef __borlandc__
Hthread= (HANDLE) _beginthread (void (_userentry *) (void *)) Pthread_start,
Attr->dwstacksize? Attr->dwstacksize:
65535, (void*) map);
#else
Hthread= (HANDLE) _beginthread (void (__cdecl *) (void *)) Pthread_start,
Attr->dwstacksize? Attr->dwstacksize:
65535, (void*) map);
#endif
Dbug_print ("Info", ("Hthread=%lu", (long) hthread));
*thread_id=map->pthreadself=hthread;
Pthread_mutex_unlock (&thr_lock_thread);
if (hthread = = (HANDLE)-1)
{
int Error=errno;
Dbug_print ("Error"),
("Can ' t create thread to handle request (Error%d)", error));
Dbug_return (Error? Error:-1);
}
VOID (SetThreadPriority (Hthread, attr->priority));
Dbug_return (0);
}

The key code is the following three sentences:
Copy Code code as follows:

map->func=func;
map->param=param;
_beginthread (void (__cdecl *) (void *)) Pthread_start,
Attr->dwstacksize? Attr->dwstacksize:
65535, (void*) map);

From this we can see that the name of the new thread created is a fixed function--pthread_start, and the function we are going to create Func is mounted on the map, and the parameters of the function are also mounted on the map so that we can deduce the Pthread_ In the start function, this code is sure to appear:
Map->func (Map->param);
MySQL did not choose the direct _beginthread (func, stack_size, param) Form, but carried out a package, do not know what the benefits are, may be the idea of cattle is not my small rookie can epiphany, digress ~ ~
So far, we only set breakpoints on the Pthread_create function, debug the boot mysqld, break the breakpoint, and look at the system's thread status:


The first time we entered the pthread_create, no thread began to create, logically, the system thread should have only one main thread, but now more so, these should be the InnoDB storage engine created by the thread (specifically in plugin_init). Based on the name of the thread, combined with annotations, guessing the role of these threads.
Io_handler_thread: You can tell from the name that these are I/O threads and are responsible for disk I/O.
Svr_error_monitor_thread: should be a server error monitoring thread.
Svr_lock_timeout_thread: It should be a lock-related thread.
Svr_master_thread:
/*************************************************************************
The master thread controlling the server. */
The server controls the thread, which should be the specific thread for the job.
Svr_monitor_thread:
/*************************************************************************
A thread prints the info output by various InnoDB monitors. */
Monitor threads, responsible for printing information.

Indifferent drifting over it, do not go to the careful, we only care about the pthread_create created thread. Depending on the debugging, several threads with the same name are found _threadstart, as follows:

When debugging, look at the stack to know the creators and roles of these three threads, as shown below

Created by

handler function

Create_shutdown_thread

Handle_shutdown

Start_handle_manager

Handle_manager

Handle_connections_methods

Handle_connections_sockets

Creator: Invokes pthread_create to create the thread's function.

handler function: invokes the specific thread function of the thread created by Pthread_create.

By name we can see that the handle_connections_sockets should be the thread that handles the connection, in the order that it should be, andonly after all the other necessary threads in the system have been created can the listener thread (the connection thread) be created. That is, the listener thread should be the last created by the system.

Found the thread we need to login, next time for this thread, analyze how to log in and what resources to assign to the user after logging in. It's getting late, I'm washing and sleeping
Author: cnblogs Heart Uncensored

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.