py_innodb_page_info.py工具使用

目录

一、Linux安装Python3

1. 解压包

2. 安装环境

3. 生成编译脚本

4. 检查python3.10的编译器

5. 建立Python3和pip3的软链

6. 添加到PATH

7. 验证Python3和pip3是否正常

二、py_innodb_page_info.py工具

1. py_innodb_page_info.py

2. mylib.py

3. include.py

三、使用py_innodb_page_info.py工具

四、参考资料


一、Linux安装Python3

1. 解压包

tar -zxvf Python-3.10.5.tgz

2. 安装环境

yum install gcc

yum install libffi-devel -y

3. 生成编译脚本

        进入解压目录下,使用下述命令来配置编译目录。注意--prefix路径是当前解压目录。

./configure --prefix=/home/python3/Python-3.10.5

py_innodb_page_info.py工具使用_第1张图片

        编译并安装,若下图所示:

make & make install 

4. 检查python3.10的编译器

        进入bin目录下执行以下命令:

./python3.10

py_innodb_page_info.py工具使用_第2张图片

5. 建立Python3和pip3的软链

        建立Python3和pip3的软链,若以下命令:

ln -s /home/python3/Python-3.10.5/bin/python3 /usr/bin/python3

ln -s /home/python3/Python-3.10.5/bin/pip3 /usr/bin/pip3 

6. 添加到PATH

  1. 进入:vim /etc/profile
  2. python的bin加入到PATH中:export PATH=$PATH:$JAVA_HOME/bin:/home/python3/Python-3.10.5/bin
  3. 重启profile:source ~/.bash_profile

py_innodb_page_info.py工具使用_第3张图片

7. 验证Python3和pip3是否正常

python3 -V

pip3 -V

二、py_innodb_page_info.py工具

        此工具是《MySQL技术内幕 InnoDB存储引擎》作者姜承尧写的用来分析表空间中的各页的类型和信息,用python编写

        此工具由三个文件组成:py_innodb_page_info.py、mylib.py、include.py。注意:需要放在同一个目录下

1. py_innodb_page_info.py

#! /usr/bin/env python
#encoding=utf-8
import mylib
from sys import argv
from mylib import myargv

if __name__ == '__main__':
    myargv = myargv(argv)
    if myargv.parse_cmdline() == 0:
        pass
    else:
        mylib.get_innodb_page_type(myargv)

2. mylib.py

#encoding=utf-8
import os
import include
from include import *

TABLESPACE_NAME='/home/MySQL5.7/mysql-5.7.35/data/test_mysql/page_structure.ibd'
VARIABLE_FIELD_COUNT = 1
NULL_FIELD_COUNT = 0

class myargv(object):
    def __init__(self, argv):
        self.argv = argv
        self.parms = {}
        self.tablespace = ''
   
    def parse_cmdline(self):
        argv = self.argv
        if len(argv) == 1:
            print 'Usage: python py_innodb_page_info.py [OPTIONS] tablespace_file'
            print 'For more options, use python py_innodb_page_info.py -h'
            return 0
        while argv:
            if argv[0][0] == '-':
                if argv[0][1] == 'h':
                    self.parms[argv[0]] = ''
                    argv = argv[1:]
                    break
                if argv[0][1] == 'v':
                    self.parms[argv[0]] = ''
                    argv = argv[1:]           
                else:
                    self.parms[argv[0]] = argv[1]
                    argv = argv[2:]
            else:
                self.tablespace = argv[0]
                argv = argv[1:]
        if self.parms.has_key('-h'):
            print 'Get InnoDB Page Info'
            print 'Usage: python py_innodb_page_info.py [OPTIONS] tablespace_file\n'
            print 'The following options may be given as the first argument:'
            print '-h        help '
            print '-o output put the result to file'
            print '-t number thread to anayle the tablespace file'
            print '-v        verbose mode'
            return 0
        return 1
       
def mach_read_from_n(page,start_offset,length):
    ret = page[start_offset:start_offset+length]
    return ret.encode('hex')
   
def get_innodb_page_type(myargv):
    f=file(myargv.tablespace,'rb')
    fsize = os.path.getsize(f.name)/INNODB_PAGE_SIZE
    ret = {}
    for i in range(fsize):
        page = f.read(INNODB_PAGE_SIZE)
        page_offset = mach_read_from_n(page,FIL_PAGE_OFFSET,4)
        page_type = mach_read_from_n(page,FIL_PAGE_TYPE,2)
        if myargv.parms.has_key('-v'):
            if page_type == '45bf':
                page_level = mach_read_from_n(page,FIL_PAGE_DATA+PAGE_LEVEL,2)
                print "page offset %s, page type <%s>, page level <%s>"%(page_offset,innodb_page_type[page_type],page_level)
            else:
                print "page offset %s, page type <%s>"%(page_offset,innodb_page_type[page_type])
        if not ret.has_key(page_type):
            ret[page_type] = 1
        else:
            ret[page_type] = ret[page_type] + 1
    print "Total number of page: %d:"%fsize
    for type in ret:
        print "%s: %s"%(innodb_page_type[type],ret[type])

