<commit_before>package main

import (
	"container/list"
	"fmt"
	"sort"
	"strings"

	"github.com/divan/gotrace/trace"
)

func ConvertEvents(events []*trace.Event) (Commands, error) {
	var c Commands

	sends := list.New()

	var lastG uint64
	for _, ev := range events {
		switch ev.Type {
		case trace.EvGoStart:
			if len(ev.Stk) > 0 {
				if strings.HasPrefix(ev.Stk[0].Fn, "runtime") {
					if ev.Stk[0].Fn != "runtime.main" {
						lastG = ev.Args[0]
						break
					}
				}
				c.StartGoroutine(ev.Ts, ev.Stk[0].Fn, ev.G, lastG)
			}
		case trace.EvGoCreate:
			lastG = ev.G
		case trace.EvGoEnd:
			c.StopGoroutine(ev.Ts, "", ev.G)
			lastG = 1
		case trace.EvGoSend:
			sends.PushBack(ev)
		case trace.EvGoRecv:
			send := findSource(sends, ev)
			if send == nil {
				fmt.Println("[WARN] Recv w/o Send:", ev)
				continue
			}
			c.ChanSend(send.Ts, ev.Args[1], send.G, ev.G, send.Args[2])
		case trace.EvGCStart, trace.EvGCDone, trace.EvGCScanStart, trace.EvGCScanDone:
			lastG = 1
		case trace.EvGoSched, trace.EvGoPreempt,
			trace.EvGoSleep, trace.EvGoBlock, trace.EvGoBlockSelect, trace.EvGoBlockSend, trace.EvGoBlockRecv,
			trace.EvGoBlockSync, trace.EvGoBlockCond, trace.EvGoBlockNet,
			trace.EvGoSysBlock:
			lastG = 1
		case trace.EvGoStop:
			lastG = 1
		}
	}

	// sort events
	sort.Sort(ByTimestamp(c))

	// insert stop main
	// TODO: figure out why it's not in the trace<commit_msg>Fix goroutines heuristics. Add debug output.<commit_after>package main

import (
	"container/list"
	"fmt"
	"sort"
	"strings"

	"github.com/divan/gotrace/trace"
)

func ConvertEvents(events []*trace.Event) (Commands, error) {
	var c Commands

	sends := list.New()

	debug := false
	var lastG uint64
	for _, ev := range events {
		switch ev.Type {
		case trace.EvGoStart:
			if len(ev.Stk) > 0 {
				if strings.HasPrefix(ev.Stk[0].Fn, "runtime") {
					if ev.Stk[0].Fn != "runtime.main" {
						lastG = ev.Args[0]
						break
					}
				}
				if debug {
					fmt.Println(" ---> Create:", ev.G, "from", lastG)
				}
				c.StartGoroutine(ev.Ts, ev.Stk[0].Fn, ev.G, lastG)
			}
		case trace.EvGoCreate:
			lastG = ev.G
		case trace.EvGoEnd:
			c.StopGoroutine(ev.Ts, "", ev.G)
			lastG = ev.G
			if debug {
				fmt.Println("End:", ev.G)
			}
		case trace.EvGoSend:
			sends.PushBack(ev)
			if debug {
				fmt.Printf("[DD] %d, Send: G:%d, CH: %d, EvID: %d, Val:%d\n", ev.Ts, ev.G, ev.Args[1], ev.Args[0], ev.Args[2])
			}
		case trace.EvGoRecv:
			if debug {
				fmt.Printf("[DD] %d, Recv: G:%d, CH: %d, EvID: %d, Val:%d - %v\n", ev.Ts, ev.G, ev.Args[1], ev.Args[0], ev.Args[2], ev)
			}
			send := findSource(sends, ev)
			if send == nil {
				if debug {
					//fmt.Println("[DD] Close channel (probably):", ev.G, ev.Args[1])
				}
				continue
			}
			if debug {
				fmt.Printf("[DD] %d, Recv->Send: FromG:%d, ToG: %d, CH: %d, EvID: %d, Val:%d\n", send.Ts, send.G, ev.G, ev.Args[1], ev.Args[0], ev.Args[2])
			}
			c.ChanSend(send.Ts, ev.Args[1], send.G, ev.G, send.Args[2])
		case trace.EvGCStart, trace.EvGCDone, trace.EvGCScanStart, trace.EvGCScanDone:
			lastG = 1
			/*
				case trace.EvGoSched, trace.EvGoPreempt,
					trace.EvGoSleep, trace.EvGoBlock, trace.EvGoBlockSelect, trace.EvGoBlockSend, trace.EvGoBlockRecv,
					trace.EvGoBlockSync, trace.EvGoBlockCond, trace.EvGoBlockNet,
					trace.EvGoSysBlock:
					fmt.Println("Ev:", ev.Type, ev.G, ev.Args)
					//lastG = ev.G
			*/
		case trace.EvGoStop:
			if debug {
				fmt.Println("Stop:", ev.G)
			}
			lastG = 1
		default:
			if debug {
				fmt.Println("Ev:", ev.Type, ev.G, ev.Args)
			}
		}
	}

	// sort events
	sort.Sort(ByTimestamp(c))

	// insert stop main
	// TODO: figure out why it's not in the trace
{'fraction_non_alphanumeric': 0.16584967320261437, 'fraction_numerical': 0.009259259259259259, 'mean_word_length': 2.8622502628811777, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3332112', 'n_tokens_mistral': 1779, 'n_tokens_neox': 1523, 'n_words': 322}