// Initializes and returns a cpux profiler.
544615
func NewProfiler() *Profiler {
	prof := cpux.NewProfiler()
	return &Profiler{Profiler: prof, Builder: fb.NewBuilder(0)}
}
117769
func (prof *Profiler) Get() (nodes *Nodes, err error) {
	nodes = &Nodes{}
	var x int32 // index of nodeX currently being processed.

	// First see if the node dir exists, return any error.
	_, err = os.Stat(prof.nodePath)
	if err != nil {
		return nil, err
	}
	// Loop and increment x after each dir read; stop when the nodeX dir to be
	// processed doesn't exist.
	for {
		var n Node
		p := prof.nodeXPath(x)
		// get the cpulist
		n.ID = x
		n.CPUList, err = prof.CPUList(p)
		if err != nil {
			// if the dir didn't exist; there are no more nodes to process
			if os.IsNotExist(err) {
				return nodes, nil
			}
			// any other error will be passed back
			return nil, err
		}
		nodes.Node = append(nodes.Node, n)
		x++
	}
}
482771
func (s *systemInfo) CPUModel() string {
	err := s.collectPlatformInfo()
	if err != nil {
		return "unknown"
	}
	return s.cpuModel
}
1154646
func (prof *Profiler) Serialize(stts *stats.CPUStats) []byte {
	// ensure the Builder is in a usable state.
	prof.Builder.Reset()
	cpusF := make([]fb.UOffsetT, len(stts.CPU))
	ids := make([]fb.UOffsetT, len(stts.CPU))
	for i := 0; i < len(ids); i++ {
		ids[i] = prof.Builder.CreateString(stts.CPU[i].ID)
	}
	for i := 0; i < len(cpusF); i++ {
		structs.CPUStart(prof.Builder)
		structs.CPUAddID(prof.Builder, ids[i])
		structs.CPUAddUser(prof.Builder, stts.CPU[i].User)
		structs.CPUAddNice(prof.Builder, stts.CPU[i].Nice)
		structs.CPUAddSystem(prof.Builder, stts.CPU[i].System)
		structs.CPUAddIdle(prof.Builder, stts.CPU[i].Idle)
		structs.CPUAddIOWait(prof.Builder, stts.CPU[i].IOWait)
		structs.CPUAddIRQ(prof.Builder, stts.CPU[i].IRQ)
		structs.CPUAddSoftIRQ(prof.Builder, stts.CPU[i].SoftIRQ)
		structs.CPUAddSteal(prof.Builder, stts.CPU[i].Steal)
		structs.CPUAddQuest(prof.Builder, stts.CPU[i].Quest)
		structs.CPUAddQuestNice(prof.Builder, stts.CPU[i].QuestNice)
		cpusF[i] = structs.CPUEnd(prof.Builder)
	}
	structs.CPUStatsStartCPUVector(prof.Builder, len(cpusF))
	for i := len(cpusF) - 1; i >= 0; i-- {
		prof.Builder.PrependUOffsetT(cpusF[i])
	}
	cpusV := prof.Builder.EndVector(len(cpusF))
	structs.CPUStatsStart(prof.Builder)
	structs.CPUStatsAddClkTck(prof.Builder, stts.ClkTck)
	structs.CPUStatsAddTimestamp(prof.Builder, stts.Timestamp)
	structs.CPUStatsAddCtxt(prof.Builder, stts.Ctxt)
	structs.CPUStatsAddBTime(prof.Builder, stts.BTime)
	structs.CPUStatsAddProcesses(prof.Builder, stts.Processes)
	structs.CPUStatsAddCPU(prof.Builder, cpusV)
	prof.Builder.Finish(structs.CPUStatsEnd(prof.Builder))
	p := prof.Builder.Bytes[prof.Builder.Head():]
	// copy them (otherwise gets lost in reset)
	tmp := make([]byte, len(p))
	copy(tmp, p)
	return tmp
}
1296635
func NewProfiler() (prof *Profiler, err error) {
	p, err := o.NewProfiler()
	if err != nil {
		return nil, err
	}
	return &Profiler{Profiler: p}, nil
}
1283841
func (d *Debug) Profile(seconds int) ([]byte, error) {
	r := d.c.newRequest("GET", "/debug/pprof/profile")

	// Capture a profile for the specified number of seconds
	r.params.Set("seconds", strconv.Itoa(seconds))

	_, resp, err := d.c.doRequest(r)
	if err != nil {
		return nil, fmt.Errorf("error making request: %s", err)
	}
	defer resp.Body.Close()

	// We return a raw response because we're just passing through a response
	// from the pprof handlers
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, fmt.Errorf("error decoding body: %s", err)
	}

	return body, nil
}
413844
func (prof *Profiler) Get() (*CPUs, error) {
	cpus := &CPUs{CPU: make([]CPU, prof.NumCPU)}
	var err error
	var pids []int32 // the physical ids encountered

	hasFreq := prof.hasCPUFreq()
	for x := 0; x < prof.NumCPU; x++ {
		var cpu CPU
		var found bool

		cpu.PhysicalPackageID, err = prof.physicalPackageID(x)
		if err != nil {
			return nil, err
		}
		// see if this is a new physical id; if so, add it to the inventory
		for _, v := range pids {
			if v == cpu.PhysicalPackageID {
				found = true
				break
			}
		}
		if !found {
			pids = append(pids, cpu.PhysicalPackageID)
		}
		cpu.CoreID, err = prof.coreID(x)
		if err != nil {
			return nil, err
		}
		err := prof.cache(x, &cpu)
		if err != nil {
			return nil, err
		}
		if hasFreq {
			cpu.MHzMin, err = prof.cpuMHzMin(x)
			if err != nil {
				return nil, err
			}
			cpu.MHzMax, err = prof.cpuMHzMax(x)
			if err != nil {
				return nil, err
			}
		}
		cpus.CPU[x] = cpu
	}
	cpus.Sockets = int32(len(pids))
	cpus.Possible, err = prof.Possible()
	if err != nil {
		return nil, err
	}

	cpus.Online, err = prof.Online()
	if err != nil {
		return nil, err
	}

	cpus.Offline, err = prof.Offline()
	if err != nil {
		return nil, err
	}

	cpus.Present, err = prof.Present()
	if err != nil {
		return nil, err
	}

	return cpus, nil
}
712240
func (prof *Profiler) Get() (inf *CPUInfo, err error) {
	var (
		cpuCnt, i, pos, nameLen int
		n                       uint64
		physIDs                 []int32 // tracks unique physical IDs encountered
		pidFound                bool
		v                       byte
		tmp                     string
		cpu                     CPU
	)
	err = prof.Reset()
	if err != nil {
		return nil, err
	}
	inf = &CPUInfo{Timestamp: time.Now().UTC().UnixNano()}
	for {
		prof.Line, err = prof.ReadSlice('\n')
		if err != nil {
			if err == io.EOF {
				break
			}
			return nil, &joe.ReadError{Err: err}
		}
		prof.Val = prof.Val[:0]
		// First grab the attribute name; everything up to the ':'.  The key may have
		// spaces and has trailing spaces; that gets trimmed.
		for i, v = range prof.Line {
			if v == 0x3A {
				prof.Val = prof.Line[:i]
				pos = i + 1
				break
			}
			//prof.Val = append(prof.Val, v)
		}
		prof.Val = joe.TrimTrailingSpaces(prof.Val[:])
		nameLen = len(prof.Val)
		// if there's no name; skip.
		if nameLen == 0 {
			continue
		}
		// if there's anything left, the value is everything else; trim spaces
		if pos+1 < len(prof.Line) {
			prof.Val = append(prof.Val, joe.TrimTrailingSpaces(prof.Line[pos+1:])...)
		}
		v = prof.Val[0]
		if v == 'a' {
			v = prof.Val[1]
			if v == 'd' { // address sizes
				cpu.AddressSizes = strings.Split(string(prof.Val[nameLen:]), ", ")
				continue
			}
			if v == 'p' { // apicid
				n, err = helpers.ParseUint(prof.Val[nameLen:])
				if err != nil {
					return nil, &joe.ParseError{Info: string(prof.Val[:nameLen]), Err: err}
				}
				cpu.APICID = int32(n)
			}
			continue
		}
		if v == 'c' {
			v = prof.Val[1]
			if v == 'p' {
				v = prof.Val[4]
				if v == 'c' { // cpu cores
					n, err = helpers.ParseUint(prof.Val[nameLen:])
					if err != nil {
						return nil, &joe.ParseError{Info: string(prof.Val[:nameLen]), Err: err}
					}
					cpu.CPUCores = int32(n)
					continue
				}
				if v == 'f' { // cpu family
					cpu.CPUFamily = string(prof.Val[nameLen:])
					continue
				}
				if v == 'M' { // cpu MHz
					f, err := strconv.ParseFloat(string(prof.Val[nameLen:]), 32)
					if err != nil {
						return nil, &joe.ParseError{Info: string(prof.Val[:nameLen]), Err: err}
					}
					cpu.CPUMHz = float32(f)
					continue
				}
				if v == 'd' { // cpuid level
					cpu.CPUIDLevel = string(prof.Val[nameLen:])
				}
				continue
			}
			v = prof.Val[5]
			if v == '_' { // cache_alignment
				n, err = helpers.ParseUint(prof.Val[nameLen:])
				if err != nil {
					return nil, &joe.ParseError{Info: string(prof.Val[:nameLen]), Err: err}
				}

				cpu.CacheAlignment = uint16(n)
				continue
			}
			if v == ' ' { // cache size
				cpu.CacheSize = string(prof.Val[nameLen:])
				continue
			}
			if v == 's' { // clflush size
				n, err = helpers.ParseUint(prof.Val[nameLen:])
				if err != nil {
					return nil, &joe.ParseError{Info: string(prof.Val[:nameLen]), Err: err}
				}
				cpu.CLFlushSize = uint16(n)
				continue
			}
			if v == 'i' { // core id
				n, err = helpers.ParseUint(prof.Val[nameLen:])
				if err != nil {
					return nil, &joe.ParseError{Info: string(prof.Val[:nameLen]), Err: err}
				}
				cpu.CoreID = int32(n)
			}
			continue
		}
		if v == 'f' {
			v = prof.Val[1]
			if v == 'l' { // flags
				cpu.Flags = strings.Split(string(prof.Val[nameLen:]), " ")
				continue
			}
			if v == 'p' {
				if nameLen == 3 { // fpu
					cpu.FPU = string(prof.Val[nameLen:])
				} else { // fpu_exception
					cpu.FPUException = string(prof.Val[nameLen:])
				}
			}
			continue
		}
		if v == 'm' {
			v = prof.Val[1]
			if v == 'i' { // microcode
				cpu.Microcode = string(prof.Val[nameLen:])
				continue
			}
			if v == 'o' {
				if nameLen == 5 { // model
					cpu.Model = string(prof.Val[nameLen:])
					continue
				}
				cpu.ModelName = string(prof.Val[nameLen:])
			}
			continue
		}
		if v == 'p' {
			v = prof.Val[1]
			if v == 'h' { // physical id
				n, err = helpers.ParseUint(prof.Val[nameLen:])
				if err != nil {
					return nil, &joe.ParseError{Info: string(prof.Val[:nameLen]), Err: err}
				}
				cpu.PhysicalID = int32(n)
				for i := range physIDs {
					if physIDs[i] == cpu.PhysicalID {
						pidFound = true
						break
					}
				}
				if pidFound {
					pidFound = false // reset for next use
				} else {
					// physical id hasn't been encountered yet; add it
					physIDs = append(physIDs, cpu.PhysicalID)
				}
				continue
			}
			if v == 'o' { // power management
				tmp = string(prof.Val[nameLen:])
				if tmp == "" {
					continue
				}
				cpu.PowerManagement = strings.Split(tmp, " ")
				continue
			}
			// processor starts information about a processor.
			if v == 'r' { // processor
				if cpuCnt > 0 {
					inf.CPU = append(inf.CPU, cpu)
				}
				cpuCnt++
				n, err = helpers.ParseUint(prof.Val[nameLen:])
				if err != nil {
					return nil, &joe.ParseError{Info: string(prof.Val[:nameLen]), Err: err}
				}
				cpu = CPU{Processor: int32(n)}
			}
			continue
		}
		if v == 's' {
			v = prof.Val[1]
			if v == 'i' { // siblings
				n, err = helpers.ParseUint(prof.Val[nameLen:])
				if err != nil {
					return nil, &joe.ParseError{Info: string(prof.Val[:nameLen]), Err: err}
				}
				cpu.Siblings = int8(n)
				continue
			}
			if v == 't' { // stepping
				cpu.Stepping = string(prof.Val[nameLen:])
			}
			continue
		}
		if v == 'b' {
			if prof.Val[1] == 'o' { // bogomips
				f, err := strconv.ParseFloat(string(prof.Val[nameLen:]), 32)
				if err != nil {
					return nil, &joe.ParseError{Info: string(prof.Val[:nameLen]), Err: err}
				}
				cpu.BogoMIPS = float32(f)
				continue
			}
			if prof.Val[1] == 'u' { // bugs
				tmp = string(prof.Val[nameLen:])
				if tmp != "" {
					cpu.Bugs = strings.Split(tmp, " ")
				}
			}
			continue
		}
		if v == 'i' { // initial apicid
			n, err = helpers.ParseUint(prof.Val[nameLen:])
			if err != nil {
				return nil, &joe.ParseError{Info: string(prof.Val[:nameLen]), Err: err}
			}
			cpu.InitialAPICID = int32(n)
			continue
		}
		if v == 'w' { // WP
			cpu.WP = string(prof.Val[nameLen:])
			continue
		}
		if v == 'v' { // vendor_id
			cpu.VendorID = string(prof.Val[nameLen:])
		}
		if v == 'T' { //tlb size
			cpu.TLBSize = string(prof.Val[nameLen:])
		}
	}
	// append the current processor informatin
	inf.CPU = append(inf.CPU, cpu)
	inf.Sockets = int32(len(physIDs))
	return inf, nil
}
290752
func (prof *Profiler) coreID(x int) (int32, error) {
	v, err := ioutil.ReadFile(prof.coreIDPath(x))
	if err != nil {
		return 0, err
	}
	id, err := strconv.Atoi(string(v[:len(v)-1]))
	if err != nil {
		return 0, fmt.Errorf("cpu%d core_id: conversion error: %s", x, err)
	}
	return int32(id), nil
}
115611
func NewProfiler() (prof *Profiler, err error) {
	p, err := netdev.NewProfiler()
	if err != nil {
		return nil, err
	}
	prior, err := p.Get()
	if err != nil {
		return nil, err
	}
	return &Profiler{Profiler: p, prior: *prior}, nil
}
887798
func (prof *Profiler) calculateUtilization(cur *stats.CPUStats) *CPUUtil {
	u := &CPUUtil{
		Timestamp:  cur.Timestamp,
		TimeDelta:  cur.Timestamp - prof.prior.Timestamp,
		BTimeDelta: int32(cur.Timestamp/1000000000 - cur.BTime),
		CtxtDelta:  cur.Ctxt - prof.prior.Ctxt,
		Processes:  int32(cur.Processes),
		CPU:        make([]Utilization, len(cur.CPU)),
	}
	var dUser, dNice, dSys, dIdle, tot float32
	// Rest of the calculations are per core
	for i := 0; i < len(cur.CPU); i++ {
		v := Utilization{ID: cur.CPU[i].ID}
		dUser = float32(cur.CPU[i].User - prof.prior.CPU[i].User)
		dNice = float32(cur.CPU[i].Nice - prof.prior.CPU[i].Nice)
		dSys = float32(cur.CPU[i].System - prof.prior.CPU[i].System)
		dIdle = float32(cur.CPU[i].Idle - prof.prior.CPU[i].Idle)
		tot = dUser + dNice + dSys + dIdle
		v.Usage = (dUser + dNice + dSys) / tot * float32(cur.ClkTck)
		v.User = dUser / tot * float32(cur.ClkTck)
		v.Nice = dNice / tot * float32(cur.ClkTck)
		v.System = dSys / tot * float32(cur.ClkTck)
		v.Idle = dIdle / tot * float32(cur.ClkTck)
		v.IOWait = float32(cur.CPU[i].IOWait-prof.prior.CPU[i].IOWait) / tot * float32(cur.ClkTck)
		u.CPU[i] = v
	}
	return u
}
876277
func (c *CPUs) GetCPU(pID, coreID int32) (cpu CPU, found bool) {
	for i := 0; i < len(c.CPU); i++ {
		if c.CPU[i].PhysicalPackageID == pID && c.CPU[i].CoreID == coreID {
			return c.CPU[i], true
		}
	}
	return CPU{}, false
}
420683
func startProfiler(profilerType, dirPath string) (minioProfiler, error) {
	var err error
	if dirPath == "" {
		dirPath, err = ioutil.TempDir("", "profile")
		if err != nil {
			return nil, err
		}
	}

	var profiler interface {
		Stop()
	}

	var profilerFileName string

	// Enable profiler and set the name of the file that pkg/pprof
	// library creates to store profiling data.
	switch profilerType {
	case "cpu":
		profiler = profile.Start(profile.CPUProfile, profile.NoShutdownHook, profile.ProfilePath(dirPath))
		profilerFileName = "cpu.pprof"
	case "mem":
		profiler = profile.Start(profile.MemProfile, profile.NoShutdownHook, profile.ProfilePath(dirPath))
		profilerFileName = "mem.pprof"
	case "block":
		profiler = profile.Start(profile.BlockProfile, profile.NoShutdownHook, profile.ProfilePath(dirPath))
		profilerFileName = "block.pprof"
	case "mutex":
		profiler = profile.Start(profile.MutexProfile, profile.NoShutdownHook, profile.ProfilePath(dirPath))
		profilerFileName = "mutex.pprof"
	case "trace":
		profiler = profile.Start(profile.TraceProfile, profile.NoShutdownHook, profile.ProfilePath(dirPath))
		profilerFileName = "trace.out"
	default:
		return nil, errors.New("profiler type unknown")
	}

	return &profilerWrapper{
		stopFn: profiler.Stop,
		pathFn: func() string {
			return filepath.Join(dirPath, profilerFileName)
		},
	}, nil
}
1233227
func startProfiling() (func(), error) {
	// start CPU profiling as early as possible
	ofi, err := os.Create(cpuProfile)
	if err != nil {
		return nil, err
	}
	err = pprof.StartCPUProfile(ofi)
	if err != nil {
		ofi.Close()
		return nil, err
	}
	go func() {
		for range time.NewTicker(time.Second * 30).C {
			err := writeHeapProfileToFile()
			if err != nil {
				log.Error(err)
			}
		}
	}()

	stopProfiling := func() {
		pprof.StopCPUProfile()
		ofi.Close() // captured by the closure
	}
	return stopProfiling, nil
}
222448
func (c cpuInfo) tsx() bool {
	return c.features&(mpx|rtm) == mpx|rtm
}
1143807
func (remote *RemoteDebugger) StartProfiler() error {
	_, err := remote.SendRequest("Profiler.start", nil)
	return err
}
912347
func (h *HandlerT) CpuProfile(file string, nsec uint) error {
	if err := h.StartCPUProfile(file); err != nil {
		return err
	}
	time.Sleep(time.Duration(nsec) * time.Second)
	h.StopCPUProfile()
	return nil
}