Last active
August 27, 2016 05:11
-
-
Save hidva/f17169ccc569ec766b4e9a6369fa947d to your computer and use it in GitHub Desktop.
周期性对指定进程的 /proc/pid/statm 文件进行采样.
This file contains 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/python | |
#coding:utf-8 | |
import time | |
import sys | |
import getopt | |
import os | |
def PrintHelpAndExit() : | |
print "Usage: %s -f path -p pid -t time" % (sys.argv[0]) | |
print "-t time; 单位为秒, 但可以为浮点数, 来实现更高精度." | |
sys.exit(1) | |
opts, non_opts = getopt.getopt(sys.argv[1:], "f:p:t:") | |
output_filepath = "" | |
pid = "" | |
sleep_time = 0.1 # 秒 | |
for opt, arg in opts : | |
if opt == "-f" : | |
output_filepath = arg | |
elif opt == "-p" : | |
pid = arg | |
elif opt == "-t" : | |
sleep_time = float(arg) | |
if sleep_time == 0 or len(output_filepath) == 0 or len(pid) == 0 : | |
PrintHelpAndExit() | |
input_filepath = "/proc/" + pid + "/statm" | |
output_file = open(output_filepath, "ab", 0) | |
while True : | |
with open(input_filepath, "rb") as input : | |
info = input.next() | |
output_file.write(", ".join(info.split()) + "\n") | |
time.sleep(sleep_time); | |
Author
hidva
commented
Aug 27, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment