micromegas 0.4.0__py3-none-any.whl → 0.6.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
micromegas/perfetto.py CHANGED
@@ -1,4 +1,5 @@
1
1
  import crc
2
+ import pyarrow
2
3
  from tqdm import tqdm
3
4
 
4
5
 
@@ -17,29 +18,13 @@ def crc64_str(s):
17
18
  return calculator.checksum(str.encode(s))
18
19
 
19
20
 
20
- def generate_batches(df_blocks):
21
- nb_events_threshold = 1024 * 1024
22
- begin = df_blocks.iloc[0]["begin_time"]
23
- end = df_blocks.iloc[0]["end_time"]
24
- nb_events = 0
25
- for index, block in df_blocks.iterrows():
26
- nb_events += block["nb_objects"]
27
- end = block["end_time"]
28
- if nb_events > nb_events_threshold:
29
- yield (begin, end, nb_events)
30
- begin = block["end_time"]
31
- nb_events = 0
32
- if nb_events > 0:
33
- yield (begin, end, nb_events)
34
-
35
-
36
21
  class Writer:
37
22
  """
38
23
  Fetches thread events from the analytics server and formats them in the perfetto format.
39
24
  Traces can be viewed using https://ui.perfetto.dev/
40
25
  """
41
26
 
42
- def __init__(self, client, process_id, exe):
27
+ def __init__(self, client, process_id, begin, end, exe):
43
28
  load_perfetto_protos()
44
29
  from protos.perfetto.trace import trace_pb2, trace_packet_pb2
45
30
 
@@ -51,6 +36,8 @@ class Writer:
51
36
  self.trace = trace_pb2.Trace()
52
37
  self.packets = self.trace.packet
53
38
  self.process_uuid = crc64_str(process_id)
39
+ self.begin = begin
40
+ self.end = end
54
41
 
55
42
  packet = trace_packet_pb2.TracePacket()
56
43
  packet.track_descriptor.uuid = self.process_uuid
@@ -84,16 +71,10 @@ class Writer:
84
71
  iid = len(self.source_locations) + 1
85
72
  self.source_locations[loc] = iid
86
73
  return iid, is_new
87
-
74
+
88
75
  def append_thread(self, stream_id, thread_name, thread_id):
89
76
  from protos.perfetto.trace import trace_pb2, trace_packet_pb2, track_event
90
77
 
91
- df_blocks = self.client.query_blocks(
92
- begin=None, end=None, limit=100_000, stream_id=stream_id
93
- )
94
- if df_blocks.empty:
95
- return
96
-
97
78
  packet = trace_packet_pb2.TracePacket()
98
79
  thread_uuid = crc64_str(stream_id)
99
80
  packet.track_descriptor.uuid = thread_uuid
@@ -104,11 +85,15 @@ class Writer:
104
85
  self.packets.append(packet)
105
86
  trusted_packet_sequence_id = 1
106
87
 
107
- batches = list(generate_batches(df_blocks))
108
- for begin, end, limit in tqdm(batches, unit="event batches"):
109
- df_spans = self.client.query_spans(
110
- begin, end, limit=limit, stream_id=stream_id
111
- )
88
+ sql = """
89
+ SELECT *
90
+ FROM view_instance('thread_spans', '{stream_id}');
91
+ """.format(
92
+ stream_id=stream_id
93
+ )
94
+
95
+ for rb_spans in self.client.query_stream(sql, self.begin, self.end):
96
+ df_spans = pyarrow.Table.from_batches([rb_spans]).to_pandas()
112
97
  begin_ns = df_spans["begin"].astype("int64")
113
98
  end_ns = df_spans["end"].astype("int64")
114
99
  for index, span in df_spans.iterrows():
@@ -125,7 +110,9 @@ class Writer:
125
110
  packet.track_event.category_iids.append(category_iid)
126
111
 
127
112
  source_location = (span["filename"], span["line"])
128
- source_location_iid, new_source_location = self.get_location_iid(source_location)
113
+ source_location_iid, new_source_location = self.get_location_iid(
114
+ source_location
115
+ )
129
116
  packet.track_event.source_location_iid = source_location_iid
130
117
  if self.first:
131
118
  # this is necessary for interning to work
@@ -171,35 +158,41 @@ class Writer:
171
158
  f.write(self.trace.SerializeToString())
172
159
 
173
160
 
