-
Notifications
You must be signed in to change notification settings - Fork 17
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
base: master
Are you sure you want to change the base?
Conversation
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.
There was a problem hiding this 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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
lib/modules/cpu.go
Outdated
func (c *Cpu) parseCpuLine(flds []string) (float64, error) { | ||
if len(flds) < 7 { | ||
return 0, errors.New("invalid line in /proc/stat") | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
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.
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.