// DoParallelContainerStopBenchmark starts routineNumber of goroutines and let them stop containers, returns latencies
// of all the stopping calls in nanoseconds. There is a global rate limit on stopping calls per second.
439939
func (d *DockerHelper) DoParallelContainerStopBenchmark(qps float64, routineNumber int) []int {
	wg := &sync.WaitGroup{}
	ids := d.GetContainerIDs()
	idTable := make([][]string, routineNumber)
	for i := 0; i < len(ids); i++ {
		idTable[i%routineNumber] = append(idTable[i%routineNumber], ids[i])
	}
	wg.Add(routineNumber)
	ratelimit := ratelimit.NewBucketWithRate(qps, int64(routineNumber))
	latenciesTable := make([][]int, routineNumber)
	for i := 0; i < routineNumber; i++ {
		go func(index int) {
			latencies := []int{}
			for _, id := range idTable[index] {
				ratelimit.Wait(1)
				start := time.Now()
				d.StopContainers([]string{id})
				d.RemoveContainers([]string{id})
				latencies = append(latencies, int(time.Since(start).Nanoseconds()))
			}
			latenciesTable[index] = latencies
			wg.Done()
		}(i)
	}
	wg.Wait()
	allLatencies := []int{}
	for _, latencies := range latenciesTable {
		allLatencies = append(allLatencies, latencies...)
	}
	return allLatencies
}
236765
func (c *connectionBenchmark) run() *result {
	var err error
	if c.requestRate == 0 {
		c.elapsed, err = c.runFullThrottle()
	} else {
		c.elapsed, err = c.runRateLimited()
	}
	return &result{summary: c.summarize(), err: err}
}
680174
func (impl *repoImpl) runProcessorsTask(c context.Context, t *tasks.RunProcessors) error {
	// Fetch the instance to see what processors are still pending.
	inst := (&model.Instance{}).FromProto(c, t.Instance)
	switch err := datastore.Get(c, inst); {
	case err == datastore.ErrNoSuchEntity:
		return fmt.Errorf("instance %q is unexpectedly gone from the datastore", inst.InstanceID)
	case err != nil:
		return transient.Tag.Apply(err)
	}

	results := map[string]processing.Result{}

	// Grab processors we haven't ran yet.
	var run []processing.Processor
	for _, id := range inst.ProcessorsPending {
		if proc := impl.procsMap[id]; proc != nil {
			run = append(run, proc)
		} else {
			logging.Errorf(c, "Skipping unknown processor %q", id)
			results[id] = processing.Result{Err: fmt.Errorf("unknown processor %q", id)}
		}
	}

	// Exit early if there's nothing to run.
	if len(run) == 0 {
		return impl.updateProcessors(c, t.Instance, results)
	}

	// Open the package for reading.
	pkg, err := impl.packageReader(c, t.Instance.Instance)
	switch {
	case transient.Tag.In(err):
		return err // retry the whole thing
	case err != nil:
		// The package is fatally broken, give up.
		logging.WithError(err).Errorf(c, "The package can't be opened, failing all processors")
		for _, proc := range run {
			results[proc.ID()] = processing.Result{Err: err}
		}
		return impl.updateProcessors(c, t.Instance, results)
	}

	// Run the processors sequentially, since PackageReader is not very friendly
	// to concurrent access.
	var transientErrs errors.MultiError
	for _, proc := range run {
		logging.Infof(c, "Running processor %q", proc.ID())
		res, err := proc.Run(c, inst, pkg)
		if err != nil {
			logging.WithError(err).Errorf(c, "Processor %q failed transiently", proc.ID())
			transientErrs = append(transientErrs, err)
		} else {
			if res.Err != nil {
				logging.WithError(res.Err).Errorf(c, "Processor %q failed fatally", proc.ID())
			}
			results[proc.ID()] = res
		}
	}

	// Store what we've got, even if some processor may have failed to run.
	updErr := impl.updateProcessors(c, t.Instance, results)

	// Prefer errors from processors over 'updErr' if both happen. Processor
	// errors are more interesting.
	switch {
	case len(transientErrs) != 0:
		return transient.Tag.Apply(transientErrs)
	case updErr != nil:
		return updErr
	}
	return nil
}
256513
func (c *connectionBenchmark) runRateLimited() (time.Duration, error) {
	var (
		interval = c.expectedInterval.Nanoseconds()
		stop     = time.After(c.duration)
		start    = time.Now()
		limit    = rate.Every(c.expectedInterval)
		limiter  = rate.NewLimiter(limit, c.burst)
		ctx      = context.Background()
	)
	for {
		select {
		case <-stop:
			return time.Since(start), nil
		default:
		}

		limiter.WaitN(ctx, c.burst)
		for i := 0; i < c.burst; i++ {
			before := time.Now()
			err := c.requester.Request()
			latency := time.Since(before).Nanoseconds()
			if err != nil {
				if err := c.errorHistogram.RecordCorrectedValue(latency, interval); err != nil {
					return 0, err
				}
				if err := c.uncorrectedErrorHistogram.RecordValue(latency); err != nil {
					return 0, err
				}
				c.errorTotal++
			} else {
				if err := c.successHistogram.RecordCorrectedValue(latency, interval); err != nil {
					return 0, err
				}
				if err := c.uncorrectedSuccessHistogram.RecordValue(latency); err != nil {
					return 0, err
				}
				c.successTotal++
			}
		}
	}
}
631877
func NewParallelRunner(ctx context.Context, rootUINode *Node, cp *CheckpointWriter, tasks []*workflowpb.Task, executeFunc func(context.Context, *workflowpb.Task) error, concurrencyLevel level, enableApprovals bool) *ParallelRunner {
	if len(tasks) < 1 {
		log.Fatal("BUG: No tasks passed into ParallelRunner")
	}

	phaseID := path.Dir(tasks[0].Id)
	phaseUINode, err := rootUINode.GetChildByPath(phaseID)
	if err != nil {
		log.Fatalf("BUG: nodepath %v not found", phaseID)
	}

	p := &ParallelRunner{
		ctx:                 ctx,
		uiLogger:            logutil.NewMemoryLogger(),
		rootUINode:          rootUINode,
		phaseUINode:         phaseUINode,
		checkpointWriter:    cp,
		tasks:               tasks,
		executeFunc:         executeFunc,
		concurrencyLevel:    concurrencyLevel,
		retryActionRegistry: make(map[string]chan struct{}),
		enableApprovals:     enableApprovals,
	}

	if p.enableApprovals {
		p.initApprovalActions()
	}

	return p
}
1036935
func (c *Consumer) CountCallback(totalSize int64) counter.CountCallback {
	return func(count int64) {
		c.Progress(float64(count) / float64(totalSize))
	}
}
519210
func (crs *CopRuntimeStats) RecordOneCopTask(address string, summary *tipb.ExecutorExecutionSummary) {
	crs.Lock()
	defer crs.Unlock()
	crs.stats[address] = append(crs.stats[address],
		&RuntimeStats{int32(*summary.NumIterations), int64(*summary.TimeProcessedNs), int64(*summary.NumProducedRows)})
}
136273
func (ve *SignerValidatorEndpoint) OnStop() {
	if ve.cancelPingCh != nil {
		close(ve.cancelPingCh)
	}
	ve.Close()
}
703513
func (wk *WebmKeeper) handleRandomAccessPoint(el *edtd.Elem) error {
	switch el.Name {
	case "Cluster":
		wk.lastCluster = wk.body.Len()
		wk.resetTracking()
		return nil

	case "SimpleBlock", "Block":

		trackNumber, keyframe, err := webm.AsKeyBlock(el)
		if err != nil {
			return err
		}
		if trackNumber > 2 || trackNumber < 1 {
			return fmt.Errorf("impossible track number %d", trackNumber)
		}
		trackNumber--

		wk.incTrackCount(trackNumber)
		if keyframe {
			wk.trackBlockKeyframe[trackNumber] = true
		}

		if wk.trackBlockCount[0] == 1 &&
			wk.trackBlockCount[1] == 1 &&
			wk.trackBlockKeyframe[0] &&
			wk.trackBlockKeyframe[1] {

			b := make([]byte, wk.body.Len() - wk.lastCluster)
			copy(b, wk.body.Bytes()[wk.lastCluster:])
			wk.body.Reset()
			wk.body.Write(b)
			wk.lastCluster = 0
		}

		return nil

	default:
		return nil
	}
}
428063
func ParseAnnotationsCPUCount(s *specs.Spec, annotation string, def int32) int32 {
	if m := parseAnnotationsUint64(s.Annotations, annotation, 0); m != 0 {
		return int32(m)
	}
	if s.Windows != nil &&
		s.Windows.Resources != nil &&
		s.Windows.Resources.CPU != nil &&
		s.Windows.Resources.CPU.Count != nil &&
		*s.Windows.Resources.CPU.Count > 0 {
		return int32(*s.Windows.Resources.CPU.Count)
	}
	return def
}
1162959
func (cmd *BenchCommand) stopProfiling() {
	if cpuprofile != nil {
		pprof.StopCPUProfile()
		cpuprofile.Close()
		cpuprofile = nil
	}

	if memprofile != nil {
		pprof.Lookup("heap").WriteTo(memprofile, 0)
		memprofile.Close()
		memprofile = nil
	}

	if blockprofile != nil {
		pprof.Lookup("block").WriteTo(blockprofile, 0)
		blockprofile.Close()
		blockprofile = nil
		runtime.SetBlockProfileRate(0)
	}
}
1120352
func (opt IteratorOptions) StopTime() int64 {
	if opt.Ascending {
		return opt.EndTime
	}
	return opt.StartTime
}
121310
func (n *Node) stopInProc() {
	if n.inprocHandler != nil {
		n.inprocHandler.Stop()
		n.inprocHandler = nil
	}
}
243926
func (m *Mirrors) StopAllAndWait() {
	m.reposMu.Lock()
	for k, state := range m.repos {
		close(state.stop)
		state.repo.Clean()
		delete(m.repos, k)
	}
	m.reposMu.Unlock()
	m.wg.Wait()
}
315242
func stopTracing(ctx context.Context) {
	// Handle scenario where die() is called early in startup
	if ctx == nil {
		return
	}

	if !tracing {
		return
	}

	span := opentracing.SpanFromContext(ctx)
	if span != nil {
		span.Finish()
	}

	// report all possible spans to the collector
	tracerCloser.Close()

	tracing = false
	startTracingCalled = false
	stopTracingCalled = false
}
181907
func onBenchmark(cf *CLIConf) {
	tc, err := makeClient(cf, false)
	if err != nil {
		utils.FatalError(err)
	}
	result, err := tc.Benchmark(cf.Context, client.Benchmark{
		Command:  cf.RemoteCommand,
		Threads:  cf.BenchThreads,
		Duration: cf.BenchDuration,
		Rate:     cf.BenchRate,
	})
	if err != nil {
		fmt.Fprintln(os.Stderr, utils.UserMessageFromError(err))
		os.Exit(255)
	}
	fmt.Printf("\n")
	fmt.Printf("* Requests originated: %v\n", result.RequestsOriginated)
	fmt.Printf("* Requests failed: %v\n", result.RequestsFailed)
	if result.LastError != nil {
		fmt.Printf("* Last error: %v\n", result.LastError)
	}
	fmt.Printf("\nHistogram\n\n")
	t := asciitable.MakeTable([]string{"Percentile", "Duration"})
	for _, quantile := range []float64{25, 50, 75, 90, 95, 99, 100} {
		t.AddRow([]string{fmt.Sprintf("%v", quantile),
			fmt.Sprintf("%v ms", result.Histogram.ValueAtQuantile(quantile)),
		})
	}
	io.Copy(os.Stdout, t.AsBuffer())
	fmt.Printf("\n")
}
623035
func grabLockOrStop(lockFunc, unlockFunc func(), stopCh chan struct{}) (stopped bool) {
	// Grab the lock as we need it for cluster setup, which needs to happen
	// before advertising;
	lockGrabbedCh := make(chan struct{})
	go func() {
		// Grab the lock
		lockFunc()
		// If stopCh has been closed, which only happens while the
		// stateLock is held, we have actually terminated, so we just
		// instantly give up the lock, otherwise we notify that it's ready
		// for consumption
		select {
		case <-stopCh:
			unlockFunc()
		default:
			close(lockGrabbedCh)
		}
	}()

	select {
	case <-stopCh:
		return true
	case <-lockGrabbedCh:
		// We now have the lock and can use it
	}

	return false
}