Skip to content

Instantly share code, notes, and snippets.

@peo3
Created April 21, 2011 14:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peo3/934551 to your computer and use it in GitHub Desktop.
Save peo3/934551 to your computer and use it in GitHub Desktop.
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

where can i find that 'linux' module?

@sfriesel
Copy link

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

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