当前位置:首页 » 其他

py_innodb_page_info.py

2011-11-16 12:29 本站整理 浏览(5)

#py_innodb_page_info.py

#!/usr/bin/envpython
#encoding=utf-8
importmylib
fromsysimportargv
frommylibimportmyargv

if__name__=='__main__':
	myargv=myargv(argv)
	ifmyargv.parse_cmdline()==0:
		pass
	else:
		mylib.get_innodb_page_type(myargv)
--------------

#mylib.py
#encoding=utf-8
importos
importinclude
fromincludeimport*

TABLESPACE_NAME='D:\\mysql_data\\test\\t.ibd'
VARIABLE_FIELD_COUNT=1
NULL_FIELD_COUNT=0

classmyargv(object):
	def__init__(self,argv):
		self.argv=argv
		self.parms={}
		self.tablespace=''
	
	defparse_cmdline(self):
		argv=self.argv
		iflen(argv)==1:
			print'Usage:pythonpy_innodb_page_info.py[OPTIONS]tablespace_file'
			print'Formoreoptions,usepythonpy_innodb_page_info.py-h'
			return0
		whileargv:
			ifargv[0][0]=='-':
				ifargv[0][1]=='h':
					self.parms[argv[0]]=''
					argv=argv[1:]
					break
				ifargv[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:]
		ifself.parms.has_key('-h'):
			print'GetInnoDBPageInfo'
			print'Usage:pythonpy_innodb_page_info.py[OPTIONS]tablespace_file\n'
			print'Thefollowingoptionsmaybegivenasthefirstargument:'
			print'-hhelp'
			print'-ooutputputtheresulttofile'
			print'-tnumberthreadtoanaylethetablespacefile'
			print'-vverbosemode'
			return0
		return1
		
defmach_read_from_n(page,start_offset,length):
	ret=page[start_offset:start_offset+length]
	returnret.encode('hex')
	
defget_innodb_page_type(myargv):
	f=file(myargv.tablespace,'rb')
	fsize=os.path.getsize(f.name)/INNODB_PAGE_SIZE
	ret={}
	foriinrange(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)
		ifmyargv.parms.has_key('-v'):
			ifpage_type=='45bf':
				page_level=mach_read_from_n(page,FIL_PAGE_DATA+PAGE_LEVEL,2)
				print"pageoffset%s,pagetype<%s>,pagelevel<%s>"%(page_offset,innodb_page_type[page_type],page_level)
			else:
				print"pageoffset%s,pagetype<%s>"%(page_offset,innodb_page_type[page_type])
		ifnotret.has_key(page_type):
			ret[page_type]=1
		else:
			ret[page_type]=ret[page_type]+1
	print"Totalnumberofpage:%d:"%fsize
	fortypeinret:
		print"%s:%s"%(innodb_page_type[type],ret[type])

-----------
#include.py

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

#Startofthedataonthepage
FIL_PAGE_DATA=38

FIL_PAGE_OFFSET=4#pageoffsetinsidespace
FIL_PAGE_TYPE=24#Filepagetype

#Typesofanundologsegment*/
TRX_UNDO_INSERT=1
TRX_UNDO_UPDATE=2

#Onapageofanyfilesegment,datamaybeputstartingfromthisoffset
FSEG_PAGE_DATA=FIL_PAGE_DATA

#Theoffsetoftheundologpageheaderonpagesoftheundolog
TRX_UNDO_PAGE_HDR=FSEG_PAGE_DATA

PAGE_LEVEL=26	#levelofthenodeinanindextree;theleaflevelisthelevel0*/
				
innodb_page_type={
	'0000':u'FreshlyAllocatedPage',
	'0002':u'UndoLogPage',
	'0003':u'FileSegmentinode',
	'0004':u'InsertBufferFreeList',
	'0005':u'InsertBufferBitmap',
	'0006':u'SystemPage',
	'0007':u'TransactionsystemPage',
	'0008':u'FileSpaceHeader',
	'0009':u'扩展描述页',
	'000a':u'UncompressedBLOBPage',
	'000b':u'1stcompressedBLOBPage',
	'000c':u'SubsequentcompressedBLOBPage',
	'45bf':u'B-treeNode'
	}
	
innodb_page_direction={
	'0000':'Unknown(0x0000)',
	'0001':'PageLeft',
	'0002':'PageRight',
	'0003':'PageSameRec',
	'0004':'PageSamePage',
	'0005':'PageNoDirection',
	'ffff':'Unkown2(0xffff)'
}

INNODB_PAGE_SIZE=1024*16#InnoDBPage16K