1

我正在尝试为我的项目自动化一项任务,我必须解析一个日志文件并搜索特定的 pid。如果该 pid 遇到超过 50 次,那么我必须打印我的消息。

我的代码:

#!/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"; 

  } 

}

日志:

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. 

使用我的代码,我无法处理其他 pid,即如何仅计算日志中存在超过 50 次的 pid。如果发生 UI 冻结,则只有一个 pid 会超过此计数。

4

2 回答 2

3

哈希对于跟踪出现了多少xxxxx很有用。

例如

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";
    }
}

但是,通常您只想为每个超出限制的 PID 打印一条警告消息,因此我们会将散列的处理保存到最后,例如:

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" );
    }
}

稍后,如果您想让代码更简洁一些,您可以将最后一部分重写为:

my @failedpids = grep { $pids{$_} > 100 } keys %pids;
print( "Failed PIDs were: " );
print( join( ", ", sort { $a <=> $b } @failedpids ) );
print( "\n" );
于 2013-09-18T11:22:12.077 回答
1

这实际上只是一个简单的计数练习。您使用正则表达式捕获字符串,并使用哈希计算其频率:

perl -nlwe '$a{$1}++ if /Input event injection from pid (\d+)/ 
           END { for (keys %a) { print qq($_ -> $a{$_}) } }' inject.log

我从你的输入中得到这个输出:

2061 -> 38
2019 -> 2
2041 -> 2

作为一个程序文件,它看起来像这样:

while (<>) {
    $a{$1}++ if /Input event injection from pid (\d+)/;
}
for my $key (keys %a) {
    print "$key -> $a{$key}\n";
}

<>菱形运算符要么读取 STDIN,要么将参数视为要用于输入的文件名。$a{$1}++使用从哈希中的正则表达式捕获的字符串,并++增加其值。

当然,我为所有元素选择了一个简单的打印,但您可以简单地为超过 50 的值添加一个检查:

next if $a{$key} < 50;

关于您的代码的一些注释:

  • 不要使用 C 风格的 for 循环,除非你真的需要知道你正在循环的数组的索引。而是使用 Perl 风格,通常称为“foreach”:for my $line (@lines)
  • 如果不需要,不要将整个文件读入数组。您可以像这样简单地迭代文件句柄:while (<$fh>)- 这将在每次迭代中读取一行$_(或您选择的变量,例如while (my $foo = <$fh>)),直到文件结束eof
  • 很高兴看到您正在使用use strict和警告。通常,use warnings最好-w在 shebang 上使用,因为它更具可读性,而且它是一个词法编译指示,而不是全局的。
于 2013-09-18T11:38:19.293 回答