Creates profiles in pprof format.
| 256 | |
| 257 | |
| 258 | class PprofProfiler(object): |
| 259 | """Creates profiles in pprof format.""" |
| 260 | |
| 261 | def __init__(self, graph, run_metadata): |
| 262 | """Constructor. |
| 263 | |
| 264 | Args: |
| 265 | graph: A `Graph` instance. |
| 266 | run_metadata: A list of `RunMetadata` objects. |
| 267 | """ |
| 268 | self._graph = graph |
| 269 | self._run_metadata = run_metadata |
| 270 | self._string_table = StringTable() |
| 271 | self._functions = Functions(self._string_table) |
| 272 | self._locations = Locations(self._functions) |
| 273 | |
| 274 | def profile(self): |
| 275 | """Generates pprof profiles. |
| 276 | |
| 277 | Returns: |
| 278 | Dictionary mapping from device name to proto in `profile_pb2.Profile` |
| 279 | format. |
| 280 | """ |
| 281 | profiles = {} |
| 282 | data_generator_func = self._get_profile_data_generator() |
| 283 | for device_index, device_stats in enumerate( |
| 284 | self._run_metadata.step_stats.dev_stats): |
| 285 | # Create profile |
| 286 | pprof_proto = self._get_pprof_proto(data_generator_func(device_stats)) |
| 287 | if not pprof_proto.sample: |
| 288 | print( |
| 289 | 'Not enough data to create profile for device %s. Did you pass ' |
| 290 | 'RunMetadata to session.run call?' % device_stats.device) |
| 291 | continue |
| 292 | # Add device name comment |
| 293 | device_count = len(self._run_metadata.step_stats.dev_stats) |
| 294 | device_description = ( |
| 295 | 'Device %d of %d: %s' % |
| 296 | (device_index + 1, device_count, device_stats.device)) |
| 297 | device_description_str_index = self._string_table.next_index() |
| 298 | pprof_proto.string_table.append(device_description) |
| 299 | pprof_proto.comment.append(device_description_str_index) |
| 300 | profiles[device_stats.device] = pprof_proto |
| 301 | return profiles |
| 302 | |
| 303 | def _get_pprof_proto(self, profile_datum_generator): |
| 304 | """Returns profile data in pprof proto format. |
| 305 | |
| 306 | Args: |
| 307 | profile_datum_generator: Generator outputting `ProfileDatum` objects. |
| 308 | |
| 309 | Returns: |
| 310 | A proto in pprof format. |
| 311 | """ |
| 312 | pprof_profile = profile_pb2.Profile() |
| 313 | samples = Samples(self._string_table) |
| 314 | |
| 315 | for datum in profile_datum_generator: |