Created
January 22, 2026 02:43
-
-
Save congwang-mk/d9d38725335eff0aedad0eb3df0cd48a to your computer and use it in GitHub Desktop.
Python tool to convert TSC to ms
This file contains hidden or 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
| #!/usr/bin/env python3 | |
| import sys | |
| import re | |
| def get_tsc_freq_mhz(): | |
| with open('/proc/cpuinfo', 'r') as f: | |
| for line in f: | |
| if line.startswith('cpu MHz'): | |
| match = re.search(r':\s*([\d.]+)', line) | |
| if match: | |
| return float(match.group(1)) | |
| raise RuntimeError("Could not find cpu MHz in /proc/cpuinfo") | |
| def main(): | |
| if len(sys.argv) != 3: | |
| print(f"Usage: {sys.argv[0]} <start_tsc> <end_tsc>") | |
| sys.exit(1) | |
| start_tsc = int(sys.argv[1]) | |
| end_tsc = int(sys.argv[2]) | |
| diff = end_tsc - start_tsc | |
| freq_mhz = get_tsc_freq_mhz() | |
| freq_khz = freq_mhz * 1000 | |
| elapsed_ms = diff / freq_khz | |
| elapsed_us = diff * 1000 / freq_khz | |
| print(f"TSC start: {start_tsc}") | |
| print(f"TSC end: {end_tsc}") | |
| print(f"TSC diff: {diff}") | |
| print(f"CPU freq: {freq_mhz:.3f} MHz") | |
| print(f"Elapsed: {elapsed_ms:.3f} ms ({elapsed_us:.1f} us)") | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment