-
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
Open
mmm444
wants to merge
2
commits into
davidscholberg:master
Choose a base branch
from
mmm444:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package modules | ||
|
||
import ( | ||
"bufio" | ||
"errors" | ||
"fmt" | ||
"github.com/davidscholberg/go-i3barjson" | ||
"os" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// Cpu represents the configuration for the cpu block. | ||
type Cpu struct { | ||
BlockConfigBase `yaml:",inline"` | ||
Cpu string `yaml:"cpu"` // name of the cpu to display (from /proc/stat), empty = all | ||
CritUsage float64 `yaml:"crit_usage"` | ||
|
||
lastIdle uint64 | ||
lastTotal uint64 | ||
} | ||
|
||
// UpdateBlock updates the status of the CPU block. | ||
// The block displays the CPU usage since the last update. | ||
func (c *Cpu) UpdateBlock(b *i3barjson.Block) { | ||
b.Color = c.Color | ||
|
||
usage, err := c.getCpuUsage() | ||
if err != nil { | ||
b.FullText = err.Error() | ||
b.Urgent = true | ||
return | ||
} | ||
|
||
b.Urgent = (c.CritUsage > 0) && (usage > c.CritUsage) | ||
b.FullText = fmt.Sprintf("%s%2.0f%%", c.Label, 100*usage) | ||
} | ||
|
||
// getCpuUsage returns the usage of the configured CPU. It does by reading /proc/stat and | ||
// calculating (1 - idle_time/total_time). | ||
func (c *Cpu) getCpuUsage() (float64, error) { | ||
f, err := os.Open("/proc/stat") | ||
if err != nil { | ||
return 0, err | ||
} | ||
defer f.Close() | ||
|
||
// find the line that corresponds to the configured CPU | ||
s := bufio.NewScanner(f) | ||
for s.Scan() { | ||
flds := strings.Fields(s.Text()) | ||
if c.Cpu == "" || (len(flds) > 0 && flds[0] == c.Cpu) { | ||
return c.parseCpuLine(flds[1:]) | ||
} | ||
} | ||
if s.Err() != nil { | ||
return 0, s.Err() | ||
} | ||
return 0, fmt.Errorf("cpu %s not found", c.Cpu) | ||
} | ||
|
||
// parseCpuLine extracts the usage from the text fields of one line of /proc/stat. | ||
func (c *Cpu) parseCpuLine(flds []string) (float64, error) { | ||
// CPU lines have 10 fields since kernel version 2.6.33 | ||
if len(flds) < 10 { | ||
return 0, errors.New("invalid line in /proc/stat") | ||
} | ||
var total, idle uint64 | ||
for i := range flds { | ||
val, err := strconv.ParseUint(flds[i], 10, 64) | ||
if err != nil { | ||
return 0, errors.New("invalid number") | ||
} | ||
total += val | ||
if i == 3 { | ||
idle += val | ||
} | ||
|
||
} | ||
usage := 1 - (float64(idle-c.lastIdle) / float64(total-c.lastTotal)) | ||
c.lastTotal, c.lastIdle = total, idle | ||
return usage, nil | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
andlastTotal
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:
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.