3.1-rc3 (revision d9ca08bb)
otf2_high_level_writer_example.py

Python high-level writing example

1 #!/usr/bin/env python
2 #
3 # This file is part of the Score-P software (http://www.score-p.org)
4 #
5 # Copyright (c) 2015-2016, 2021,
6 # Technische Universitaet Dresden, Germany
7 #
8 # This software may be modified and distributed under the terms of
9 # a BSD-style license. See the COPYING file in the package base
10 # directory for details.
11 #
12 
13 import otf2
14 from otf2.enums import Type
15 import time
16 
17 
18 TIMER_GRANULARITY = 1000000
19 
20 
21 def t():
22  return int(round(time.time() * TIMER_GRANULARITY))
23 
24 
25 with otf2.writer.open("TestArchive", timer_resolution=TIMER_GRANULARITY) as trace:
26 
27  function = trace.definitions.region("My Function")
28 
29  parent_node = trace.definitions.system_tree_node("node")
30  system_tree_node = trace.definitions.system_tree_node("myHost", parent=parent_node)
31 
32  trace.definitions.system_tree_node_property(system_tree_node, "color", value="black")
33  trace.definitions.system_tree_node_property(system_tree_node, "rack #", value=42)
34 
35  location_group = trace.definitions.location_group("Initial Process",
36  system_tree_parent=system_tree_node)
37 
38  attr = trace.definitions.attribute("StringTest", "A test attribute", Type.STRING)
39  float_attr = trace.definitions.attribute("FloatTest", "Another test attribute",
40  Type.DOUBLE)
41 
42  writer = trace.event_writer("Main Thread", group=location_group)
43 
44  # Write enter and leave event
45  writer.enter(t(), function, {attr: "Hello World"})
46  writer.leave(t(), function, attributes={float_attr: 42.0, attr: "Wurst?"})
47 
48  # Get convenience metric object and write one metric event
49  temperature = trace.definitions.metric("Time since last coffee", unit="min")
50  writer.metric(t(), temperature, 72.0)
51 
52  # Get metric members
53  temp_member = trace.definitions.metric_member("Temperature", "C", otf2.MetricType.OTHER,
54  otf2.MetricMode.ABSOLUTE_POINT)
55  power_member = trace.definitions.metric_member("Power", "W")
56  # Add metric members to the metric class object
57  mclass = trace.definitions.metric_class([temp_member, power_member])
58  # Add metric object to the location object
59  writer.metric(t(), mclass, [42.0, 12345.6])