Reputation: 587
I am trying to automate one task for my project there i have to parse one log file and search for specific pid. If that pid encounter more then 50 time then i have to print my message.
My code:
#!/usr/bin/perl -w
use strict;
open(my $fh,"<","UIlogs.txt") or die "can not open the file $!";
my @lines = <$fh>;
close($fh);
my $count=0;
for(my $i=0; $i<=$#lines;$i++)
{
if($lines[$i] =~ m/Input event injection from pid (\d+) failed/gi)
{
#print"UI freez\n";
print"$1\n";
}
}
Log:
07-10 16:36:43.632 784 784 W InputManager: Input event injection from pid 2019 failed.
07-10 16:36:43.632 784 795 W InputManager: Input event injection from pid 2019 failed.
07-10 16:36:45.114 2041 2041 D AndroidRuntime:
07-10 16:36:45.114 2041 2041 D AndroidRuntime: >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
07-10 16:36:45.114 2041 2041 D AndroidRuntime: CheckJNI is OFF
07-10 16:36:45.124 2041 2041 D dalvikvm: Trying to load lib libjavacore.so 0x0
07-10 16:36:45.124 2041 2041 D dalvikvm: Added shared lib libjavacore.so 0x0
07-10 16:36:45.134 2041 2041 D dalvikvm: Trying to load lib libnativehelper.so 0x0
07-10 16:36:45.134 2041 2041 D dalvikvm: Added shared lib libnativehelper.so 0x0
07-10 16:36:45.244 2041 2041 D AndroidRuntime: Calling main entry com.android.commands.input.Input
07-10 16:36:45.244 2041 2041 I Input : injectKeyEvent: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_DPAD_CENTER, scanCode=0, metaState=0, flags=0x0, repeatCount=0, eventTime=166218, downTime=166218, deviceId=-1, source=0x101 }
07-10 16:36:45.254 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:45.254 2041 2041 I Input : injectKeyEvent: KeyEvent { action=ACTION_UP, keyCode=KEYCODE_DPAD_CENTER, scanCode=0, metaState=0, flags=0x0, repeatCount=0, eventTime=166218, downTime=166218, deviceId=-1, source=0x101 }
07-10 16:36:45.254 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:45.254 2041 2041 D AndroidRuntime: Shutting down VM
07-10 16:36:45.254 2041 2050 D dalvikvm: GC_CONCURRENT freed 94K, 17% free 479K/576K, paused 0ms+0ms, total 3ms
07-10 16:36:45.254 784 1016 W InputManager: Input event injection from pid 2041 failed.
07-10 16:36:45.254 784 1083 W InputManager: Input event injection from pid 2041 failed.
07-10 16:36:52.932 2061 2061 D AndroidRuntime:
07-10 16:36:52.932 2061 2061 D AndroidRuntime: >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
07-10 16:36:52.932 2061 2061 D AndroidRuntime: CheckJNI is OFF
07-10 16:36:52.942 2061 2061 D dalvikvm: Trying to load lib libjavacore.so 0x0
07-10 16:36:52.942 2061 2061 D dalvikvm: Added shared lib libjavacore.so 0x0
07-10 16:36:52.952 2061 2061 D dalvikvm: Trying to load lib libnativehelper.so 0x0
07-10 16:36:52.952 2061 2061 D dalvikvm: Added shared lib libnativehelper.so 0x0
07-10 16:36:53.082 2061 2061 D AndroidRuntime: Calling main entry com.android.commands.monkey.Monkey
07-10 16:36:53.092 2061 2061 D dalvikvm: Note: class Landroid/app/ActivityManagerNative; has 163 unimplemented (abstract) methods
07-10 16:36:53.112 784 1076 I ActivityManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.android.contacts/.activities.PeopleActivity} from pid 2061
07-10 16:36:53.122 784 1076 I ActivityManager: Start proc com.android.contacts for activity com.android.contacts/.activities.PeopleActivity: pid=2077 uid=10000 gids={50000, 3003, 1015, 1028}
07-10 16:36:53.212 2077 2077 I ContactsIntentResolver: Called with action: android.intent.action.MAIN
07-10 16:36:53.232 2077 2092 I AccountTypeManager: Loaded meta-data for 1 account types, 0 accounts in 19ms(wall) 5ms(cpu)
07-10 16:36:53.272 2077 2077 I ContactPhotoManager: Cache adj: 1.0
07-10 16:36:53.463 2077 2077 D libEGL : loaded /vendor/lib/egl/libEGL_adreno.so
07-10 16:36:53.463 2077 2077 D libEGL : loaded /vendor/lib/egl/libGLESv1_CM_adreno.so
07-10 16:36:53.463 2077 2077 D libEGL : loaded /vendor/lib/egl/libGLESv2_adreno.so
07-10 16:36:53.473 2077 2077 I Adreno-EGL: <qeglDrvAPI_eglInitialize:316>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB_2.6.04.02.02.109.046_msm8960_JB_2.6_CL3925444_release_AU (CL3925444)
07-10 16:36:53.473 2077 2077 I Adreno-EGL: OpenGL ES Shader Compiler Version: 17.01.08
07-10 16:36:53.473 2077 2077 I Adreno-EGL: Build Date: 07/09/13 Tue
07-10 16:36:53.473 2077 2077 I Adreno-EGL: Local Branch: mybranch1230998
07-10 16:36:53.473 2077 2077 I Adreno-EGL: Remote Branch: quic/jb_3.1
07-10 16:36:53.473 2077 2077 I Adreno-EGL: Local Patches: NONE
07-10 16:36:53.473 2077 2077 I Adreno-EGL: Reconstruct Branch: AU_LINUX_ANDROID_JB_2.6.04.02.02.109.046 + e363aa2 + 76767df + NOTHING
07-10 16:36:53.523 2077 2077 D OpenGLRenderer: Enabling debug mode 0
07-10 16:36:53.623 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:53.623 784 857 I ActivityManager: Displayed com.android.contacts/.activities.PeopleActivity: +511ms
07-10 16:36:53.623 784 1016 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:53.633 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:53.633 784 1103 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:54.133 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:54.133 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:54.133 784 1083 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:54.133 784 1016 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:54.634 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:54.634 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:54.634 784 796 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:54.634 784 795 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:55.144 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:55.144 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:55.144 784 1103 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:55.144 784 1191 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:55.645 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:55.645 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:55.645 784 1016 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:55.655 784 1083 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:56.156 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:56.156 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:56.156 784 784 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:56.156 784 795 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:56.666 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:56.666 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:56.666 784 796 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:56.666 784 1076 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:57.167 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:57.167 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:57.167 784 1190 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:57.167 784 1191 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:57.677 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:57.677 784 1103 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:57.677 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:57.677 784 1083 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:58.188 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:58.188 784 1016 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:58.188 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:58.188 784 795 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:58.698 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:58.698 784 784 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:58.698 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:58.698 784 796 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:59.039 784 910 W QCNEJ : |CORE| UNKOWN Unsolicited Event 6
07-10 16:36:59.209 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:59.209 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:59.209 784 1190 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:59.209 784 1103 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:59.719 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:59.719 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:59.719 784 1083 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:59.719 784 795 W InputManager: Input event injection from pid 2061 failed.
07-10 16:37:00.230 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:37:00.230 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:37:00.230 784 1191 W InputManager: Input event injection from pid 2061 failed.
07-10 16:37:00.230 784 1076 W InputManager: Input event injection from pid 2061 failed.
07-10 16:37:00.740 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:37:00.740 784 1103 W InputManager: Input event injection from pid 2061 failed.
07-10 16:37:00.740 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:37:00.740 784 1190 W InputManager: Input event injection from pid 2061 failed.
07-10 16:37:01.251 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:37:01.251 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:37:01.251 784 1016 W InputManager: Input event injection from pid 2061 failed.
07-10 16:37:01.251 784 784 W InputManager: Input event injection from pid 2061 failed.
07-10 16:37:01.762 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:37:01.762 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:37:01.762 784 796 W InputManager: Input event injection from pid 2061 failed.
07-10 16:37:01.762 784 1076 W InputManager: Input event injection from pid 2061 failed.
07-10 16:37:02.272 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:37:02.272 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:37:02.272 784 1191 W InputManager: Input event injection from pid 2061 failed.
07-10 16:37:02.272 784 1103 W InputManager: Input event injection from pid 2061 failed.
07-10 16:37:02.773 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:37:02.773 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:37:02.773 784 1190 W InputManager: Input event injection from pid 2061 failed.
07-10 16:37:02.783 784 795 W InputManager: Input event injection from pid 2061 failed.
With my code i am not able to handle other pid i.e how to count only that pid which is there in the log for more then 50 time . Only one pid would be cross this count if UI freez will happen.
Upvotes: 1
Views: 365
Reputation: 67900
This is really just a simple counting exercise. You capture a string with a regex, and you count its frequency with a hash:
perl -nlwe '$a{$1}++ if /Input event injection from pid (\d+)/
END { for (keys %a) { print qq($_ -> $a{$_}) } }' inject.log
I get this output from your input:
2061 -> 38
2019 -> 2
2041 -> 2
As a program file, it would look like this:
while (<>) {
$a{$1}++ if /Input event injection from pid (\d+)/;
}
for my $key (keys %a) {
print "$key -> $a{$key}\n";
}
<>
the diamond operator either reads STDIN, or treats the arguments as file names to be used for input. $a{$1}++
uses the string captured from the regex in a hash, and ++
increments its value.
Of course I chose a simple print for all elements, but you could simply add a check for values over 50:
next if $a{$key} < 50;
Some notes on your code:
for my $line (@lines)
while (<$fh>)
-- this will read one line per iteration into $_
(or the variable of your choice, e.g. while (my $foo = <$fh>)
), until end of file eof
.use strict
and warnings. Usually, use warnings
is preferred to using -w
on the shebang, since it is more readable, and also it is a lexical pragma, rather than global.Upvotes: 1
Reputation: 10864
A hash is useful for keeping track of how many xxxxx appears.
e.g.
if($lines[$i] =~ m/Input event injection from pid (\d+) failed/gi)
{
my $foundpid = $1;
print "Found pid:$1\n";
$pids{$foundpid} += 1;
if ( $pids{$foundpid} > 100 ) {
print "PID $foundpid is extra naughty today!\n";
}
}
Usually you only want to print one warning message for each PID that exceeds your limit, however, so we would save the processing of the hash to the end, e.g.:
my %pids = (); # initialise hash
for ( ... ) { # process lines
if ( ... ) { # match line
$pids{$foundpid} += 1;
}
}
# now scan our hash of pids and filter those that failed the condition
foreach my $pid ( sort { $a <=> $b keys %pids ) ) {
if ( $pids{$pid} > 100 ) {
print( "Bad PID $pid was seen " . $pids{$pid} . " times!\n" );
}
}
Later if you want to make your code a little more terse you could re-write that last part as:
my @failedpids = grep { $pids{$_} > 100 } keys %pids;
print( "Failed PIDs were: " );
print( join( ", ", sort { $a <=> $b } @failedpids ) );
print( "\n" );
Upvotes: 3