-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from walles/johan/top-ram-programs
Graph top RAM using programs Adds a graphical visualization of which the top memory using programs are. They are grouped and summed by their name, so if you have multiple Firefox processes their RAM use will be summed into one entry for the purpose of this visualization. See updated screenshot for details.
- Loading branch information
Showing
9 changed files
with
136 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import sys | ||
import operator | ||
|
||
from px import px_terminal | ||
|
||
from . import px_process | ||
|
||
if sys.version_info.major >= 3: | ||
# For mypy PEP-484 static typing validation | ||
from typing import List # NOQA | ||
from typing import Dict # NOQA | ||
from typing import Tuple # NOQA | ||
from six import text_type # NOQA | ||
|
||
GROUPS_COUNT = 4 | ||
|
||
|
||
def get_categories(all_processes): | ||
# type: (List[px_process.PxProcess]) -> List[Tuple[text_type, int]] | ||
""" | ||
Group processes by pretty names, keeping track of the total rss_kb in each | ||
group. | ||
Return the top groups in order plus one "other" which is the sum of the | ||
rest. The total number of returned groups will be GROUPS_COUNT. | ||
""" | ||
|
||
names_to_kilobytes = {} # type: Dict[text_type, int] | ||
for process in all_processes: | ||
base_kb = 0 | ||
if process.command in names_to_kilobytes: | ||
base_kb = names_to_kilobytes[process.command] | ||
else: | ||
base_kb = 0 | ||
|
||
names_to_kilobytes[process.command] = base_kb + process.rss_kb | ||
|
||
sorted_names = sorted( | ||
names_to_kilobytes.items(), key=operator.itemgetter(1), reverse=True | ||
) | ||
other_total_kb = 0 | ||
return_me = [] # type: List[Tuple[text_type, int]] | ||
for i, name_and_kilobytes in enumerate(sorted_names): | ||
if i < GROUPS_COUNT - 1: | ||
return_me.append(name_and_kilobytes) | ||
else: | ||
other_total_kb += name_and_kilobytes[1] | ||
return_me.append(("...", other_total_kb)) | ||
|
||
return return_me | ||
|
||
|
||
def rambar(ram_bar_length, all_processes): | ||
# type: (int, List[px_process.PxProcess]) -> text_type | ||
|
||
categories = get_categories(all_processes) | ||
total_kilobytes = 0 | ||
for category in categories: | ||
total_kilobytes += category[1] | ||
|
||
bar = u"" | ||
for i, category in enumerate(categories): | ||
name = category[0] | ||
kilobytes = category[1] | ||
|
||
chars = int(round(ram_bar_length * kilobytes * 1.0 / total_kilobytes)) | ||
if i == len(categories) - 1: | ||
# Use all remaining chars | ||
chars = ram_bar_length - px_terminal.visual_length(bar) | ||
|
||
add_to_bar = px_terminal.get_string_of_length(" " + name, chars) | ||
if i == 0: | ||
# First red | ||
add_to_bar = px_terminal.red(add_to_bar) | ||
elif i == 1: | ||
# Second yellow | ||
add_to_bar = px_terminal.yellow(add_to_bar) | ||
elif i % 2 == 1: | ||
# Then alternating between normal and inverse video | ||
add_to_bar = px_terminal.inverse_video(add_to_bar) | ||
|
||
bar += add_to_bar | ||
|
||
return bar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters