Skip to content

Instantly share code, notes, and snippets.

@hashbrowncipher
Last active September 22, 2015 02:39
Show Gist options
  • Select an option

  • Save hashbrowncipher/8696d4095468adca32cc to your computer and use it in GitHub Desktop.

Select an option

Save hashbrowncipher/8696d4095468adca32cc to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from __future__ import print_function
import argparse
import math
import sys
import time
def schedstat_loop(interval):
# scale time spent in the scheduler by this factor
scale = 1024
multiplier = scale / (1e9 * interval)
prev_delays = []
headings = ['Min', 'Max', 'MaxCPU', 'Mean', 'StdDev']
loops = 0
print(' '.join(map('{0: >16}'.format, headings)))
while True:
schedstat_lines = open('/proc/schedstat').readlines()
delays = [
int(i.split(' ')[8]) for i
in schedstat_lines if i.startswith('cpu')
]
# support CPU offlining
if len(delays) != len(prev_delays):
prev_delays = delays
diffs = [int((a - b) * multiplier) for a, b in zip(delays, prev_delays)]
count = len(diffs)
mean = sum(diffs) / count
(max_, argmax) = max((v, i) for (i, v) in enumerate(diffs))
stddev = int((sum([(i - mean)**2 for i in diffs]) / count)**0.5)
output = [min(diffs), max_, argmax, int(mean), stddev]
print(' '.join(map('{0: 16b}'.format, output)))
prev_delays = delays
loops += 1
time.sleep(interval)
def main():
parser = argparse.ArgumentParser(description='Display measurements about scheduler latency')
parser.add_argument('interval', type=float, default=0.05)
args = parser.parse_args()
schedstat_loop(args.interval)
if __name__ == '__main__':
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment