Mysql源代码阅读笔记(一) 代码流程

代码分析基于最新的5.5.21mysql

Mysql服务器的main()在/sql/main.cc,实际的入口点在/sql/mysqld.cc。sql


咱们直接从mysqld_main()开始阅读。shell

先理解一些比较重要的宏定义:
express

(1)宏HAVE_NPTL: 
服务器

这个宏若是打开了会去读一个系统变量LD_ASSUME_KERNEL,并把他赋给一个全局变量ld_assume_kernel_is_set,这个系统变量设置了系统线程的实现模型。linux内核在2.6版本后,缺省的线程模型是NPTL。app

http://blog.csdn.net/theorytree/article/details/7336523    详细状况请阅读我博客里的这篇文章。socket


对于以HAVE_前缀的的宏,另有以下文章阐述:
ide

http://blog.csdn.net/theorytree/article/details/7337692  这里介绍了CMake的相关知识,经过CMake在影子编译目录的include目录里面生成了my_config.h文件。函数

my_config.h文件经过my_global.h文件包含到具体的须要使用HAVE_前缀的宏的源文件中。ui


(2)EMBEDDED_LIBRARY

该宏用于控制  嵌入式库和客户端/服务器版本不一样的那部分代码。

Mysql 能够编译为一个libmysqld库(嵌入式服务器),使用嵌入式MySQL服务器库,可以在客户端应用程序中使用具有所有特性的MySQL服务器。

主要优势在于,增长了速度,并使得嵌入式应用程序的管理更简单。嵌入式服务器库是以MySQL的客户端/服务器版本为基础的,采用C/C++语言编写。 其结果是嵌入式服务器也是用C/C++语言编写的。 在其余语言中,嵌入式服务器不可用。


这个宏能够经过CMake配置,

-DWITH_EMBEDDED_SERVER=1 打开这个宏
在plug.cmake函数里有它的定义:

PROPERTIES COMPILE_DEFINITIONS "MYSQL_SERVER;EMBEDDED_LIBRARY")


整个代码的流程以下:咱们讲分更细的文章详细介绍每部分:

Mysql服务器监听:




Mysql的整体结构图




Mysql 源代码的目录结构 :(如下来自官方文档)


/BUILD The compilation configuration and make files for all platforms supported.
Use this folder for compilation and linking.


/client The MySQL command-line client tool.


/dbug Utilities for use in debugging (see Chapter 5 for more details).


/Docs Documentation for the current release. Linux users should usegenerate-text-files.pl in the support subfolder to generate the documentation. Windows users are provided with a manual.chm file.


/include The base system include files and headers.


/libmysql The C client API used for creating embedded systems. (See Chapter 6 formore details.)


/libmysqld The core server API files. Also used in creating embedded systems.(See Chapter 6 for more details.)


/mysql-test The MySQL system test suite. (See Chapter 4 for more details.)


/mysys The majority of the core operating system API wrappers and helper functions.


/regex A regular expression library. Used in the query optimizer and execution to resolve expressions.


/scripts A set of shell script-based utilities.


/sql The main system code. You should start your exploration from this folder.


/sql-bench A set of benchmarking utilities.


/SSL A set of Secure Socket Layer utilities and definitions.


/storage The MySQL pluggable storage engine source code is located inside this folder. Also included is the storage engine example code. (See Chapter 7 for more details.)


/strings The core string handling wrappers. Use these for all of your string handling needs.


/support-files A set of preconfigured configuration files for compiling with different options.


/tests A set of test programs and test files.


/vio The network and socket layer code.

/zlib Data compression tools.