3. include.py

#encoding=utf-8
INNODB_PAGE_SIZE = 16*1024*1024

# Start of the data on the page
FIL_PAGE_DATA = 38
FIL_PAGE_OFFSET = 4 # page offset inside space
FIL_PAGE_TYPE = 24 # File page type

# Types of an undo log segment */
TRX_UNDO_INSERT = 1
TRX_UNDO_UPDATE = 2

# On a page of any file segment, data may be put starting from this offset
FSEG_PAGE_DATA = FIL_PAGE_DATA

# The offset of the undo log page header on pages of the undo log
TRX_UNDO_PAGE_HDR = FSEG_PAGE_DATA

PAGE_LEVEL = 26    #level of the node in an index tree; the leaf level is the level 0 */         

innodb_page_type={
    '0000':u'Freshly Allocated Page',
    '0002':u'Undo Log Page',
    '0003':u'File Segment inode',
    '0004':u'Insert Buffer Free List',
    '0005':u'Insert Buffer Bitmap',
    '0006':u'System Page',
    '0007':u'Transaction system Page',
    '0008':u'File Space Header',
    '0009':u'扩展描述页',
    '000a':u'Uncompressed BLOB Page',
    '000b':u'1st compressed BLOB Page',
    '000c':u'Subsequent compressed BLOB Page',
    '45bf':u'B-tree Node'
}

innodb_page_direction={
    '0000': 'Unknown(0x0000)',
    '0001': 'Page Left',
    '0002': 'Page Right',
    '0003': 'Page Same Rec',
    '0004': 'Page Same Page',
    '0005': 'Page No Direction',
    'ffff': 'Unkown2(0xffff)'
}
INNODB_PAGE_SIZE=1024*16

三、使用py_innodb_page_info.py工具

        进入py_innodb_page_info.py所在的目录下,使用以下命令:

python py_innodb_page_info.py -v /home/MySQL5.7/mysql-5.7.35/data/test_mysql/test_init_table.ibd

[root@488c1daa7967 py_innodb_page_info]# 
[root@488c1daa7967 py_innodb_page_info]# pwd
/home/py_innodb_page_info
[root@488c1daa7967 py_innodb_page_info]# 
[root@488c1daa7967 py_innodb_page_info]# 
[root@488c1daa7967 py_innodb_page_info]# python py_innodb_page_info.py -v /home/MySQL5.7/mysql-5.7.35/data/test_mysql/test_init_table.ibd
page offset 00000000, page type 
page offset 00000001, page type 
page offset 00000002, page type 
page offset 00000003, page type , page level <0000>
page offset 00000000, page type 
page offset 00000000, page type 
Total number of page: 6:
Freshly Allocated Page: 2
Insert Buffer Bitmap: 1
File Space Header: 1
B-tree Node: 1
File Segment inode: 1
内容 描述
page offset

1. 该页在表空间的偏移量;

2. 00000003页:3 * 16KB = 49152 = 0xc000,

    打开.ibd文件,找到0xc000,就是00000003页的起始位置。

page type

页的类型,其中:

1. B-tree Node:B+树节点页,page level <0000>是叶子节点;

                                              其他是非叶子节点;

    Freshly Allocated Page:最新分配的页(空闲页);

    Insert Buffer Bitmap:插入缓冲Bitmap页;

page level

1. B+树的高度;

2. <0000>是叶子节点;非0是非叶子节点

Total number of page 表空间的页总数
Freshly Allocated Page 表空间的空闲页总数
Insert Buffer Bitmap 插入缓冲Bitmap的页数
File Space Header File Space Header的页数
B-tree Node B+树节点的页数量(叶子节点 + 非叶子节点)
File Segment inode File Segment inode的页数

四、参考资料

Linux 安装Python3_Alex_Sheng_Sea的博客-CSDN博客_linux安装python

Download Python | Python.org

py_innodb_page_info工具使用_Hehuyi_In的博客-CSDN博客_py_innodb_page_info

你可能感兴趣的:(mysql,mysql,数据库,python)