Help With PyParsing of output from win32pdhutil.ShowAllProcesses()

Steve sreisscruz at gmail.com
Tue Sep 11 20:08:47 EDT 2007


Hi All,

I did a lot of digging into the code in the module, win32pdhutil, and
decided to create some custom methods.


added to : import win32pdhutil



def ShowAllProcessesAsList():

    object = find_pdh_counter_localized_name("Process")
    items, instances =
win32pdh.EnumObjectItems(None,None,object,win32pdh.PERF_DETAIL_WIZARD)

    # Need to track multiple instances of the same name.
    instance_dict = {}
    all_process_dict = {}

    for instance in instances:
        try:
            instance_dict[instance] = instance_dict[instance] + 1
        except KeyError:
            instance_dict[instance] = 0

    # Bit of a hack to get useful info.

    items = [find_pdh_counter_localized_name("ID Process")] + items[:
5]
#    print items
#    print "Process Name", string.join(items,",")

    all_process_dict['Headings'] = items                    # add
headings to dict

    for instance, max_instances in instance_dict.items():

        for inum in xrange(max_instances+1):
            hq = win32pdh.OpenQuery()
            hcs = []
            row = []

            for item in items:
                path =
win32pdh.MakeCounterPath( (None,object,instance,None, inum, item) )
                hcs.append(win32pdh.AddCounter(hq, path))

            win32pdh.CollectQueryData(hq)
            # as per http://support.microsoft.com/default.aspx?scid=kb;EN-US;q262938,
some "%" based
            # counters need two collections
            time.sleep(0.01)
            win32pdh.CollectQueryData(hq)
#            print "%-15s\t" % (instance[:15]),

            row.append(instance[:15])

            for hc in hcs:
                type, val = win32pdh.GetFormattedCounterValue(hc,
win32pdh.PDH_FMT_LONG)
#                print "item : %5d" % (val),
                row.append(val)
                win32pdh.RemoveCounter(hc)

#            print
#            print ' row = ', instance ,row
            all_process_dict[instance] = row                    # add
current row to dict

            win32pdh.CloseQuery(hq)

    return all_process_dict


def ShowSingleProcessAsList(sProcessName):

    object = find_pdh_counter_localized_name("Process")
    items, instances =
win32pdh.EnumObjectItems(None,None,object,win32pdh.PERF_DETAIL_WIZARD)

    # Need to track multiple instances of the same name.
    instance_dict = {}
    all_process_dict = {}

    for instance in instances:
        try:
            instance_dict[instance] = instance_dict[instance] + 1
        except KeyError:
            instance_dict[instance] = 0

    # Bit of a hack to get useful info.

    items = [find_pdh_counter_localized_name("ID Process")] + items[:
5]
#    print items
#    print "Process Name", string.join(items,",")

#    all_process_dict['Headings'] = items                    # add
headings to dict

#    print 'instance dict = ', instance_dict
#    print

    if sProcessName in instance_dict:
        instance = sProcessName
        max_instances = instance_dict[sProcessName]
#        print sProcessName, '  max_instances = ', max_instances

        for inum in xrange(max_instances+1):
            hq = win32pdh.OpenQuery()
            hcs = []
            row = []

            for item in items:
                path =
win32pdh.MakeCounterPath( (None,object,instance,None, inum, item) )
                hcs.append(win32pdh.AddCounter(hq, path))

            try:
              win32pdh.CollectQueryData(hq)
            except:
              all_process_dict[sProcessName] =
[0,0,0,0,0,0,0]          # process not found - set to all zeros
              break

            # as per http://support.microsoft.com/default.aspx?scid=kb;EN-US;q262938,
some "%" based
            # counters need two collections
            time.sleep(0.01)
            win32pdh.CollectQueryData(hq)
#            print "%-15s\t" % (instance[:15]),

            row.append(instance[:15])

            for hc in hcs:
                type, val = win32pdh.GetFormattedCounterValue(hc,
win32pdh.PDH_FMT_LONG)
#                print "item : %5d" % (val),
                row.append(val)
                win32pdh.RemoveCounter(hc)

#            print
#            print ' row = ', instance ,row
            all_process_dict[instance] = row                    # add
current row to dict

            win32pdh.CloseQuery(hq)
    else:
      all_process_dict[sProcessName] = [0,0,0,0,0,0,0]          #
process not found - set to all zeros

    return all_process_dict

=============================

Demo :

import win32pdhutil      # with customized methods in win32pdhutil
(above)


###################################################################
#              GetMemoryStats                                     #
###################################################################

def GetMemoryStats(sProcessName, iPauseTime):

  Memory_Dict = {}

## Headings   ['ProcessName', '% Processor Time', '% User Time', '%
Privileged Time', 'Virtual Bytes Peak', 'Virtual Bytes']
##machine process  =  {'firefox': ['firefox', 2364, 0, 0, 0,
242847744, 211558400]}

  loop_counter = 0

  print('\n\n** Starting Free Memory Sampler **\n\n')
  print('Process : %s\n  Delay : %d  seconds\n\n') % (sProcessName,
iPauseTime)
  print('\n\nPress : Ctrl-C to stop and output stats...\n\n')


  try:

      while 1:
        print('Sample : %d') % loop_counter
        row = []

        machine_process =
win32pdhutil2.ShowSingleProcessAsList(sProcessName)
#        print 'machine process  = ', machine_process
        row.append(machine_process[sProcessName]
[5])                         # Virtual Bytes Peak
        row.append(machine_process[sProcessName]
[6])                         # Virtual Bytes

        Memory_Dict[loop_counter] =
row                                      # add values to the
dictionary
        loop_counter += 1
        time.sleep(iPauseTime)

  except KeyboardInterrupt:                 # Ctrl-C encountered
    print "End of Sample...\n\n"


  return Memory_Dict


###################################################################
#############           M A I N         ###########################
###################################################################

def Main():

  iPause_time = 5                             # pause time - seconds
  sProcessName = 'firefox'                    # Process to watch
  sReportFileName = 'MemoryStats.csv'         # output filename

  Memory_Dict = GetMemoryStats(sProcessName, iPause_time)


  outfile = open(sReportFileName,"w")       # send output to a file
  outfile.write('SampleTime, VirtualBytesMax, VirtualBytes\n')


  for current_stat in Memory_Dict:
    line = ('%s,%d,%d\n') % (current_stat, Memory_Dict[current_stat]
[0],Memory_Dict[current_stat][1] )
    outfile.write(line)


  outfile.close()            # close output file


if __name__ == "__main__":
  Main()


-------------------------

I have found that the process that you want to want to monitor needs
to be started before this script is started. The script will handle
when the process disappears and set the stats to zeros.

Enjoy!

Steve




More information about the Python-list mailing list