kidwahoo94 has asked for the wisdom of the Perl Monks concerning the following question:

Can anyone lead me to some good info on using perl to check thread counts for processes running in the Win32 environment. Or even if it can be done.

-We are what we do repeatedly; therefore, excellence is not an event but a habit.......

edited: Thu Apr 3 21:06:01 2003 by jeffa - removed code tag

Replies are listed 'Best First'.
Re: Thread Counts (PerfLib)
by tye (Sage) on Apr 03, 2003 at 19:56 UTC
Re: Thread Counts
by BrowserUk (Patriarch) on Apr 04, 2003 at 03:41 UTC

    As tye pointed out, Win32::PerfLib is the way to get the info you want.

    However, using this module is extremely non-intuative due to the amount and complexity of the information returned and the weird format it is returned in, so here's some crude code to show how to get and display some of the info available, including the process name/ID/thread count.

    #! perl -w use strict; use Data::Dumper; use Win32::PerfLib; my (%counters, %r_counters); Win32::PerfLib::GetCounterNames('', \%counters); # Build a reverse lookup table. Names are clearer than numbers. @r_counters{values %counters} = (keys %counters); # retrieve the id for process object my $processObj_id = $r_counters{Process}; # retrieve the id for the process ID counter my $processCounter_id = $r_counters{'ID Process'}; my %processes; # create connection to $server my $perflib = new Win32::PerfLib(''); # get the performance data for the process object $perflib->GetObjectList($processObj_id, \%processes); $perflib->Close(); my $pInstances = $processes{Objects}{$processObj_id}{Instances}; foreach my $p (keys %{$pInstances}) { my $pICounters = $pInstances->{$p}{Counters}; print "\nProcess name=$pInstances->{$p}{Name}\n"; foreach my $i (keys %{$pICounters}) { printf "%s=%s %s\n", $counters{$pICounters->{$i}{CounterNameTitleIndex}}||'?', $pICounters->{$i}{Counter}, exists $pICounters->{$i}{Display} ? $pICounters->{$i}{Disp +lay} : ''; } # print Dumper $pICounters; # Uncomment to view other available inf +o }

    Some sample output


    Examine what is said, not who speaks.
    1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
    2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
    3) Any sufficiently advanced technology is indistinguishable from magic.
    Arthur C. Clarke.
      Other options include using Win32::API and calling standard Win32 API functions, OR using Win32::OLE and WMI like so:

      use strict; use Win32::OLE; my $WMI = Win32::OLE->GetObject("winmgmts:"); my $Procs = $WMI->InstancesOf("Win32_Process"); foreach my $Proc (in $Procs) { printf( "PID %u has %u threads.\n", $Proc->ProcessId, $Proc->ThreadCount, ); }

      Very quick & easy. The Win32_Process class has other valuable properties too. See the class documentation @ MSDN for more info.

A reply falls below the community's threshold of quality. You may see it by logging in.