174
- def get_process_cpu_streams(client, process_id):
175
- def prop_to_dict(props):
176
- prop_dict = {}
177
- for p in props:
178
- prop_dict[p["key"]] = p["value"]
179
- return prop_dict
180
-
181
- def get_thread_name(prop_dict):
182
- return prop_dict["thread-name"]
183
-
184
- def get_thread_id(prop_dict):
185
- return int(prop_dict["thread-id"])
186
-
187
- df_streams = client.query_streams(
188
- begin=None, end=None, limit=1024, tag_filter="cpu", process_id=process_id
161
+ def get_process_cpu_streams(client, process_id, begin, end):
162
+ sql = """
163
+ SELECT stream_id,
164
+ property_get("streams.properties", 'thread-name') as thread_name,
165
+ property_get("streams.properties", 'thread-id') as thread_id
166
+ FROM blocks
167
+ WHERE process_id = '{process_id}'
168
+ AND array_has("streams.tags", 'cpu')
169
+ GROUP BY stream_id, thread_name, thread_id
170
+ """.format(
171
+ process_id=process_id
189
172
  )
190
- df_streams["properties"] = df_streams["properties"].apply(prop_to_dict)
191
- df_streams["thread_name"] = df_streams["properties"].apply(get_thread_name)
192
- df_streams["thread_id"] = df_streams["properties"].apply(get_thread_id)
173
+ df_streams = client.query(sql)
193
174
  return df_streams
194
175
 
195
176
 
196
- def write_process_trace(client, process_id, trace_filepath):
197
- process_df = client.find_process(process_id)
198
- assert process_df.shape[0] == 1
199
- process = process_df.iloc[0]
200
- streams = get_process_cpu_streams(client, process_id)
201
- writer = Writer(client, process_id, process["exe"])
202
- for index, stream in tqdm(list(streams.iterrows()), unit="threads"):
203
- stream_id = stream["thread_id"]
177
+ def get_exe(client, process_id, begin, end):
178
+ sql = """
179
+ SELECT "processes.exe" as exe
180
+ FROM blocks
181
+ WHERE process_id='{process_id}'
182
+ LIMIT 1;""".format(
183
+ process_id=process_id
184
+ )
185
+ return client.query(sql, begin, end).iloc[0]["exe"]
186
+
187
+
188
+ def write_process_trace(client, process_id, begin, end, trace_filepath):
189
+ exe = get_exe(client, process_id, begin, end)
190
+ print(exe)
191
+ streams = get_process_cpu_streams(client, process_id, begin, end)
192
+ writer = Writer(client, process_id, begin, end, exe)
193
+ progress_bar = tqdm(list(streams.iterrows()), unit="threads")
194
+ for index, stream in progress_bar:
195
+ progress_bar.set_description(stream["thread_name"])
196
+ stream_id = int(stream["thread_id"])
204
197
  writer.append_thread(stream["stream_id"], stream["thread_name"], stream_id)
205
198
  writer.write_file(trace_filepath)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: micromegas
3
- Version: 0.4.0
3
+ Version: 0.6.0
4
4
  Summary: Python analytics client for https://github.com/madesroches/micromegas/
5
5
  Author: Marc-Antoine Desroches
6
6
  Author-email: madesroches@gmail.com
@@ -8,7 +8,7 @@ micromegas/flightsql/arrow_flatbuffers.py,sha256=egpmS59sNFwWmtG2wMeE92MMIyGyZbs
8
8
  micromegas/flightsql/arrow_ipc_reader.py,sha256=3rxyEgqo5100e0TT9ZKZxNe7lX6Lk0mS6yRoiIJtH6Q,3163
9
9
  micromegas/flightsql/client.py,sha256=v4igU4nHZdaqBAw5qDtDNl6UBYiLvVnCJk1qDeO8LjE,11241
10
10
  micromegas/flightsql/time.py,sha256=EH3SUEpFvY0lNMj9mOcvfUJuSgrQ3YX4aJnwteK2qhk,582
11
- micromegas/perfetto.py,sha256=yuIe5iKvca61aWMBQNziSGM-DHcOEsiobtKx2SsNQ3E,7829
11
+ micromegas/perfetto.py,sha256=mXtkQMZ5y9LhyanKWjf3BwehEW_vVR_MtTPrUgsehZ4,7449
12
12
  micromegas/thirdparty/perfetto/protos/perfetto/common/android_energy_consumer_descriptor_pb2.py,sha256=l8QNXqnB-mJIkuFr2s1YoLQXHm3G-ZcOGp_OW_hQ0TE,1887
13
13
  micromegas/thirdparty/perfetto/protos/perfetto/common/android_log_constants_pb2.py,sha256=O5zDZkV8Nji0O2ryJRP4FTWdgdOBlDymWNcpNNDOFxk,2017
14
14
  micromegas/thirdparty/perfetto/protos/perfetto/common/builtin_clock_pb2.py,sha256=7qLL_BENTxRFQH8DfHDvyWAkgwy0VHrOaE8XhL8iZgk,1822
@@ -215,6 +215,6 @@ micromegas/thirdparty/perfetto/protos/perfetto/trace/translation/translation_tab
215
215
  micromegas/thirdparty/perfetto/protos/perfetto/trace/trigger_pb2.py,sha256=We7Yi8o3cEcrSNxY1zLUUO6tEWnD36C2f3O_s8_qv0I,1435
216
216
  micromegas/thirdparty/perfetto/protos/perfetto/trace/ui_state_pb2.py,sha256=Af-SXwhroNhRXMrtw6e2eU1liCImMRxSdmkt_AuSHf8,1752
217
217
  micromegas/time.py,sha256=eD9fWF2UHxaf-92yd1X2SEgUcpKypqPsvjBosLdpnQA,1026
218
- micromegas-0.4.0.dist-info/METADATA,sha256=XCqcxBrAlumvF1iUfQtuZdq7y9yE-enxWdKqDCAp_jA,30640
219
- micromegas-0.4.0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
220
- micromegas-0.4.0.dist-info/RECORD,,
218
+ micromegas-0.6.0.dist-info/METADATA,sha256=8M_DiFWlliOn25fdvjkbw6CZPk99zM3-pxXZ9LjRbz0,30640
219
+ micromegas-0.6.0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
220
+ micromegas-0.6.0.dist-info/RECORD,,