0

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

  1. 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.

  2. Wait some time.

  3. 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))
4
  • If I understand well, you need to check if the bit 55 is set (it means one). You can read the 64bit value, AND with 2 elevated 54, which is 18014398509481984 and test if the result is zero. If so, the bit is not set. Looks to be rather easy in python. readValue & 18014398509481984 wiki.python.org/moin/BitwiseOperators Commented May 3, 2017 at 7:05
  • Instead of return perhaps you want to make a generator that yields And one doesn't return on error - one raises an exception. Also, with with you do not need try-finally-close as that's all that with does. Commented May 3, 2017 at 7:18
  • In the first step, I want the code to print out the content of each page table entry. This would be 64 bits for each page table entry. Then, I have to check if the bit number 55 is set or not. Commented May 9, 2017 at 4:01
  • Were you ever able to resolve this? I am having a similar problem, and I found this related question: stackoverflow.com/questions/6284810/… that has a script posted in answer. Commented Oct 18, 2018 at 4:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.