The main class that keeps the data used by the stats viewer.
| 61 | |
| 62 | |
| 63 | class StatsViewer(object): |
| 64 | """The main class that keeps the data used by the stats viewer.""" |
| 65 | |
| 66 | def __init__(self, data_name, name_filter): |
| 67 | """Creates a new instance. |
| 68 | |
| 69 | Args: |
| 70 | data_name: the name of the file containing the counters. |
| 71 | name_filter: The regexp filter to apply to counter names. |
| 72 | """ |
| 73 | self.data_name = data_name |
| 74 | self.name_filter = name_filter |
| 75 | |
| 76 | # The handle created by mmap.mmap to the counters file. We need |
| 77 | # this to clean it up on exit. |
| 78 | self.shared_mmap = None |
| 79 | |
| 80 | # A mapping from counter names to the ui element that displays |
| 81 | # them |
| 82 | self.ui_counters = {} |
| 83 | |
| 84 | # The counter collection used to access the counters file |
| 85 | self.data = None |
| 86 | |
| 87 | # The Tkinter root window object |
| 88 | self.root = None |
| 89 | |
| 90 | def Run(self): |
| 91 | """The main entry-point to running the stats viewer.""" |
| 92 | try: |
| 93 | self.data = self.MountSharedData() |
| 94 | # OpenWindow blocks until the main window is closed |
| 95 | self.OpenWindow() |
| 96 | finally: |
| 97 | self.CleanUp() |
| 98 | |
| 99 | def MountSharedData(self): |
| 100 | """Mount the binary counters file as a memory-mapped file. If |
| 101 | something goes wrong print an informative message and exit the |
| 102 | program.""" |
| 103 | if not os.path.exists(self.data_name): |
| 104 | maps_name = "/proc/%s/maps" % self.data_name |
| 105 | if not os.path.exists(maps_name): |
| 106 | print("\"%s\" is neither a counter file nor a PID." % self.data_name) |
| 107 | sys.exit(1) |
| 108 | maps_file = open(maps_name, "r") |
| 109 | try: |
| 110 | self.data_name = None |
| 111 | for m in re.finditer(r"/dev/shm/\S*", maps_file.read()): |
| 112 | if os.path.exists(m.group(0)): |
| 113 | self.data_name = m.group(0) |
| 114 | break |
| 115 | if self.data_name is None: |
| 116 | print("Can't find counter file in maps for PID %s." % self.data_name) |
| 117 | sys.exit(1) |
| 118 | finally: |
| 119 | maps_file.close() |
| 120 | data_file = open(self.data_name, "r") |