|
| 1 | +import json |
1 | 2 | import os
|
2 | 3 |
|
3 | 4 | import pandas
|
@@ -43,6 +44,84 @@ def check(self):
|
43 | 44 | events.append(filename)
|
44 | 45 | self.files = events
|
45 | 46 |
|
| 47 | + def to_perfetto(self, outfile): |
| 48 | + """ |
| 49 | + Generate perfetto json output file for events. |
| 50 | +
|
| 51 | + # See format at: |
| 52 | + # https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview?tab=t.0 |
| 53 | + """ |
| 54 | + df = self.to_dataframe() |
| 55 | + |
| 56 | + # Give an arbitrary id to each filename |
| 57 | + ids = {} |
| 58 | + count = 0 |
| 59 | + |
| 60 | + # We will thread the pids as the different runs of lammps, |
| 61 | + # and the thread ids as the library ids |
| 62 | + |
| 63 | + # TODO: this can be cleaned up and moved into own perfetto.py |
| 64 | + # I won't do this until I've added the close event and tested again |
| 65 | + with open(outfile, "w") as fd: |
| 66 | + fd.write("[") |
| 67 | + |
| 68 | + for pid, tag in enumerate(df.basename.unique()): |
| 69 | + subset = df[df.basename == tag] |
| 70 | + |
| 71 | + # Subtract the minimum timestamp for each run so we always start at 0 |
| 72 | + start_time = subset.timestamp.min() |
| 73 | + subset.loc[:, "timestamp"] = subset.timestamp - start_time |
| 74 | + |
| 75 | + for row in subset.iterrows(): |
| 76 | + # Get a faux process id |
| 77 | + if row[1].normalized_path not in ids: |
| 78 | + ids[row[1].normalized_path] = count |
| 79 | + count += 1 |
| 80 | + identifier = ids[row[1].normalized_path] |
| 81 | + if row[1].ms_in_state is not None: |
| 82 | + fd.write( |
| 83 | + json.dumps( |
| 84 | + { |
| 85 | + "name": row[1].normalized_path, |
| 86 | + "pid": pid, |
| 87 | + "tid": identifier, |
| 88 | + "ts": row[1].timestamp, |
| 89 | + "dur": row[1].ms_in_state, |
| 90 | + # Beginning of phase event |
| 91 | + "ph": "X", |
| 92 | + "cat": tag, |
| 93 | + "args": { |
| 94 | + "name": row[1].normalized_path, |
| 95 | + "path": row[1].path, |
| 96 | + "result": row[1].basename, |
| 97 | + "function": row[1].function, |
| 98 | + }, |
| 99 | + } |
| 100 | + ) |
| 101 | + ) |
| 102 | + else: |
| 103 | + fd.write( |
| 104 | + json.dumps( |
| 105 | + { |
| 106 | + "name": row[1].normalized_path, |
| 107 | + "pid": pid, |
| 108 | + "tid": identifier, |
| 109 | + "ts": row[1].timestamp, |
| 110 | + # Beginning of phase event |
| 111 | + "ph": "B", |
| 112 | + "cat": tag, |
| 113 | + "args": { |
| 114 | + "name": row[1].normalized_path, |
| 115 | + "path": row[1].path, |
| 116 | + "result": row[1].basename, |
| 117 | + "function": row[1].function, |
| 118 | + }, |
| 119 | + } |
| 120 | + ) |
| 121 | + ) |
| 122 | + fd.write("\n") |
| 123 | + fd.write("]") |
| 124 | + |
46 | 125 | def iter_events(self, operation="Open"):
|
47 | 126 | """
|
48 | 127 | Iterate through files and yield event object
|
|
0 commit comments