Trace.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. var Class = require('./Class');
  2. var each = require('./each');
  3. var map = require('./map');
  4. exports = Class({
  5. initialize: function Trace() {
  6. var _this = this;
  7. var events =
  8. arguments.length > 0 && arguments[0] !== undefined
  9. ? arguments[0]
  10. : [];
  11. this._processes = {};
  12. each(events, function(event) {
  13. return _this.addEvent(event);
  14. });
  15. },
  16. addEvent: function(event) {
  17. var process = this.getProcess(event.pid);
  18. process.addEvent(event);
  19. },
  20. rmEvent: function(event) {
  21. var process = this.getProcess(event.pid);
  22. process.rmEvent(event);
  23. },
  24. getProcess: function(id) {
  25. var process = this._processes[id];
  26. if (!process) {
  27. process = new Process(id);
  28. this._processes[id] = process;
  29. }
  30. return process;
  31. },
  32. rmProcess: function(id) {
  33. delete this._processes[id];
  34. },
  35. processes: function() {
  36. return map(this._processes);
  37. },
  38. toJSON: function() {
  39. var events = [];
  40. each(this.processes(), function(process) {
  41. events.push.apply(events, process.toJSON());
  42. });
  43. return events;
  44. }
  45. });
  46. var Process = Class({
  47. initialize: function Process(id) {
  48. this._id = id;
  49. this._name = '';
  50. this._threads = {};
  51. this._metadata = {};
  52. },
  53. id: function() {
  54. return this._id;
  55. },
  56. name: function() {
  57. return this._name;
  58. },
  59. addEvent: function(event) {
  60. if (event.cat === '__metadata') {
  61. if (event.name === 'process_name') {
  62. this._name = event.args.name;
  63. }
  64. if (event.tid === 0) {
  65. this._metadata[event.name] = event.args;
  66. return;
  67. }
  68. }
  69. var thread = this.getThread(event.tid);
  70. thread.addEvent(event);
  71. },
  72. rmEvent: function(event) {
  73. var thread = this.getThread(event.tid);
  74. thread.rmEvent(event);
  75. },
  76. getThread: function(id) {
  77. var thread = this._threads[id];
  78. if (!thread) {
  79. thread = new Thread(id, this.id());
  80. this._threads[id] = thread;
  81. }
  82. return thread;
  83. },
  84. rmThread: function(id) {
  85. delete this._threads[id];
  86. },
  87. threads: function() {
  88. return map(this._threads);
  89. },
  90. toJSON: function() {
  91. var _this2 = this;
  92. var events = [];
  93. each(this._metadata, function(args, name) {
  94. events.push(createMetaEvent(_this2._id, 0, name, args));
  95. });
  96. each(this.threads(), function(thread) {
  97. events.push.apply(events, thread.toJSON());
  98. });
  99. return events;
  100. }
  101. });
  102. var Thread = Class({
  103. initialize: function Thread(id, pid) {
  104. this._id = id;
  105. this._pid = pid;
  106. this._name = '';
  107. this._events = [];
  108. this._metadata = {};
  109. },
  110. id: function() {
  111. return this._id;
  112. },
  113. name: function() {
  114. return this._name;
  115. },
  116. addEvent: function(event) {
  117. if (event.cat === '__metadata') {
  118. if (event.name === 'thread_name') {
  119. this._name = event.args.name;
  120. }
  121. this._metadata[event.name] = event.args;
  122. return;
  123. }
  124. this._events.push(event);
  125. },
  126. rmEvent: function(event) {
  127. var events = this._events;
  128. events.splice(events.indexOf(event), 1);
  129. },
  130. events: function() {
  131. return map(this._events);
  132. },
  133. toJSON: function() {
  134. var _this3 = this;
  135. var events = [];
  136. each(this._metadata, function(args, name) {
  137. events.push(createMetaEvent(_this3._pid, _this3._id, name, args));
  138. });
  139. each(this.events(), function(event) {
  140. events.push(event);
  141. });
  142. return events;
  143. }
  144. });
  145. function createMetaEvent(pid, tid, name, args) {
  146. return {
  147. args: args,
  148. cat: '__metadata',
  149. name: name,
  150. ph: 'M',
  151. pid: pid,
  152. tid: tid,
  153. ts: 0
  154. };
  155. }
  156. module.exports = exports;