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))
  • 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 https://wiki.python.org/moin/BitwiseOperators – Maurizio Benedetti May 03 '17 at 07:05
  • Instead of `return` perhaps you want to make a generator that *`yield`s* 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. – Antti Haapala -- Слава Україні May 03 '17 at 07: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. – user3234347 May 09 '17 at 04:01
  • Were you ever able to resolve this? I am having a similar problem, and I found this related question: https://stackoverflow.com/questions/6284810/proc-pid-pagemaps-and-proc-pid-maps-linux that has a script posted in answer. – Wajahat Oct 18 '18 at 04:36

0 Answers0