Example #1
0
def enableProfiler(signum, config):
    """
    Enables the profiler on specified signal
    """
    try:
        # If the profiler is available on the system then enable it
        import gevent_profiler
        
        if config.has_key('summaryOutput'):
            gevent_profiler.set_summary_output(config['summaryOutput'])

        if config.has_key('statsOutput'):
            gevent_profiler.set_stats_output(config['statsOutput'])

        if config.has_key('traceOutput'):
            gevent_profiler.set_trace_output(config['traceOutput'])

        if config.has_key('printPercentage'):
            gevent_profiler.print_percentages(config['printPercentage'])

        if config.has_key('countTimeBlocking'):
            gevent_profiler.time_blocking(config['countTimeBlocking'])

        gevent_profiler.attach_on_signal(signum=signum, duration=config['duration'])
        logging.getLogger().info("The profiler is enabled and waiting on signal %s." % signum)

    except Exception as ex:
        logging.getLogger().warn("Failed enabling the profiler: %s" % ex)
Example #2
0
    def begin(self):
        ts = time.time()

        gevent_profiler.set_stats_output("nose-gevent-profiler-%s-stats.txt" %
                                         ts)
        gevent_profiler.set_summary_output(
            "nose-gevent-profiler-%s-summary.txt" % ts)
        gevent_profiler.set_trace_output(None)
Example #3
0
    def GET(self):
        web.header('Content-Type', 'text/html')
        params = web.input(_sheep_app_server=None, _sheep_profile_count=None, _sheep_pid=None)
        if not web.ctx.env.get('QUERY_STRING', None):
            return FORM_HTML % socket.gethostname()

        count = params._sheep_profile_count
        backend = params._sheep_app_server
        pid = params._sheep_pid
        if not (count and backend):
            return '''params invaild'''
        count = int(count)
        if not pid:
            pid = os.getpid()
            raise web.seeother("/_sheep/profile/?_sheep_profile_count=%d&_sheep_app_server=%s&_sheep_pid=%d" % \
                    (count, backend, pid))
        else:
            pid = int(pid)

        html = FRESH_HTML
        num_file = os.path.join('/tmp','%s.%d.counter' % (web.ctx.env['SERVER_NAME'], pid))
        stat_file = os.path.join('/tmp', '%s.%d.prof' % (web.ctx.env['SERVER_NAME'], pid))
        now = os.path.getsize(num_file) if os.path.exists(num_file) else 0
        html = html % (now, count, count, backend.encode('utf8'), pid)

        if not getattr(SHEEPApplication, 'old_call', None):
            setattr(SHEEPApplication, 'old_call', SHEEPApplication.__call__)

        if not os.path.exists(num_file) and os.path.exists(stat_file):
            return output(stat_file)
        elif not os.path.exists(num_file):
            with open(num_file, 'w+') as f:
                f.write('')

            gevent_profiler.set_stats_output(stat_file)
            gevent_profiler.set_trace_output(None)

            def monkey_call(obj, environ, start_response):
                if os.path.exists(num_file):
                    with open(num_file, 'a') as f:
                        f.write('1')
                return obj.old_call(environ, start_response)
            SHEEPApplication.__call__ = monkey_call
            gevent_profiler.attach()
        elif os.path.getsize(num_file) >= count:
            os.remove(num_file)
            try:
                gevent_profiler.detach()
            except:
                pass
            SHEEPApplication.__call__ = SHEEPApplication.old_call
            return output(stat_file)
        return html
Example #4
0
def start_profiler(prefix, pcts=False):
    """
    Start the gevent-profiler, outputting to the passed prefix-stats and prefix-summary.txt.

    You must call stop_profiler at a later point. The profiler is not safe to run twice at once
    as it keeps global state internally.

    @param  prefix      The prefix to use when naming the output files.
    @param  pcts        Whether the profiler should add percentages as well.
    """
    gevent_profiler.set_stats_output('%s-stats.txt' % prefix)
    gevent_profiler.set_summary_output('%s-summary.txt' % prefix)
    gevent_profiler.print_percentages(pcts)

    gevent_profiler.set_trace_output(None)  #'%s-trace.txt' % prefix)   @TODO make optional
    gevent_profiler.attach()
