Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A python script doing the same operations as cgroup_event_listener.c
#!/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()
@joemiller
Copy link

joemiller commented Aug 13, 2012

where can i find that 'linux' module?

Loading

@sfriesel
Copy link

sfriesel commented Jan 21, 2013

https://gist.github.com/934279 you have to compile it yourself though.

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment