I am going to have a LinuxIO training in the company in the near future. I will sort out the relevant materials and share with you the position of various I/O monitoring tools in the LinuxIO Architecture & nbsp
I am going to have a Linux I/O training in the company in the near future. I will sort out the relevant materials and share them with you.
#! /Usr/bin/env python #-*-coding: gbk-*-import osimport reimport timeit from ctypes import CDLL, create_string_buffer, c_ulong, c_longlongfrom optparse import OptionParserusage = ''' % prog-s strace. log-f fileno. map ''' _ glibc = None_glibc_pread = None_c_char_buf = None_open_file = [] def getlines (filename): _ lines = [] with open (filename, 'r') as _ f: for line in _ f: if line. strip ()! = "": _ Lines. append (line. strip () return _ lines def parsew.line (): parser = OptionParser (usage) parser. add_option ("-s", "-- strace", dest = "strace_filename", help = "strace file", metavar = "FILE") parser. add_option ("-f", "-- fileno", dest = "fileno_filename", help = "fileno file", metavar = "FILE") (options, args) = parser. parse_args () if options. strace_filename is None: parser. error ("strace is not specified. ") Ifnot OS. path. exists (options. strace_filename): parser. error ("strace file does not exist. ") if options. fileno_filename is None: parser. error ("fileno is not specified. ") ifnot OS. path. exists (options. strace_filename): parser. error ("fileno file does not exist. ") return options. strace_filename, options. fileno_filename # [type,...] # [pread, fno, count, offset] # pread (15, "", 4348,140 156928) def pars E_strace (filename): lines = getlines (filename) action = [] _ regex_str = r' (pread | pread64) [^ \ d] * (\ d + ), \ s * [^,] *, \ s * ([\ dkKmM * + \-.] *), \ s * ([\ dkKmM * + \-.] *) 'for I in lines: _ match = re. match (_ regex_str, I) if _ match is None: continue # skip invalid rows _ type, _ fn, _ count, _ off = _ match. group (1), _ match. group (2), _ match. group (3), _ match. group (4) _ off = _ off. replace ('K', "* 1024 "). replace ('K', "* 1024 "). replace ('M', "* 1048576 "). replace ('M', "* 1048576") _ count = _ count. replace ('K', "* 1024 "). replace ('K', "* 1024 "). replace ('M', "* 1048576 "). replace ('M', "* 1048576") # print _ off action. append ([_ type, _ fn, str (int (eval (_ count), str (int (eval (_ off)]) return action def parse_fileno (filename): lines = getlines (filename) fmap ={} for I in lines: if I. strip (). startswith ("#"): continue # comment row _ spl It = [j. strip () for j in I. split ("=")] if len (_ split )! = 2: continue # invalid row fno, fname = _ split [0], _ split [1] fmap [fno] = fname return fmap def simulate_before (strace, fmap ): global _ open_file, _ c_char_buf rfmap ={} for I in fmap. values (): _ f = open (I, "r + B") # print "open {0 }:{ 1 }". format (_ f. fileno (), I) _ open_file.append (_ f) rfmap [I] = str (_ f. fileno () # reverse ing to_read = 4*1024 # default 4 K buffor I in strace: I [1] = rfmap [fmap [I [1] # fid-> fname-> fid ing conversion to_read = max (to_read, int (I [2]) # print "read buffer len: % d Byte" % to_read _ c_char_buf = create_string_buffer (to_read) def simulate_after (): global _ open_file for _ f in _ open_file: _ f. close () def simulate (actions): # timeit. time. sleep (10) # rest for 2 seconds so that the IO interval start = timeit. time. time () for act in actions: _ simulate _ (act) finish = timeit. time. time () return finish-start def _ simulate _ (act): global _ glibc, _ glibc_pread, _ c_char_buf if "pread" in act [0]: _ fno = int (act [1]) _ buf = _ c_char_buf _ count = c_ulong (int (act [2]) _ off = c_longlong (int (act [3]) _ glibc_pread (_ fno, _ buf, _ count, _ off) # print _ glibc. time (None) else: passpassdef loadlibc (): global _ glibc, _ glibc_pread _ glibc = CDLL ("libc. so.6 ") _ glibc_pread = _ glibc. pread64 if _ name _ = "_ main _": _ strace, _ fileno = parsesponline () # parse the command line parameter loadlibc () # loading dynamic library _ action = parse_strace (_ strace) # Parsing action File _ fmap = parse_fileno (_ fileno) # parsing file name ing file simulate_before (_ action, _ fmap) # preprocessing # print "total io operate: % d" % (len (_ action) # for act in _ action: print "". join (act) print "% f" % simulate (_ action)