def group_perf_by_events(filedir):

    """
    Given a perf trace generated by: perf script > out.perf,
        return a list of all the events (each event is a CPU sample)
    Each event is a list of strings (lines) of the trace

    Example of 2 events:
        line 1 of each event: the program name; thread id (tid); time (in sec); cycles took for the trace (not sure)
        line 2 of each event: the top of the stack
        rest lines: the call stack

    ====== event 0 ======
    ['perf  3389  5446.582354:          1 cycles: \n', 
    '\t    7fff81068a3a native_write_msr_safe ([kernel.kallsyms])\n', 
    '\t    7fff8100df8c core_pmu_enable_all ([kernel.kallsyms])\n', 
    '\t    7fff8100902f x86_pmu_enable ([kernel.kallsyms])\n', 
    '\t    7fff811850ed perf_pmu_enable.part.87 ([kernel.kallsyms])\n', 
    '\t    7fff81188098 perf_event_context_sched_in ([kernel.kallsyms])\n', 
    '\t    7fff8118a86d perf_event_exec ([kernel.kallsyms])\n', 
    '\t    7fff812229df setup_new_exec ([kernel.kallsyms])\n', 
    '\t    7fff81278c2e load_elf_binary ([kernel.kallsyms])\n', 
    '\t    7fff812210a4 search_binary_handler ([kernel.kallsyms])\n', 
    '\t    7fff812226d8 do_execveat_common.isra.31 ([kernel.kallsyms])\n', 
    '\t    7fff81222b3a sys_execve ([kernel.kallsyms])\n', 
    '\t    7fff8184b3d5 return_from_execve ([kernel.kallsyms])\n', 
    '\t    7f2a78c1c7f7 [unknown] ([unknown])\n', '\n']
    ====== event 1 ======
    ['demo_sift1M_rea  3390  5446.586203:          1 cycles: \n', 
    '\t    7fff81068a3a native_write_msr_safe ([kernel.kallsyms])\n', 
    '\t    7fff8100df8c core_pmu_enable_all ([kernel.kallsyms])\n', 
    '\t    7fff8100902f x86_pmu_enable ([kernel.kallsyms])\n', 
    '\t    7fff811850ed perf_pmu_enable.part.87 ([kernel.kallsyms])\n', 
    '\t    7fff81188098 perf_event_context_sched_in ([kernel.kallsyms])\n', 
    '\t    7fff8118a42a __perf_event_task_sched_in ([kernel.kallsyms])\n', 
    '\t    7fff810b085e finish_task_switch ([kernel.kallsyms])\n', 
    '\t    7fff810b440f schedule_tail ([kernel.kallsyms])\n', 
    '\t    7fff8184b49f ret_from_fork ([kernel.kallsyms])\n', 
    '\t          1074e1 __clone (/lib/x86_64-linux-gnu/libc-2.23.so)\n', 
    '\t               0 [unknown] ([unknown])\n', '\n']
    """

    lines = None
    with open(filedir, 'r') as file:
        lines = file.readlines()
     
    all_events = []

    line_count = 0
    event_count = 0

    current_event = []
    for line in lines:
        line_count += 1
        current_event.append(line)
        if line == '\n': 
            event_count += 1
            all_events.append(current_event)
            current_event = []

    print('line_count: ', line_count)
    print('event_count: ', event_count)

    return all_events