Example #1
0
    def render_resource_usage(self):
        """
        Render our resource usage using jQuery+Sparklines in our WebKit view
        """
        global cpu_intervals, mem_intervals, cpu_details, mem_details
        global read_intervals, write_intervals, read_bytes, write_bytes
        global open_files, open_connections
        script = """
            jQuery('#cpu_graph').sparkline(%s, {'height': 75, 'width': 250,
                spotRadius: 3, fillColor: '#73d216', lineColor: '#4e9a06'});
            jQuery('#mem_graph').sparkline(%s, {'height': 75, 'width': 250,
                lineColor: '#5c3566', fillColor: '#75507b',
                minSpotColor: false, maxSpotColor: false, spotColor: '#f57900',
                spotRadius: 3});
            jQuery('#cpu_details').text('%s');
            jQuery('#mem_details').text('%s');
            jQuery('#read_graph').sparkline(%s, {'height': 75, 'width': 250,
                lineColor: '#a40000', fillColor: '#cc0000',
                minSpotColor: false, maxSpotColor: false, spotColor: '#729fcf',
                spotRadius: 3});
            jQuery('#write_graph').sparkline(%s, {'height': 75, 'width': 250,
                lineColor: '#ce5c00', fillColor: '#f57900',
                minSpotColor: false, maxSpotColor: false, spotColor: '#8ae234',
                spotRadius: 3});
            jQuery('#read_details').text('%s');
            jQuery('#write_details').text('%s');
        """ % (cpu_intervals, mem_intervals, cpu_details, mem_details,
               read_intervals, write_intervals, humanize_bytes(read_bytes),
               humanize_bytes(write_bytes))

        for i, thread in enumerate(thread_intervals):
            script += """
                jQuery('#thread_graph').sparkline(%s, {
                    %s'lineColor': '#%s', 'fillColor': false, 'spotRadius': 3,
                    'spotColor': '#%s'});
            """ % (thread_intervals[thread], i != 0 and "'composite': true,"
                   or "'height': 75, 'width': 575,", thread_colors[thread],
                   thread_colors[thread])

        if open_files:
            script += """
                jQuery('#open_files').html('%s');
            """ % ''.join(['<tr%s><td>%s</td></tr>' %
                           (i % 2 and ' class="alt"' or '', open_file)
                           for i, open_file in enumerate(open_files)])

        if open_connections:
            row = '<tr%s><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>'
            script += """
                jQuery('#open_connections').html('%s');
            """ % ''.join([row % (i % 2 and ' class="alt"' or '',
                                  conn['type'], conn['local'],
                                  conn['remote'], conn['status'])
                           for i, conn in enumerate(open_connections)])

        self.info_view.execute_script(script)
        return True
    def run(self):
        global cpu_intervals, mem_intervals, cpu_details, mem_details
        global read_count, read_bytes, write_count, write_bytes
        global read_intervals, write_intervals, thread_intervals
        global open_files, open_connections
        global process_status
        while True:
            try:
                if self.process:
                    if len(cpu_intervals) >= INTERVALS:
                        cpu_intervals = cpu_intervals[1:INTERVALS]
                        mem_intervals = mem_intervals[1:INTERVALS]
                        read_intervals = read_intervals[1:INTERVALS]
                        write_intervals = write_intervals[1:INTERVALS]

                    cpu_intervals.append(
                        self.process.get_cpu_percent(interval=POLL_INTERVAL))
                    mem_intervals.append(self.process.get_memory_info().rss)
                    cputimes = self.process.get_cpu_times()
                    cpu_details = '%0.2f%% (%s user, %s system)' % (
                            cpu_intervals[-1], cputimes.user, cputimes.system)
                    meminfo = self.process.get_memory_info()
                    mem_details = '%0.2f%% (%s RSS, %s VMS)' % (
                            self.process.get_memory_percent(),
                            humanize_bytes(meminfo.rss),
                            humanize_bytes(cputimes.system))

                    io = self.process.get_io_counters()
                    read_since_last = io.read_bytes - read_bytes
                    read_intervals.append(read_since_last)
                    read_count = io.read_count
                    read_bytes = io.read_bytes
                    write_since_last = io.write_bytes - write_bytes
                    write_intervals.append(write_since_last)
                    write_count = io.write_count
                    write_bytes = io.write_bytes

                    for thread in self.process.get_threads():
                        if thread.id not in thread_intervals:
                            thread_intervals[thread.id] = []
                            thread_colors[thread.id] = get_color()
                            thread_totals[thread.id] = 0.0

                        if len(thread_intervals[thread.id]) >= INTERVALS:
                            thread_intervals[thread.id] = \
                                    thread_intervals[thread.id][1:INTERVALS]

                        # FIXME: we should figure out some way to visually
                        # distinguish between user and system time.
                        total = thread.system_time + thread.user_time
                        amount_since = total - thread_totals[thread.id]
                        thread_intervals[thread.id].append(
                                float('%.2f' % amount_since))
                        thread_totals[thread.id] = total

                    # Open connections
                    connections = []
                    for i, conn in enumerate(self.process.get_connections()):
                        if conn.type == socket.SOCK_STREAM:
                            type = 'TCP'
                        elif conn.type == socket.SOCK_DGRAM:
                            type = 'UDP'
                        else:
                            type = 'UNIX'
                        lip, lport = conn.local_address
                        if not conn.remote_address:
                            rip = rport = '*'
                        else:
                            rip, rport = conn.remote_address
                        connections.append({
                            'type': type,
                            'status': conn.status,
                            'local': '%s:%s' % (lip, lport),
                            'remote': '%s:%s' % (rip, rport),
                            })
                    open_connections = connections

                    # Open files
                    files = []
                    for open_file in self.process.get_open_files():
                        files.append(open_file.path)
                    open_files = files

                else:
                    time.sleep(1)

            except psutil.NoSuchProcess:
                log.warn("Lost Process")
                self.process = None
                process_status = '[Terminated]'