github.com/gocuntian/go@v0.0.0-20160610041250-fee02d270bf8/src/internal/trace/order.go (about)

     1  // Copyright 2016 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package trace
     6  
     7  import (
     8  	"fmt"
     9  	"sort"
    10  )
    11  
    12  type eventBatch struct {
    13  	events   []*Event
    14  	selected bool
    15  }
    16  
    17  type orderEvent struct {
    18  	ev    *Event
    19  	batch int
    20  	g     uint64
    21  	init  gState
    22  	next  gState
    23  }
    24  
    25  type gStatus int
    26  
    27  type gState struct {
    28  	seq    uint64
    29  	status gStatus
    30  }
    31  
    32  const (
    33  	gDead gStatus = iota
    34  	gRunnable
    35  	gRunning
    36  	gWaiting
    37  
    38  	unordered = ^uint64(0)
    39  	garbage   = ^uint64(0) - 1
    40  	noseq     = ^uint64(0)
    41  	seqinc    = ^uint64(0) - 1
    42  )
    43  
    44  // order1007 merges a set of per-P event batches into a single, consistent stream.
    45  // The high level idea is as follows. Events within an individual batch are in
    46  // correct order, because they are emitted by a single P. So we need to produce
    47  // a correct interleaving of the batches. To do this we take first unmerged event
    48  // from each batch (frontier). Then choose subset that is "ready" to be merged,
    49  // that is, events for which all dependencies are already merged. Then we choose
    50  // event with the lowest timestamp from the subset, merge it and repeat.
    51  // This approach ensures that we form a consistent stream even if timestamps are
    52  // incorrect (condition observed on some machines).
    53  func order1007(m map[int][]*Event) (events []*Event, err error) {
    54  	pending := 0
    55  	var batches []*eventBatch
    56  	for _, v := range m {
    57  		pending += len(v)
    58  		batches = append(batches, &eventBatch{v, false})
    59  	}
    60  	gs := make(map[uint64]gState)
    61  	var frontier []orderEvent
    62  	for ; pending != 0; pending-- {
    63  		for i, b := range batches {
    64  			if b.selected || len(b.events) == 0 {
    65  				continue
    66  			}
    67  			ev := b.events[0]
    68  			g, init, next := stateTransition(ev)
    69  			if !transitionReady(g, gs[g], init) {
    70  				continue
    71  			}
    72  			frontier = append(frontier, orderEvent{ev, i, g, init, next})
    73  			b.events = b.events[1:]
    74  			b.selected = true
    75  			// Get rid of "Local" events, they are intended merely for ordering.
    76  			switch ev.Type {
    77  			case EvGoStartLocal:
    78  				ev.Type = EvGoStart
    79  			case EvGoUnblockLocal:
    80  				ev.Type = EvGoUnblock
    81  			case EvGoSysExitLocal:
    82  				ev.Type = EvGoSysExit
    83  			}
    84  		}
    85  		if len(frontier) == 0 {
    86  			return nil, fmt.Errorf("no consistent ordering of events possible")
    87  		}
    88  		sort.Sort(orderEventList(frontier))
    89  		f := frontier[0]
    90  		frontier[0] = frontier[len(frontier)-1]
    91  		frontier = frontier[:len(frontier)-1]
    92  		events = append(events, f.ev)
    93  		transition(gs, f.g, f.init, f.next)
    94  		if !batches[f.batch].selected {
    95  			panic("frontier batch is not selected")
    96  		}
    97  		batches[f.batch].selected = false
    98  	}
    99  
   100  	// At this point we have a consistent stream of events.
   101  	// Make sure time stamps respect the ordering.
   102  	// The tests will skip (not fail) the test case if they see this error.
   103  	if !sort.IsSorted(eventList(events)) {
   104  		return nil, ErrTimeOrder
   105  	}
   106  
   107  	// The last part is giving correct timestamps to EvGoSysExit events.
   108  	// The problem with EvGoSysExit is that actual syscall exit timestamp (ev.Args[2])
   109  	// is potentially acquired long before event emission. So far we've used
   110  	// timestamp of event emission (ev.Ts).
   111  	// We could not set ev.Ts = ev.Args[2] earlier, because it would produce
   112  	// seemingly broken timestamps (misplaced event).
   113  	// We also can't simply update the timestamp and resort events, because
   114  	// if timestamps are broken we will misplace the event and later report
   115  	// logically broken trace (instead of reporting broken timestamps).
   116  	lastSysBlock := make(map[uint64]int64)
   117  	for _, ev := range events {
   118  		switch ev.Type {
   119  		case EvGoSysBlock, EvGoInSyscall:
   120  			lastSysBlock[ev.G] = ev.Ts
   121  		case EvGoSysExit:
   122  			ts := int64(ev.Args[2])
   123  			if ts == 0 {
   124  				continue
   125  			}
   126  			block := lastSysBlock[ev.G]
   127  			if block == 0 {
   128  				return nil, fmt.Errorf("stray syscall exit")
   129  			}
   130  			if ts < block {
   131  				return nil, ErrTimeOrder
   132  			}
   133  			ev.Ts = ts
   134  		}
   135  	}
   136  	sort.Stable(eventList(events))
   137  
   138  	return
   139  }
   140  
   141  // stateTransition returns goroutine state (sequence and status) when the event
   142  // becomes ready for merging (init) and the goroutine state after the event (next).
   143  func stateTransition(ev *Event) (g uint64, init, next gState) {
   144  	switch ev.Type {
   145  	case EvGoCreate:
   146  		g = ev.Args[0]
   147  		init = gState{0, gDead}
   148  		next = gState{1, gRunnable}
   149  	case EvGoWaiting, EvGoInSyscall:
   150  		g = ev.G
   151  		init = gState{1, gRunnable}
   152  		next = gState{2, gWaiting}
   153  	case EvGoStart:
   154  		g = ev.G
   155  		init = gState{ev.Args[1], gRunnable}
   156  		next = gState{ev.Args[1] + 1, gRunning}
   157  	case EvGoStartLocal:
   158  		// noseq means that this event is ready for merging as soon as
   159  		// frontier reaches it (EvGoStartLocal is emitted on the same P
   160  		// as the corresponding EvGoCreate/EvGoUnblock, and thus the latter
   161  		// is already merged).
   162  		// seqinc is a stub for cases when event increments g sequence,
   163  		// but since we don't know current seq we also don't know next seq.
   164  		g = ev.G
   165  		init = gState{noseq, gRunnable}
   166  		next = gState{seqinc, gRunning}
   167  	case EvGoBlock, EvGoBlockSend, EvGoBlockRecv, EvGoBlockSelect,
   168  		EvGoBlockSync, EvGoBlockCond, EvGoBlockNet, EvGoSleep, EvGoSysBlock:
   169  		g = ev.G
   170  		init = gState{noseq, gRunning}
   171  		next = gState{noseq, gWaiting}
   172  	case EvGoSched, EvGoPreempt:
   173  		g = ev.G
   174  		init = gState{noseq, gRunning}
   175  		next = gState{noseq, gRunnable}
   176  	case EvGoUnblock, EvGoSysExit:
   177  		g = ev.Args[0]
   178  		init = gState{ev.Args[1], gWaiting}
   179  		next = gState{ev.Args[1] + 1, gRunnable}
   180  	case EvGoUnblockLocal, EvGoSysExitLocal:
   181  		g = ev.Args[0]
   182  		init = gState{noseq, gWaiting}
   183  		next = gState{seqinc, gRunnable}
   184  	case EvGCStart:
   185  		g = garbage
   186  		init = gState{ev.Args[0], gDead}
   187  		next = gState{ev.Args[0] + 1, gDead}
   188  	default:
   189  		// no ordering requirements
   190  		g = unordered
   191  	}
   192  	return
   193  }
   194  
   195  func transitionReady(g uint64, curr, init gState) bool {
   196  	return g == unordered || (init.seq == noseq || init.seq == curr.seq) && init.status == curr.status
   197  }
   198  
   199  func transition(gs map[uint64]gState, g uint64, init, next gState) {
   200  	if g == unordered {
   201  		return
   202  	}
   203  	curr := gs[g]
   204  	if !transitionReady(g, curr, init) {
   205  		panic("event sequences are broken")
   206  	}
   207  	switch next.seq {
   208  	case noseq:
   209  		next.seq = curr.seq
   210  	case seqinc:
   211  		next.seq = curr.seq + 1
   212  	}
   213  	gs[g] = next
   214  }
   215  
   216  // order1005 merges a set of per-P event batches into a single, consistent stream.
   217  func order1005(m map[int][]*Event) (events []*Event, err error) {
   218  	for _, batch := range m {
   219  		events = append(events, batch...)
   220  	}
   221  	for _, ev := range events {
   222  		if ev.Type == EvGoSysExit {
   223  			// EvGoSysExit emission is delayed until the thread has a P.
   224  			// Give it the real sequence number and time stamp.
   225  			ev.seq = int64(ev.Args[1])
   226  			if ev.Args[2] != 0 {
   227  				ev.Ts = int64(ev.Args[2])
   228  			}
   229  		}
   230  	}
   231  	sort.Sort(eventSeqList(events))
   232  	if !sort.IsSorted(eventList(events)) {
   233  		return nil, ErrTimeOrder
   234  	}
   235  	return
   236  }
   237  
   238  type orderEventList []orderEvent
   239  
   240  func (l orderEventList) Len() int {
   241  	return len(l)
   242  }
   243  
   244  func (l orderEventList) Less(i, j int) bool {
   245  	return l[i].ev.Ts < l[j].ev.Ts
   246  }
   247  
   248  func (l orderEventList) Swap(i, j int) {
   249  	l[i], l[j] = l[j], l[i]
   250  }
   251  
   252  type eventList []*Event
   253  
   254  func (l eventList) Len() int {
   255  	return len(l)
   256  }
   257  
   258  func (l eventList) Less(i, j int) bool {
   259  	return l[i].Ts < l[j].Ts
   260  }
   261  
   262  func (l eventList) Swap(i, j int) {
   263  	l[i], l[j] = l[j], l[i]
   264  }
   265  
   266  type eventSeqList []*Event
   267  
   268  func (l eventSeqList) Len() int {
   269  	return len(l)
   270  }
   271  
   272  func (l eventSeqList) Less(i, j int) bool {
   273  	return l[i].seq < l[j].seq
   274  }
   275  
   276  func (l eventSeqList) Swap(i, j int) {
   277  	l[i], l[j] = l[j], l[i]
   278  }