'Disable the profiler, and if necessary dump a truncated pstats output'

def disable_profiling(self):

    self._profiler.disable()
    s = StringIO()
    ps = pstats.Stats(self._profiler, stream=s).sort_stats('cumulative')
    ps.print_stats(self.PSTATS_LIMIT)
    log.debug(s.getvalue())
    log.debug('Agent    profiling    is    disabled')
    if self.DUMP_TO_FILE:
        try:
            ps.dump_stats(self.STATS_DUMP_FILE)
            log.debug('Pstats    dumps    are    enabled.    Dumping    pstats    output    to    {0}'.format(self.STATS_DUMP_FILE))
        except IOError:
            (f_handle, f_path) = tempfile.mkstemp(prefix='collector-stats-', suffix='.dmp')
            os.close(f_handle)
            log.debug('Dumping    pstats    output    to    {}    failed,    writing    to    {}    instead.'.format(self.STATS_DUMP_FILE, f_path))
            ps.dump_stats(f_path)
