Skip to content

Instantly share code, notes, and snippets.

@nkavanagh
Created February 5, 2015 20:33
Show Gist options
  • Save nkavanagh/d27ef0b5a1957c1cfbfa to your computer and use it in GitHub Desktop.
Save nkavanagh/d27ef0b5a1957c1cfbfa to your computer and use it in GitHub Desktop.
Collect profiling information from `procfs`
#!/usr/bin/env python
# encoding: utf-8
"""
Profile system performance
Created by Niall Kavanagh <[email protected]> on 2/5/2015
"""
from __future__ import print_function
import argparse
import time
import datetime
import re
def profile_memory():
profile = {}
pattern = re.compile('^(.+):\s*(.+)$')
with open('/proc/meminfo') as f:
for line in f:
match = pattern.match(line)
if match:
profile[match.group(1)] = match.group(2)
return profile
def profile_load():
profile = {}
pattern = re.compile('^(\S+)\s+(\S+)\s+(\S+)\s+')
with open('/proc/loadavg') as f:
for line in f:
match = pattern.match(line)
if match:
profile['LoadAvg1'] = match.group(1)
profile['LoadAvg5'] = match.group(2)
profile['LoadAvg15'] = match.group(3)
return profile
def profile_processes():
profile = {}
# this pattern will not match lot of multi-field lines
pattern = re.compile('^(\S+)\s+(\d+)$')
with open('/proc/stat') as f:
for line in f:
match = pattern.match(line)
if match:
profile[match.group(1)] = match.group(2)
return profile
def profile_system(metrics):
memory = profile_memory()
load = profile_load()
processes = profile_processes()
profiles = [memory, load, processes]
ts = time.time()
profile = {
'Time': datetime.datetime.fromtimestamp(ts)
.strftime('%Y-%m-%d %H:%M:%S')
}
for metric in metrics:
for p in profiles:
if metric in p:
profile[metric] = p[metric]
return profile
def main():
# default profile metrics TODO arg for this
metrics = [
'Time',
'MemTotal',
'MemFree',
'LoadAvg1',
'procs_running',
'procs_blocked'
]
# command line args
parser = argparse.ArgumentParser(description='Profiles system performance \
and outputs CSV.')
parser.add_argument('--duration', default=60, type=int,
help='number of seconds to profile for')
parser.add_argument('--frequency', default=5, type=int,
help='how often to sample')
args = parser.parse_args()
print(','.join(metrics))
for i in range(0, args.duration/args.frequency):
profile = profile_system(metrics)
values = []
for metric in metrics:
values.append(profile[metric])
print(','.join(values))
time.sleep(args.frequency)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment