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