// calculatePct returns the percent utilization for CPU states. 100.00 => 100.00%
514073
func calculatePcts(lastTimes cpu.TimesStat, curTimes cpu.TimesStat) cpu.TimesStat {
	totalDelta := curTimes.Total() - lastTimes.Total()
	return cpu.TimesStat{
		User:      formatPct(curTimes.User-lastTimes.User, totalDelta),
		System:    formatPct(curTimes.System-lastTimes.System, totalDelta),
		Idle:      formatPct(curTimes.Idle-lastTimes.Idle, totalDelta),
		Nice:      formatPct(curTimes.Nice-lastTimes.Nice, totalDelta),
		Iowait:    formatPct(curTimes.Iowait-lastTimes.Iowait, totalDelta),
		Irq:       formatPct(curTimes.Irq-lastTimes.Irq, totalDelta),
		Softirq:   formatPct(curTimes.Softirq-lastTimes.Softirq, totalDelta),
		Steal:     formatPct(curTimes.Steal-lastTimes.Steal, totalDelta),
		Guest:     formatPct(curTimes.Guest-lastTimes.Guest, totalDelta),
		GuestNice: formatPct(curTimes.GuestNice-lastTimes.GuestNice, totalDelta),
		Stolen:    formatPct(curTimes.Stolen-lastTimes.Stolen, totalDelta),
	}
}
836148
func calculateCHS(ts uint64) []uint {
	var sectorsPerTrack,
		heads,
		cylinderTimesHeads,
		cylinders float64
	totalSectors := float64(ts)

	ret := make([]uint, 3)

	if totalSectors > 65535*16*255 {
		totalSectors = 65535 * 16 * 255
	}

	if totalSectors >= 65535*16*63 {
		sectorsPerTrack = 255
		heads = 16
		cylinderTimesHeads = math.Floor(totalSectors / sectorsPerTrack)
	} else {
		sectorsPerTrack = 17
		cylinderTimesHeads = math.Floor(totalSectors / sectorsPerTrack)
		heads = math.Floor((cylinderTimesHeads + 1023) / 1024)
		if heads < 4 {
			heads = 4
		}
		if (cylinderTimesHeads >= (heads * 1024)) || heads > 16 {
			sectorsPerTrack = 31
			heads = 16
			cylinderTimesHeads = math.Floor(totalSectors / sectorsPerTrack)
		}
		if cylinderTimesHeads >= (heads * 1024) {
			sectorsPerTrack = 63
			heads = 16
			cylinderTimesHeads = math.Floor(totalSectors / sectorsPerTrack)
		}
	}

	cylinders = cylinderTimesHeads / heads

	// This will floor the values
	ret[0] = uint(cylinders)
	ret[1] = uint(heads)
	ret[2] = uint(sectorsPerTrack)

	return ret
}
420190
func (c *Connect) GetCPUStats(cpuNum int, flags uint32) (*NodeCPUStats, error) {
	var cnparams C.int

	var err C.virError
	ret := C.virNodeGetCPUStatsWrapper(c.ptr, C.int(cpuNum), nil, &cnparams, C.uint(0), &err)
	if ret == -1 {
		return nil, makeError(&err)
	}

	params := make([]C.virNodeCPUStats, cnparams)
	ret = C.virNodeGetCPUStatsWrapper(c.ptr, C.int(cpuNum), (*C.virNodeCPUStats)(unsafe.Pointer(&params[0])), &cnparams, C.uint(flags), &err)
	if ret == -1 {
		return nil, makeError(&err)
	}

	stats := &NodeCPUStats{}
	for i := 0; i < int(cnparams); i++ {
		param := params[i]
		field := C.GoString((*C.char)(unsafe.Pointer(&param.field)))
		switch field {
		case C.VIR_NODE_CPU_STATS_KERNEL:
			stats.KernelSet = true
			stats.Kernel = uint64(param.value)
		case C.VIR_NODE_CPU_STATS_USER:
			stats.UserSet = true
			stats.User = uint64(param.value)
		case C.VIR_NODE_CPU_STATS_IDLE:
			stats.IdleSet = true
			stats.Idle = uint64(param.value)
		case C.VIR_NODE_CPU_STATS_IOWAIT:
			stats.IowaitSet = true
			stats.Iowait = uint64(param.value)
		case C.VIR_NODE_CPU_STATS_INTR:
			stats.IntrSet = true
			stats.Intr = uint64(param.value)
		case C.VIR_NODE_CPU_STATS_UTILIZATION:
			stats.UtilizationSet = true
			stats.Utilization = uint64(param.value)
		}
	}

	return stats, nil
}
349335
func (impl *SortImpl) CalcCost(outCount float64, childCosts []float64, children ...*memo.Group) float64 {
	cnt := math.Min(children[0].Prop.Stats.RowCount, impl.plan.GetChildReqProps(0).ExpectedCnt)
	sort := impl.plan.(*plannercore.PhysicalSort)
	impl.cost = sort.GetCost(cnt) + childCosts[0]
	return impl.cost
}
1129887
func (a *AllocatedTaskResources) Subtract(delta *AllocatedTaskResources) {
	if delta == nil {
		return
	}

	a.Cpu.Subtract(&delta.Cpu)
	a.Memory.Subtract(&delta.Memory)
}
1291914
func (s *ExponentialStrategy) Calculate(attempt int) time.Duration {
	backoffDuration := s.cfg.BackoffMultiplier *
		time.Duration(math.Pow(2, float64(attempt)))
	return backoffDuration
}
752232
func (fr *FileReport) computeCoveragePercentage() float64 {
	coveredLoc := fr.locCovered()
	notCoveredLoc := fr.locNotCovered()
	totalLoc := coveredLoc + notCoveredLoc

	if totalLoc == 0 {
		return 0.0
	}

	return round(100.0*float64(coveredLoc)/float64(totalLoc), 2)
}
950693
func getProcessCPUTime() float64 {
	var usage syscall.Rusage
	if err := syscall.Getrusage(syscall.RUSAGE_SELF, &usage); err != nil {
		log.Warn("Failed to retrieve CPU time", "err", err)
		return 0
	}
	return float64(usage.Utime.Sec+usage.Stime.Sec) + float64(usage.Utime.Usec+usage.Stime.Usec)/1000000
}
955116
func (s *Stat) ProcessStats() *ProcessStats {
	s.Lock()
	defer s.Unlock()

	// Throw away the first reading as thats averaged over the machines uptime
	pstat := C.sg_get_process_count_of(C.sg_entire_process_count)

	p := &ProcessStats{
		Total:    int(pstat.total),
		Running:  int(pstat.running),
		Sleeping: int(pstat.sleeping),
		Stopped:  int(pstat.stopped),
		Zombie:   int(pstat.zombie),
	}
	return p
}
388955
func calculateMTU(mtu uint32) uint32 {
	if mtu > MaxTotalSize {
		mtu = MaxTotalSize
	}
	return mtu - header.IPv4MinimumSize
}
173494
func (b *bdpEstimator) calculate(d [8]byte) {
	// Check if the ping acked for was the bdp ping.
	if bdpPing.data != d {
		return
	}
	b.mu.Lock()
	rttSample := time.Since(b.sentAt).Seconds()
	if b.sampleCount < 10 {
		// Bootstrap rtt with an average of first 10 rtt samples.
		b.rtt += (rttSample - b.rtt) / float64(b.sampleCount)
	} else {
		// Heed to the recent past more.
		b.rtt += (rttSample - b.rtt) * float64(alpha)
	}
	b.isSent = false
	// The number of bytes accumulated so far in the sample is smaller
	// than or equal to 1.5 times the real BDP on a saturated connection.
	bwCurrent := float64(b.sample) / (b.rtt * float64(1.5))
	if bwCurrent > b.bwMax {
		b.bwMax = bwCurrent
	}
	// If the current sample (which is smaller than or equal to the 1.5 times the real BDP) is
	// greater than or equal to 2/3rd our perceived bdp AND this is the maximum bandwidth seen so far, we
	// should update our perception of the network BDP.
	if float64(b.sample) >= beta*float64(b.bdp) && bwCurrent == b.bwMax && b.bdp != bdpLimit {
		sampleFloat := float64(b.sample)
		b.bdp = uint32(gamma * sampleFloat)
		if b.bdp > bdpLimit {
			b.bdp = bdpLimit
		}
		bdp := b.bdp
		b.mu.Unlock()
		b.updateFlowControl(bdp)
		return
	}
	b.mu.Unlock()
}
607923
func (application Application) CalculatedBuildpack() string {
	if application.Buildpack.IsSet {
		return application.Buildpack.Value
	}

	return application.DetectedBuildpack.Value
}
1235373
func CountBitsIntPopCnt(x int) int {
	if strconv.IntSize == 64 {
		return CountBitsInt64PopCnt(int64(x))
	} else if strconv.IntSize == 32 {
		return CountBitsInt32PopCnt(int32(x))
	}
	panic("strconv.IntSize must be 32 or 64")
}
881146
func calculateCount(cMap map[string]int) int {
	count := 0
	for _, val := range cMap {
		if val > 0 {
			count++
		}
		if count == 5 {
			break
		}
	}
	return count
}
565788
func (_m *Device) CachedPower() bool {
	ret := _m.Called()

	var r0 bool
	if rf, ok := ret.Get(0).(func() bool); ok {
		r0 = rf()
	} else {
		r0 = ret.Get(0).(bool)
	}

	return r0
}
1129770
func formatPct(f float64, div float64) float64 {
	// avoid division by 0
	if div == 0 {
		div = 1
	}
	rounded := math.Floor((f / div * 10000) + .5)
	return math.Max(rounded/100, 0)
}
1330032
func (s *SelectorSpread) CalculateSpreadPriorityReduce(pod *v1.Pod, meta interface{}, nodeNameToInfo map[string]*schedulernodeinfo.NodeInfo, result schedulerapi.HostPriorityList) error {
	countsByZone := make(map[string]int, 10)
	maxCountByZone := int(0)
	maxCountByNodeName := int(0)

	for i := range result {
		if result[i].Score > maxCountByNodeName {
			maxCountByNodeName = result[i].Score
		}
		zoneID := utilnode.GetZoneKey(nodeNameToInfo[result[i].Host].Node())
		if zoneID == "" {
			continue
		}
		countsByZone[zoneID] += result[i].Score
	}

	for zoneID := range countsByZone {
		if countsByZone[zoneID] > maxCountByZone {
			maxCountByZone = countsByZone[zoneID]
		}
	}

	haveZones := len(countsByZone) != 0

	maxCountByNodeNameFloat64 := float64(maxCountByNodeName)
	maxCountByZoneFloat64 := float64(maxCountByZone)
	MaxPriorityFloat64 := float64(schedulerapi.MaxPriority)

	for i := range result {
		// initializing to the default/max node score of maxPriority
		fScore := MaxPriorityFloat64
		if maxCountByNodeName > 0 {
			fScore = MaxPriorityFloat64 * (float64(maxCountByNodeName-result[i].Score) / maxCountByNodeNameFloat64)
		}
		// If there is zone information present, incorporate it
		if haveZones {
			zoneID := utilnode.GetZoneKey(nodeNameToInfo[result[i].Host].Node())
			if zoneID != "" {
				zoneScore := MaxPriorityFloat64
				if maxCountByZone > 0 {
					zoneScore = MaxPriorityFloat64 * (float64(maxCountByZone-countsByZone[zoneID]) / maxCountByZoneFloat64)
				}
				fScore = (fScore * (1.0 - zoneWeighting)) + (zoneWeighting * zoneScore)
			}
		}
		result[i].Score = int(fScore)
		if klog.V(10) {
			klog.Infof(
				"%v -> %v: SelectorSpreadPriority, Score: (%d)", pod.Name, result[i].Host, int(fScore),
			)
		}
	}
	return nil
}