Example #5
0
    def __init__(self, datadir: str) -> None:
        # create a new file every time instead of overwritting the latest profiling
        summary_file = "{:%Y%m%d_%H%M}_profile_summary".format(datetime.now())
        stats_file = "{:%Y%m%d_%H%M}_profile_stats".format(datetime.now())

        summary_path = os.path.join(datadir, summary_file)
        stats_path = os.path.join(datadir, stats_file)

        gevent_profiler.set_trace_output(None)
        gevent_profiler.set_summary_output(summary_path)
        gevent_profiler.set_stats_output(stats_path)

        GreenletProfiler.set_clock_type("cpu")
        GreenletProfiler.start()
        # gevent_profiler.attach()

        self.datadir = datadir
Example #6
0
def start_profiler(prefix, pcts=False):
    """
    Start the gevent-profiler, outputting to the passed prefix-stats and prefix-summary.txt.

    You must call stop_profiler at a later point. The profiler is not safe to run twice at once
    as it keeps global state internally.

    @param  prefix      The prefix to use when naming the output files.
    @param  pcts        Whether the profiler should add percentages as well.
    """
    gevent_profiler.set_stats_output('%s-stats.txt' % prefix)
    gevent_profiler.set_summary_output('%s-summary.txt' % prefix)
    gevent_profiler.print_percentages(pcts)

    gevent_profiler.set_trace_output(
        None)  #'%s-trace.txt' % prefix)   @TODO make optional
    gevent_profiler.attach()
Example #7
0
    def start(self):
        if self.profiling:
            return

        # create a new file every time instead of overwritting the latest profiling
        summary_file = '{:%Y%m%d_%H%M}_profile_summary'.format(datetime.now())
        stats_file = '{:%Y%m%d_%H%M}_profile_stats'.format(datetime.now())

        summary_path = os.path.join(self.datadir, summary_file)
        stats_path = os.path.join(self.datadir, stats_file)

        gevent_profiler.set_trace_output(None)
        gevent_profiler.set_summary_output(summary_path)
        gevent_profiler.set_stats_output(stats_path)

        GreenletProfiler.set_clock_type('cpu')
        GreenletProfiler.start()
        # gevent_profiler.attach()

        self.profiling = True
Example #8
0
    def start(self):
        if self.profiling:
            return

        # create a new file every time instead of overwritting the latest profiling
        summary_file = '{:%Y%m%d_%H%M}_profile_summary'.format(datetime.now())
        stats_file = '{:%Y%m%d_%H%M}_profile_stats'.format(datetime.now())

        summary_path = os.path.join(self.datadir, summary_file)
        stats_path = os.path.join(self.datadir, stats_file)

        gevent_profiler.set_trace_output(None)
        gevent_profiler.set_summary_output(summary_path)
        gevent_profiler.set_stats_output(stats_path)

        GreenletProfiler.set_clock_type('cpu')
        GreenletProfiler.start()
        # gevent_profiler.attach()

        self.profiling = True
Example #9
0
File: main.py Project: fcua/x8623
    from session.regions import run_region_reloader
    gevent.spawn(run_region_reloader)


try:
    from uwsgidecorators import postfork
except ImportError:
    import gevent.monkey
    gevent.monkey.patch_all()
    init()
else:
    init = postfork(init)

from .app import application

if __name__ == '__main__':
    import settings
    import gevent_profiler
    import signal
    from gevent.pywsgi import WSGIServer

    gevent_profiler.attach_on_signal(signum=signal.SIGUSR1, duration=30)
    gevent_profiler.set_stats_output('stats.txt')
    gevent_profiler.set_summary_output('summary.txt')
    gevent_profiler.set_trace_output('trace.txt')

    logger.info('listening %s:%d', settings.SESSION['host'],
                settings.SESSION['port'])
    WSGIServer((settings.SESSION['host'], settings.SESSION['port']),
               application).serve_forever()
Example #10
0
 def __enter__(self):
     gevent_profiler.set_stats_output(self.stats)
     gevent_profiler.set_summary_output(self.summary)
     gevent_profiler.set_trace_output(None)
     gevent_profiler.attach()
Example #11
0
    def begin(self):
        ts = time.time()

        gevent_profiler.set_stats_output("nose-gevent-profiler-%s-stats.txt" % ts)
        gevent_profiler.set_summary_output("nose-gevent-profiler-%s-summary.txt" % ts)
        gevent_profiler.set_trace_output(None)