当前位置: > Linux编程 >

20分钟 Awk 入门(2)

时间:2015-02-09 21:46来源:linux.it.net.cn 作者:IT


示例

以上的就是Awk语言的核心内容。我这里没有大量的例子,因为我趋向于使用Awk来完成快速的一次性任务。

不过我依然有一些随身携带的脚本文件,用来处理一些事情和测试。我最喜欢的一个脚本是用来处理Erlang的崩溃转储文件,形如下面的:


  1. =erl_crash_dump:0.3
  2. Tue Nov 18 02:52:44 2014
  3. Slogan: init terminating in do_boot ()
  4. System version: Erlang/OTP 17 [erts-6.2] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]
  5. Compiled: Fri Sep 19 03:23:19 2014
  6. Taints:
  7. Atoms: 12167
  8. =memory
  9. total: 19012936
  10. processes: 4327912
  11. processes_used: 4319928
  12. system: 14685024
  13. atom: 339441
  14. atom_used: 331087
  15. binary: 1367680
  16. code: 8384804
  17. ets: 382552
  18. =hash_table:atom_tab
  19. size: 9643
  20. used: 6949
  21. ...
  22. =allocator:instr
  23. option m: false
  24. option s: false
  25. option t: false
  26. =proc:<0.0.0>
  27. State: Running
  28. Name: init
  29. Spawned as: otp_ring0:start/2
  30. Run queue: 0
  31. Spawned by: []
  32. Started: Tue Nov 18 02:52:35 2014
  33. Message queue length: 0
  34. Number of heap fragments: 0
  35. Heap fragment data: 0
  36. Link list: [<0.3.0>, <0.7.0>, <0.6.0>]
  37. Reductions: 29265
  38. Stack+heap: 1598
  39. OldHeap: 610
  40. Heap unused: 656
  41. OldHeap unused: 468
  42. Memory: 18584
  43. Program counter: 0x00007f42f9566200 (init:boot_loop/2 + 64)
  44. CP: 0x0000000000000000 (invalid)
  45. =proc:<0.3.0>
  46. State: Waiting
  47. ...
  48. =port:#Port<0.0>
  49. Slot: 0
  50. Connected: <0.3.0>
  51. Links: <0.3.0>
  52. Port controls linked-in driver: efile
  53. =port:#Port<0.14>
  54. Slot: 112
  55. Connected: <0.3.0>
  56. ...

产生下面的结果:


  1. $ awk -f queue_fun.awk $PATH_TO_DUMP
  2. MESSAGE QUEUE LENGTH: CURRENT FUNCTION
  3. ======================================
  4. 10641: io:wait_io_mon_reply/2
  5. 12646: io:wait_io_mon_reply/2
  6. 32991: io:wait_io_mon_reply/2
  7. 2183837: io:wait_io_mon_reply/2
  8. 730790: io:wait_io_mon_reply/2
  9. 80194: io:wait_io_mon_reply/2
  10. ...

这是在Erlang进程里运行的函数列表,它们导致了mailboxe变得很庞大。脚本在这:


  1. # Parse Erlang Crash Dumps and correlate mailbox size to the currently running
  2. # function.
  3. #
  4. # Once in the procs section of the dump, all processes are displayed with
  5. # =proc:<0.M.N> followed by a list of their attributes, which include the
  6. # message queue length and the program counter (what code is currently
  7. # executing).
  8. #
  9. # Run as:
  10. #
  11. # $ awk -v threshold=$THRESHOLD -f queue_fun.awk $CRASHDUMP
  12. #
  13. # Where $THRESHOLD is the smallest mailbox you want inspects. Default value
  14. # is 1000.
  15. BEGIN {
  16. if (threshold == "") {
  17. threshold = 1000 # default mailbox size
  18. }
  19. procs = 0 # are we in the =procs entries?
  20. print "MESSAGE QUEUE LENGTH: CURRENT FUNCTION"
  21. print "======================================"
  22. }
  23.  
  24. # Only bother with the =proc: entries. Anything else is useless.
  25. procs == 0 && /^=proc/ { procs = 1 } # entering the =procs entries
  26. procs == 1 && /^=/ && !/^=proc/ { exit 0 } # we're done
  27.  
  28. # Message queue length: 1210
  29. # 1 2 3 4
  30. /^Message queue length: / && $4 >= threshold { flag=1; ct=$4 }
  31. /^Message queue length: / && $4 < threshold { flag=0 }
  32.  
  33. # Program counter: 0x00007f5fb8cb2238 (io:wait_io_mon_reply/2 + 56)
  34. # 1 2 3 4 5 6
  35. flag == 1 && /^Program counter: / { print ct ":", substr($4,2) }

你跟上思路没?如果跟上了,你已经了解了Awk。恭喜!

 

 

 


(责任编辑:IT)
------分隔线----------------------------