def record_to_damon_result()

in _damon_result.py [0:0]


def record_to_damon_result(file_path, f, fmt_version, max_secs):
    result = None
    parse_start_time = None

    if f == None:
        f = open(file_path, 'rb')

        # read record format version
        mark = f.read(16)
        if mark == b'damon_recfmt_ver':
            fmt_version = struct.unpack('i', f.read(4))[0]
        else:
            fmt_version = 0
            f.seek(0)
    elif not fmt_version:
        print('fmt_version is not given')
        exit(1)

    result = DAMONResult()

    while True:
        timebin = f.read(16)
        if len(timebin) != 16:
            if max_secs == None:
                f.close()
            break
        sec = struct.unpack('l', timebin[0:8])[0]
        nsec = struct.unpack('l', timebin[8:16])[0]
        end_time = sec * 1000000000 + nsec

        if not parse_start_time:
            parse_start_time = end_time
        elif max_secs != None and (
                end_time - parse_start_time > max_secs * 1000000000):
            f.seek(-16, 1)
            break

        nr_tasks = struct.unpack('I', f.read(4))[0]
        for t in range(nr_tasks):
            if fmt_version == 1:
                target_id = struct.unpack('i', f.read(4))[0]
            else:
                target_id = struct.unpack('L', f.read(8))[0]

            if not target_id in result.target_snapshots:
                result.target_snapshots[target_id] = []
            target_snapshots = result.target_snapshots[target_id]
            if len(target_snapshots) == 0:
                start_time = None
            else:
                start_time = target_snapshots[-1].end_time

            snapshot = DAMONSnapshot(start_time, end_time, target_id)
            nr_regions = struct.unpack('I', f.read(4))[0]
            for r in range(nr_regions):
                start_addr = struct.unpack('L', f.read(8))[0]
                end_addr = struct.unpack('L', f.read(8))[0]
                nr_accesses = struct.unpack('I', f.read(4))[0]
                region = DAMONRegion(start_addr, end_addr, nr_accesses)
                snapshot.regions.append(region)
            target_snapshots.append(snapshot)

    return result, f, fmt_version