Skip to content

Instantly share code, notes, and snippets.

@jhlb
Created February 25, 2014 19:34
Show Gist options
  • Save jhlb/9216070 to your computer and use it in GitHub Desktop.
Save jhlb/9216070 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import sys, os
USAGE="""progbar.py PROCESS_ID [FILE_DESCRIPTOR_NUMBER]
Usage:
progbar.py PROCESS_ID
Prints all file descriptor numbers along with the location of
each.
progbar.py PROCESS_ID FILE_DESCRIPTOR
"""
if len(sys.argv) == 2:
# Case 1: Print all file descriptor numbers and filenames.
pid = sys.argv[1]
fd_dir = os.path.join('/proc', pid, 'fd')
fds = os.listdir(fd_dir)
for fd in fds:
print fd, "=>", os.readlink(os.path.join(fd_dir, fd))
elif len(sys.argv) == 3:
# Case 2: Print the location of the file pointer, this file size,
# and the percentage of the file behind the file pointer.
pid = sys.argv[1]
fd = sys.argv[2]
# step 1: get current location
fdinfo_filename = os.path.join('/proc', pid, 'fdinfo', fd)
fdinfo_handle = file(fdinfo_filename)
location = int(fdinfo_handle.readlines()[0].split()[1])
fdinfo_handle.close()
# step 2: get total size.
file_path = os.readlink(os.path.join('/proc', pid, 'fd', fd))
size = os.stat(file_path).st_size
# report
print location, "/", size
print "%.2f%%" % (float(location)/float(size)*100)
else:
# Case 3: Print usage and exit
print USAGE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment