// Returns an initialized Profiler; ready to use.
493615
func NewProfiler() (p *Profiler, err error) {
	prof, err := procs.NewProfiler()
	if err != nil {
		return nil, err
	}
	return &Profiler{Profiler: prof}, nil
}
93152
func GetDefaultProfile(rs *specs.Spec) (*specs.LinuxSeccomp, error) {
	return setupSeccomp(DefaultProfile(), rs)
}
540937
func (prof *Profiler) Get() (k *Kernel, err error) {
	var (
		i, pos, pos2 int
		v            byte
	)
	err = prof.Reset()
	if err != nil {
		return nil, err
	}
	// This will always be linux, I think.
	k = &Kernel{OS: "linux"}
	for {
		prof.Line, err = prof.ReadSlice('\n')
		if err != nil {
			if err == io.EOF {
				break
			}
			return nil, &joe.ReadError{Err: err}
		}
		// The version is everything from the space, 0x20, prior to the version string, up to the first '(', 0x28, - 1 byte
		for i, v = range prof.Line {
			if v == 0x28 {
				// get the OS
				k.Version = string(prof.Line[pos2+1 : i-1])
				pos = i + 1
				break
			}
			// keep track of the last space encountered
			if v == 0x20 {
				pos2 = pos
				pos = i
			}
		}
		// Set the arch
		k.SetArch()
		// The CompileUser is everything up to the next ')', 0x29
		for i, v = range prof.Line[pos:] {
			if v == 0x29 {
				k.CompileUser = string(prof.Line[pos : pos+i])
				pos += i + 3
				break
			}
		}

		var inOSGCC bool
		// GCC info; this may include os specific gcc info
		for i, v = range prof.Line[pos:] {
			if v == 0x28 {
				inOSGCC = true
				k.GCC = string(prof.Line[pos : pos+i-1])
				pos2 = i + pos + 1
				continue
			}
			if v == 0x29 {
				if inOSGCC {
					k.OSGCC = string(prof.Line[pos2 : pos+i])
					inOSGCC = false
					continue
				}
				pos, pos2 = pos+i+2, pos
				break
			}
		}
		// Check if GCC is empty, this happens if there wasn't an OSGCC value
		if k.GCC == "" {
			k.GCC = string(prof.Line[pos2 : pos-1])
		}
		// Get the type information, everything up to '('
		for i, v = range prof.Line[pos:] {
			if v == 0x28 {
				k.Type = string(prof.Line[pos : pos+i-1])
				pos += i + 1
				break
			}
		}
		// The rest is the compile date.
		k.CompileDate = string(prof.Line[pos : len(prof.Line)-2])
	}
	return k, 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
}
1228998
func (c *Client) GetAccountProfile(ctx context.Context) (*Profile, error) {
	var profile Profile

	req, err := c.NewRequest("GET", profilePath, nil, nil)
	if err != nil {
		return nil, err
	}

	if err := c.Do(ctx, req, &profile); err != nil {
		return nil, err
	}

	return &profile, err
}
4783
func (prof *Profiler) Get() (inf *Info, err error) {
	var (
		i, pos, nameLen int
		v               byte
		n               uint64
	)
	err = prof.Reset()
	if err != nil {
		return nil, err
	}
	inf = &Info{}
	inf.Timestamp = time.Now().UTC().UnixNano()
	for {
		prof.Val = prof.Val[:0]
		prof.Line, err = prof.ReadSlice('\n')
		if err != nil {
			if err == io.EOF {
				break
			}
			return inf, &joe.ReadError{Err: err}
		}
		// first grab the key name (everything up to the ':')
		for i, v = range prof.Line {
			if v == ':' {
				pos = i + 1
				break
			}
			prof.Val = append(prof.Val, v)
		}
		nameLen = len(prof.Val)

		// skip all spaces
		for i, v = range prof.Line[pos:] {
			if v != ' ' {
				pos += i
				break
			}
		}

		// grab the numbers
		for _, v = range prof.Line[pos:] {
			if v == ' ' || v == '\n' {
				break
			}
			prof.Val = append(prof.Val, v)
		}
		// any conversion error results in 0
		n, err = helpers.ParseUint(prof.Val[nameLen:])
		if err != nil {
			return inf, &joe.ParseError{Info: string(prof.Val[:nameLen]), Err: err}
		}

		v = prof.Val[0]
		// evaluate the key
		if v == 'A' {
			if prof.Val[5] == 'e' && nameLen == 6 {
				inf.Active = n
			}
			continue
		}
		if v == 'I' {
			if nameLen == 8 {
				inf.Inactive = n
			}
			continue
		}
		if v == 'M' {
			v = prof.Val[3]
			if nameLen < 8 {
				if v == 'p' {
					inf.Mapped = n
					continue
				}
				if v == 'F' {
					inf.MemFree = n
				}
				continue
			}
			if v == 'A' {
				inf.MemAvailable = n
				continue
			}
			inf.MemTotal = n
			continue
		}
		if v == 'S' {
			v = prof.Val[1]
			if v == 'w' {
				if prof.Val[4] == 'C' {
					inf.SwapCached = n
					continue
				}
				if prof.Val[4] == 'F' {
					inf.SwapFree = n
					continue
				}
				inf.SwapTotal = n
				continue
			}
		}
	}
	return inf, nil
}
1024592
func (prof *Profiler) Get() (la LoadAvg, err error) {
	err = prof.Reset()
	if err != nil {
		return la, err
	}
	var (
		i, priorPos, pos, line, fieldNum int
		n                                uint64
		f                                float64
		v                                byte
	)
	la.Timestamp = time.Now().UTC().UnixNano()
	for {
		prof.Line, err = prof.ReadSlice('\n')
		if err != nil {
			if err == io.EOF {
				break
			}
			return la, &joe.ReadError{Err: err}
		}
		line++
		for {
			// space delimits the values
			for i, v = range prof.Line[pos:] {
				if v == 0x20 {
					priorPos, pos = pos, pos+i+1
					break
				}
			}
			fieldNum++
			if fieldNum <= 3 {
				f, err = strconv.ParseFloat(string(prof.Line[priorPos:pos-1]), 64)
				if err != nil {
					return la, &joe.ParseError{Info: fmt.Sprintf("line %d: field %d", line, fieldNum), Err: err}
				}
				if fieldNum == 1 {
					la.Minute = float32(f)
					continue
				}
				if fieldNum == 2 {
					la.Five = float32(f)
					continue
				}
				la.Fifteen = float32(f)
				continue
			}
			if fieldNum == 4 {
				// get the process information: separated by /
				for i, v = range prof.Line[priorPos:pos] {
					if v == '/' {
						break
					}
				}
				n, err = helpers.ParseUint(prof.Line[priorPos : priorPos+i])
				if err != nil {
					return la, &joe.ParseError{Info: fmt.Sprintf("line %d: field %d", line, fieldNum), Err: err}
				}
				la.Running = int32(n)
				n, err = helpers.ParseUint(prof.Line[priorPos+i+1 : pos-1])
				if err != nil {
					return la, &joe.ParseError{Info: fmt.Sprintf("line %d: field %d", line, fieldNum), Err: err}
				}
				la.Total = int32(n)
				continue
			}
			n, err = helpers.ParseUint(prof.Line[pos : len(prof.Line)-1])
			if err != nil {
				return la, err
			}
			la.PID = int32(n)
			break
		}
	}
	return la, nil
}
1173872
func (client *Client) GetProfile(userID string) *GetProfileCall {
	return &GetProfileCall{
		c:      client,
		userID: userID,
	}
}
856818
func ProfileHandler() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		pprof.Profile(ctx.Writer, ctx.Request)
	}
}
1168498
func NewProfiler() (prof *Profiler, err error) {
	p, err := freq.NewProfiler()
	if err != nil {
		return nil, err
	}
	return &Profiler{Profiler: p}, nil
}
722846
func (p lxdCharmProfiler) LXDProfile() lxdprofile.LXDProfile {
	if p.Charm == nil {
		return nil
	}
	if profiler, ok := p.Charm.(charm.LXDProfiler); ok {
		return profiler.LXDProfile()
	}
	return nil
}
970550
func WithProfile(profile string) oci.SpecOpts {
	return func(_ context.Context, _ oci.Client, _ *containers.Container, s *specs.Spec) error {
		s.Process.ApparmorProfile = profile
		return nil
	}
}
741396
func (s *Signer) getMatchingProfile(profile string) (*config.SigningProfile, error) {
	if profile == "" {
		return s.policy.Default, nil
	}

	for p, signingProfile := range s.policy.Profiles {
		if p == profile {
			return signingProfile, nil
		}
	}

	return nil, cferr.New(cferr.PolicyError, cferr.UnknownProfile)
}
902809
func NewProfiler() (prof *Profiler, err error) {
	p, err := util.NewProfiler()
	if err != nil {
		return nil, err
	}
	return &Profiler{Profiler: p, Builder: fb.NewBuilder(0)}, nil
}
1028799
func (s *peerRESTServer) StartProfilingHandler(w http.ResponseWriter, r *http.Request) {
	if !s.IsValid(w, r) {
		s.writeErrorResponse(w, errors.New("Invalid request"))
		return
	}

	vars := mux.Vars(r)
	profiler := vars[peerRESTProfiler]
	if profiler == "" {
		s.writeErrorResponse(w, errors.New("profiler name is missing"))
		return
	}

	if globalProfiler != nil {
		globalProfiler.Stop()
	}

	var err error
	globalProfiler, err = startProfiler(profiler, "")
	if err != nil {
		s.writeErrorResponse(w, err)
		return
	}

	w.(http.Flusher).Flush()
}
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
}
1325739
func WithDefaultProfile(name string) oci.SpecOpts {
	return func(_ context.Context, _ oci.Client, _ *containers.Container, s *specs.Spec) error {
		yes, err := isLoaded(name)
		if err != nil {
			return err
		}
		if yes {
			s.Process.ApparmorProfile = name
			return nil
		}
		p, err := loadData(name)
		if err != nil {
			return err
		}
		f, err := ioutil.TempFile(os.Getenv("XDG_RUNTIME_DIR"), p.Name)
		if err != nil {
			return err
		}
		defer f.Close()
		path := f.Name()
		defer os.Remove(path)

		if err := generate(p, f); err != nil {
			return err
		}
		if err := load(path); err != nil {
			return errors.Wrapf(err, "load apparmor profile %s", path)
		}
		s.Process.ApparmorProfile = name
		return nil
	}
}