code: def monitoring_helper(
      service_addr, duration_ms, monitoring_level, num_queries
  ):
      """Helper function to print monitoring results.
  
      Helper function to print monitoring results for num_queries times.
  
      Args:
        service_addr: Address of the TPU profiler service.
        duration_ms: Duration of one monitoring sample in milliseconds.
        monitoring_level: An integer between 1 and 2. Level 2 is more verbose than
          level 1 and shows more metrics.
        num_queries: Number of monitoring samples to collect.
      """
      if monitoring_level <= 0 or monitoring_level > 2:
          sys.exit("Please choose a monitoring level between 1 and 2.")
  
      for query in range(0, num_queries):
          res = profiler_client.monitor(
              service_addr, duration_ms, monitoring_level
          )
          print(
              "Cloud TPU Monitoring Results (Sample ",
              query,
              "):\n\n",
              res,
          )
  
file_path: tensorflow-upstream/tensorflow/python/tpu/profiler/capture_tpu_profile.py
imports: import os
  import sys
  from absl import app
  from absl import flags
  from distutils.version import LooseVersion
  from tensorflow.python.distribute.cluster_resolver import tpu_cluster_resolver
  from tensorflow.python.profiler import profiler_client
  from tensorflow.python.profiler import profiler_v2
  from tensorflow.python.framework import errors
  from tensorflow.python.framework import versions
  from tensorflow.python.platform import gfile
  from tensorflow.python.platform import tf_logging
  from tensorflow.python.tpu.profiler import version
label_desc: Use image node_id 2 for calling a global function with example usage: monitoring_helper(service_addr, duration_ms, monitoring_level, num_queries) without return types