Created
April 21, 2011 14:00
-
-
Save peo3/934551 to your computer and use it in GitHub Desktop.
A python script doing the same operations as cgroup_event_listener.c
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 | |
# Usage: sudo python cgroup_event_listener.py /sys/fs/cgroup/memory/hoge/memory.usage_in_bytes 263M | |
import sys | |
import os, os.path | |
import struct | |
import linux | |
def main(): | |
target_file = sys.argv[1] | |
threshold = sys.argv[2] | |
# Don't write in one call chain to keep open the fd | |
f = open(target_file) | |
cfd = f.fileno() | |
event_file = os.path.join(os.path.dirname(target_file), | |
'cgroup.event_control') | |
event_control = open(event_file, 'w') | |
ecfd = event_control.fileno() | |
efd = linux.eventfd(0, 0) | |
line = "%d %d %s\0"%(efd,cfd,threshold) | |
os.write(ecfd, line) | |
while True: | |
ret = os.read(efd, 64/8) | |
ret = struct.unpack('Q', ret) | |
if ret == -1: | |
print('Cannot read from eventfd') | |
sys.exit(1) | |
if not os.path.exists(event_file): | |
print('The cgroup seems to have removed.') | |
sys.exit(0) | |
print("%s %s: crossed"%(target_file,threshold)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
where can i find that 'linux' module?