I'm trying to write a piece of code in Python which tracks page dirtying of a specific process in Linux. I got the solution description in the Linux Kernel documents as follow:
The soft-dirty is a bit on a PTE which helps to track which pages a task writes to. In order to do this tracking one should
Clear soft-dirty bits from the task's PTEs.
This is done by writing "4" into the /proc/PID/clear_refs file of the task in question.
Wait some time.
Read soft-dirty bits from the PTEs.
This is done by reading from the /proc/PID/pagemap. The bit 55 of the 64-bit qword is the soft-dirty one. If set, the respective PTE was written to since step 1.
I have a problem in performing step 3. I wrote a code in python which always returns a zero. I do not know how I can read bit 55 in each PTE. I appreciate any help in this regards.
Here is I wrote in Python:
#!/usr/bin/python
import sys
import os
import struct
def read_entry(pid, size=8):
maps_path = "/proc/{0}/pagemap".format(pid)
if not os.path.isfile(maps_path):
print "Process {0} doesn't exist.".format(pid)[0]
return
with open(maps_path, 'rb') as f:
try:
f.seek(0)
while True:
return struct.unpack('Q', f.read(size))
finally:
f.close()
if __name__ == "__main__":
pid = sys.argv[1]
print (read_entry(pid))