OLD | NEW |
| 1 #!/usr/bin/env python |
| 2 # |
1 # Copyright 2008 the V8 project authors. All rights reserved. | 3 # Copyright 2008 the V8 project authors. All rights reserved. |
2 # Redistribution and use in source and binary forms, with or without | 4 # Redistribution and use in source and binary forms, with or without |
3 # modification, are permitted provided that the following conditions are | 5 # modification, are permitted provided that the following conditions are |
4 # met: | 6 # met: |
5 # | 7 # |
6 # * Redistributions of source code must retain the above copyright | 8 # * Redistributions of source code must retain the above copyright |
7 # notice, this list of conditions and the following disclaimer. | 9 # notice, this list of conditions and the following disclaimer. |
8 # * Redistributions in binary form must reproduce the above | 10 # * Redistributions in binary form must reproduce the above |
9 # copyright notice, this list of conditions and the following | 11 # copyright notice, this list of conditions and the following |
10 # disclaimer in the documentation and/or other materials provided | 12 # disclaimer in the documentation and/or other materials provided |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 | 44 |
43 | 45 |
44 # The interval, in milliseconds, between ui updates | 46 # The interval, in milliseconds, between ui updates |
45 UPDATE_INTERVAL_MS = 100 | 47 UPDATE_INTERVAL_MS = 100 |
46 | 48 |
47 | 49 |
48 # Mapping from counter prefix to the formatting to be used for the counter | 50 # Mapping from counter prefix to the formatting to be used for the counter |
49 COUNTER_LABELS = {"t": "%i ms.", "c": "%i"} | 51 COUNTER_LABELS = {"t": "%i ms.", "c": "%i"} |
50 | 52 |
51 | 53 |
52 # The magic number used to check if a file is not a counters file | 54 # The magic numbers used to check if a file is not a counters file |
53 COUNTERS_FILE_MAGIC_NUMBER = 0xDEADFACE | 55 COUNTERS_FILE_MAGIC_NUMBER = 0xDEADFACE |
| 56 CHROME_COUNTERS_FILE_MAGIC_NUMBER = 0x13131313 |
54 | 57 |
55 | 58 |
56 class StatsViewer(object): | 59 class StatsViewer(object): |
57 """The main class that keeps the data used by the stats viewer.""" | 60 """The main class that keeps the data used by the stats viewer.""" |
58 | 61 |
59 def __init__(self, data_name): | 62 def __init__(self, data_name): |
60 """Creates a new instance. | 63 """Creates a new instance. |
61 | 64 |
62 Args: | 65 Args: |
63 data_name: the name of the file containing the counters. | 66 data_name: the name of the file containing the counters. |
(...skipping 28 matching lines...) Expand all Loading... |
92 something goes wrong print an informative message and exit the | 95 something goes wrong print an informative message and exit the |
93 program.""" | 96 program.""" |
94 if not os.path.exists(self.data_name): | 97 if not os.path.exists(self.data_name): |
95 print "File %s doesn't exist." % self.data_name | 98 print "File %s doesn't exist." % self.data_name |
96 sys.exit(1) | 99 sys.exit(1) |
97 data_file = open(self.data_name, "r") | 100 data_file = open(self.data_name, "r") |
98 size = os.fstat(data_file.fileno()).st_size | 101 size = os.fstat(data_file.fileno()).st_size |
99 fileno = data_file.fileno() | 102 fileno = data_file.fileno() |
100 self.shared_mmap = mmap.mmap(fileno, size, access=mmap.ACCESS_READ) | 103 self.shared_mmap = mmap.mmap(fileno, size, access=mmap.ACCESS_READ) |
101 data_access = SharedDataAccess(self.shared_mmap) | 104 data_access = SharedDataAccess(self.shared_mmap) |
102 if data_access.IntAt(0) != COUNTERS_FILE_MAGIC_NUMBER: | 105 if data_access.IntAt(0) == COUNTERS_FILE_MAGIC_NUMBER: |
103 print "File %s is not stats data." % self.data_name | 106 return CounterCollection(data_access) |
104 sys.exit(1) | 107 elif data_access.IntAt(0) == CHROME_COUNTERS_FILE_MAGIC_NUMBER: |
105 return CounterCollection(data_access) | 108 return ChromeCounterCollection(data_access) |
| 109 print "File %s is not stats data." % self.data_name |
| 110 sys.exit(1) |
106 | 111 |
107 def CleanUp(self): | 112 def CleanUp(self): |
108 """Cleans up the memory mapped file if necessary.""" | 113 """Cleans up the memory mapped file if necessary.""" |
109 if self.shared_mmap: | 114 if self.shared_mmap: |
110 self.shared_mmap.close() | 115 self.shared_mmap.close() |
111 | 116 |
112 def UpdateCounters(self): | 117 def UpdateCounters(self): |
113 """Read the contents of the memory-mapped file and update the ui if | 118 """Read the contents of the memory-mapped file and update the ui if |
114 necessary. If the same counters are present in the file as before | 119 necessary. If the same counters are present in the file as before |
115 we just update the existing labels. If any counters have been added | 120 we just update the existing labels. If any counters have been added |
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 | 354 |
350 def Counter(self, index): | 355 def Counter(self, index): |
351 """Return the index'th counter.""" | 356 """Return the index'th counter.""" |
352 return Counter(self.data, 16 + index * self.CounterSize()) | 357 return Counter(self.data, 16 + index * self.CounterSize()) |
353 | 358 |
354 def CounterSize(self): | 359 def CounterSize(self): |
355 """Return the size of a single counter.""" | 360 """Return the size of a single counter.""" |
356 return 4 + self.max_name_size | 361 return 4 + self.max_name_size |
357 | 362 |
358 | 363 |
| 364 class ChromeCounter(object): |
| 365 """A pointer to a single counter withing a binary counters file.""" |
| 366 |
| 367 def __init__(self, data, name_offset, value_offset): |
| 368 """Create a new instance. |
| 369 |
| 370 Args: |
| 371 data: the shared data access object containing the counter |
| 372 name_offset: the byte offset of the start of this counter's name |
| 373 value_offset: the byte offset of the start of this counter's value |
| 374 """ |
| 375 self.data = data |
| 376 self.name_offset = name_offset |
| 377 self.value_offset = value_offset |
| 378 |
| 379 def Value(self): |
| 380 """Return the integer value of this counter.""" |
| 381 return self.data.IntAt(self.value_offset) |
| 382 |
| 383 def Name(self): |
| 384 """Return the ascii name of this counter.""" |
| 385 result = "" |
| 386 index = self.name_offset |
| 387 current = self.data.ByteAt(index) |
| 388 while current: |
| 389 result += chr(current) |
| 390 index += 1 |
| 391 current = self.data.ByteAt(index) |
| 392 return result |
| 393 |
| 394 |
| 395 class ChromeCounterCollection(object): |
| 396 """An overlay over a counters file that provides access to the |
| 397 individual counters contained in the file.""" |
| 398 |
| 399 _HEADER_SIZE = 4 * 4 |
| 400 _NAME_SIZE = 32 |
| 401 |
| 402 def __init__(self, data): |
| 403 """Create a new instance. |
| 404 |
| 405 Args: |
| 406 data: the shared data access object |
| 407 """ |
| 408 self.data = data |
| 409 self.max_counters = data.IntAt(8) |
| 410 self.max_threads = data.IntAt(12) |
| 411 self.counter_names_offset = \ |
| 412 self._HEADER_SIZE + self.max_threads * (self._NAME_SIZE + 2 * 4) |
| 413 self.counter_values_offset = \ |
| 414 self.counter_names_offset + self.max_counters * self._NAME_SIZE |
| 415 |
| 416 def CountersInUse(self): |
| 417 """Return the number of counters in active use.""" |
| 418 for i in xrange(self.max_counters): |
| 419 if self.data.ByteAt(self.counter_names_offset + i * self._NAME_SIZE) == 0: |
| 420 return i |
| 421 return self.max_counters |
| 422 |
| 423 def Counter(self, i): |
| 424 """Return the i'th counter.""" |
| 425 return ChromeCounter(self.data, |
| 426 self.counter_names_offset + i * self._NAME_SIZE, |
| 427 self.counter_values_offset + i * self.max_threads * 4) |
| 428 |
| 429 |
359 def Main(data_file): | 430 def Main(data_file): |
360 """Run the stats counter. | 431 """Run the stats counter. |
361 | 432 |
362 Args: | 433 Args: |
363 data_file: The counters file to monitor. | 434 data_file: The counters file to monitor. |
364 """ | 435 """ |
365 StatsViewer(data_file).Run() | 436 StatsViewer(data_file).Run() |
366 | 437 |
367 | 438 |
368 if __name__ == "__main__": | 439 if __name__ == "__main__": |
369 if len(sys.argv) != 2: | 440 if len(sys.argv) != 2: |
370 print "Usage: stats-viewer.py <stats data>" | 441 print "Usage: stats-viewer.py <stats data>" |
371 sys.exit(1) | 442 sys.exit(1) |
372 Main(sys.argv[1]) | 443 Main(sys.argv[1]) |
OLD | NEW |