TraceFormatter.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright 2008, Yahoo! Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.yahoo.zookeeper.server;
  17. import java.io.FileInputStream;
  18. import java.io.IOException;
  19. import java.nio.ByteBuffer;
  20. import java.nio.channels.FileChannel;
  21. import java.text.DateFormat;
  22. import java.util.Date;
  23. import com.yahoo.zookeeper.ZooDefs.OpCode;
  24. public class TraceFormatter {
  25. static String op2String(int op) {
  26. switch (op) {
  27. case OpCode.notification:
  28. return "notification";
  29. case OpCode.create:
  30. return "create";
  31. case OpCode.delete:
  32. return "delete";
  33. case OpCode.exists:
  34. return "exists";
  35. case OpCode.getData:
  36. return "getDate";
  37. case OpCode.setData:
  38. return "setData";
  39. case OpCode.getACL:
  40. return "getACL";
  41. case OpCode.setACL:
  42. return "setACL";
  43. case OpCode.getChildren:
  44. return "getChildren";
  45. case OpCode.ping:
  46. return "ping";
  47. case OpCode.createSession:
  48. return "createSession";
  49. case OpCode.closeSession:
  50. return "closeSession";
  51. case OpCode.error:
  52. return "error";
  53. default:
  54. return "unknown " + op;
  55. }
  56. }
  57. /**
  58. * @param args
  59. * @throws IOException
  60. */
  61. public static void main(String[] args) throws IOException {
  62. if (args.length != 1) {
  63. System.err.println("USAGE: TraceFormatter trace_file");
  64. System.exit(2);
  65. }
  66. FileChannel fc = new FileInputStream(args[0]).getChannel();
  67. while (true) {
  68. ByteBuffer bb = ByteBuffer.allocate(41);
  69. fc.read(bb);
  70. bb.flip();
  71. byte app = bb.get();
  72. long time = bb.getLong();
  73. long id = bb.getLong();
  74. int cxid = bb.getInt();
  75. long zxid = bb.getLong();
  76. int txnType = bb.getInt();
  77. int type = bb.getInt();
  78. int len = bb.getInt();
  79. bb = ByteBuffer.allocate(len);
  80. fc.read(bb);
  81. bb.flip();
  82. String path = "n/a";
  83. if (bb.remaining() > 0) {
  84. if (type != OpCode.createSession) {
  85. int pathLen = bb.getInt();
  86. byte b[] = new byte[pathLen];
  87. bb.get(b);
  88. path = new String(b);
  89. }
  90. }
  91. System.out.println(DateFormat.getDateTimeInstance(DateFormat.SHORT,
  92. DateFormat.LONG).format(new Date(time))
  93. + ": "
  94. + (char) app
  95. + " id="
  96. + Long.toHexString(id)
  97. + " cxid="
  98. + cxid
  99. + " op="
  100. + op2String(type)
  101. + " zxid="
  102. + Long.toHexString(zxid)
  103. + " txnType="
  104. + txnType
  105. + " len="
  106. + len + " path=" + path);
  107. }
  108. }
  109. }