-
Notifications
You must be signed in to change notification settings - Fork 712
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #742 from weaveworks/284-profiling
Various CPU usage gains from profiling
- Loading branch information
Showing
25 changed files
with
2,436 additions
and
52 deletions.
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
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,72 @@ | ||
package process | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/armon/go-metrics" | ||
"github.com/coocood/freecache" | ||
|
||
"github.com/weaveworks/scope/common/fs" | ||
) | ||
|
||
const ( | ||
generalTimeout = 30 // seconds | ||
statsTimeout = 10 //seconds | ||
) | ||
|
||
var ( | ||
hitMetricsKey = []string{"process", "cache", "hit"} | ||
missMetricsKey = []string{"process", "cache", "miss"} | ||
) | ||
|
||
var fileCache = freecache.NewCache(1024 * 16) | ||
|
||
type entry struct { | ||
buf []byte | ||
err error | ||
ts time.Time | ||
} | ||
|
||
func cachedReadFile(path string) ([]byte, error) { | ||
key := []byte(path) | ||
if v, err := fileCache.Get(key); err == nil { | ||
metrics.IncrCounter(hitMetricsKey, 1.0) | ||
return v, nil | ||
} | ||
|
||
buf, err := fs.ReadFile(path) | ||
fileCache.Set(key, buf, generalTimeout) | ||
metrics.IncrCounter(missMetricsKey, 1.0) | ||
return buf, err | ||
} | ||
|
||
// we cache the stats, but for a shorter period | ||
func readStats(path string) (int, int, error) { | ||
var ( | ||
key = []byte(path) | ||
buf []byte | ||
err error | ||
) | ||
if buf, err = fileCache.Get(key); err == nil { | ||
metrics.IncrCounter(hitMetricsKey, 1.0) | ||
} else { | ||
buf, err = fs.ReadFile(path) | ||
if err != nil { | ||
return -1, -1, err | ||
} | ||
fileCache.Set(key, buf, statsTimeout) | ||
metrics.IncrCounter(missMetricsKey, 1.0) | ||
} | ||
splits := strings.Fields(string(buf)) | ||
ppid, err := strconv.Atoi(splits[3]) | ||
if err != nil { | ||
return -1, -1, err | ||
} | ||
threads, err := strconv.Atoi(splits[19]) | ||
if err != nil { | ||
return -1, -1, err | ||
} | ||
return ppid, threads, nil | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.