Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added CPU usage block #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Added CPU usage block #19

wants to merge 2 commits into from

Conversation

mmm444
Copy link

@mmm444 mmm444 commented May 4, 2017

The CPU usage displays the ratio of non-idle time since the last
update. User can specify the critical usage level above which the
block is rendered as urgent.

The usage CPU times are read from /proc/stat.

The CPU usage displays the ratio of non-idle time since the last
update. User can specify the critical usage level above which the
block is rendered as urgent.

The usage CPU times are read from /proc/stat.
Copy link
Owner

@davidscholberg davidscholberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @mmm444, thank you for this pull request! My apologies that it took me so long to get to this.

I think this is a great addition to goblocks, but I just have a couple of minor implementation concerns I'd like you to take a look at.

CritUsage float64 `yaml:"crit_usage"`

lastIdle uint64
lastTotal uint64
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little bit uneasy with putting the lastIdle and lastTotal variables in this struct. Strictly speaking, this struct is for configuration data, not dynamic data that needs to persist over update calls. However, I do realize that there's currently no good way to persist dynamic data from one update call to the next.

I'm kind of leaning towards making these global variables rather than fields of the Cpu struct. I know that's not ideal, but it will at least keep dynamic data out of the configuration struct until we have a better solution for persisting data.

Any thoughts on this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand.

I think it would be great if the whole goblocks design was based around blocks that are once configured from YAML and live until the program exits. The block would then encapsulate its configuration, state and provide an interface to render itself on screen. I actually though this is the way how it worked when I coded the CPU block. I think it is more documentation/mental and probably pointer receiver problem than anything else.

I was also thinking about moving the state of the block to a global variable and the best design I can come with is this:

type cpuState struct {
	lastIdle  uint64
	lastTotal uint64
}

// name of the CPU -> block state
var cpuStates map[string]cpuState

It has the shorcoming that it does not support creating a block for the same cpu twice. It is rather theoretical problem but it points to a more higher level problem.

BTW is guaranteed that the UpdateBlock functions are called from one goroutine? Otherwise the state access would have to be synchronized.

func (c *Cpu) parseCpuLine(flds []string) (float64, error) {
if len(flds) < 7 {
return 0, errors.New("invalid line in /proc/stat")
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just curious why you chose 7 fields as the threshold for a valid line. If you look at man proc, the CPU lines have had 10 fields since kernel version 2.6.33. I'm not really interested in supporting a kernel version that old, so I'd say just make it 10, unless there's a compelling reason to do otherwise.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@mmm444
Copy link
Author

mmm444 commented Jul 26, 2017

This time I am sorry for my interplanetary-scale latency.

Use only lines with 10 or more fields. This is the format
used since Linux kernel 2.6.33.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants