// LogCurrentSystemLoad logs the current system load and lists all processes
// consuming more than cpuWatermark of the CPU
175981
func LogCurrentSystemLoad(logFunc LogFunc) {
	loadInfo, err := load.Avg()
	if err == nil {
		logFunc("Load 1-min: %.2f 5-min: %.2f 15min: %.2f",
			loadInfo.Load1, loadInfo.Load5, loadInfo.Load15)
	}

	memInfo, err := mem.VirtualMemory()
	if err == nil && memInfo != nil {
		logFunc("Memory: Total: %d Used: %d (%.2f%%) Free: %d Buffers: %d Cached: %d",
			toMB(memInfo.Total), toMB(memInfo.Used), memInfo.UsedPercent, toMB(memInfo.Free), toMB(memInfo.Buffers), toMB(memInfo.Cached))
	}

	swapInfo, err := mem.SwapMemory()
	if err == nil && swapInfo != nil {
		logFunc("Swap: Total: %d Used: %d (%.2f%%) Free: %d",
			toMB(swapInfo.Total), toMB(swapInfo.Used), swapInfo.UsedPercent, toMB(swapInfo.Free))
	}

	procs, err := process.Processes()
	if err == nil {
		for _, p := range procs {
			cpuPercent, _ := p.CPUPercent()
			if cpuPercent > cpuWatermark {
				name, _ := p.Name()
				status, _ := p.Status()
				memPercent, _ := p.MemoryPercent()
				cmdline, _ := p.Cmdline()

				memExt := ""
				if memInfo, err := p.MemoryInfo(); memInfo != nil && err == nil {
					memExt = fmt.Sprintf("RSS: %d VMS: %d Data: %d Stack: %d Locked: %d Swap: %d",
						toMB(memInfo.RSS), toMB(memInfo.VMS), toMB(memInfo.Data),
						toMB(memInfo.Stack), toMB(memInfo.Locked), toMB(memInfo.Swap))
				}

				logFunc("NAME %s STATUS %s PID %d CPU: %.2f%% MEM: %.2f%% CMDLINE: %s MEM-EXT: %s",
					name, status, p.Pid, cpuPercent, memPercent, cmdline, memExt)
			}
		}
	}
}
754954
func (s *server) LoadStream(c context.Context, req *logdog.LoadStreamRequest) (*logdog.LoadStreamResponse, error) {
	log.Fields{
		"project": req.Project,
		"id":      req.Id,
	}.Infof(c, "Loading log stream state.")

	id := coordinator.HashID(req.Id)
	if err := id.Normalize(); err != nil {
		log.WithError(err).Errorf(c, "Invalid stream ID.")
		return nil, grpcutil.Errf(codes.InvalidArgument, "Invalid ID (%s): %s", id, err)
	}

	ls := &coordinator.LogStream{ID: coordinator.HashID(req.Id)}
	lst := ls.State(c)

	if err := ds.Get(c, lst, ls); err != nil {
		if anyNoSuchEntity(err) {
			log.WithError(err).Errorf(c, "No such entity in datastore.")

			// The state isn't registered, so this stream does not exist.
			return nil, grpcutil.Errf(codes.NotFound, "Log stream was not found.")
		}

		log.WithError(err).Errorf(c, "Failed to load log stream.")
		return nil, grpcutil.Internal
	}

	// The log stream and state loaded successfully.
	resp := logdog.LoadStreamResponse{
		State: buildLogStreamState(ls, lst),
	}
	if req.Desc {
		resp.Desc = ls.Descriptor
	}
	resp.ArchivalKey = lst.ArchivalKey
	resp.Age = google.NewDuration(ds.RoundTime(clock.Now(c)).Sub(lst.Updated))

	log.Fields{
		"id":              lst.ID(),
		"terminalIndex":   resp.State.TerminalIndex,
		"archived":        resp.State.Archived,
		"purged":          resp.State.Purged,
		"age":             google.DurationFromProto(resp.Age),
		"archivalKeySize": len(resp.ArchivalKey),
	}.Infof(c, "Successfully loaded log stream state.")
	return &resp, nil
}
1186425
func (c *ClientConfig) Load(d *distconf.Distconf) {
	c.SourceName = d.Str("signalfuse.sourceName", "")
	c.AuthToken = d.Str("sf.metrics.auth_token", "")
	c.Endpoint = d.Str("sf.metrics.statsendpoint", "")
	c.ReportingInterval = d.Duration("sf.metrics.report_interval", time.Second)
	c.TimeKeeper = timekeeper.RealTime{}
	c.OsHostname = os.Hostname
	c.DisableCompression = d.Bool("sf.metrics.disableCompression", false)
}
1073820
func LogPeriodicSystemLoad(logFunc LogFunc, interval time.Duration) CloseChan {
	closeChan := make(CloseChan)
	go func() {
		for {
			LogCurrentSystemLoad(logFunc)

			select {
			case <-closeChan:
				return
			default:
			}

			time.Sleep(interval)
		}
	}()

	return closeChan
}
515879
func (lst *LogStreamState) Load(pmap ds.PropertyMap) error {
	// Discard derived properties.
	delete(pmap, "_Terminated")
	delete(pmap, ArchivalStateKey)

	return ds.GetPLS(lst).Load(pmap)
}
99879
func Load(configFile string, logger log.Logger) (*GatewayConfig, error) {
	p, err := loadNoDefault(configFile, logger)
	if err == nil {
		c := pointer.FillDefaultFrom(p, DefaultGatewayConfig()).(*GatewayConfig)
		if err = os.Setenv("gatewayServerName", *c.ServerName); err == nil {
			if c.ClusterName == nil || *c.ClusterName == "" {
				return nil, errors.New("gateway now requires configuring ClusterName at top level of config")
			}
			// fill in environment variable values to override config file
			loadFromEnv(c)
			return c, nil
		}
	}
	return nil, err
}
628694
func (f *FileStore) CurrentGeneration() int {
	f.mu.RLock()
	defer f.mu.RUnlock()
	return f.currentGeneration
}
1155596
func (m *MemoryBackend) Log(event LogEvent, client *Client, pkt packet.Generic, msg *packet.Message, err error) {
	// call logger if available
	if m.Logger != nil {
		m.Logger(event, client, pkt, msg, err)
	}
}
1028452
func (m *deviceMounter) Load(devPrefixes []string) error {
	return m.load(devPrefixes, deviceFindMountPoint)
}
187654
func (c *Core) loadCurrentRequestCounters(ctx context.Context, now time.Time) error {
	datepath := now.Format(requestCounterDatePathFormat)
	counter, err := c.loadRequestCounters(ctx, datepath)
	if err != nil {
		return err
	}
	if counter != nil {
		c.counters.activePath = datepath
		atomic.StoreUint64(c.counters.requests, *counter.Total)
	}
	return nil
}
743199
func GetSystem() *api.SystemStats {
	status := new(api.SystemStats)
	if load, err := load.Avg(); err == nil {
		status.Load = &api.SystemStats_Loadstats{
			Load1:  float32(load.Load1),
			Load5:  float32(load.Load5),
			Load15: float32(load.Load15),
		}
	}
	status.Cpu = &api.SystemStats_CPUStats{
		Percentage: float32(cpuPercentage),
	}
	if cpu, err := cpu.Times(false); err == nil && len(cpu) == 1 {
		status.Cpu.User = float32(cpu[0].User)
		status.Cpu.System = float32(cpu[0].System)
		status.Cpu.Idle = float32(cpu[0].Idle)
	}
	if mem, err := mem.VirtualMemory(); err == nil {
		status.Memory = &api.SystemStats_MemoryStats{
			Total:     mem.Total,
			Available: mem.Available,
			Used:      mem.Used,
		}
	}
	return status
}
737453
func LogStats() {
	var m runtime.MemStats
	runtime.ReadMemStats(&m)
	log.Infof("Alloc=%v TotalAlloc=%v Sys=%v NumGC=%v Goroutines=%d",
		m.Alloc/1024, m.TotalAlloc/1024, m.Sys/1024, m.NumGC, runtime.NumGoroutine())

}
461133
func preNotifySystem() {
	// start the service now to prevent timeouts waiting for daemon to start
	// but still (eventually) complete all requests that are sent after this
	if service != nil {
		err := service.started()
		if err != nil {
			logrus.Fatal(err)
		}
	}
}
999320
func (s *server) LogEntries() []*LogEntry {
	s.log.mutex.RLock()
	defer s.log.mutex.RUnlock()
	return s.log.entries
}
412312
func (f *FlagLoader) Load(s interface{}) error {
	strct := structs.New(s)
	structName := strct.Name()

	flagSet := flag.NewFlagSet(structName, f.ErrorHandling)
	f.flagSet = flagSet

	for _, field := range strct.Fields() {
		f.processField(field.Name(), field)
	}

	flagSet.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
		flagSet.PrintDefaults()
		fmt.Fprintf(os.Stderr, "\nGenerated environment variables:\n")
		e := &EnvironmentLoader{
			Prefix:    f.EnvPrefix,
			CamelCase: f.CamelCase,
		}
		e.PrintEnvs(s)
		fmt.Println("")
	}

	args := filterArgs(os.Args[1:])
	if f.Args != nil {
		args = f.Args
	}

	return flagSet.Parse(args)
}
748643
func (fs FS) NewSystemCpufreq() ([]SystemCPUCpufreqStats, error) {
	var g errgroup.Group

	cpus, err := filepath.Glob(fs.Path("devices/system/cpu/cpu[0-9]*"))
	if err != nil {
		return nil, err
	}

	systemCpufreq := make([]SystemCPUCpufreqStats, len(cpus))
	for i, cpu := range cpus {
		cpuName := strings.TrimPrefix(filepath.Base(cpu), "cpu")

		cpuCpufreqPath := filepath.Join(cpu, "cpufreq")
		_, err = os.Stat(cpuCpufreqPath)
		if os.IsNotExist(err) {
			continue
		} else if err != nil {
			return nil, err
		}

		// Execute the parsing of each CPU in parallel.
		// This is done because the kernel intentionally delays access to each CPU by
		// 50 milliseconds to avoid DDoSing possibly expensive functions.
		i := i // https://golang.org/doc/faq#closures_and_goroutines
		g.Go(func() error {
			cpufreq, err := parseCpufreqCpuinfo(cpuCpufreqPath)
			if err == nil {
				cpufreq.Name = cpuName
				systemCpufreq[i] = *cpufreq
			}
			return err
		})
	}

	if err = g.Wait(); err != nil {
		return nil, err
	}

	return systemCpufreq, nil
}
1080918
func (l *Logger) consume() {
	go func() {
		// Process the single next task in sequence until completion,
		// then consume the next task.
		for task := range l.tasks {
			l.logTask(task)
		}
	}()

	defer close(l.tasks)

	pending := make([]Task, 0)

	for {
		// If there is a pending task, "peek" it off of the set of
		// pending tasks.
		var next Task
		if len(pending) > 0 {
			next = pending[0]
		}

		if next == nil {
			// If there was no pending task, wait for either a)
			// l.queue to close, or b) a new task to be submitted.
			task, ok := <-l.queue
			if !ok {
				// If the queue is closed, no more new tasks may
				// be added.
				return
			}

			// Otherwise, add a new task to the set of tasks to
			// process immediately, since there is no current
			// buffer.
			l.tasks <- task
		} else {
			// If there is a pending task, wait for either a) a
			// write to process the task to become non-blocking, or
			// b) a new task to enter the queue.
			select {
			case task, ok := <-l.queue:
				if !ok {
					// If the queue is closed, no more tasks
					// may be added.
					return
				}
				// Otherwise, add the next task to the set of
				// pending, active tasks.
				pending = append(pending, task)
			case l.tasks <- next:
				// Or "pop" the peeked task off of the pending
				// set.
				pending = pending[1:]
			}
		}
	}
}